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