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 format
21 * 'YYYY-MM-DD' or 'YYYY-MM'. When null, the calendar will show today's date, but not select
24 mw
.widgets
.CalendarWidget
= function MWWCalendarWidget( config
) {
25 // Config initialization
26 config
= config
|| {};
29 mw
.widgets
.CalendarWidget
.parent
.call( this, config
);
32 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$element
} ) );
35 this.precision
= config
.precision
|| 'day';
36 // Currently selected date (day or month)
38 // Current UI state (date and precision we're displaying right now)
40 this.displayLayer
= this.getDisplayLayers()[ 0 ]; // 'month', 'year', 'duodecade'
42 this.$header
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-header' );
43 this.$bodyOuterWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-outer-wrapper' );
44 this.$bodyWrapper
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body-wrapper' );
45 this.$body
= $( '<div>' ).addClass( 'mw-widget-calendarWidget-body' );
46 this.labelButton
= new OO
.ui
.ButtonWidget( {
50 classes
: [ 'mw-widget-calendarWidget-labelButton' ]
52 this.upButton
= new OO
.ui
.ButtonWidget( {
56 classes
: [ 'mw-widget-calendarWidget-upButton' ]
58 this.prevButton
= new OO
.ui
.ButtonWidget( {
62 classes
: [ 'mw-widget-calendarWidget-prevButton' ]
64 this.nextButton
= new OO
.ui
.ButtonWidget( {
68 classes
: [ 'mw-widget-calendarWidget-nextButton' ]
72 this.labelButton
.connect( this, { click
: 'onUpButtonClick' } );
73 this.upButton
.connect( this, { click
: 'onUpButtonClick' } );
74 this.prevButton
.connect( this, { click
: 'onPrevButtonClick' } );
75 this.nextButton
.connect( this, { click
: 'onNextButtonClick' } );
77 focus
: this.onFocus
.bind( this ),
78 mousedown
: this.onClick
.bind( this ),
79 keydown
: this.onKeyDown
.bind( this )
84 .addClass( 'mw-widget-calendarWidget' )
85 .append( this.$header
, this.$bodyOuterWrapper
.append( this.$bodyWrapper
.append( this.$body
) ) );
87 this.prevButton
.$element
,
88 this.nextButton
.$element
,
89 this.upButton
.$element
,
90 this.labelButton
.$element
92 this.setDate( config
.date
!== undefined ? config
.date
: null );
97 OO
.inheritClass( mw
.widgets
.CalendarWidget
, OO
.ui
.Widget
);
98 OO
.mixinClass( mw
.widgets
.CalendarWidget
, OO
.ui
.mixin
.TabIndexedElement
);
105 * A change event is emitted when the chosen date changes.
107 * @param {string} date Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'
113 * Get the date format ('YYYY-MM-DD' or 'YYYY-MM', depending on precision), which is used
114 * internally and for dates accepted by #setDate and returned by #getDate.
117 * @returns {string} Format
119 mw
.widgets
.CalendarWidget
.prototype.getDateFormat = function () {
127 * Get the date precision this calendar uses, 'day' or 'month'.
130 * @returns {string} Precision, 'day' or 'month'
132 mw
.widgets
.CalendarWidget
.prototype.getPrecision = function () {
133 return this.precision
;
137 * Get list of possible display layers.
140 * @returns {string[]} Layers
142 mw
.widgets
.CalendarWidget
.prototype.getDisplayLayers = function () {
143 return [ 'month', 'year', 'duodecade' ].slice( this.precision
=== 'month' ? 1 : 0 );
147 * Update the calendar.
150 * @param {string|null} [fade=null] Direction in which to fade out current calendar contents, 'previous',
152 * @returns {string} Format
154 mw
.widgets
.CalendarWidget
.prototype.updateUI = function ( fade
) {
155 var items
, today
, selected
, currentMonth
, currentYear
, currentDay
, i
, needsFade
,
156 $bodyWrapper
= this.$bodyWrapper
;
159 this.displayLayer
=== this.previousDisplayLayer
&&
160 this.date
=== this.previousDate
&&
161 this.previousMoment
&&
162 this.previousMoment
.isSame( this.moment
, this.precision
=== 'month' ? 'month' : 'day' )
169 if ( this.$oldBody
) {
170 this.$oldBody
.remove();
172 this.$oldBody
= this.$body
.addClass( 'mw-widget-calendarWidget-old-body' );
173 // Clone without children
174 this.$body
= $( this.$body
[0].cloneNode( false ) )
175 .removeClass( 'mw-widget-calendarWidget-old-body' )
176 .toggleClass( 'mw-widget-calendarWidget-body-month', this.displayLayer
=== 'month' )
177 .toggleClass( 'mw-widget-calendarWidget-body-year', this.displayLayer
=== 'year' )
178 .toggleClass( 'mw-widget-calendarWidget-body-duodecade', this.displayLayer
=== 'duodecade' );
181 selected
= moment( this.getDate(), this.getDateFormat() );
183 switch ( this.displayLayer
) {
185 this.labelButton
.setLabel( this.moment
.format( 'MMMM YYYY' ) );
186 this.upButton
.toggle( true );
188 // First week displayed is the first week spanned by the month, unless it begins on Monday, in
189 // which case first week displayed is the previous week. This makes the calendar "balanced"
190 // and also neatly handles 28-day February sometimes spanning only 4 weeks.
191 currentDay
= moment( this.moment
).startOf( 'month' ).subtract( 1, 'day' ).startOf( 'week' );
193 // Day-of-week labels. Localisation-independent: works with weeks starting on Saturday, Sunday
195 for ( i
= 0; i
< 7; i
++ ) {
198 .addClass( 'mw-widget-calendarWidget-day-heading' )
199 .text( currentDay
.format( 'dd' ) )
201 currentDay
.add( 1, 'day' );
203 currentDay
.subtract( 7, 'days' );
205 // Actual calendar month. Always displays 6 weeks, for consistency (months can span 4 to 6
207 for ( i
= 0; i
< 42; i
++ ) {
210 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-day' )
211 .toggleClass( 'mw-widget-calendarWidget-day-additional', !currentDay
.isSame( this.moment
, 'month' ) )
212 .toggleClass( 'mw-widget-calendarWidget-day-today', currentDay
.isSame( today
, 'day' ) )
213 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentDay
.isSame( selected
, 'day' ) )
214 .text( currentDay
.format( 'D' ) )
215 .data( 'date', currentDay
.date() )
216 .data( 'month', currentDay
.month() )
217 .data( 'year', currentDay
.year() )
219 currentDay
.add( 1, 'day' );
224 this.labelButton
.setLabel( this.moment
.format( 'YYYY' ) );
225 this.upButton
.toggle( true );
227 currentMonth
= moment( this.moment
).startOf( 'year' );
228 for ( i
= 0; i
< 12; i
++ ) {
231 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-month' )
232 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentMonth
.isSame( selected
, 'month' ) )
233 .text( currentMonth
.format( 'MMMM' ) )
234 .data( 'month', currentMonth
.month() )
236 currentMonth
.add( 1, 'month' );
238 // Shuffle the array to display months in columns rather than rows.
240 items
[ 0 ], items
[ 6 ], // | January | July |
241 items
[ 1 ], items
[ 7 ], // | February | August |
242 items
[ 2 ], items
[ 8 ], // | March | September |
243 items
[ 3 ], items
[ 9 ], // | April | October |
244 items
[ 4 ], items
[ 10 ], // | May | November |
245 items
[ 5 ], items
[ 11 ] // | June | December |
250 this.labelButton
.setLabel( null );
251 this.upButton
.toggle( false );
253 currentYear
= moment( { year
: Math
.floor( this.moment
.year() / 20 ) * 20 } );
254 for ( i
= 0; i
< 20; i
++ ) {
257 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-year' )
258 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentYear
.isSame( selected
, 'year' ) )
259 .text( currentYear
.format( 'YYYY' ) )
260 .data( 'year', currentYear
.year() )
262 currentYear
.add( 1, 'year' );
267 this.$body
.append
.apply( this.$body
, items
);
270 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-up' )
271 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-down' )
272 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-previous' )
273 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-next' );
275 needsFade
= this.previousDisplayLayer
!== this.displayLayer
;
276 if ( this.displayLayer
=== 'month' ) {
277 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'month' );
278 } else if ( this.displayLayer
=== 'year' ) {
279 needsFade
= needsFade
|| !this.moment
.isSame( this.previousMoment
, 'year' );
280 } else if ( this.displayLayer
=== 'duodecade' ) {
281 needsFade
= needsFade
|| (
282 Math
.floor( this.moment
.year() / 20 ) * 20 !==
283 Math
.floor( this.previousMoment
.year() / 20 ) * 20
287 if ( fade
&& needsFade
) {
288 this.$oldBody
.find( '.mw-widget-calendarWidget-item-selected' )
289 .removeClass( 'mw-widget-calendarWidget-item-selected' );
290 if ( fade
=== 'previous' || fade
=== 'up' ) {
291 this.$body
.insertBefore( this.$oldBody
);
292 } else if ( fade
=== 'next' || fade
=== 'down' ) {
293 this.$body
.insertAfter( this.$oldBody
);
295 setTimeout( function () {
296 $bodyWrapper
.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade
);
299 this.$oldBody
.replaceWith( this.$body
);
302 this.previousMoment
= moment( this.moment
);
303 this.previousDisplayLayer
= this.displayLayer
;
304 this.previousDate
= this.date
;
306 this.$body
.on( 'click', this.onBodyClick
.bind( this ) );
310 * Handle click events on the "up" button, switching to less precise view.
313 mw
.widgets
.CalendarWidget
.prototype.onUpButtonClick = function () {
315 layers
= this.getDisplayLayers(),
316 currentLayer
= layers
.indexOf( this.displayLayer
);
317 if ( currentLayer
!== layers
.length
- 1 ) {
319 this.displayLayer
= layers
[ currentLayer
+ 1 ];
320 this.updateUI( 'up' );
327 * Handle click events on the "previous" button, switching to previous pane.
330 mw
.widgets
.CalendarWidget
.prototype.onPrevButtonClick = function () {
331 switch ( this.displayLayer
) {
333 this.moment
.subtract( 1, 'month' );
336 this.moment
.subtract( 1, 'year' );
339 this.moment
.subtract( 20, 'years' );
342 this.updateUI( 'previous' );
346 * Handle click events on the "next" button, switching to next pane.
349 mw
.widgets
.CalendarWidget
.prototype.onNextButtonClick = function () {
350 switch ( this.displayLayer
) {
352 this.moment
.add( 1, 'month' );
355 this.moment
.add( 1, 'year' );
358 this.moment
.add( 20, 'years' );
361 this.updateUI( 'next' );
365 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
366 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
370 mw
.widgets
.CalendarWidget
.prototype.onBodyClick = function ( e
) {
372 previousMoment
= moment( this.moment
),
373 $target
= $( e
.target
),
374 layers
= this.getDisplayLayers(),
375 currentLayer
= layers
.indexOf( this.displayLayer
);
376 if ( $target
.data( 'year' ) !== undefined ) {
377 this.moment
.year( $target
.data( 'year' ) );
379 if ( $target
.data( 'month' ) !== undefined ) {
380 this.moment
.month( $target
.data( 'month' ) );
382 if ( $target
.data( 'date' ) !== undefined ) {
383 this.moment
.date( $target
.data( 'date' ) );
385 if ( currentLayer
=== 0 ) {
386 this.setDateFromMoment();
388 this.precision
=== 'day' && this.moment
.isBefore( previousMoment
, 'month' ) ? 'previous' :
389 this.precision
=== 'day' && this.moment
.isAfter( previousMoment
, 'month' ) ? 'next' : null
393 this.displayLayer
= layers
[ currentLayer
- 1 ];
394 this.updateUI( 'down' );
401 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
402 * When null, the calendar will show today's date, but not select it. When invalid, the date
405 mw
.widgets
.CalendarWidget
.prototype.setDate = function ( date
) {
406 var mom
= date
!== null ? moment( date
, this.getDateFormat() ) : moment();
407 if ( mom
.isValid() ) {
409 if ( date
!== null ) {
410 this.setDateFromMoment();
411 } else if ( this.date
!== null ) {
413 this.emit( 'change', this.date
);
415 this.displayLayer
= this.getDisplayLayers()[ 0 ];
421 * Reset the user interface of this widget to reflect selected date.
423 mw
.widgets
.CalendarWidget
.prototype.resetUI = function () {
424 this.moment
= moment( this.getDate(), this.getDateFormat() );
425 this.displayLayer
= this.getDisplayLayers()[ 0 ];
430 * Set the date from moment object.
434 mw
.widgets
.CalendarWidget
.prototype.setDateFromMoment = function () {
435 // Switch to English locale to avoid number formatting. We want the internal value to be
436 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
437 var newDate
= moment( this.moment
).locale( 'en' ).format( this.getDateFormat() );
438 if ( this.date
!== newDate
) {
440 this.emit( 'change', this.date
);
445 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
448 * @returns {string|null} Date string
450 mw
.widgets
.CalendarWidget
.prototype.getDate = function () {
455 * Handle focus events.
459 mw
.widgets
.CalendarWidget
.prototype.onFocus = function () {
460 this.displayLayer
= this.getDisplayLayers()[ 0 ];
461 this.updateUI( 'down' );
465 * Handle mouse click events.
468 * @param {jQuery.Event} e Mouse click event
470 mw
.widgets
.CalendarWidget
.prototype.onClick = function ( e
) {
471 if ( !this.isDisabled() && e
.which
=== 1 ) {
472 // Prevent unintended focussing
478 * Handle key down events.
481 * @param {jQuery.Event} e Key down event
483 mw
.widgets
.CalendarWidget
.prototype.onKeyDown = function ( e
) {
486 dir
= OO
.ui
.Element
.static.getDir( this.$element
),
488 nextDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.RIGHT
: OO
.ui
.Keys
.LEFT
,
489 prevDirectionKey
= dir
=== 'ltr' ? OO
.ui
.Keys
.LEFT
: OO
.ui
.Keys
.RIGHT
,
490 updateInDirection
= null;
492 if ( !this.isDisabled() ) {
494 case prevDirectionKey
:
495 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'day' );
496 updateInDirection
= 'previous';
498 case nextDirectionKey
:
499 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'day' );
500 updateInDirection
= 'next';
503 this.moment
.subtract( 1, this.precision
=== 'month' ? 'month' : 'week' );
504 updateInDirection
= 'previous';
506 case OO
.ui
.Keys
.DOWN
:
507 this.moment
.add( 1, this.precision
=== 'month' ? 'month' : 'week' );
508 updateInDirection
= 'next';
510 case OO
.ui
.Keys
.PAGEUP
:
511 this.moment
.subtract( 1, this.precision
=== 'month' ? 'year' : 'month' );
512 updateInDirection
= 'previous';
514 case OO
.ui
.Keys
.PAGEDOWN
:
515 this.moment
.add( 1, this.precision
=== 'month' ? 'year' : 'month' );
516 updateInDirection
= 'next';
520 if ( updateInDirection
) {
521 this.displayLayer
= this.getDisplayLayers()[ 0 ];
522 this.setDateFromMoment();
523 this.updateUI( updateInDirection
);
529 }( jQuery
, mediaWiki
) );