2 * MediaWiki Widgets – CalendarWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
11 * Creates an mw.widgets.CalendarWidget object.
13 * You will most likely want to use mw.widgets.DateInputWidget instead of CalendarWidget directly.
16 * @extends OO.ui.Widget
17 * @mixins OO.ui.mixin.TabIndexedElement
18 * @mixins OO.ui.mixin.FloatableElement
21 * @param {Object} [config] Configuration options
22 * @cfg {boolean} [lazyInitOnToggle=false] Don't build most of the interface until
23 * `.toggle( true )` is called. Meant to be used when the calendar is not immediately visible.
24 * @cfg {string} [precision='day'] Date precision to use, 'day' or 'month'
25 * @cfg {string|null} [date=null] Day or month date (depending on `precision`), in the format
26 * 'YYYY-MM-DD' or 'YYYY-MM'. When null, the calendar will show today's date, but not select
29 mw
.widgets
.CalendarWidget
= function MWWCalendarWidget( config
) {
30 // Config initialization
31 config
= config
|| {};
34 mw
.widgets
.CalendarWidget
.parent
.call( this, config
);
37 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$element
} ) );
38 OO
.ui
.mixin
.FloatableElement
.call( this, config
);
41 this.lazyInitOnToggle
= !!config
.lazyInitOnToggle
;
42 this.precision
= config
.precision
|| 'day';
43 // Currently selected date (day or month)
45 // Current UI state (date and precision we're displaying right now)
47 this.displayLayer
= this.getDisplayLayers()[ 0 ]; // 'month', 'year', 'duodecade'
49 this.$header
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-header' );
50 this.$bodyOuterWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-outer-wrapper' );
51 this.$bodyWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-wrapper' );
52 this.$body
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body' );
56 focus
: this.onFocus
.bind( this ),
57 mousedown
: this.onClick
.bind( this ),
58 keydown
: this.onKeyDown
.bind( this )
63 .addClass( 'mw-widget-calendarWidget' )
64 .append( this.$header
, this.$bodyOuterWrapper
.append( this.$bodyWrapper
.append( this.$body
) ) );
65 if ( !this.lazyInitOnToggle
) {
66 this.buildHeaderButtons();
68 this.setDate( config
.date
!== undefined ? config
.date
: null );
73 OO
.inheritClass( mw
.widgets
.CalendarWidget
, OO
.ui
.Widget
);
74 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.TabIndexedElement
);
75 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.FloatableElement
);
82 * A change event is emitted when the chosen date changes.
84 * @param {string} date Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'
90 * Get the date format ('YYYY-MM-DD' or 'YYYY-MM', depending on precision), which is used
91 * internally and for dates accepted by #setDate and returned by #getDate.
94 * @return {string} Format
96 mw
.widgets
.CalendarWidget
.prototype.getDateFormat = function () {
104 * Get the date precision this calendar uses, 'day' or 'month'.
107 * @return {string} Precision, 'day' or 'month'
109 mw
.widgets
.CalendarWidget
.prototype.getPrecision = function () {
110 return this.precision
;
114 * Get list of possible display layers.
117 * @return {string[]} Layers
119 mw
.widgets
.CalendarWidget
.prototype.getDisplayLayers = function () {
120 return [ 'month', 'year', 'duodecade' ].slice( this.precision
=== 'month' ? 1 : 0 );
124 * Update the calendar.
127 * @param {string|null} [fade=null] Direction in which to fade out current calendar contents,
128 * 'previous', 'next', 'up' or 'down'; or 'auto', which has the same result as 'previous' or
129 * 'next' depending on whether the current date is later or earlier than the previous.
131 mw
.widgets
.CalendarWidget
.prototype.updateUI = function ( fade
) {
132 var items
, today
, selected
, currentMonth
, currentYear
, currentDay
, i
, needsFade
,
133 $bodyWrapper
= this.$bodyWrapper
;
135 if ( this.lazyInitOnToggle
) {
136 // We're being called from the constructor and not being shown yet, do nothing
141 this.displayLayer
=== this.previousDisplayLayer
&&
142 this.date
=== this.previousDate
&&
143 this.previousMoment
&&
144 this.previousMoment
.isSame( this.moment
, this.precision
=== 'month' ? 'month' : 'day' )
150 if ( fade
=== 'auto' ) {
151 if ( !this.previousMoment
) {
153 } else if ( this.previousMoment
.isBefore( this.moment
, this.precision
=== 'month' ? 'month' : 'day' ) ) {
155 } else if ( this.previousMoment
.isAfter( this.moment
, this.precision
=== 'month' ? 'month' : 'day' ) ) {
163 if ( this.$oldBody
) {
164 this.$oldBody
.remove();
166 this.$oldBody
= this.$body
.addClass( 'mw-widget-calendarWidget-old-body' );
167 // Clone without children
168 this.$body
= $( this.$body
[ 0 ].cloneNode( false ) )
169 .removeClass( 'mw-widget-calendarWidget-old-body' )
170 .toggleClass( 'mw-widget-calendarWidget-body-month', this.displayLayer
=== 'month' )
171 .toggleClass( 'mw-widget-calendarWidget-body-year', this.displayLayer
=== 'year' )
172 .toggleClass( 'mw-widget-calendarWidget-body-duodecade', this.displayLayer
=== 'duodecade' );
175 selected
= moment( this.getDate(), this.getDateFormat() );
177 switch ( this.displayLayer
) {
179 this.labelButton
.setLabel( this.moment
.format( 'MMMM YYYY' ) );
180 this.upButton
.toggle( true );
182 // First week displayed is the first week spanned by the month, unless it begins on Monday, in
183 // which case first week displayed is the previous week. This makes the calendar "balanced"
184 // and also neatly handles 28-day February sometimes spanning only 4 weeks.
185 currentDay
= moment( this.moment
).startOf( 'month' ).subtract( 1, 'day' ).startOf( 'week' );
187 // Day-of-week labels. Localisation-independent: works with weeks starting on Saturday, Sunday
189 for ( i
= 0; i
< 7; i
++ ) {
192 .addClass( 'mw-widget-calendarWidget-day-heading' )
193 .text( currentDay
.format( 'dd' ) )
195 currentDay
.add( 1, 'day' );
197 currentDay
.subtract( 7, 'days' );
199 // Actual calendar month. Always displays 6 weeks, for consistency (months can span 4 to 6
201 for ( i
= 0; i
< 42; i
++ ) {
204 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-day' )
205 .toggleClass( 'mw-widget-calendarWidget-day-additional', !currentDay
.isSame( this.moment
, 'month' ) )
206 .toggleClass( 'mw-widget-calendarWidget-day-today', currentDay
.isSame( today
, 'day' ) )
207 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentDay
.isSame( selected
, 'day' ) )
208 .text( currentDay
.format( 'D' ) )
209 .data( 'date', currentDay
.date() )
210 .data( 'month', currentDay
.month() )
211 .data( 'year', currentDay
.year() )
213 currentDay
.add( 1, 'day' );
218 this.labelButton
.setLabel( this.moment
.format( 'YYYY' ) );
219 this.upButton
.toggle( true );
221 currentMonth
= moment( this.moment
).startOf( 'year' );
222 for ( i
= 0; i
< 12; i
++ ) {
225 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-month' )
226 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentMonth
.isSame( selected
, 'month' ) )
227 .text( currentMonth
.format( 'MMMM' ) )
228 .data( 'month', currentMonth
.month() )
230 currentMonth
.add( 1, 'month' );
232 // Shuffle the array to display months in columns rather than rows.
234 items
[ 0 ], items
[ 6 ], // | January | July |
235 items
[ 1 ], items
[ 7 ], // | February | August |
236 items
[ 2 ], items
[ 8 ], // | March | September |
237 items
[ 3 ], items
[ 9 ], // | April | October |
238 items
[ 4 ], items
[ 10 ], // | May | November |
239 items
[ 5 ], items
[ 11 ] // | June | December |
244 this.labelButton
.setLabel( null );
245 this.upButton
.toggle( false );
247 currentYear
= moment( { year
: Math
.floor( this.moment
.year() / 20 ) * 20 } );
248 for ( i
= 0; i
< 20; i
++ ) {
251 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-year' )
252 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentYear
.isSame( selected
, 'year' ) )
253 .text( currentYear
.format( 'YYYY' ) )
254 .data( 'year', currentYear
.year() )
256 currentYear
.add( 1, 'year' );
261 this.$body
.append
.apply( this.$body
, items
);
264 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-up' )
265 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-down' )
266 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-previous' )
267 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-next' );
269 needsFade
= this.previousDisplayLayer
!== this.displayLayer
;
270 if ( this.displayLayer
=== 'month' ) {
271 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'month' );
272 } else if ( this.displayLayer
=== 'year' ) {
273 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'year' );
274 } else if ( this.displayLayer
=== 'duodecade' ) {
275 needsFade
= needsFade
|| (
276 Math
.floor( this.moment
.year() / 20 ) * 20 !==
277 Math
.floor( this.previousMoment
.year() / 20 ) * 20
281 if ( fade
&& needsFade
) {
282 this.$oldBody
.find( '.mw-widget-calendarWidget-item-selected' )
283 .removeClass( 'mw-widget-calendarWidget-item-selected' );
284 if ( fade
=== 'previous' || fade
=== 'up' ) {
285 this.$body
.insertBefore( this.$oldBody
);
286 } else if ( fade
=== 'next' || fade
=== 'down' ) {
287 this.$body
.insertAfter( this.$oldBody
);
289 setTimeout( function () {
290 $bodyWrapper
.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade
);
293 this.$oldBody
.replaceWith( this.$body
);
296 this.previousMoment
= moment( this.moment
);
297 this.previousDisplayLayer
= this.displayLayer
;
298 this.previousDate
= this.date
;
300 this.$body
.on( 'click', this.onBodyClick
.bind( this ) );
304 * Construct and display buttons to navigate the calendar.
308 mw
.widgets
.CalendarWidget
.prototype.buildHeaderButtons = function () {
309 this.labelButton
= new OO
.ui
.ButtonWidget( {
313 classes
: [ 'mw-widget-calendarWidget-labelButton' ]
315 this.upButton
= new OO
.ui
.ButtonWidget( {
319 classes
: [ 'mw-widget-calendarWidget-upButton' ]
321 this.prevButton
= new OO
.ui
.ButtonWidget( {
325 classes
: [ 'mw-widget-calendarWidget-prevButton' ]
327 this.nextButton
= new OO
.ui
.ButtonWidget( {
331 classes
: [ 'mw-widget-calendarWidget-nextButton' ]
334 this.labelButton
.connect( this, { click
: 'onUpButtonClick' } );
335 this.upButton
.connect( this, { click
: 'onUpButtonClick' } );
336 this.prevButton
.connect( this, { click
: 'onPrevButtonClick' } );
337 this.nextButton
.connect( this, { click
: 'onNextButtonClick' } );
340 this.prevButton
.$element
,
341 this.nextButton
.$element
,
342 this.upButton
.$element
,
343 this.labelButton
.$element
348 * Handle click events on the "up" button, switching to less precise view.
352 mw
.widgets
.CalendarWidget
.prototype.onUpButtonClick = function () {
354 layers
= this.getDisplayLayers(),
355 currentLayer
= layers
.indexOf( this.displayLayer
);
356 if ( currentLayer
!== layers
.length
- 1 ) {
358 this.displayLayer
= layers
[ currentLayer
+ 1 ];
359 this.updateUI( 'up' );
366 * Handle click events on the "previous" button, switching to previous pane.
370 mw
.widgets
.CalendarWidget
.prototype.onPrevButtonClick = function () {
371 switch ( this.displayLayer
) {
373 this.moment
.subtract( 1, 'month' );
376 this.moment
.subtract( 1, 'year' );
379 this.moment
.subtract( 20, 'years' );
382 this.updateUI( 'previous' );
386 * Handle click events on the "next" button, switching to next pane.
390 mw
.widgets
.CalendarWidget
.prototype.onNextButtonClick = function () {
391 switch ( this.displayLayer
) {
393 this.moment
.add( 1, 'month' );
396 this.moment
.add( 1, 'year' );
399 this.moment
.add( 20, 'years' );
402 this.updateUI( 'next' );
406 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
407 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
411 * @param {jQuery.Event} e Click event
413 mw
.widgets
.CalendarWidget
.prototype.onBodyClick = function ( e
) {
415 $target
= $( e
.target
),
416 layers
= this.getDisplayLayers(),
417 currentLayer
= layers
.indexOf( this.displayLayer
);
418 if ( $target
.data( 'year' ) !== undefined ) {
419 this.moment
.year( $target
.data( 'year' ) );
421 if ( $target
.data( 'month' ) !== undefined ) {
422 this.moment
.month( $target
.data( 'month' ) );
424 if ( $target
.data( 'date' ) !== undefined ) {
425 this.moment
.date( $target
.data( 'date' ) );
427 if ( currentLayer
=== 0 ) {
428 this.setDateFromMoment();
429 this.updateUI( 'auto' );
432 this.displayLayer
= layers
[ currentLayer
- 1 ];
433 this.updateUI( 'down' );
440 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
441 * When null, the calendar will show today's date, but not select it. When invalid, the date
444 mw
.widgets
.CalendarWidget
.prototype.setDate = function ( date
) {
445 var mom
= date
!== null ? moment( date
, this.getDateFormat() ) : moment();
446 if ( mom
.isValid() ) {
448 if ( date
!== null ) {
449 this.setDateFromMoment();
450 } else if ( this.date
!== null ) {
452 this.emit( 'change', this.date
);
454 this.displayLayer
= this.getDisplayLayers()[ 0 ];
460 * Reset the user interface of this widget to reflect selected date.
462 mw
.widgets
.CalendarWidget
.prototype.resetUI = function () {
463 this.moment
= this.getDate() !== null ? moment( this.getDate(), this.getDateFormat() ) : moment();
464 this.displayLayer
= this.getDisplayLayers()[ 0 ];
469 * Set the date from moment object.
473 mw
.widgets
.CalendarWidget
.prototype.setDateFromMoment = function () {
474 // Switch to English locale to avoid number formatting. We want the internal value to be
475 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
476 var newDate
= moment( this.moment
).locale( 'en' ).format( this.getDateFormat() );
477 if ( this.date
!== newDate
) {
479 this.emit( 'change', this.date
);
484 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
487 * @return {string|null} Date string
489 mw
.widgets
.CalendarWidget
.prototype.getDate = function () {
494 * Handle focus events.
498 mw
.widgets
.CalendarWidget
.prototype.onFocus = function () {
499 this.displayLayer
= this.getDisplayLayers()[ 0 ];
500 this.updateUI( 'down' );
504 * Handle mouse click events.
507 * @param {jQuery.Event} e Mouse click event
508 * @return {boolean} False to cancel the default event
510 mw
.widgets
.CalendarWidget
.prototype.onClick = function ( e
) {
511 if ( !this.isDisabled() && e
.which
=== 1 ) {
512 // Prevent unintended focussing
518 * Handle key down events.
521 * @param {jQuery.Event} e Key down event
522 * @return {boolean} False to cancel the default event
524 mw
.widgets
.CalendarWidget
.prototype.onKeyDown = function ( e
) {
526 dir
= OO
.ui
.Element
.static.getDir( this.$element
),
527 nextDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.RIGHT
: OO
.ui
.Keys
.LEFT
,
528 prevDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.LEFT
: OO
.ui
.Keys
.RIGHT
,
531 if ( !this.isDisabled() ) {
533 case prevDirectionKey
:
534 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'day' );
536 case nextDirectionKey
:
537 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'day' );
540 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'week' );
542 case OO
.ui
.Keys
.DOWN
:
543 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'week' );
545 case OO
.ui
.Keys
.PAGEUP
:
546 this.moment
.subtract( 1, this.precision
=== 'month' ? 'year' : 'month' );
548 case OO
.ui
.Keys
.PAGEDOWN
:
549 this.moment
.add( 1, this.precision
=== 'month' ? 'year' : 'month' );
557 this.displayLayer
= this.getDisplayLayers()[ 0 ];
558 this.setDateFromMoment();
559 this.updateUI( 'auto' );
568 mw
.widgets
.CalendarWidget
.prototype.toggle = function ( visible
) {
569 if ( this.lazyInitOnToggle
&& visible
) {
570 this.lazyInitOnToggle
= false;
571 this.buildHeaderButtons();
576 mw
.widgets
.CalendarWidget
.parent
.prototype.toggle
.call( this, visible
);
578 if ( this.$floatableContainer
) {
579 this.togglePositioning( this.isVisible() );
585 }( jQuery
, mediaWiki
) );