Merge "Align "What's this" vertically"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.CalendarWidget.js
1 /*!
2 * MediaWiki Widgets – CalendarWidget class.
3 *
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 /* global moment */
8 ( function ( $, mw ) {
9
10 /**
11 * Creates an mw.widgets.CalendarWidget object.
12 *
13 * You will most likely want to use mw.widgets.DateInputWidget instead of CalendarWidget directly.
14 *
15 * @class
16 * @extends OO.ui.Widget
17 * @mixins OO.ui.mixin.TabIndexedElement
18 * @mixins OO.ui.mixin.FloatableElement
19 *
20 * @constructor
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
27 * it.
28 */
29 mw.widgets.CalendarWidget = function MWWCalendarWidget( config ) {
30 // Config initialization
31 config = config || {};
32
33 // Parent constructor
34 mw.widgets.CalendarWidget.parent.call( this, config );
35
36 // Mixin constructors
37 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$element } ) );
38 OO.ui.mixin.FloatableElement.call( this, config );
39
40 // Properties
41 this.lazyInitOnToggle = !!config.lazyInitOnToggle;
42 this.precision = config.precision || 'day';
43 // Currently selected date (day or month)
44 this.date = null;
45 // Current UI state (date and precision we're displaying right now)
46 this.moment = null;
47 this.displayLayer = this.getDisplayLayers()[ 0 ]; // 'month', 'year', 'duodecade'
48
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
54 // Events
55 this.$element.on( {
56 focus: this.onFocus.bind( this ),
57 mousedown: this.onClick.bind( this ),
58 keydown: this.onKeyDown.bind( this )
59 } );
60
61 // Initialization
62 this.$element
63 .addClass( 'mw-widget-calendarWidget' )
64 .append( this.$header, this.$bodyOuterWrapper.append( this.$bodyWrapper.append( this.$body ) ) );
65 if ( !this.lazyInitOnToggle ) {
66 this.buildHeaderButtons();
67 }
68 this.setDate( config.date !== undefined ? config.date : null );
69 };
70
71 /* Inheritance */
72
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 );
76
77 /* Events */
78
79 /**
80 * @event change
81 *
82 * A change event is emitted when the chosen date changes.
83 *
84 * @param {string} date Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'
85 */
86
87 /* Methods */
88
89 /**
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.
92 *
93 * @private
94 * @return {string} Format
95 */
96 mw.widgets.CalendarWidget.prototype.getDateFormat = function () {
97 return {
98 day: 'YYYY-MM-DD',
99 month: 'YYYY-MM'
100 }[ this.precision ];
101 };
102
103 /**
104 * Get the date precision this calendar uses, 'day' or 'month'.
105 *
106 * @private
107 * @return {string} Precision, 'day' or 'month'
108 */
109 mw.widgets.CalendarWidget.prototype.getPrecision = function () {
110 return this.precision;
111 };
112
113 /**
114 * Get list of possible display layers.
115 *
116 * @private
117 * @return {string[]} Layers
118 */
119 mw.widgets.CalendarWidget.prototype.getDisplayLayers = function () {
120 return [ 'month', 'year', 'duodecade' ].slice( this.precision === 'month' ? 1 : 0 );
121 };
122
123 /**
124 * Update the calendar.
125 *
126 * @private
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.
130 */
131 mw.widgets.CalendarWidget.prototype.updateUI = function ( fade ) {
132 var items, today, selected, currentMonth, currentYear, currentDay, i, needsFade,
133 $bodyWrapper = this.$bodyWrapper;
134
135 if ( this.lazyInitOnToggle ) {
136 // We're being called from the constructor and not being shown yet, do nothing
137 return;
138 }
139
140 if (
141 this.displayLayer === this.previousDisplayLayer &&
142 this.date === this.previousDate &&
143 this.previousMoment &&
144 this.previousMoment.isSame( this.moment, this.precision === 'month' ? 'month' : 'day' )
145 ) {
146 // Already displayed
147 return;
148 }
149
150 if ( fade === 'auto' ) {
151 if ( !this.previousMoment ) {
152 fade = null;
153 } else if ( this.previousMoment.isBefore( this.moment, this.precision === 'month' ? 'month' : 'day' ) ) {
154 fade = 'next';
155 } else if ( this.previousMoment.isAfter( this.moment, this.precision === 'month' ? 'month' : 'day' ) ) {
156 fade = 'previous';
157 } else {
158 fade = null;
159 }
160 }
161
162 items = [];
163 if ( this.$oldBody ) {
164 this.$oldBody.remove();
165 }
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' );
173
174 today = moment();
175 selected = moment( this.getDate(), this.getDateFormat() );
176
177 switch ( this.displayLayer ) {
178 case 'month':
179 this.labelButton.setLabel( this.moment.format( 'MMMM YYYY' ) );
180 this.upButton.toggle( true );
181
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' );
186
187 // Day-of-week labels. Localisation-independent: works with weeks starting on Saturday, Sunday
188 // or Monday.
189 for ( i = 0; i < 7; i++ ) {
190 items.push(
191 $( '<div>' )
192 .addClass( 'mw-widget-calendarWidget-day-heading' )
193 .text( currentDay.format( 'dd' ) )
194 );
195 currentDay.add( 1, 'day' );
196 }
197 currentDay.subtract( 7, 'days' );
198
199 // Actual calendar month. Always displays 6 weeks, for consistency (months can span 4 to 6
200 // weeks).
201 for ( i = 0; i < 42; i++ ) {
202 items.push(
203 $( '<div>' )
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() )
212 );
213 currentDay.add( 1, 'day' );
214 }
215 break;
216
217 case 'year':
218 this.labelButton.setLabel( this.moment.format( 'YYYY' ) );
219 this.upButton.toggle( true );
220
221 currentMonth = moment( this.moment ).startOf( 'year' );
222 for ( i = 0; i < 12; i++ ) {
223 items.push(
224 $( '<div>' )
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() )
229 );
230 currentMonth.add( 1, 'month' );
231 }
232 // Shuffle the array to display months in columns rather than rows:
233 // | Jan | Jul |
234 // | Feb | Aug |
235 // | Mar | Sep |
236 // | Apr | Oct |
237 // | May | Nov |
238 // | Jun | Dec |
239 items = [
240 items[ 0 ], items[ 6 ],
241 items[ 1 ], items[ 7 ],
242 items[ 2 ], items[ 8 ],
243 items[ 3 ], items[ 9 ],
244 items[ 4 ], items[ 10 ],
245 items[ 5 ], items[ 11 ]
246 ];
247 break;
248
249 case 'duodecade':
250 this.labelButton.setLabel( null );
251 this.upButton.toggle( false );
252
253 currentYear = moment( { year: Math.floor( this.moment.year() / 20 ) * 20 } );
254 for ( i = 0; i < 20; i++ ) {
255 items.push(
256 $( '<div>' )
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() )
261 );
262 currentYear.add( 1, 'year' );
263 }
264 break;
265 }
266
267 this.$body.append.apply( this.$body, items );
268
269 $bodyWrapper
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' );
274
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
284 );
285 }
286
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 );
294 }
295 setTimeout( function () {
296 $bodyWrapper.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade );
297 } );
298 } else {
299 this.$oldBody.replaceWith( this.$body );
300 }
301
302 this.previousMoment = moment( this.moment );
303 this.previousDisplayLayer = this.displayLayer;
304 this.previousDate = this.date;
305
306 this.$body.on( 'click', this.onBodyClick.bind( this ) );
307 };
308
309 /**
310 * Construct and display buttons to navigate the calendar.
311 *
312 * @private
313 */
314 mw.widgets.CalendarWidget.prototype.buildHeaderButtons = function () {
315 this.labelButton = new OO.ui.ButtonWidget( {
316 tabIndex: -1,
317 label: '',
318 framed: false,
319 classes: [ 'mw-widget-calendarWidget-labelButton' ]
320 } );
321 this.upButton = new OO.ui.ButtonWidget( {
322 tabIndex: -1,
323 framed: false,
324 icon: 'collapse',
325 classes: [ 'mw-widget-calendarWidget-upButton' ]
326 } );
327 this.prevButton = new OO.ui.ButtonWidget( {
328 tabIndex: -1,
329 framed: false,
330 icon: 'previous',
331 classes: [ 'mw-widget-calendarWidget-prevButton' ]
332 } );
333 this.nextButton = new OO.ui.ButtonWidget( {
334 tabIndex: -1,
335 framed: false,
336 icon: 'next',
337 classes: [ 'mw-widget-calendarWidget-nextButton' ]
338 } );
339
340 this.labelButton.connect( this, { click: 'onUpButtonClick' } );
341 this.upButton.connect( this, { click: 'onUpButtonClick' } );
342 this.prevButton.connect( this, { click: 'onPrevButtonClick' } );
343 this.nextButton.connect( this, { click: 'onNextButtonClick' } );
344
345 this.$header.append(
346 this.prevButton.$element,
347 this.nextButton.$element,
348 this.upButton.$element,
349 this.labelButton.$element
350 );
351 };
352
353 /**
354 * Handle click events on the "up" button, switching to less precise view.
355 *
356 * @private
357 */
358 mw.widgets.CalendarWidget.prototype.onUpButtonClick = function () {
359 var
360 layers = this.getDisplayLayers(),
361 currentLayer = layers.indexOf( this.displayLayer );
362 if ( currentLayer !== layers.length - 1 ) {
363 // One layer up
364 this.displayLayer = layers[ currentLayer + 1 ];
365 this.updateUI( 'up' );
366 } else {
367 this.updateUI();
368 }
369 };
370
371 /**
372 * Handle click events on the "previous" button, switching to previous pane.
373 *
374 * @private
375 */
376 mw.widgets.CalendarWidget.prototype.onPrevButtonClick = function () {
377 switch ( this.displayLayer ) {
378 case 'month':
379 this.moment.subtract( 1, 'month' );
380 break;
381 case 'year':
382 this.moment.subtract( 1, 'year' );
383 break;
384 case 'duodecade':
385 this.moment.subtract( 20, 'years' );
386 break;
387 }
388 this.updateUI( 'previous' );
389 };
390
391 /**
392 * Handle click events on the "next" button, switching to next pane.
393 *
394 * @private
395 */
396 mw.widgets.CalendarWidget.prototype.onNextButtonClick = function () {
397 switch ( this.displayLayer ) {
398 case 'month':
399 this.moment.add( 1, 'month' );
400 break;
401 case 'year':
402 this.moment.add( 1, 'year' );
403 break;
404 case 'duodecade':
405 this.moment.add( 20, 'years' );
406 break;
407 }
408 this.updateUI( 'next' );
409 };
410
411 /**
412 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
413 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
414 * what gets clicked.
415 *
416 * @private
417 * @param {jQuery.Event} e Click event
418 */
419 mw.widgets.CalendarWidget.prototype.onBodyClick = function ( e ) {
420 var
421 $target = $( e.target ),
422 layers = this.getDisplayLayers(),
423 currentLayer = layers.indexOf( this.displayLayer );
424 if ( $target.data( 'year' ) !== undefined ) {
425 this.moment.year( $target.data( 'year' ) );
426 }
427 if ( $target.data( 'month' ) !== undefined ) {
428 this.moment.month( $target.data( 'month' ) );
429 }
430 if ( $target.data( 'date' ) !== undefined ) {
431 this.moment.date( $target.data( 'date' ) );
432 }
433 if ( currentLayer === 0 ) {
434 this.setDateFromMoment();
435 this.updateUI( 'auto' );
436 } else {
437 // One layer down
438 this.displayLayer = layers[ currentLayer - 1 ];
439 this.updateUI( 'down' );
440 }
441 };
442
443 /**
444 * Set the date.
445 *
446 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
447 * When null, the calendar will show today's date, but not select it. When invalid, the date
448 * is not changed.
449 */
450 mw.widgets.CalendarWidget.prototype.setDate = function ( date ) {
451 var mom = date !== null ? moment( date, this.getDateFormat() ) : moment();
452 if ( mom.isValid() ) {
453 this.moment = mom;
454 if ( date !== null ) {
455 this.setDateFromMoment();
456 } else if ( this.date !== null ) {
457 this.date = null;
458 this.emit( 'change', this.date );
459 }
460 this.displayLayer = this.getDisplayLayers()[ 0 ];
461 this.updateUI();
462 }
463 };
464
465 /**
466 * Reset the user interface of this widget to reflect selected date.
467 */
468 mw.widgets.CalendarWidget.prototype.resetUI = function () {
469 this.moment = this.getDate() !== null ? moment( this.getDate(), this.getDateFormat() ) : moment();
470 this.displayLayer = this.getDisplayLayers()[ 0 ];
471 this.updateUI();
472 };
473
474 /**
475 * Set the date from moment object.
476 *
477 * @private
478 */
479 mw.widgets.CalendarWidget.prototype.setDateFromMoment = function () {
480 // Switch to English locale to avoid number formatting. We want the internal value to be
481 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
482 var newDate = moment( this.moment ).locale( 'en' ).format( this.getDateFormat() );
483 if ( this.date !== newDate ) {
484 this.date = newDate;
485 this.emit( 'change', this.date );
486 }
487 };
488
489 /**
490 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
491 * not be localised.
492 *
493 * @return {string|null} Date string
494 */
495 mw.widgets.CalendarWidget.prototype.getDate = function () {
496 return this.date;
497 };
498
499 /**
500 * Handle focus events.
501 *
502 * @private
503 */
504 mw.widgets.CalendarWidget.prototype.onFocus = function () {
505 this.displayLayer = this.getDisplayLayers()[ 0 ];
506 this.updateUI( 'down' );
507 };
508
509 /**
510 * Handle mouse click events.
511 *
512 * @private
513 * @param {jQuery.Event} e Mouse click event
514 * @return {boolean} False to cancel the default event
515 */
516 mw.widgets.CalendarWidget.prototype.onClick = function ( e ) {
517 if ( !this.isDisabled() && e.which === 1 ) {
518 // Prevent unintended focussing
519 return false;
520 }
521 };
522
523 /**
524 * Handle key down events.
525 *
526 * @private
527 * @param {jQuery.Event} e Key down event
528 * @return {boolean} False to cancel the default event
529 */
530 mw.widgets.CalendarWidget.prototype.onKeyDown = function ( e ) {
531 var
532 dir = OO.ui.Element.static.getDir( this.$element ),
533 nextDirectionKey = dir === 'ltr' ? OO.ui.Keys.RIGHT : OO.ui.Keys.LEFT,
534 prevDirectionKey = dir === 'ltr' ? OO.ui.Keys.LEFT : OO.ui.Keys.RIGHT,
535 changed = true;
536
537 if ( !this.isDisabled() ) {
538 switch ( e.which ) {
539 case prevDirectionKey:
540 this.moment.subtract( 1, this.precision === 'month' ? 'month' : 'day' );
541 break;
542 case nextDirectionKey:
543 this.moment.add( 1, this.precision === 'month' ? 'month' : 'day' );
544 break;
545 case OO.ui.Keys.UP:
546 this.moment.subtract( 1, this.precision === 'month' ? 'month' : 'week' );
547 break;
548 case OO.ui.Keys.DOWN:
549 this.moment.add( 1, this.precision === 'month' ? 'month' : 'week' );
550 break;
551 case OO.ui.Keys.PAGEUP:
552 this.moment.subtract( 1, this.precision === 'month' ? 'year' : 'month' );
553 break;
554 case OO.ui.Keys.PAGEDOWN:
555 this.moment.add( 1, this.precision === 'month' ? 'year' : 'month' );
556 break;
557 default:
558 changed = false;
559 break;
560 }
561
562 if ( changed ) {
563 this.displayLayer = this.getDisplayLayers()[ 0 ];
564 this.setDateFromMoment();
565 this.updateUI( 'auto' );
566 return false;
567 }
568 }
569 };
570
571 /**
572 * @inheritdoc
573 */
574 mw.widgets.CalendarWidget.prototype.toggle = function ( visible ) {
575 if ( this.lazyInitOnToggle && visible ) {
576 this.lazyInitOnToggle = false;
577 this.buildHeaderButtons();
578 this.updateUI();
579 }
580
581 // Parent method
582 mw.widgets.CalendarWidget.parent.prototype.toggle.call( this, visible );
583
584 if ( this.$floatableContainer ) {
585 this.togglePositioning( this.isVisible() );
586 }
587
588 return this;
589 };
590
591 }( jQuery, mediaWiki ) );