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 {string} [precision='day'] Date precision to use, 'day' or 'month'
23 * @cfg {string|null} [date=null] Day or month date (depending on `precision`), in the format
24 * 'YYYY-MM-DD' or 'YYYY-MM'. When null, the calendar will show today's date, but not select
27 mw
.widgets
.CalendarWidget
= function MWWCalendarWidget( config
) {
28 // Config initialization
29 config
= config
|| {};
32 mw
.widgets
.CalendarWidget
.parent
.call( this, config
);
35 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$element
} ) );
36 OO
.ui
.mixin
.FloatableElement
.call( this, config
);
39 this.precision
= config
.precision
|| 'day';
40 // Currently selected date (day or month)
42 // Current UI state (date and precision we're displaying right now)
44 this.displayLayer
= this.getDisplayLayers()[ 0 ]; // 'month', 'year', 'duodecade'
46 this.$header
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-header' );
47 this.$bodyOuterWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-outer-wrapper' );
48 this.$bodyWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-wrapper' );
49 this.$body
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body' );
50 this.labelButton
= new OO
.ui
.ButtonWidget( {
54 classes
: [ 'mw-widget-calendarWidget-labelButton' ]
56 this.upButton
= new OO
.ui
.ButtonWidget( {
60 classes
: [ 'mw-widget-calendarWidget-upButton' ]
62 this.prevButton
= new OO
.ui
.ButtonWidget( {
66 classes
: [ 'mw-widget-calendarWidget-prevButton' ]
68 this.nextButton
= new OO
.ui
.ButtonWidget( {
72 classes
: [ 'mw-widget-calendarWidget-nextButton' ]
76 this.labelButton
.connect( this, { click
: 'onUpButtonClick' } );
77 this.upButton
.connect( this, { click
: 'onUpButtonClick' } );
78 this.prevButton
.connect( this, { click
: 'onPrevButtonClick' } );
79 this.nextButton
.connect( this, { click
: 'onNextButtonClick' } );
81 focus
: this.onFocus
.bind( this ),
82 mousedown
: this.onClick
.bind( this ),
83 keydown
: this.onKeyDown
.bind( this )
88 .addClass( 'mw-widget-calendarWidget' )
89 .append( this.$header
, this.$bodyOuterWrapper
.append( this.$bodyWrapper
.append( this.$body
) ) );
91 this.prevButton
.$element
,
92 this.nextButton
.$element
,
93 this.upButton
.$element
,
94 this.labelButton
.$element
96 this.setDate( config
.date
!== undefined ? config
.date
: null );
101 OO
.inheritClass( mw
.widgets
.CalendarWidget
, OO
.ui
.Widget
);
102 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.TabIndexedElement
);
103 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.FloatableElement
);
110 * A change event is emitted when the chosen date changes.
112 * @param {string} date Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'
118 * Get the date format ('YYYY-MM-DD' or 'YYYY-MM', depending on precision), which is used
119 * internally and for dates accepted by #setDate and returned by #getDate.
122 * @return {string} Format
124 mw
.widgets
.CalendarWidget
.prototype.getDateFormat = function () {
132 * Get the date precision this calendar uses, 'day' or 'month'.
135 * @return {string} Precision, 'day' or 'month'
137 mw
.widgets
.CalendarWidget
.prototype.getPrecision = function () {
138 return this.precision
;
142 * Get list of possible display layers.
145 * @return {string[]} Layers
147 mw
.widgets
.CalendarWidget
.prototype.getDisplayLayers = function () {
148 return [ 'month', 'year', 'duodecade' ].slice( this.precision
=== 'month' ? 1 : 0 );
152 * Update the calendar.
155 * @param {string|null} [fade=null] Direction in which to fade out current calendar contents,
156 * 'previous', 'next', 'up' or 'down'; or 'auto', which has the same result as 'previous' or
157 * 'next' depending on whether the current date is later or earlier than the previous.
159 mw
.widgets
.CalendarWidget
.prototype.updateUI = function ( fade
) {
160 var items
, today
, selected
, currentMonth
, currentYear
, currentDay
, i
, needsFade
,
161 $bodyWrapper
= this.$bodyWrapper
;
164 this.displayLayer
=== this.previousDisplayLayer
&&
165 this.date
=== this.previousDate
&&
166 this.previousMoment
&&
167 this.previousMoment
.isSame( this.moment
, this.precision
=== 'month' ? 'month' : 'day' )
173 if ( fade
=== 'auto' ) {
174 if ( !this.previousMoment
) {
176 } else if ( this.previousMoment
.isBefore( this.moment
, this.precision
=== 'month' ? 'month' : 'day' ) ) {
178 } else if ( this.previousMoment
.isAfter( this.moment
, this.precision
=== 'month' ? 'month' : 'day' ) ) {
186 if ( this.$oldBody
) {
187 this.$oldBody
.remove();
189 this.$oldBody
= this.$body
.addClass( 'mw-widget-calendarWidget-old-body' );
190 // Clone without children
191 this.$body
= $( this.$body
[ 0 ].cloneNode( false ) )
192 .removeClass( 'mw-widget-calendarWidget-old-body' )
193 .toggleClass( 'mw-widget-calendarWidget-body-month', this.displayLayer
=== 'month' )
194 .toggleClass( 'mw-widget-calendarWidget-body-year', this.displayLayer
=== 'year' )
195 .toggleClass( 'mw-widget-calendarWidget-body-duodecade', this.displayLayer
=== 'duodecade' );
198 selected
= moment( this.getDate(), this.getDateFormat() );
200 switch ( this.displayLayer
) {
202 this.labelButton
.setLabel( this.moment
.format( 'MMMM YYYY' ) );
203 this.upButton
.toggle( true );
205 // First week displayed is the first week spanned by the month, unless it begins on Monday, in
206 // which case first week displayed is the previous week. This makes the calendar "balanced"
207 // and also neatly handles 28-day February sometimes spanning only 4 weeks.
208 currentDay
= moment( this.moment
).startOf( 'month' ).subtract( 1, 'day' ).startOf( 'week' );
210 // Day-of-week labels. Localisation-independent: works with weeks starting on Saturday, Sunday
212 for ( i
= 0; i
< 7; i
++ ) {
215 .addClass( 'mw-widget-calendarWidget-day-heading' )
216 .text( currentDay
.format( 'dd' ) )
218 currentDay
.add( 1, 'day' );
220 currentDay
.subtract( 7, 'days' );
222 // Actual calendar month. Always displays 6 weeks, for consistency (months can span 4 to 6
224 for ( i
= 0; i
< 42; i
++ ) {
227 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-day' )
228 .toggleClass( 'mw-widget-calendarWidget-day-additional', !currentDay
.isSame( this.moment
, 'month' ) )
229 .toggleClass( 'mw-widget-calendarWidget-day-today', currentDay
.isSame( today
, 'day' ) )
230 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentDay
.isSame( selected
, 'day' ) )
231 .text( currentDay
.format( 'D' ) )
232 .data( 'date', currentDay
.date() )
233 .data( 'month', currentDay
.month() )
234 .data( 'year', currentDay
.year() )
236 currentDay
.add( 1, 'day' );
241 this.labelButton
.setLabel( this.moment
.format( 'YYYY' ) );
242 this.upButton
.toggle( true );
244 currentMonth
= moment( this.moment
).startOf( 'year' );
245 for ( i
= 0; i
< 12; i
++ ) {
248 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-month' )
249 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentMonth
.isSame( selected
, 'month' ) )
250 .text( currentMonth
.format( 'MMMM' ) )
251 .data( 'month', currentMonth
.month() )
253 currentMonth
.add( 1, 'month' );
255 // Shuffle the array to display months in columns rather than rows.
257 items
[ 0 ], items
[ 6 ], // | January | July |
258 items
[ 1 ], items
[ 7 ], // | February | August |
259 items
[ 2 ], items
[ 8 ], // | March | September |
260 items
[ 3 ], items
[ 9 ], // | April | October |
261 items
[ 4 ], items
[ 10 ], // | May | November |
262 items
[ 5 ], items
[ 11 ] // | June | December |
267 this.labelButton
.setLabel( null );
268 this.upButton
.toggle( false );
270 currentYear
= moment( { year
: Math
.floor( this.moment
.year() / 20 ) * 20 } );
271 for ( i
= 0; i
< 20; i
++ ) {
274 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-year' )
275 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentYear
.isSame( selected
, 'year' ) )
276 .text( currentYear
.format( 'YYYY' ) )
277 .data( 'year', currentYear
.year() )
279 currentYear
.add( 1, 'year' );
284 this.$body
.append
.apply( this.$body
, items
);
287 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-up' )
288 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-down' )
289 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-previous' )
290 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-next' );
292 needsFade
= this.previousDisplayLayer
!== this.displayLayer
;
293 if ( this.displayLayer
=== 'month' ) {
294 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'month' );
295 } else if ( this.displayLayer
=== 'year' ) {
296 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'year' );
297 } else if ( this.displayLayer
=== 'duodecade' ) {
298 needsFade
= needsFade
|| (
299 Math
.floor( this.moment
.year() / 20 ) * 20 !==
300 Math
.floor( this.previousMoment
.year() / 20 ) * 20
304 if ( fade
&& needsFade
) {
305 this.$oldBody
.find( '.mw-widget-calendarWidget-item-selected' )
306 .removeClass( 'mw-widget-calendarWidget-item-selected' );
307 if ( fade
=== 'previous' || fade
=== 'up' ) {
308 this.$body
.insertBefore( this.$oldBody
);
309 } else if ( fade
=== 'next' || fade
=== 'down' ) {
310 this.$body
.insertAfter( this.$oldBody
);
312 setTimeout( function () {
313 $bodyWrapper
.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade
);
316 this.$oldBody
.replaceWith( this.$body
);
319 this.previousMoment
= moment( this.moment
);
320 this.previousDisplayLayer
= this.displayLayer
;
321 this.previousDate
= this.date
;
323 this.$body
.on( 'click', this.onBodyClick
.bind( this ) );
327 * Handle click events on the "up" button, switching to less precise view.
331 mw
.widgets
.CalendarWidget
.prototype.onUpButtonClick = function () {
333 layers
= this.getDisplayLayers(),
334 currentLayer
= layers
.indexOf( this.displayLayer
);
335 if ( currentLayer
!== layers
.length
- 1 ) {
337 this.displayLayer
= layers
[ currentLayer
+ 1 ];
338 this.updateUI( 'up' );
345 * Handle click events on the "previous" button, switching to previous pane.
349 mw
.widgets
.CalendarWidget
.prototype.onPrevButtonClick = function () {
350 switch ( this.displayLayer
) {
352 this.moment
.subtract( 1, 'month' );
355 this.moment
.subtract( 1, 'year' );
358 this.moment
.subtract( 20, 'years' );
361 this.updateUI( 'previous' );
365 * Handle click events on the "next" button, switching to next pane.
369 mw
.widgets
.CalendarWidget
.prototype.onNextButtonClick = function () {
370 switch ( this.displayLayer
) {
372 this.moment
.add( 1, 'month' );
375 this.moment
.add( 1, 'year' );
378 this.moment
.add( 20, 'years' );
381 this.updateUI( 'next' );
385 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
386 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
391 mw
.widgets
.CalendarWidget
.prototype.onBodyClick = function ( e
) {
393 $target
= $( e
.target
),
394 layers
= this.getDisplayLayers(),
395 currentLayer
= layers
.indexOf( this.displayLayer
);
396 if ( $target
.data( 'year' ) !== undefined ) {
397 this.moment
.year( $target
.data( 'year' ) );
399 if ( $target
.data( 'month' ) !== undefined ) {
400 this.moment
.month( $target
.data( 'month' ) );
402 if ( $target
.data( 'date' ) !== undefined ) {
403 this.moment
.date( $target
.data( 'date' ) );
405 if ( currentLayer
=== 0 ) {
406 this.setDateFromMoment();
407 this.updateUI( 'auto' );
410 this.displayLayer
= layers
[ currentLayer
- 1 ];
411 this.updateUI( 'down' );
418 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
419 * When null, the calendar will show today's date, but not select it. When invalid, the date
422 mw
.widgets
.CalendarWidget
.prototype.setDate = function ( date
) {
423 var mom
= date
!== null ? moment( date
, this.getDateFormat() ) : moment();
424 if ( mom
.isValid() ) {
426 if ( date
!== null ) {
427 this.setDateFromMoment();
428 } else if ( this.date
!== null ) {
430 this.emit( 'change', this.date
);
432 this.displayLayer
= this.getDisplayLayers()[ 0 ];
438 * Reset the user interface of this widget to reflect selected date.
440 mw
.widgets
.CalendarWidget
.prototype.resetUI = function () {
441 this.moment
= this.getDate() !== null ? moment( this.getDate(), this.getDateFormat() ) : moment();
442 this.displayLayer
= this.getDisplayLayers()[ 0 ];
447 * Set the date from moment object.
451 mw
.widgets
.CalendarWidget
.prototype.setDateFromMoment = function () {
452 // Switch to English locale to avoid number formatting. We want the internal value to be
453 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
454 var newDate
= moment( this.moment
).locale( 'en' ).format( this.getDateFormat() );
455 if ( this.date
!== newDate
) {
457 this.emit( 'change', this.date
);
462 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
465 * @return {string|null} Date string
467 mw
.widgets
.CalendarWidget
.prototype.getDate = function () {
472 * Handle focus events.
476 mw
.widgets
.CalendarWidget
.prototype.onFocus = function () {
477 this.displayLayer
= this.getDisplayLayers()[ 0 ];
478 this.updateUI( 'down' );
482 * Handle mouse click events.
485 * @param {jQuery.Event} e Mouse click event
487 mw
.widgets
.CalendarWidget
.prototype.onClick = function ( e
) {
488 if ( !this.isDisabled() && e
.which
=== 1 ) {
489 // Prevent unintended focussing
495 * Handle key down events.
498 * @param {jQuery.Event} e Key down event
500 mw
.widgets
.CalendarWidget
.prototype.onKeyDown = function ( e
) {
503 dir
= OO
.ui
.Element
.static.getDir( this.$element
),
505 nextDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.RIGHT
: OO
.ui
.Keys
.LEFT
,
506 prevDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.LEFT
: OO
.ui
.Keys
.RIGHT
,
509 if ( !this.isDisabled() ) {
511 case prevDirectionKey
:
512 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'day' );
514 case nextDirectionKey
:
515 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'day' );
518 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'week' );
520 case OO
.ui
.Keys
.DOWN
:
521 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'week' );
523 case OO
.ui
.Keys
.PAGEUP
:
524 this.moment
.subtract( 1, this.precision
=== 'month' ? 'year' : 'month' );
526 case OO
.ui
.Keys
.PAGEDOWN
:
527 this.moment
.add( 1, this.precision
=== 'month' ? 'year' : 'month' );
535 this.displayLayer
= this.getDisplayLayers()[ 0 ];
536 this.setDateFromMoment();
537 this.updateUI( 'auto' );
546 mw
.widgets
.CalendarWidget
.prototype.toggle = function ( visible
) {
548 mw
.widgets
.CalendarWidget
.parent
.prototype.toggle
.call( this, visible
);
550 if ( this.$floatableContainer
) {
551 this.togglePositioning( this.isVisible() );
557 }( jQuery
, mediaWiki
) );