Merge "RCFilters: Tweak opt-out preference description"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets.datetime / DiscordianDateTimeFormatter.js
1 ( function () {
2
3 /**
4 * Provides various methods needed for formatting dates and times. This
5 * implementation implments the [Discordian calendar][1], mainly for testing with
6 * something very different from the usual Gregorian calendar.
7 *
8 * Being intended mainly for testing, niceties like i18n and better
9 * configurability have been omitted.
10 *
11 * [1]: https://en.wikipedia.org/wiki/Discordian_calendar
12 *
13 * @class
14 * @extends mw.widgets.datetime.DateTimeFormatter
15 *
16 * @constructor
17 * @param {Object} [config] Configuration options
18 */
19 mw.widgets.datetime.DiscordianDateTimeFormatter = function MwWidgetsDatetimeDiscordianDateTimeFormatter( config ) {
20 config = $.extend( {}, config );
21
22 // Parent constructor
23 mw.widgets.datetime.DiscordianDateTimeFormatter.super.call( this, config );
24 };
25
26 /* Setup */
27
28 OO.inheritClass( mw.widgets.datetime.DiscordianDateTimeFormatter, mw.widgets.datetime.DateTimeFormatter );
29
30 /* Static */
31
32 /**
33 * @inheritdoc
34 */
35 mw.widgets.datetime.DiscordianDateTimeFormatter.static.formats = {
36 '@time': '${hour|0}:${minute|0}:${second|0}',
37 '@date': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#}',
38 '@datetime': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
39 '@default': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
40 };
41
42 /* Methods */
43
44 /**
45 * @inheritdoc
46 *
47 * Additional fields implemented here are:
48 * - ${year|#}: Year as a number
49 * - ${season|#}: Season as a number
50 * - ${season|full}: Season as a string
51 * - ${day|#}: Day of the month as a number
52 * - ${day|0}: Day of the month as a number with leading 0
53 * - ${dow|full}: Day of the week as a string
54 * - ${hour|#}: Hour as a number
55 * - ${hour|0}: Hour as a number with leading 0
56 * - ${minute|#}: Minute as a number
57 * - ${minute|0}: Minute as a number with leading 0
58 * - ${second|#}: Second as a number
59 * - ${second|0}: Second as a number with leading 0
60 * - ${millisecond|#}: Millisecond as a number
61 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
62 */
63 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getFieldForTag = function ( tag, params ) {
64 var spec = null;
65
66 switch ( tag + '|' + params[ 0 ] ) {
67 case 'year|#':
68 spec = {
69 component: 'Year',
70 calendarComponent: true,
71 type: 'number',
72 size: 4,
73 zeropad: false
74 };
75 break;
76
77 case 'season|#':
78 spec = {
79 component: 'Season',
80 calendarComponent: true,
81 type: 'number',
82 size: 1,
83 intercalarySize: { 1: 0 },
84 zeropad: false
85 };
86 break;
87
88 case 'season|full':
89 spec = {
90 component: 'Season',
91 calendarComponent: true,
92 type: 'string',
93 intercalarySize: { 1: 0 },
94 values: {
95 1: 'Chaos',
96 2: 'Discord',
97 3: 'Confusion',
98 4: 'Bureaucracy',
99 5: 'The Aftermath'
100 }
101 };
102 break;
103
104 case 'dow|full':
105 spec = {
106 component: 'DOW',
107 calendarComponent: true,
108 editable: false,
109 type: 'string',
110 intercalarySize: { 1: 0 },
111 values: {
112 '-1': 'N/A',
113 0: 'Sweetmorn',
114 1: 'Boomtime',
115 2: 'Pungenday',
116 3: 'Prickle-Prickle',
117 4: 'Setting Orange'
118 }
119 };
120 break;
121
122 case 'day|#':
123 case 'day|0':
124 spec = {
125 component: 'Day',
126 calendarComponent: true,
127 type: 'string',
128 size: 2,
129 intercalarySize: { 1: 13 },
130 zeropad: params[ 0 ] === '0',
131 formatValue: function ( v ) {
132 if ( v === 'tib' ) {
133 return 'St. Tib\'s Day';
134 }
135 return mw.widgets.datetime.DateTimeFormatter.prototype.formatSpecValue.call( this, v );
136 },
137 parseValue: function ( v ) {
138 if ( /^\s*(st.?\s*)?tib('?s)?(\s*day)?\s*$/i.test( v ) ) {
139 return 'tib';
140 }
141 return mw.widgets.datetime.DateTimeFormatter.prototype.parseSpecValue.call( this, v );
142 }
143 };
144 break;
145
146 case 'hour|#':
147 case 'hour|0':
148 case 'minute|#':
149 case 'minute|0':
150 case 'second|#':
151 case 'second|0':
152 spec = {
153 component: tag.charAt( 0 ).toUpperCase() + tag.slice( 1 ),
154 calendarComponent: false,
155 type: 'number',
156 size: 2,
157 zeropad: params[ 0 ] === '0'
158 };
159 break;
160
161 case 'millisecond|#':
162 case 'millisecond|0':
163 spec = {
164 component: 'Millisecond',
165 calendarComponent: false,
166 type: 'number',
167 size: 3,
168 zeropad: params[ 0 ] === '0'
169 };
170 break;
171
172 default:
173 return mw.widgets.datetime.DiscordianDateTimeFormatter.super.prototype.getFieldForTag.call( this, tag, params );
174 }
175
176 if ( spec ) {
177 if ( spec.editable === undefined ) {
178 spec.editable = true;
179 }
180 if ( spec.component !== 'Day' ) {
181 spec.formatValue = this.formatSpecValue;
182 spec.parseValue = this.parseSpecValue;
183 }
184 if ( spec.values ) {
185 spec.size = Math.max.apply(
186 // eslint-disable-next-line jquery/no-map-util
187 null, $.map( spec.values, function ( v ) { return v.length; } )
188 );
189 }
190 }
191
192 return spec;
193 };
194
195 /**
196 * Get components from a Date object
197 *
198 * Components are:
199 * - Year {number}
200 * - Season {number} 1-5
201 * - Day {number|string} 1-73 or 'tib'
202 * - DOW {number} 0-4, or -1 on St. Tib's Day
203 * - Hour {number} 0-23
204 * - Minute {number} 0-59
205 * - Second {number} 0-59
206 * - Millisecond {number} 0-999
207 * - intercalary {string} '1' on St. Tib's Day
208 *
209 * @param {Date|null} date
210 * @return {Object} Components
211 */
212 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getComponentsFromDate = function ( date ) {
213 var ret, day, month,
214 monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ];
215
216 if ( !( date instanceof Date ) ) {
217 date = this.defaultDate;
218 }
219
220 if ( this.local ) {
221 day = date.getDate();
222 month = date.getMonth();
223 ret = {
224 Year: date.getFullYear() + 1166,
225 Hour: date.getHours(),
226 Minute: date.getMinutes(),
227 Second: date.getSeconds(),
228 Millisecond: date.getMilliseconds(),
229 zone: date.getTimezoneOffset()
230 };
231 } else {
232 day = date.getUTCDate();
233 month = date.getUTCMonth();
234 ret = {
235 Year: date.getUTCFullYear() + 1166,
236 Hour: date.getUTCHours(),
237 Minute: date.getUTCMinutes(),
238 Second: date.getUTCSeconds(),
239 Millisecond: date.getUTCMilliseconds(),
240 zone: 0
241 };
242 }
243
244 if ( month === 1 && day === 29 ) {
245 ret.Season = 1;
246 ret.Day = 'tib';
247 ret.DOW = -1;
248 ret.intercalary = '1';
249 } else {
250 day = monthDays[ month ] + day - 1;
251 ret.Season = Math.floor( day / 73 ) + 1;
252 ret.Day = ( day % 73 ) + 1;
253 ret.DOW = day % 5;
254 ret.intercalary = '';
255 }
256
257 return ret;
258 };
259
260 /**
261 * @inheritdoc
262 */
263 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.adjustComponent = function ( date, component, delta, mode ) {
264 return this.getDateFromComponents(
265 this.adjustComponentInternal(
266 this.getComponentsFromDate( date ), component, delta, mode
267 )
268 );
269 };
270
271 /**
272 * Adjust the components directly
273 *
274 * @private
275 * @param {Object} components Modified in place
276 * @param {string} component
277 * @param {number} delta
278 * @param {string} mode
279 * @return {Object} components
280 */
281 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.adjustComponentInternal = function ( components, component, delta, mode ) {
282 var i, min, max, range, next, preTib, postTib, wasTib;
283
284 if ( delta === 0 ) {
285 return components;
286 }
287
288 switch ( component ) {
289 case 'Year':
290 min = 1166;
291 max = 11165;
292 next = null;
293 break;
294 case 'Season':
295 min = 1;
296 max = 5;
297 next = 'Year';
298 break;
299 case 'Week':
300 if ( components.Day === 'tib' ) {
301 components.Day = 59; // Could choose either one...
302 components.Season = 1;
303 }
304 min = 1;
305 max = 73;
306 next = 'Season';
307 break;
308 case 'Day':
309 min = 1;
310 max = 73;
311 next = 'Season';
312 break;
313 case 'Hour':
314 min = 0;
315 max = 23;
316 next = 'Day';
317 break;
318 case 'Minute':
319 min = 0;
320 max = 59;
321 next = 'Hour';
322 break;
323 case 'Second':
324 min = 0;
325 max = 59;
326 next = 'Minute';
327 break;
328 case 'Millisecond':
329 min = 0;
330 max = 999;
331 next = 'Second';
332 break;
333 default:
334 return components;
335 }
336
337 switch ( mode ) {
338 case 'overflow':
339 case 'clip':
340 case 'wrap':
341 }
342
343 if ( component === 'Day' ) {
344 i = Math.abs( delta );
345 delta = delta < 0 ? -1 : 1;
346 preTib = delta > 0 ? 59 : 60;
347 postTib = delta > 0 ? 60 : 59;
348 while ( i-- > 0 ) {
349 if ( components.Day === preTib && components.Season === 1 && this.isLeapYear( components.Year ) ) {
350 components.Day = 'tib';
351 } else if ( components.Day === 'tib' ) {
352 components.Day = postTib;
353 components.Season = 1;
354 } else {
355 components.Day += delta;
356 if ( components.Day < min ) {
357 switch ( mode ) {
358 case 'overflow':
359 components.Day = max;
360 this.adjustComponentInternal( components, 'Season', -1, mode );
361 break;
362 case 'wrap':
363 components.Day = max;
364 break;
365 case 'clip':
366 components.Day = min;
367 i = 0;
368 break;
369 }
370 }
371 if ( components.Day > max ) {
372 switch ( mode ) {
373 case 'overflow':
374 components.Day = min;
375 this.adjustComponentInternal( components, 'Season', 1, mode );
376 break;
377 case 'wrap':
378 components.Day = min;
379 break;
380 case 'clip':
381 components.Day = max;
382 i = 0;
383 break;
384 }
385 }
386 }
387 }
388 } else {
389 if ( component === 'Week' ) {
390 component = 'Day';
391 delta *= 5;
392 }
393 if ( components.Day === 'tib' ) {
394 // For sanity
395 components.Season = 1;
396 }
397 switch ( mode ) {
398 case 'overflow':
399 if ( components.Day === 'tib' && ( component === 'Season' || component === 'Year' ) ) {
400 components.Day = 59; // Could choose either one...
401 wasTib = true;
402 } else {
403 wasTib = false;
404 }
405 i = Math.abs( delta );
406 delta = delta < 0 ? -1 : 1;
407 while ( i-- > 0 ) {
408 components[ component ] += delta;
409 if ( components[ component ] < min ) {
410 components[ component ] = max;
411 components = this.adjustComponentInternal( components, next, -1, mode );
412 }
413 if ( components[ component ] > max ) {
414 components[ component ] = min;
415 components = this.adjustComponentInternal( components, next, 1, mode );
416 }
417 }
418 if ( wasTib && components.Season === 1 && this.isLeapYear( components.Year ) ) {
419 components.Day = 'tib';
420 }
421 break;
422 case 'wrap':
423 range = max - min + 1;
424 components[ component ] += delta;
425 while ( components[ component ] < min ) {
426 components[ component ] += range;
427 }
428 while ( components[ component ] > max ) {
429 components[ component ] -= range;
430 }
431 break;
432 case 'clip':
433 components[ component ] += delta;
434 if ( components[ component ] < min ) {
435 components[ component ] = min;
436 }
437 if ( components[ component ] > max ) {
438 components[ component ] = max;
439 }
440 break;
441 }
442 if ( components.Day === 'tib' &&
443 ( components.Season !== 1 || !this.isLeapYear( components.Year ) )
444 ) {
445 components.Day = 59; // Could choose either one...
446 }
447 }
448
449 return components;
450 };
451
452 /**
453 * @inheritdoc
454 */
455 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getDateFromComponents = function ( components ) {
456 var month, day, days,
457 date = new Date(),
458 monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ];
459
460 components = $.extend( {}, this.getComponentsFromDate( null ), components );
461 if ( components.Day === 'tib' ) {
462 month = 1;
463 day = 29;
464 } else {
465 days = components.Season * 73 + components.Day - 74;
466 month = 0;
467 while ( days >= monthDays[ month + 1 ] ) {
468 month++;
469 }
470 day = days - monthDays[ month ] + 1;
471 }
472
473 if ( components.zone ) {
474 // Can't just use the constructor because that's stupid about ancient years.
475 date.setFullYear( components.Year - 1166, month, day );
476 date.setHours( components.Hour, components.Minute, components.Second, components.Millisecond );
477 } else {
478 // Date.UTC() is stupid about ancient years too.
479 date.setUTCFullYear( components.Year - 1166, month, day );
480 date.setUTCHours( components.Hour, components.Minute, components.Second, components.Millisecond );
481 }
482
483 return date;
484 };
485
486 /**
487 * Get whether the year is a leap year
488 *
489 * @private
490 * @param {number} year
491 * @return {boolean}
492 */
493 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.isLeapYear = function ( year ) {
494 year -= 1166;
495 if ( year % 4 ) {
496 return false;
497 } else if ( year % 100 ) {
498 return true;
499 }
500 return ( year % 400 ) === 0;
501 };
502
503 /**
504 * @inheritdoc
505 */
506 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getCalendarHeadings = function () {
507 return [ 'SM', 'BT', 'PD', 'PP', null, 'SO' ];
508 };
509
510 /**
511 * @inheritdoc
512 */
513 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.sameCalendarGrid = function ( date1, date2 ) {
514 var components1 = this.getComponentsFromDate( date1 ),
515 components2 = this.getComponentsFromDate( date2 );
516
517 return components1.Year === components2.Year && components1.Season === components2.Season;
518 };
519
520 /**
521 * @inheritdoc
522 */
523 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getCalendarData = function ( date ) {
524 var dt, components, season, i, row,
525 ret = {
526 dayComponent: 'Day',
527 weekComponent: 'Week',
528 monthComponent: 'Season'
529 },
530 seasons = [ 'Chaos', 'Discord', 'Confusion', 'Bureaucracy', 'The Aftermath' ],
531 seasonStart = [ 0, -3, -1, -4, -2 ];
532
533 if ( !( date instanceof Date ) ) {
534 date = this.defaultDate;
535 }
536
537 components = this.getComponentsFromDate( date );
538 components.Day = 1;
539 season = components.Season;
540
541 ret.header = seasons[ season - 1 ] + ' ' + components.Year;
542
543 if ( seasonStart[ season - 1 ] ) {
544 this.adjustComponentInternal( components, 'Day', seasonStart[ season - 1 ], 'overflow' );
545 }
546
547 ret.rows = [];
548 do {
549 row = [];
550 for ( i = 0; i < 6; i++ ) {
551 dt = this.getDateFromComponents( components );
552 row[ i ] = {
553 display: components.Day === 'tib' ? 'Tib' : String( components.Day ),
554 date: dt,
555 extra: components.Season < season ? 'prev' : components.Season > season ? 'next' : null
556 };
557
558 this.adjustComponentInternal( components, 'Day', 1, 'overflow' );
559 if ( components.Day !== 'tib' && i === 3 ) {
560 row[ ++i ] = null;
561 }
562 }
563
564 ret.rows.push( row );
565 } while ( components.Season === season );
566
567 return ret;
568 };
569
570 }() );