Remove last remnants of pre-1.16 live preview
[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 items = [
234 items[ 0 ], items[ 6 ], // | January | July |
235 items[ 1 ], items[ 7 ], // | February | August |
236 items[ 2 ], items[ 8 ], // | March | September |
237 items[ 3 ], items[ 9 ], // | April | October |
238 items[ 4 ], items[ 10 ], // | May | November |
239 items[ 5 ], items[ 11 ] // | June | December |
240 ];
241 break;
242
243 case 'duodecade':
244 this.labelButton.setLabel( null );
245 this.upButton.toggle( false );
246
247 currentYear = moment( { year: Math.floor( this.moment.year() / 20 ) * 20 } );
248 for ( i = 0; i < 20; i++ ) {
249 items.push(
250 $( '<div>' )
251 .addClass( 'mw-widget-calendarWidget-item mw-widget-calendarWidget-year' )
252 .toggleClass( 'mw-widget-calendarWidget-item-selected', currentYear.isSame( selected, 'year' ) )
253 .text( currentYear.format( 'YYYY' ) )
254 .data( 'year', currentYear.year() )
255 );
256 currentYear.add( 1, 'year' );
257 }
258 break;
259 }
260
261 this.$body.append.apply( this.$body, items );
262
263 $bodyWrapper
264 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-up' )
265 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-down' )
266 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-previous' )
267 .removeClass( 'mw-widget-calendarWidget-body-wrapper-fade-next' );
268
269 needsFade = this.previousDisplayLayer !== this.displayLayer;
270 if ( this.displayLayer === 'month' ) {
271 needsFade = needsFade || !this.moment.isSame( this.previousMoment, 'month' );
272 } else if ( this.displayLayer === 'year' ) {
273 needsFade = needsFade || !this.moment.isSame( this.previousMoment, 'year' );
274 } else if ( this.displayLayer === 'duodecade' ) {
275 needsFade = needsFade || (
276 Math.floor( this.moment.year() / 20 ) * 20 !==
277 Math.floor( this.previousMoment.year() / 20 ) * 20
278 );
279 }
280
281 if ( fade && needsFade ) {
282 this.$oldBody.find( '.mw-widget-calendarWidget-item-selected' )
283 .removeClass( 'mw-widget-calendarWidget-item-selected' );
284 if ( fade === 'previous' || fade === 'up' ) {
285 this.$body.insertBefore( this.$oldBody );
286 } else if ( fade === 'next' || fade === 'down' ) {
287 this.$body.insertAfter( this.$oldBody );
288 }
289 setTimeout( function () {
290 $bodyWrapper.addClass( 'mw-widget-calendarWidget-body-wrapper-fade-' + fade );
291 }.bind( this ), 0 );
292 } else {
293 this.$oldBody.replaceWith( this.$body );
294 }
295
296 this.previousMoment = moment( this.moment );
297 this.previousDisplayLayer = this.displayLayer;
298 this.previousDate = this.date;
299
300 this.$body.on( 'click', this.onBodyClick.bind( this ) );
301 };
302
303 /**
304 * Construct and display buttons to navigate the calendar.
305 *
306 * @private
307 */
308 mw.widgets.CalendarWidget.prototype.buildHeaderButtons = function () {
309 this.labelButton = new OO.ui.ButtonWidget( {
310 tabIndex: -1,
311 label: '',
312 framed: false,
313 classes: [ 'mw-widget-calendarWidget-labelButton' ]
314 } );
315 this.upButton = new OO.ui.ButtonWidget( {
316 tabIndex: -1,
317 framed: false,
318 icon: 'collapse',
319 classes: [ 'mw-widget-calendarWidget-upButton' ]
320 } );
321 this.prevButton = new OO.ui.ButtonWidget( {
322 tabIndex: -1,
323 framed: false,
324 icon: 'previous',
325 classes: [ 'mw-widget-calendarWidget-prevButton' ]
326 } );
327 this.nextButton = new OO.ui.ButtonWidget( {
328 tabIndex: -1,
329 framed: false,
330 icon: 'next',
331 classes: [ 'mw-widget-calendarWidget-nextButton' ]
332 } );
333
334 this.labelButton.connect( this, { click: 'onUpButtonClick' } );
335 this.upButton.connect( this, { click: 'onUpButtonClick' } );
336 this.prevButton.connect( this, { click: 'onPrevButtonClick' } );
337 this.nextButton.connect( this, { click: 'onNextButtonClick' } );
338
339 this.$header.append(
340 this.prevButton.$element,
341 this.nextButton.$element,
342 this.upButton.$element,
343 this.labelButton.$element
344 );
345 };
346
347 /**
348 * Handle click events on the "up" button, switching to less precise view.
349 *
350 * @private
351 */
352 mw.widgets.CalendarWidget.prototype.onUpButtonClick = function () {
353 var
354 layers = this.getDisplayLayers(),
355 currentLayer = layers.indexOf( this.displayLayer );
356 if ( currentLayer !== layers.length - 1 ) {
357 // One layer up
358 this.displayLayer = layers[ currentLayer + 1 ];
359 this.updateUI( 'up' );
360 } else {
361 this.updateUI();
362 }
363 };
364
365 /**
366 * Handle click events on the "previous" button, switching to previous pane.
367 *
368 * @private
369 */
370 mw.widgets.CalendarWidget.prototype.onPrevButtonClick = function () {
371 switch ( this.displayLayer ) {
372 case 'month':
373 this.moment.subtract( 1, 'month' );
374 break;
375 case 'year':
376 this.moment.subtract( 1, 'year' );
377 break;
378 case 'duodecade':
379 this.moment.subtract( 20, 'years' );
380 break;
381 }
382 this.updateUI( 'previous' );
383 };
384
385 /**
386 * Handle click events on the "next" button, switching to next pane.
387 *
388 * @private
389 */
390 mw.widgets.CalendarWidget.prototype.onNextButtonClick = function () {
391 switch ( this.displayLayer ) {
392 case 'month':
393 this.moment.add( 1, 'month' );
394 break;
395 case 'year':
396 this.moment.add( 1, 'year' );
397 break;
398 case 'duodecade':
399 this.moment.add( 20, 'years' );
400 break;
401 }
402 this.updateUI( 'next' );
403 };
404
405 /**
406 * Handle click events anywhere in the body of the widget, which contains the matrix of days,
407 * months or years to choose. Maybe change the pane or switch to more precise view, depending on
408 * what gets clicked.
409 *
410 * @private
411 */
412 mw.widgets.CalendarWidget.prototype.onBodyClick = function ( e ) {
413 var
414 $target = $( e.target ),
415 layers = this.getDisplayLayers(),
416 currentLayer = layers.indexOf( this.displayLayer );
417 if ( $target.data( 'year' ) !== undefined ) {
418 this.moment.year( $target.data( 'year' ) );
419 }
420 if ( $target.data( 'month' ) !== undefined ) {
421 this.moment.month( $target.data( 'month' ) );
422 }
423 if ( $target.data( 'date' ) !== undefined ) {
424 this.moment.date( $target.data( 'date' ) );
425 }
426 if ( currentLayer === 0 ) {
427 this.setDateFromMoment();
428 this.updateUI( 'auto' );
429 } else {
430 // One layer down
431 this.displayLayer = layers[ currentLayer - 1 ];
432 this.updateUI( 'down' );
433 }
434 };
435
436 /**
437 * Set the date.
438 *
439 * @param {string|null} [date=null] Day or month date, in the format 'YYYY-MM-DD' or 'YYYY-MM'.
440 * When null, the calendar will show today's date, but not select it. When invalid, the date
441 * is not changed.
442 */
443 mw.widgets.CalendarWidget.prototype.setDate = function ( date ) {
444 var mom = date !== null ? moment( date, this.getDateFormat() ) : moment();
445 if ( mom.isValid() ) {
446 this.moment = mom;
447 if ( date !== null ) {
448 this.setDateFromMoment();
449 } else if ( this.date !== null ) {
450 this.date = null;
451 this.emit( 'change', this.date );
452 }
453 this.displayLayer = this.getDisplayLayers()[ 0 ];
454 this.updateUI();
455 }
456 };
457
458 /**
459 * Reset the user interface of this widget to reflect selected date.
460 */
461 mw.widgets.CalendarWidget.prototype.resetUI = function () {
462 this.moment = this.getDate() !== null ? moment( this.getDate(), this.getDateFormat() ) : moment();
463 this.displayLayer = this.getDisplayLayers()[ 0 ];
464 this.updateUI();
465 };
466
467 /**
468 * Set the date from moment object.
469 *
470 * @private
471 */
472 mw.widgets.CalendarWidget.prototype.setDateFromMoment = function () {
473 // Switch to English locale to avoid number formatting. We want the internal value to be
474 // '2015-07-24' and not '٢٠١٥-٠٧-٢٤' even if the UI language is Arabic.
475 var newDate = moment( this.moment ).locale( 'en' ).format( this.getDateFormat() );
476 if ( this.date !== newDate ) {
477 this.date = newDate;
478 this.emit( 'change', this.date );
479 }
480 };
481
482 /**
483 * Get current date, in the format 'YYYY-MM-DD' or 'YYYY-MM', depending on precision. Digits will
484 * not be localised.
485 *
486 * @return {string|null} Date string
487 */
488 mw.widgets.CalendarWidget.prototype.getDate = function () {
489 return this.date;
490 };
491
492 /**
493 * Handle focus events.
494 *
495 * @private
496 */
497 mw.widgets.CalendarWidget.prototype.onFocus = function () {
498 this.displayLayer = this.getDisplayLayers()[ 0 ];
499 this.updateUI( 'down' );
500 };
501
502 /**
503 * Handle mouse click events.
504 *
505 * @private
506 * @param {jQuery.Event} e Mouse click event
507 */
508 mw.widgets.CalendarWidget.prototype.onClick = function ( e ) {
509 if ( !this.isDisabled() && e.which === 1 ) {
510 // Prevent unintended focussing
511 return false;
512 }
513 };
514
515 /**
516 * Handle key down events.
517 *
518 * @private
519 * @param {jQuery.Event} e Key down event
520 */
521 mw.widgets.CalendarWidget.prototype.onKeyDown = function ( e ) {
522 var
523 /*jshint -W024*/
524 dir = OO.ui.Element.static.getDir( this.$element ),
525 /*jshint +W024*/
526 nextDirectionKey = dir === 'ltr' ? OO.ui.Keys.RIGHT : OO.ui.Keys.LEFT,
527 prevDirectionKey = dir === 'ltr' ? OO.ui.Keys.LEFT : OO.ui.Keys.RIGHT,
528 changed = true;
529
530 if ( !this.isDisabled() ) {
531 switch ( e.which ) {
532 case prevDirectionKey:
533 this.moment.subtract( 1, this.precision === 'month' ? 'month' : 'day' );
534 break;
535 case nextDirectionKey:
536 this.moment.add( 1, this.precision === 'month' ? 'month' : 'day' );
537 break;
538 case OO.ui.Keys.UP:
539 this.moment.subtract( 1, this.precision === 'month' ? 'month' : 'week' );
540 break;
541 case OO.ui.Keys.DOWN:
542 this.moment.add( 1, this.precision === 'month' ? 'month' : 'week' );
543 break;
544 case OO.ui.Keys.PAGEUP:
545 this.moment.subtract( 1, this.precision === 'month' ? 'year' : 'month' );
546 break;
547 case OO.ui.Keys.PAGEDOWN:
548 this.moment.add( 1, this.precision === 'month' ? 'year' : 'month' );
549 break;
550 default:
551 changed = false;
552 break;
553 }
554
555 if ( changed ) {
556 this.displayLayer = this.getDisplayLayers()[ 0 ];
557 this.setDateFromMoment();
558 this.updateUI( 'auto' );
559 return false;
560 }
561 }
562 };
563
564 /**
565 * @inheritdoc
566 */
567 mw.widgets.CalendarWidget.prototype.toggle = function ( visible ) {
568 if ( this.lazyInitOnToggle && visible ) {
569 this.lazyInitOnToggle = false;
570 this.buildHeaderButtons();
571 this.updateUI();
572 }
573
574 // Parent method
575 mw.widgets.CalendarWidget.parent.prototype.toggle.call( this, visible );
576
577 if ( this.$floatableContainer ) {
578 this.togglePositioning( this.isVisible() );
579 }
580
581 return this;
582 };
583
584 }( jQuery, mediaWiki ) );