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.
14 * @extends OO.ui.Widget
15 * @mixins OO.ui.mixin.TabIndexedElement
18 * @param {Object} [config] Configuration options
19 * @cfg {string} [precision='day'] Date precision to use, 'day' or 'month'
20 * @cfg {string|null} [date=null] Day or month date (depending on `precision`), in the
21 * format 'YYYY-MM-DD' or 'YYYY-MM'. When null, defaults to current date.
23 mw
.widgets
.CalendarWidget
= function MWWCalendarWidget( config
) {
24 // Config initialization
25 config
= config
|| {};
28 mw
.widgets
.CalendarWidget
.parent
.call( this, config
);
31 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$element
} ) );
34 this.precision
= config
.precision
|| 'day';
35 // Currently selected date (day or month)
37 // Current UI state (date and precision we're displaying right now)
39 this.displayLayer
= this.getDisplayLayers()[ 0 ]; // 'month', 'year', 'duodecade'
41 this.$header
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-header' );
42 this.$bodyOuterWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-outer-wrapper' );
43 this.$bodyWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-wrapper' );
44 this.$body
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body' );
45 this.labelButton
= new OO
.ui
.ButtonWidget( {
49 classes
: [ 'mw-widget-calendarWidget-labelButton' ]
51 this.upButton
= new OO
.ui
.ButtonWidget( {
55 classes
: [ 'mw-widget-calendarWidget-upButton' ]
57 this.prevButton
= new OO
.ui
.ButtonWidget( {
61 classes
: [ 'mw-widget-calendarWidget-prevButton' ]
63 this.nextButton
= new OO
.ui
.ButtonWidget( {
67 classes
: [ 'mw-widget-calendarWidget-nextButton' ]
71 this.labelButton
.connect( this, { click
: 'onUpButtonClick' } );
72 this.upButton
.connect( this, { click
: 'onUpButtonClick' } );
73 this.prevButton
.connect( this, { click
: 'onPrevButtonClick' } );
74 this.nextButton
.connect( this, { click
: 'onNextButtonClick' } );
76 focus
: this.onFocus
.bind( this ),
77 mousedown
: this.onClick
.bind( this ),
78 keydown
: this.onKeyDown
.bind( this )
83 .addClass( 'mw-widget-calendarWidget' )
84 .append( this.$header
, this.$bodyOuterWrapper
.append( this.$bodyWrapper
.append( this.$body
) ) );
86 this.prevButton
.$element
,
87 this.nextButton
.$element
,
88 this.upButton
.$element
,
89 this.labelButton
.$element
91 this.setDate( config
.date
!== undefined ? config
.date
: null );
96 OO
.inheritClass( mw
.widgets
.CalendarWidget
, OO
.ui
.Widget
);
97 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.TabIndexedElement
);
104 * A change event is emitted when the chosen date changes.
106 * @param {string} date Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'
112 * Get the date format ('YYYY-MM-DD' or 'YYYY-MM', depending on precision), which is used
113 * internally and for dates accepted by #setDate and returned by #getDate.
116 * @returns {string} Format
118 mw
.widgets
.CalendarWidget
.prototype.getDateFormat = function () {
126 * Get the date precision this calendar uses, 'day' or 'month'.
129 * @returns {string} Precision, 'day' or 'month'
131 mw
.widgets
.CalendarWidget
.prototype.getPrecision = function () {
132 return this.precision
;
136 * Get list of possible display layers.
139 * @returns {string[]} Layers
141 mw
.widgets
.CalendarWidget
.prototype.getDisplayLayers = function () {
142 return [ 'month', 'year', 'duodecade' ].slice( this.precision
=== 'month' ? 1 : 0 );
146 * Update the calendar.
149 * @param {string|null} [fade=null] Direction in which to fade out current calendar contents, 'previous',
151 * @returns {string} Format
153 mw
.widgets
.CalendarWidget
.prototype.updateUI = function ( fade
) {
154 var items
, today
, selected
, currentMonth
, currentYear
, currentDay
, i
, needsFade
,
155 $bodyWrapper
= this.$bodyWrapper
;
158 this.displayLayer
=== this.previousDisplayLayer
&&
159 this.previousMoment
&&
160 this.previousMoment
.isSame( this.moment
, this.precision
=== 'month' ? 'month' : 'day' )
167 if ( this.$oldBody
) {
168 this.$oldBody
.remove();
170 this.$oldBody
= this.$body
.addClass( 'mw-widget-calendarWidget-old-body' );
171 // Clone without children
172 this.$body
= $( this.$body
[0].cloneNode( false ) )
173 .removeClass( 'mw-widget-calendarWidget-old-body' )
174 .toggleClass( 'mw-widget-calendarWidget-body-month', this.displayLayer
=== 'month' )
175 .toggleClass( 'mw-widget-calendarWidget-body-year', this.displayLayer
=== 'year' )
176 .toggleClass( 'mw-widget-calendarWidget-body-duodecade', this.displayLayer
=== 'duodecade' );
179 selected
= moment( this.getDate(), this.getDateFormat() );
181 switch ( this.displayLayer
) {
183 this.labelButton
.setLabel( this.moment
.format( 'MMMM YYYY' ) );
184 this.upButton
.toggle( true );
186 // First week displayed is the first week spanned by the month, unless it begins on Monday, in
187 // which case first week displayed is the previous week. This makes the calendar "balanced"
188 // and also neatly handles 28-day February sometimes spanning only 4 weeks.
189 currentDay
= moment( this.moment
).startOf( 'month' ).subtract( 1, 'day' ).startOf( 'week' );
191 // Day-of-week labels. Localisation-independent: works with weeks starting on Saturday, Sunday
193 for ( i
= 0; i
< 7; i
++ ) {
196 .addClass( 'mw-widget-calendarWidget-day-heading' )
197 .text( currentDay
.format( 'dd' ) )
199 currentDay
.add( 1, 'day' );
201 currentDay
.subtract( 7, 'days' );
203 // Actual calendar month. Always displays 6 weeks, for consistency (months can span 4 to 6
205 for ( i
= 0; i
< 42; i
++ ) {
208 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-day' )
209 .toggleClass( 'mw-widget-calendarWidget-day-additional', !currentDay
.isSame( this.moment
, 'month' ) )
210 .toggleClass( 'mw-widget-calendarWidget-day-today', currentDay
.isSame( today
, 'day' ) )
211 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentDay
.isSame( selected
, 'day' ) )
212 .text( currentDay
.format( 'D' ) )
213 .data( 'date', currentDay
.date() )
214 .data( 'month', currentDay
.month() )
215 .data( 'year', currentDay
.year() )
217 currentDay
.add( 1, 'day' );
222 this.labelButton
.setLabel( this.moment
.format( 'YYYY' ) );
223 this.upButton
.toggle( true );
225 currentMonth
= moment( this.moment
).startOf( 'year' );
226 for ( i
= 0; i
< 12; i
++ ) {
229 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-month' )
230 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentMonth
.isSame( selected
, 'month' ) )
231 .text( currentMonth
.format( 'MMMM' ) )
232 .data( 'month', currentMonth
.month() )
234 currentMonth
.add( 1, 'month' );
236 // Shuffle the array to display months in columns rather than rows.
238 items
[ 0 ], items
[ 6 ], // | January | July |
239 items
[ 1 ], items
[ 7 ], // | February | August |
240 items
[ 2 ], items
[ 8 ], // | March | September |
241 items
[ 3 ], items
[ 9 ], // | April | October |
242 items
[ 4 ], items
[ 10 ], // | May | November |
243 items
[ 5 ], items
[ 11 ] // | June | December |
248 this.labelButton
.setLabel( null );
249 this.upButton
.toggle( false );
251 currentYear
= moment( { year
: Math
.floor( this.moment
.year() / 20 ) * 20 } );
252 for ( i
= 0; i
< 20; i
++ ) {
255 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-year' )
256 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentYear
.isSame( selected
, 'year' ) )
257 .text( currentYear
.format( 'YYYY' ) )
258 .data( 'year', currentYear
.year() )
260 currentYear
.add( 1, 'year' );
265 this.$body
.append
.apply( this.$body
, items
);
268 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-up' )
269 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-down' )
270 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-previous' )
271 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-next' );
273 needsFade
= this.previousDisplayLayer
!== this.displayLayer
;
274 if ( this.displayLayer
=== 'month' ) {
275 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'month' );
276 } else if ( this.displayLayer
=== 'year' ) {
277 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'year' );
278 } else if ( this.displayLayer
=== 'duodecade' ) {
279 needsFade
= needsFade
|| (
280 Math
.floor( this.moment
.year() / 20 ) * 20 !==
281 Math
.floor( this.previousMoment
.year() / 20 ) * 20
285 if ( fade
&& needsFade
) {
286 this.$oldBody
.find( '.mw-widget-calendarWidget-item-selected' )
287 .removeClass( 'mw-widget-calendarWidget-item-selected' );
288 if ( fade
=== 'previous' || fade
=== 'up' ) {
289 this.$body
.insertBefore( this.$oldBody
);
290 } else if ( fade
=== 'next' || fade
=== 'down' ) {
291 this.$body
.insertAfter( this.$oldBody
);
293 setTimeout( function () {
294 $bodyWrapper
.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade
);
297 this.$oldBody
.replaceWith( this.$body
);
300 this.previousMoment
= moment( this.moment
);
301 this.previousDisplayLayer
= this.displayLayer
;
303 this.$body
.on( 'click', this.onBodyClick
.bind( this ) );
307 * Handle click events on the "up" button, switching to less precise view.
310 mw
.widgets
.CalendarWidget
.prototype.onUpButtonClick = function () {
312 layers
= this.getDisplayLayers(),
313 currentLayer
= layers
.indexOf( this.displayLayer
);
314 if ( currentLayer
!== layers
.length
- 1 ) {
316 this.displayLayer
= layers
[ currentLayer
+ 1 ];
317 this.updateUI( 'up' );
324 * Handle click events on the "previous" button, switching to previous pane.
327 mw
.widgets
.CalendarWidget
.prototype.onPrevButtonClick = function () {
328 switch ( this.displayLayer
) {
330 this.moment
.subtract( 1, 'month' );
333 this.moment
.subtract( 1, 'year' );
336 this.moment
.subtract( 20, 'years' );
339 this.updateUI( 'previous' );
343 * Handle click events on the "next" button, switching to next pane.
346 mw
.widgets
.CalendarWidget
.prototype.onNextButtonClick = function () {
347 switch ( this.displayLayer
) {
349 this.moment
.add( 1, 'month' );
352 this.moment
.add( 1, 'year' );
355 this.moment
.add( 20, 'years' );
358 this.updateUI( 'next' );
362 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
363 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
367 mw
.widgets
.CalendarWidget
.prototype.onBodyClick = function ( e
) {
369 previousMoment
= moment( this.moment
),
370 $target
= $( e
.target
),
371 layers
= this.getDisplayLayers(),
372 currentLayer
= layers
.indexOf( this.displayLayer
);
373 if ( $target
.data( 'year' ) !== undefined ) {
374 this.moment
.year( $target
.data( 'year' ) );
376 if ( $target
.data( 'month' ) !== undefined ) {
377 this.moment
.month( $target
.data( 'month' ) );
379 if ( $target
.data( 'date' ) !== undefined ) {
380 this.moment
.date( $target
.data( 'date' ) );
382 if ( currentLayer
=== 0 ) {
383 this.setDateFromMoment();
385 this.precision
=== 'day' && this.moment
.isBefore( previousMoment
, 'month' ) ? 'previous' :
386 this.precision
=== 'day' && this.moment
.isAfter( previousMoment
, 'month' ) ? 'next' : null
390 this.displayLayer
= layers
[ currentLayer
- 1 ];
391 this.updateUI( 'down' );
398 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
399 * When null, defaults to current date. When invalid, the date is not changed.
401 mw
.widgets
.CalendarWidget
.prototype.setDate = function ( date
) {
402 var mom
= date
!== null ? moment( date
, this.getDateFormat() ) : moment();
403 if ( mom
.isValid() ) {
405 this.setDateFromMoment();
406 this.displayLayer
= this.getDisplayLayers()[ 0 ];
412 * Reset the user interface of this widget to reflect selected date.
414 mw
.widgets
.CalendarWidget
.prototype.resetUI = function () {
415 this.moment
= moment( this.getDate(), this.getDateFormat() );
416 this.displayLayer
= this.getDisplayLayers()[ 0 ];
421 * Set the date from moment object.
425 mw
.widgets
.CalendarWidget
.prototype.setDateFromMoment = function () {
426 // Switch to English locale to avoid number formatting. We want the internal value to be
427 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
428 var newDate
= moment( this.moment
).locale( 'en' ).format( this.getDateFormat() );
429 if ( this.date
!== newDate
) {
431 this.emit( 'change', this.date
);
436 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
439 * @returns {string} Date string
441 mw
.widgets
.CalendarWidget
.prototype.getDate = function () {
446 * Handle focus events.
450 mw
.widgets
.CalendarWidget
.prototype.onFocus = function () {
451 this.displayLayer
= this.getDisplayLayers()[ 0 ];
452 this.updateUI( 'down' );
456 * Handle mouse click events.
459 * @param {jQuery.Event} e Mouse click event
461 mw
.widgets
.CalendarWidget
.prototype.onClick = function ( e
) {
462 if ( !this.isDisabled() && e
.which
=== 1 ) {
463 // Prevent unintended focussing
469 * Handle key down events.
472 * @param {jQuery.Event} e Key down event
474 mw
.widgets
.CalendarWidget
.prototype.onKeyDown = function ( e
) {
477 dir
= OO
.ui
.Element
.static.getDir( this.$element
),
479 nextDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.RIGHT
: OO
.ui
.Keys
.LEFT
,
480 prevDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.LEFT
: OO
.ui
.Keys
.RIGHT
,
481 updateInDirection
= null;
483 if ( !this.isDisabled() ) {
485 case prevDirectionKey
:
486 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'day' );
487 updateInDirection
= 'previous';
489 case nextDirectionKey
:
490 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'day' );
491 updateInDirection
= 'next';
494 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'week' );
495 updateInDirection
= 'previous';
497 case OO
.ui
.Keys
.DOWN
:
498 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'week' );
499 updateInDirection
= 'next';
501 case OO
.ui
.Keys
.PAGEUP
:
502 this.moment
.subtract( 1, this.precision
=== 'month' ? 'year' : 'month' );
503 updateInDirection
= 'previous';
505 case OO
.ui
.Keys
.PAGEDOWN
:
506 this.moment
.add( 1, this.precision
=== 'month' ? 'year' : 'month' );
507 updateInDirection
= 'next';
511 if ( updateInDirection
) {
512 this.displayLayer
= this.getDisplayLayers()[ 0 ];
513 this.setDateFromMoment();
514 this.updateUI( updateInDirection
);
520 }( jQuery
, mediaWiki
) );