Merge "Bypass login page if no user input is required."
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets.datetime / ProlepticGregorianDateTimeFormatter.js
1 ( function ( $, mw ) {
2
3 /**
4 * Provides various methods needed for formatting dates and times. This
5 * implementation implments the proleptic Gregorian calendar over years
6 * 0000–9999.
7 *
8 * @class
9 * @extends mw.widgets.datetime.DateTimeFormatter
10 *
11 * @constructor
12 * @param {Object} [config] Configuration options
13 * @cfg {Object} [fullMonthNames] Mapping 1–12 to full month names.
14 * @cfg {Object} [shortMonthNames] Mapping 1–12 to abbreviated month names.
15 * If {@link #fullMonthNames fullMonthNames} is given and this is not,
16 * defaults to the first three characters from that setting.
17 * @cfg {Object} [fullDayNames] Mapping 0–6 to full day of week names. 0 is Sunday, 6 is Saturday.
18 * @cfg {Object} [shortDayNames] Mapping 0–6 to abbreviated day of week names. 0 is Sunday, 6 is Saturday.
19 * If {@link #fullDayNames fullDayNames} is given and this is not, defaults to
20 * the first three characters from that setting.
21 * @cfg {string[]} [dayLetters] Weekday column headers for a calendar. Array of 7 strings.
22 * If {@link #fullDayNames fullDayNames} or {@link #shortDayNames shortDayNames}
23 * are given and this is not, defaults to the first character from
24 * shortDayNames.
25 * @cfg {string[]} [hour12Periods] AM and PM texts. Array of 2 strings, AM and PM.
26 * @cfg {number} [weekStartsOn=0] What day the week starts on: 0 is Sunday, 1 is Monday, 6 is Saturday.
27 */
28 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter = function MwWidgetsDatetimeProlepticGregorianDateTimeFormatter( config ) {
29 this.constructor.static.setupDefaults();
30
31 config = $.extend( {
32 weekStartsOn: 0,
33 hour12Periods: this.constructor.static.hour12Periods
34 }, config );
35
36 if ( config.fullMonthNames && !config.shortMonthNames ) {
37 config.shortMonthNames = {};
38 $.each( config.fullMonthNames, function ( k, v ) {
39 config.shortMonthNames[ k ] = v.substr( 0, 3 );
40 }.bind( this ) );
41 }
42 if ( config.shortDayNames && !config.dayLetters ) {
43 config.dayLetters = [];
44 $.each( config.shortDayNames, function ( k, v ) {
45 config.dayLetters[ k ] = v.substr( 0, 1 );
46 }.bind( this ) );
47 }
48 if ( config.fullDayNames && !config.dayLetters ) {
49 config.dayLetters = [];
50 $.each( config.fullDayNames, function ( k, v ) {
51 config.dayLetters[ k ] = v.substr( 0, 1 );
52 }.bind( this ) );
53 }
54 if ( config.fullDayNames && !config.shortDayNames ) {
55 config.shortDayNames = {};
56 $.each( config.fullDayNames, function ( k, v ) {
57 config.shortDayNames[ k ] = v.substr( 0, 3 );
58 }.bind( this ) );
59 }
60 config = $.extend( {
61 fullMonthNames: this.constructor.static.fullMonthNames,
62 shortMonthNames: this.constructor.static.shortMonthNames,
63 fullDayNames: this.constructor.static.fullDayNames,
64 shortDayNames: this.constructor.static.shortDayNames,
65 dayLetters: this.constructor.static.dayLetters
66 }, config );
67
68 // Parent constructor
69 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter[ 'super' ].call( this, config );
70
71 // Properties
72 this.weekStartsOn = config.weekStartsOn % 7;
73 this.fullMonthNames = config.fullMonthNames;
74 this.shortMonthNames = config.shortMonthNames;
75 this.fullDayNames = config.fullDayNames;
76 this.shortDayNames = config.shortDayNames;
77 this.dayLetters = config.dayLetters;
78 this.hour12Periods = config.hour12Periods;
79 };
80
81 /* Setup */
82
83 OO.inheritClass( mw.widgets.datetime.ProlepticGregorianDateTimeFormatter, mw.widgets.datetime.DateTimeFormatter );
84
85 /* Static */
86
87 /**
88 * @inheritdoc
89 */
90 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.formats = {
91 '@time': '${hour|0}:${minute|0}:${second|0}',
92 '@date': '$!{dow|short} ${day|#} ${month|short} ${year|#}',
93 '@datetime': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
94 '@default': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
95 };
96
97 /**
98 * Default full month names.
99 *
100 * @static
101 * @inheritable
102 * @property {Object}
103 */
104 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.fullMonthNames = null;
105
106 /**
107 * Default abbreviated month names.
108 *
109 * @static
110 * @inheritable
111 * @property {Object}
112 */
113 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.shortMonthNames = null;
114
115 /**
116 * Default full day of week names.
117 *
118 * @static
119 * @inheritable
120 * @property {Object}
121 */
122 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.fullDayNames = null;
123
124 /**
125 * Default abbreviated day of week names.
126 *
127 * @static
128 * @inheritable
129 * @property {Object}
130 */
131 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.shortDayNames = null;
132
133 /**
134 * Default day letters.
135 *
136 * @static
137 * @inheritable
138 * @property {string[]}
139 */
140 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.dayLetters = null;
141
142 /**
143 * Default AM/PM indicators
144 *
145 * @static
146 * @inheritable
147 * @property {string[]}
148 */
149 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.hour12Periods = null;
150
151 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.static.setupDefaults = function () {
152 mw.widgets.datetime.DateTimeFormatter.static.setupDefaults.call( this );
153
154 if ( this.fullMonthNames && !this.shortMonthNames ) {
155 this.shortMonthNames = {};
156 $.each( this.fullMonthNames, function ( k, v ) {
157 this.shortMonthNames[ k ] = v.substr( 0, 3 );
158 }.bind( this ) );
159 }
160 if ( this.shortDayNames && !this.dayLetters ) {
161 this.dayLetters = [];
162 $.each( this.shortDayNames, function ( k, v ) {
163 this.dayLetters[ k ] = v.substr( 0, 1 );
164 }.bind( this ) );
165 }
166 if ( this.fullDayNames && !this.dayLetters ) {
167 this.dayLetters = [];
168 $.each( this.fullDayNames, function ( k, v ) {
169 this.dayLetters[ k ] = v.substr( 0, 1 );
170 }.bind( this ) );
171 }
172 if ( this.fullDayNames && !this.shortDayNames ) {
173 this.shortDayNames = {};
174 $.each( this.fullDayNames, function ( k, v ) {
175 this.shortDayNames[ k ] = v.substr( 0, 3 );
176 }.bind( this ) );
177 }
178
179 if ( !this.fullMonthNames ) {
180 this.fullMonthNames = {
181 1: mw.msg( 'january' ),
182 2: mw.msg( 'february' ),
183 3: mw.msg( 'march' ),
184 4: mw.msg( 'april' ),
185 5: mw.msg( 'may_long' ),
186 6: mw.msg( 'june' ),
187 7: mw.msg( 'july' ),
188 8: mw.msg( 'august' ),
189 9: mw.msg( 'september' ),
190 10: mw.msg( 'october' ),
191 11: mw.msg( 'november' ),
192 12: mw.msg( 'december' )
193 };
194 }
195 if ( !this.shortMonthNames ) {
196 this.shortMonthNames = {
197 1: mw.msg( 'jan' ),
198 2: mw.msg( 'feb' ),
199 3: mw.msg( 'mar' ),
200 4: mw.msg( 'apr' ),
201 5: mw.msg( 'may' ),
202 6: mw.msg( 'jun' ),
203 7: mw.msg( 'jul' ),
204 8: mw.msg( 'aug' ),
205 9: mw.msg( 'sep' ),
206 10: mw.msg( 'oct' ),
207 11: mw.msg( 'nov' ),
208 12: mw.msg( 'dec' )
209 };
210 }
211
212 if ( !this.fullDayNames ) {
213 this.fullDayNames = {
214 0: mw.msg( 'sunday' ),
215 1: mw.msg( 'monday' ),
216 2: mw.msg( 'tuesday' ),
217 3: mw.msg( 'wednesday' ),
218 4: mw.msg( 'thursday' ),
219 5: mw.msg( 'friday' ),
220 6: mw.msg( 'saturday' )
221 };
222 }
223 if ( !this.shortDayNames ) {
224 this.shortDayNames = {
225 0: mw.msg( 'sun' ),
226 1: mw.msg( 'mon' ),
227 2: mw.msg( 'tue' ),
228 3: mw.msg( 'wed' ),
229 4: mw.msg( 'thu' ),
230 5: mw.msg( 'fri' ),
231 6: mw.msg( 'sat' )
232 };
233 }
234 if ( !this.dayLetters ) {
235 this.dayLetters = [];
236 $.each( this.shortDayNames, function ( k, v ) {
237 this.dayLetters[ k ] = v.substr( 0, 1 );
238 }.bind( this ) );
239 }
240
241 if ( !this.hour12Periods ) {
242 this.hour12Periods = [
243 mw.msg( 'period-am' ),
244 mw.msg( 'period-pm' )
245 ];
246 }
247 };
248
249 /* Methods */
250
251 /**
252 * @inheritdoc
253 *
254 * Additional fields implemented here are:
255 * - ${year|#}: Year as a number
256 * - ${year|0}: Year as a number, zero-padded to 4 digits
257 * - ${month|#}: Month as a number
258 * - ${month|0}: Month as a number with leading 0
259 * - ${month|short}: Month from 'shortMonthNames' configuration setting
260 * - ${month|full}: Month from 'fullMonthNames' configuration setting
261 * - ${day|#}: Day of the month as a number
262 * - ${day|0}: Day of the month as a number with leading 0
263 * - ${dow|short}: Day of the week from 'shortDayNames' configuration setting
264 * - ${dow|full}: Day of the week from 'fullDayNames' configuration setting
265 * - ${hour|#}: Hour as a number
266 * - ${hour|0}: Hour as a number with leading 0
267 * - ${hour|12}: Hour in a 12-hour clock as a number
268 * - ${hour|012}: Hour in a 12-hour clock as a number, with leading 0
269 * - ${hour|period}: Value from 'hour12Periods' configuration setting
270 * - ${minute|#}: Minute as a number
271 * - ${minute|0}: Minute as a number with leading 0
272 * - ${second|#}: Second as a number
273 * - ${second|0}: Second as a number with leading 0
274 * - ${millisecond|#}: Millisecond as a number
275 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
276 */
277 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.getFieldForTag = function ( tag, params ) {
278 var spec = null;
279
280 switch ( tag + '|' + params[ 0 ] ) {
281 case 'year|#':
282 case 'year|0':
283 spec = {
284 component: 'year',
285 type: 'number',
286 size: 4,
287 zeropad: params[ 0 ] === '0'
288 };
289 break;
290
291 case 'month|short':
292 case 'month|full':
293 spec = {
294 component: 'month',
295 type: 'string',
296 values: params[ 0 ] === 'short' ? this.shortMonthNames : this.fullMonthNames
297 };
298 break;
299
300 case 'dow|short':
301 case 'dow|full':
302 spec = {
303 component: 'dow',
304 editable: false,
305 type: 'string',
306 values: params[ 0 ] === 'short' ? this.shortDayNames : this.fullDayNames
307 };
308 break;
309
310 case 'month|#':
311 case 'month|0':
312 case 'day|#':
313 case 'day|0':
314 case 'hour|#':
315 case 'hour|0':
316 case 'minute|#':
317 case 'minute|0':
318 case 'second|#':
319 case 'second|0':
320 spec = {
321 component: tag,
322 type: 'number',
323 size: 2,
324 zeropad: params[ 0 ] === '0'
325 };
326 break;
327
328 case 'hour|12':
329 case 'hour|012':
330 spec = {
331 component: 'hour12',
332 type: 'number',
333 size: 2,
334 zeropad: params[ 0 ] === '012'
335 };
336 break;
337
338 case 'hour|period':
339 spec = {
340 component: 'hour12period',
341 type: 'boolean',
342 values: this.hour12Periods
343 };
344 break;
345
346 case 'millisecond|#':
347 case 'millisecond|0':
348 spec = {
349 component: 'millisecond',
350 type: 'number',
351 size: 3,
352 zeropad: params[ 0 ] === '0'
353 };
354 break;
355
356 default:
357 return mw.widgets.datetime.ProlepticGregorianDateTimeFormatter[ 'super' ].prototype.getFieldForTag.call( this, tag, params );
358 }
359
360 if ( spec ) {
361 if ( spec.editable === undefined ) {
362 spec.editable = true;
363 }
364 spec.formatValue = this.formatSpecValue;
365 spec.parseValue = this.parseSpecValue;
366 if ( spec.values ) {
367 spec.size = Math.max.apply(
368 null, $.map( spec.values, function ( v ) { return v.length; } )
369 );
370 }
371 }
372
373 return spec;
374 };
375
376 /**
377 * Get components from a Date object
378 *
379 * Components are:
380 * - year {number}
381 * - month {number} (1-12)
382 * - day {number} (1-31)
383 * - dow {number} (0-6, 0 is Sunday)
384 * - hour {number} (0-23)
385 * - hour12 {number} (1-12)
386 * - hour12period {boolean}
387 * - minute {number} (0-59)
388 * - second {number} (0-59)
389 * - millisecond {number} (0-999)
390 * - zone {number}
391 *
392 * @param {Date|null} date
393 * @return {Object} Components
394 */
395 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.getComponentsFromDate = function ( date ) {
396 var ret;
397
398 if ( !( date instanceof Date ) ) {
399 date = this.defaultDate;
400 }
401
402 if ( this.local ) {
403 ret = {
404 year: date.getFullYear(),
405 month: date.getMonth() + 1,
406 day: date.getDate(),
407 dow: date.getDay() % 7,
408 hour: date.getHours(),
409 minute: date.getMinutes(),
410 second: date.getSeconds(),
411 millisecond: date.getMilliseconds(),
412 zone: date.getTimezoneOffset()
413 };
414 } else {
415 ret = {
416 year: date.getUTCFullYear(),
417 month: date.getUTCMonth() + 1,
418 day: date.getUTCDate(),
419 dow: date.getUTCDay() % 7,
420 hour: date.getUTCHours(),
421 minute: date.getUTCMinutes(),
422 second: date.getUTCSeconds(),
423 millisecond: date.getUTCMilliseconds(),
424 zone: 0
425 };
426 }
427
428 ret.hour12period = ret.hour >= 12 ? 1 : 0;
429 ret.hour12 = ret.hour % 12;
430 if ( ret.hour12 === 0 ) {
431 ret.hour12 = 12;
432 }
433
434 return ret;
435 };
436
437 /**
438 * @inheritdoc
439 */
440 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.getDateFromComponents = function ( components ) {
441 var date = new Date();
442
443 components = $.extend( {}, components );
444 if ( components.hour === undefined && components.hour12 !== undefined && components.hour12period !== undefined ) {
445 components.hour = ( components.hour12 % 12 ) + ( components.hour12period ? 12 : 0 );
446 }
447 components = $.extend( {}, this.getComponentsFromDate( null ), components );
448
449 if ( components.zone ) {
450 // Can't just use the constructor because that's stupid about ancient years.
451 date.setFullYear( components.year, components.month - 1, components.day );
452 date.setHours( components.hour, components.minute, components.second, components.millisecond );
453 } else {
454 // Date.UTC() is stupid about ancient years too.
455 date.setUTCFullYear( components.year, components.month - 1, components.day );
456 date.setUTCHours( components.hour, components.minute, components.second, components.millisecond );
457 }
458
459 return date;
460 };
461
462 /**
463 * @inheritdoc
464 */
465 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.adjustComponent = function ( date, component, delta, mode ) {
466 var min, max, range, components;
467
468 if ( !( date instanceof Date ) ) {
469 date = this.defaultDate;
470 }
471 components = this.getComponentsFromDate( date );
472
473 switch ( component ) {
474 case 'year':
475 min = 0;
476 max = 9999;
477 break;
478 case 'month':
479 min = 1;
480 max = 12;
481 break;
482 case 'day':
483 min = 1;
484 max = this.getDaysInMonth( components.month, components.year );
485 break;
486 case 'hour':
487 min = 0;
488 max = 23;
489 break;
490 case 'minute':
491 case 'second':
492 min = 0;
493 max = 59;
494 break;
495 case 'millisecond':
496 min = 0;
497 max = 999;
498 break;
499 case 'hour12period':
500 component = 'hour';
501 min = 0;
502 max = 23;
503 delta *= 12;
504 break;
505 case 'hour12':
506 component = 'hour';
507 min = components.hour12period ? 12 : 0;
508 max = components.hour12period ? 23 : 11;
509 break;
510 default:
511 return new Date( date.getTime() );
512 }
513
514 components[ component ] += delta;
515 range = max - min + 1;
516 switch ( mode ) {
517 case 'overflow':
518 // Date() will mostly handle it automatically. But months need
519 // manual handling to prevent e.g. Jan 31 => Mar 3.
520 if ( component === 'month' || component === 'year' ) {
521 while ( components.month < 1 ) {
522 components[ component ] += 12;
523 components.year--;
524 }
525 while ( components.month > 12 ) {
526 components[ component ] -= 12;
527 components.year++;
528 }
529 }
530 break;
531 case 'wrap':
532 while ( components[ component ] < min ) {
533 components[ component ] += range;
534 }
535 while ( components[ component ] > max ) {
536 components[ component ] -= range;
537 }
538 break;
539 case 'clip':
540 if ( components[ component ] < min ) {
541 components[ component ] = min;
542 }
543 if ( components[ component ] < max ) {
544 components[ component ] = max;
545 }
546 break;
547 }
548 if ( component === 'month' || component === 'year' ) {
549 components.day = Math.min( components.day, this.getDaysInMonth( components.month, components.year ) );
550 }
551
552 return this.getDateFromComponents( components );
553 };
554
555 /**
556 * Get the number of days in a month
557 *
558 * @protected
559 * @param {number} month
560 * @param {number} year
561 * @return {number}
562 */
563 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.getDaysInMonth = function ( month, year ) {
564 switch ( month ) {
565 case 4:
566 case 6:
567 case 9:
568 case 11:
569 return 30;
570 case 2:
571 if ( year % 4 ) {
572 return 28;
573 } else if ( year % 100 ) {
574 return 29;
575 }
576 return ( year % 400 ) ? 28 : 29;
577 default:
578 return 31;
579 }
580 };
581
582 /**
583 * @inheritdoc
584 */
585 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.getCalendarHeadings = function () {
586 var a = this.dayLetters;
587
588 if ( this.weekStartsOn ) {
589 return a.slice( this.weekStartsOn ).concat( a.slice( 0, this.weekStartsOn ) );
590 } else {
591 return a.slice( 0 ); // clone
592 }
593 };
594
595 /**
596 * @inheritdoc
597 */
598 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.sameCalendarGrid = function ( date1, date2 ) {
599 if ( this.local ) {
600 return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth();
601 } else {
602 return date1.getUTCFullYear() === date2.getUTCFullYear() && date1.getUTCMonth() === date2.getUTCMonth();
603 }
604 };
605
606 /**
607 * @inheritdoc
608 */
609 mw.widgets.datetime.ProlepticGregorianDateTimeFormatter.prototype.getCalendarData = function ( date ) {
610 var dt, t, d, e, i, row,
611 getDate = this.local ? 'getDate' : 'getUTCDate',
612 setDate = this.local ? 'setDate' : 'setUTCDate',
613 ret = {
614 dayComponent: 'day',
615 monthComponent: 'month'
616 };
617
618 if ( !( date instanceof Date ) ) {
619 date = this.defaultDate;
620 }
621
622 dt = new Date( date.getTime() );
623 dt[ setDate ]( 1 );
624 t = dt.getTime();
625
626 if ( this.local ) {
627 ret.header = this.fullMonthNames[ dt.getMonth() + 1 ] + ' ' + dt.getFullYear();
628 d = dt.getDay() % 7;
629 e = this.getDaysInMonth( dt.getMonth() + 1, dt.getFullYear() );
630 } else {
631 ret.header = this.fullMonthNames[ dt.getUTCMonth() + 1 ] + ' ' + dt.getUTCFullYear();
632 d = dt.getUTCDay() % 7;
633 e = this.getDaysInMonth( dt.getUTCMonth() + 1, dt.getUTCFullYear() );
634 }
635
636 if ( this.weekStartsOn ) {
637 d = ( d + 7 - this.weekStartsOn ) % 7;
638 }
639 d = 1 - d;
640
641 ret.rows = [];
642 while ( d <= e ) {
643 row = [];
644 for ( i = 0; i < 7; i++, d++ ) {
645 dt = new Date( t );
646 dt[ setDate ]( d );
647 row[ i ] = {
648 display: String( dt[ getDate ]() ),
649 date: dt,
650 extra: d < 1 ? 'prev' : d > e ? 'next' : null
651 };
652 }
653 ret.rows.push( row );
654 }
655
656 return ret;
657 };
658
659 }( jQuery, mediaWiki ) );