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' );
53 this.labelButton
= new OO
.ui
.ButtonWidget( {
57 classes
: [ 'mw-widget-calendarWidget-labelButton' ]
59 this.upButton
= new OO
.ui
.ButtonWidget( {
63 classes
: [ 'mw-widget-calendarWidget-upButton' ]
65 this.prevButton
= new OO
.ui
.ButtonWidget( {
69 classes
: [ 'mw-widget-calendarWidget-prevButton' ]
71 this.nextButton
= new OO
.ui
.ButtonWidget( {
75 classes
: [ 'mw-widget-calendarWidget-nextButton' ]
79 this.labelButton
.connect( this, { click
: 'onUpButtonClick' } );
80 this.upButton
.connect( this, { click
: 'onUpButtonClick' } );
81 this.prevButton
.connect( this, { click
: 'onPrevButtonClick' } );
82 this.nextButton
.connect( this, { click
: 'onNextButtonClick' } );
84 focus
: this.onFocus
.bind( this ),
85 mousedown
: this.onClick
.bind( this ),
86 keydown
: this.onKeyDown
.bind( this )
91 .addClass( 'mw-widget-calendarWidget' )
92 .append( this.$header
, this.$bodyOuterWrapper
.append( this.$bodyWrapper
.append( this.$body
) ) );
94 this.prevButton
.$element
,
95 this.nextButton
.$element
,
96 this.upButton
.$element
,
97 this.labelButton
.$element
99 this.setDate( config
.date
!== undefined ? config
.date
: null );
104 OO
.inheritClass( mw
.widgets
.CalendarWidget
, OO
.ui
.Widget
);
105 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.TabIndexedElement
);
106 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.FloatableElement
);
113 * A change event is emitted when the chosen date changes.
115 * @param {string} date Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'
121 * Get the date format ('YYYY-MM-DD' or 'YYYY-MM', depending on precision), which is used
122 * internally and for dates accepted by #setDate and returned by #getDate.
125 * @return {string} Format
127 mw
.widgets
.CalendarWidget
.prototype.getDateFormat = function () {
135 * Get the date precision this calendar uses, 'day' or 'month'.
138 * @return {string} Precision, 'day' or 'month'
140 mw
.widgets
.CalendarWidget
.prototype.getPrecision = function () {
141 return this.precision
;
145 * Get list of possible display layers.
148 * @return {string[]} Layers
150 mw
.widgets
.CalendarWidget
.prototype.getDisplayLayers = function () {
151 return [ 'month', 'year', 'duodecade' ].slice( this.precision
=== 'month' ? 1 : 0 );
155 * Update the calendar.
158 * @param {string|null} [fade=null] Direction in which to fade out current calendar contents,
159 * 'previous', 'next', 'up' or 'down'; or 'auto', which has the same result as 'previous' or
160 * 'next' depending on whether the current date is later or earlier than the previous.
162 mw
.widgets
.CalendarWidget
.prototype.updateUI = function ( fade
) {
163 var items
, today
, selected
, currentMonth
, currentYear
, currentDay
, i
, needsFade
,
164 $bodyWrapper
= this.$bodyWrapper
;
166 if ( this.lazyInitOnToggle
) {
167 // We're being called from the constructor and not being shown yet, do nothing
172 this.displayLayer
=== this.previousDisplayLayer
&&
173 this.date
=== this.previousDate
&&
174 this.previousMoment
&&
175 this.previousMoment
.isSame( this.moment
, this.precision
=== 'month' ? 'month' : 'day' )
181 if ( fade
=== 'auto' ) {
182 if ( !this.previousMoment
) {
184 } else if ( this.previousMoment
.isBefore( this.moment
, this.precision
=== 'month' ? 'month' : 'day' ) ) {
186 } else if ( this.previousMoment
.isAfter( this.moment
, this.precision
=== 'month' ? 'month' : 'day' ) ) {
194 if ( this.$oldBody
) {
195 this.$oldBody
.remove();
197 this.$oldBody
= this.$body
.addClass( 'mw-widget-calendarWidget-old-body' );
198 // Clone without children
199 this.$body
= $( this.$body
[ 0 ].cloneNode( false ) )
200 .removeClass( 'mw-widget-calendarWidget-old-body' )
201 .toggleClass( 'mw-widget-calendarWidget-body-month', this.displayLayer
=== 'month' )
202 .toggleClass( 'mw-widget-calendarWidget-body-year', this.displayLayer
=== 'year' )
203 .toggleClass( 'mw-widget-calendarWidget-body-duodecade', this.displayLayer
=== 'duodecade' );
206 selected
= moment( this.getDate(), this.getDateFormat() );
208 switch ( this.displayLayer
) {
210 this.labelButton
.setLabel( this.moment
.format( 'MMMM YYYY' ) );
211 this.upButton
.toggle( true );
213 // First week displayed is the first week spanned by the month, unless it begins on Monday, in
214 // which case first week displayed is the previous week. This makes the calendar "balanced"
215 // and also neatly handles 28-day February sometimes spanning only 4 weeks.
216 currentDay
= moment( this.moment
).startOf( 'month' ).subtract( 1, 'day' ).startOf( 'week' );
218 // Day-of-week labels. Localisation-independent: works with weeks starting on Saturday, Sunday
220 for ( i
= 0; i
< 7; i
++ ) {
223 .addClass( 'mw-widget-calendarWidget-day-heading' )
224 .text( currentDay
.format( 'dd' ) )
226 currentDay
.add( 1, 'day' );
228 currentDay
.subtract( 7, 'days' );
230 // Actual calendar month. Always displays 6 weeks, for consistency (months can span 4 to 6
232 for ( i
= 0; i
< 42; i
++ ) {
235 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-day' )
236 .toggleClass( 'mw-widget-calendarWidget-day-additional', !currentDay
.isSame( this.moment
, 'month' ) )
237 .toggleClass( 'mw-widget-calendarWidget-day-today', currentDay
.isSame( today
, 'day' ) )
238 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentDay
.isSame( selected
, 'day' ) )
239 .text( currentDay
.format( 'D' ) )
240 .data( 'date', currentDay
.date() )
241 .data( 'month', currentDay
.month() )
242 .data( 'year', currentDay
.year() )
244 currentDay
.add( 1, 'day' );
249 this.labelButton
.setLabel( this.moment
.format( 'YYYY' ) );
250 this.upButton
.toggle( true );
252 currentMonth
= moment( this.moment
).startOf( 'year' );
253 for ( i
= 0; i
< 12; i
++ ) {
256 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-month' )
257 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentMonth
.isSame( selected
, 'month' ) )
258 .text( currentMonth
.format( 'MMMM' ) )
259 .data( 'month', currentMonth
.month() )
261 currentMonth
.add( 1, 'month' );
263 // Shuffle the array to display months in columns rather than rows.
265 items
[ 0 ], items
[ 6 ], // | January | July |
266 items
[ 1 ], items
[ 7 ], // | February | August |
267 items
[ 2 ], items
[ 8 ], // | March | September |
268 items
[ 3 ], items
[ 9 ], // | April | October |
269 items
[ 4 ], items
[ 10 ], // | May | November |
270 items
[ 5 ], items
[ 11 ] // | June | December |
275 this.labelButton
.setLabel( null );
276 this.upButton
.toggle( false );
278 currentYear
= moment( { year
: Math
.floor( this.moment
.year() / 20 ) * 20 } );
279 for ( i
= 0; i
< 20; i
++ ) {
282 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-year' )
283 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentYear
.isSame( selected
, 'year' ) )
284 .text( currentYear
.format( 'YYYY' ) )
285 .data( 'year', currentYear
.year() )
287 currentYear
.add( 1, 'year' );
292 this.$body
.append
.apply( this.$body
, items
);
295 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-up' )
296 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-down' )
297 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-previous' )
298 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-next' );
300 needsFade
= this.previousDisplayLayer
!== this.displayLayer
;
301 if ( this.displayLayer
=== 'month' ) {
302 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'month' );
303 } else if ( this.displayLayer
=== 'year' ) {
304 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'year' );
305 } else if ( this.displayLayer
=== 'duodecade' ) {
306 needsFade
= needsFade
|| (
307 Math
.floor( this.moment
.year() / 20 ) * 20 !==
308 Math
.floor( this.previousMoment
.year() / 20 ) * 20
312 if ( fade
&& needsFade
) {
313 this.$oldBody
.find( '.mw-widget-calendarWidget-item-selected' )
314 .removeClass( 'mw-widget-calendarWidget-item-selected' );
315 if ( fade
=== 'previous' || fade
=== 'up' ) {
316 this.$body
.insertBefore( this.$oldBody
);
317 } else if ( fade
=== 'next' || fade
=== 'down' ) {
318 this.$body
.insertAfter( this.$oldBody
);
320 setTimeout( function () {
321 $bodyWrapper
.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade
);
324 this.$oldBody
.replaceWith( this.$body
);
327 this.previousMoment
= moment( this.moment
);
328 this.previousDisplayLayer
= this.displayLayer
;
329 this.previousDate
= this.date
;
331 this.$body
.on( 'click', this.onBodyClick
.bind( this ) );
335 * Handle click events on the "up" button, switching to less precise view.
339 mw
.widgets
.CalendarWidget
.prototype.onUpButtonClick = function () {
341 layers
= this.getDisplayLayers(),
342 currentLayer
= layers
.indexOf( this.displayLayer
);
343 if ( currentLayer
!== layers
.length
- 1 ) {
345 this.displayLayer
= layers
[ currentLayer
+ 1 ];
346 this.updateUI( 'up' );
353 * Handle click events on the "previous" button, switching to previous pane.
357 mw
.widgets
.CalendarWidget
.prototype.onPrevButtonClick = function () {
358 switch ( this.displayLayer
) {
360 this.moment
.subtract( 1, 'month' );
363 this.moment
.subtract( 1, 'year' );
366 this.moment
.subtract( 20, 'years' );
369 this.updateUI( 'previous' );
373 * Handle click events on the "next" button, switching to next pane.
377 mw
.widgets
.CalendarWidget
.prototype.onNextButtonClick = function () {
378 switch ( this.displayLayer
) {
380 this.moment
.add( 1, 'month' );
383 this.moment
.add( 1, 'year' );
386 this.moment
.add( 20, 'years' );
389 this.updateUI( 'next' );
393 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
394 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
399 mw
.widgets
.CalendarWidget
.prototype.onBodyClick = function ( e
) {
401 $target
= $( e
.target
),
402 layers
= this.getDisplayLayers(),
403 currentLayer
= layers
.indexOf( this.displayLayer
);
404 if ( $target
.data( 'year' ) !== undefined ) {
405 this.moment
.year( $target
.data( 'year' ) );
407 if ( $target
.data( 'month' ) !== undefined ) {
408 this.moment
.month( $target
.data( 'month' ) );
410 if ( $target
.data( 'date' ) !== undefined ) {
411 this.moment
.date( $target
.data( 'date' ) );
413 if ( currentLayer
=== 0 ) {
414 this.setDateFromMoment();
415 this.updateUI( 'auto' );
418 this.displayLayer
= layers
[ currentLayer
- 1 ];
419 this.updateUI( 'down' );
426 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
427 * When null, the calendar will show today's date, but not select it. When invalid, the date
430 mw
.widgets
.CalendarWidget
.prototype.setDate = function ( date
) {
431 var mom
= date
!== null ? moment( date
, this.getDateFormat() ) : moment();
432 if ( mom
.isValid() ) {
434 if ( date
!== null ) {
435 this.setDateFromMoment();
436 } else if ( this.date
!== null ) {
438 this.emit( 'change', this.date
);
440 this.displayLayer
= this.getDisplayLayers()[ 0 ];
446 * Reset the user interface of this widget to reflect selected date.
448 mw
.widgets
.CalendarWidget
.prototype.resetUI = function () {
449 this.moment
= this.getDate() !== null ? moment( this.getDate(), this.getDateFormat() ) : moment();
450 this.displayLayer
= this.getDisplayLayers()[ 0 ];
455 * Set the date from moment object.
459 mw
.widgets
.CalendarWidget
.prototype.setDateFromMoment = function () {
460 // Switch to English locale to avoid number formatting. We want the internal value to be
461 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
462 var newDate
= moment( this.moment
).locale( 'en' ).format( this.getDateFormat() );
463 if ( this.date
!== newDate
) {
465 this.emit( 'change', this.date
);
470 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
473 * @return {string|null} Date string
475 mw
.widgets
.CalendarWidget
.prototype.getDate = function () {
480 * Handle focus events.
484 mw
.widgets
.CalendarWidget
.prototype.onFocus = function () {
485 this.displayLayer
= this.getDisplayLayers()[ 0 ];
486 this.updateUI( 'down' );
490 * Handle mouse click events.
493 * @param {jQuery.Event} e Mouse click event
495 mw
.widgets
.CalendarWidget
.prototype.onClick = function ( e
) {
496 if ( !this.isDisabled() && e
.which
=== 1 ) {
497 // Prevent unintended focussing
503 * Handle key down events.
506 * @param {jQuery.Event} e Key down event
508 mw
.widgets
.CalendarWidget
.prototype.onKeyDown = function ( e
) {
511 dir
= OO
.ui
.Element
.static.getDir( this.$element
),
513 nextDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.RIGHT
: OO
.ui
.Keys
.LEFT
,
514 prevDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.LEFT
: OO
.ui
.Keys
.RIGHT
,
517 if ( !this.isDisabled() ) {
519 case prevDirectionKey
:
520 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'day' );
522 case nextDirectionKey
:
523 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'day' );
526 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'week' );
528 case OO
.ui
.Keys
.DOWN
:
529 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'week' );
531 case OO
.ui
.Keys
.PAGEUP
:
532 this.moment
.subtract( 1, this.precision
=== 'month' ? 'year' : 'month' );
534 case OO
.ui
.Keys
.PAGEDOWN
:
535 this.moment
.add( 1, this.precision
=== 'month' ? 'year' : 'month' );
543 this.displayLayer
= this.getDisplayLayers()[ 0 ];
544 this.setDateFromMoment();
545 this.updateUI( 'auto' );
554 mw
.widgets
.CalendarWidget
.prototype.toggle = function ( visible
) {
555 if ( this.lazyInitOnToggle
&& visible
) {
556 this.lazyInitOnToggle
= false;
561 mw
.widgets
.CalendarWidget
.parent
.prototype.toggle
.call( this, visible
);
563 if ( this.$floatableContainer
) {
564 this.togglePositioning( this.isVisible() );
570 }( jQuery
, mediaWiki
) );