| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
mw.widgets.datetime.DateTimeFormatter = function MwWidgetsDatetimeDateTimeFormatter( config ) { |
| 25 |
this.constructor.static.setupDefaults(); |
| 26 |
|
| 27 |
config = Object.assign( { |
| 28 |
format: '@default', |
| 29 |
local: false, |
| 30 |
fullZones: this.constructor.static.fullZones, |
| 31 |
shortZones: this.constructor.static.shortZones |
| 32 |
}, config ); |
| 33 |
|
| 34 |
|
| 35 |
OO.EventEmitter.call( this ); |
| 36 |
|
| 37 |
|
| 38 |
if ( this.constructor.static.formats[ config.format ] ) { |
| 39 |
this.format = this.constructor.static.formats[ config.format ]; |
| 40 |
} else { |
| 41 |
this.format = config.format; |
| 42 |
} |
| 43 |
this.local = !!config.local; |
| 44 |
this.fullZones = config.fullZones; |
| 45 |
this.shortZones = config.shortZones; |
| 46 |
if ( config.defaultDate instanceof Date ) { |
| 47 |
this.defaultDate = config.defaultDate; |
| 48 |
} else { |
| 49 |
this.defaultDate = new Date(); |
| 50 |
if ( this.local ) { |
| 51 |
this.defaultDate.setMilliseconds( 0 ); |
| 52 |
} else { |
| 53 |
this.defaultDate.setUTCMilliseconds( 0 ); |
| 54 |
} |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
OO.initClass( mw.widgets.datetime.DateTimeFormatter ); |
| 61 |
OO.mixinClass( mw.widgets.datetime.DateTimeFormatter, OO.EventEmitter ); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
mw.widgets.datetime.DateTimeFormatter.static.formats = {}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
mw.widgets.datetime.DateTimeFormatter.static.fullZones = null; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
mw.widgets.datetime.DateTimeFormatter.static.shortZones = null; |
| 94 |
|
| 95 |
mw.widgets.datetime.DateTimeFormatter.static.setupDefaults = function () { |
| 96 |
if ( !this.fullZones ) { |
| 97 |
this.fullZones = [ |
| 98 |
mw.msg( 'timezone-utc' ), |
| 99 |
mw.msg( 'timezone-local' ) |
| 100 |
]; |
| 101 |
} |
| 102 |
if ( !this.shortZones ) { |
| 103 |
this.shortZones = [ |
| 104 |
'Z', |
| 105 |
this.fullZones[ 1 ].slice( 0, 1 ).toUpperCase() |
| 106 |
]; |
| 107 |
if ( this.shortZones[ 1 ] === 'Z' ) { |
| 108 |
this.shortZones[ 1 ] = 'L'; |
| 109 |
} |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
mw.widgets.datetime.DateTimeFormatter.prototype.getLocal = function () { |
| 130 |
return this.local; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
mw.widgets.datetime.DateTimeFormatter.prototype.toggleLocal = function ( flag ) { |
| 142 |
if ( flag === undefined ) { |
| 143 |
flag = !this.local; |
| 144 |
} else { |
| 145 |
flag = !!flag; |
| 146 |
} |
| 147 |
if ( this.local !== flag ) { |
| 148 |
this.local = flag; |
| 149 |
this.emit( 'local', this.local ); |
| 150 |
} |
| 151 |
return this; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
mw.widgets.datetime.DateTimeFormatter.prototype.getDefaultDate = function () { |
| 160 |
return new Date( this.defaultDate.getTime() ); |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
mw.widgets.datetime.DateTimeFormatter.prototype.getFieldSpec = function () { |
| 171 |
return this.parseFieldSpec( this.format ); |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
mw.widgets.datetime.DateTimeFormatter.prototype.parseFieldSpec = function ( format ) { |
| 194 |
let m, last, tag, params, spec; |
| 195 |
|
| 196 |
const |
| 197 |
ret = [], |
| 198 |
re = /(.*?)(\$(!?)\{([^}]+)\})/g; |
| 199 |
|
| 200 |
last = 0; |
| 201 |
while ( ( m = re.exec( format ) ) !== null ) { |
| 202 |
last = re.lastIndex; |
| 203 |
|
| 204 |
if ( m[ 1 ] !== '' ) { |
| 205 |
ret.push( m[ 1 ] ); |
| 206 |
} |
| 207 |
|
| 208 |
params = m[ 4 ].split( '|' ); |
| 209 |
tag = params.shift(); |
| 210 |
spec = this.getFieldForTag( tag, params ); |
| 211 |
if ( spec ) { |
| 212 |
if ( m[ 3 ] === '!' ) { |
| 213 |
spec.editable = false; |
| 214 |
} |
| 215 |
ret.push( spec ); |
| 216 |
} else { |
| 217 |
ret.push( m[ 2 ] ); |
| 218 |
} |
| 219 |
} |
| 220 |
if ( last < format.length ) { |
| 221 |
ret.push( format.slice( last ) ); |
| 222 |
} |
| 223 |
|
| 224 |
return ret; |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
mw.widgets.datetime.DateTimeFormatter.prototype.getFieldForTag = function ( tag, params ) { |
| 271 |
let c, spec = null; |
| 272 |
|
| 273 |
switch ( tag ) { |
| 274 |
case 'intercalary': |
| 275 |
case 'not-intercalary': |
| 276 |
if ( params.length < 2 || !params[ 0 ] ) { |
| 277 |
return null; |
| 278 |
} |
| 279 |
spec = { |
| 280 |
component: null, |
| 281 |
calendarComponent: false, |
| 282 |
editable: false, |
| 283 |
type: 'static', |
| 284 |
value: params.slice( 1 ).join( '|' ), |
| 285 |
size: 0, |
| 286 |
intercalarySize: {} |
| 287 |
}; |
| 288 |
if ( tag === 'intercalary' ) { |
| 289 |
spec.intercalarySize[ params[ 0 ] ] = spec.value.length; |
| 290 |
} else { |
| 291 |
spec.size = spec.value.length; |
| 292 |
spec.intercalarySize[ params[ 0 ] ] = 0; |
| 293 |
} |
| 294 |
return spec; |
| 295 |
|
| 296 |
case 'zone': |
| 297 |
switch ( params[ 0 ] ) { |
| 298 |
case '#': |
| 299 |
case ':': |
| 300 |
c = params[ 0 ] === '#' ? '' : ':'; |
| 301 |
return { |
| 302 |
component: 'zone', |
| 303 |
calendarComponent: false, |
| 304 |
editable: true, |
| 305 |
type: 'toggleLocal', |
| 306 |
size: 5 + c.length, |
| 307 |
formatValue: function ( v ) { |
| 308 |
let o, r; |
| 309 |
if ( v ) { |
| 310 |
o = new Date().getTimezoneOffset(); |
| 311 |
r = String( Math.abs( o ) % 60 ); |
| 312 |
while ( r.length < 2 ) { |
| 313 |
r = '0' + r; |
| 314 |
} |
| 315 |
r = String( Math.floor( Math.abs( o ) / 60 ) ) + c + r; |
| 316 |
while ( r.length < 4 + c.length ) { |
| 317 |
r = '0' + r; |
| 318 |
} |
| 319 |
return ( o <= 0 ? '+' : '−' ) + r; |
| 320 |
} else { |
| 321 |
return '+00' + c + '00'; |
| 322 |
} |
| 323 |
}, |
| 324 |
parseValue: function ( v ) { |
| 325 |
let m; |
| 326 |
v = String( v ).trim(); |
| 327 |
if ( ( m = /^([+-−])([0-9]{1,2}):?([0-9]{2})$/.test( v ) ) ) { |
| 328 |
return ( m[ 2 ] * 60 + m[ 3 ] ) * ( m[ 1 ] === '+' ? -1 : 1 ); |
| 329 |
} else { |
| 330 |
return undefined; |
| 331 |
} |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
case 'short': |
| 336 |
case 'full': |
| 337 |
spec = { |
| 338 |
component: 'zone', |
| 339 |
calendarComponent: false, |
| 340 |
editable: true, |
| 341 |
type: 'toggleLocal', |
| 342 |
values: params[ 0 ] === 'short' ? this.shortZones : this.fullZones, |
| 343 |
formatValue: this.formatSpecValue, |
| 344 |
parseValue: this.parseSpecValue |
| 345 |
}; |
| 346 |
spec.size = Math.max.apply( |
| 347 |
|
| 348 |
null, $.map( spec.values, ( v ) => v.length ) |
| 349 |
); |
| 350 |
return spec; |
| 351 |
} |
| 352 |
return null; |
| 353 |
|
| 354 |
default: |
| 355 |
return null; |
| 356 |
} |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
mw.widgets.datetime.DateTimeFormatter.prototype.formatSpecValue = function ( v ) { |
| 374 |
if ( v === undefined || v === null ) { |
| 375 |
return ''; |
| 376 |
} |
| 377 |
|
| 378 |
if ( typeof v === 'boolean' || this.type === 'toggleLocal' ) { |
| 379 |
v = v ? 1 : 0; |
| 380 |
} |
| 381 |
|
| 382 |
if ( this.values ) { |
| 383 |
return this.values[ v ]; |
| 384 |
} |
| 385 |
|
| 386 |
v = String( v ); |
| 387 |
if ( this.zeropad ) { |
| 388 |
while ( v.length < this.size ) { |
| 389 |
v = '0' + v; |
| 390 |
} |
| 391 |
} |
| 392 |
return v; |
| 393 |
}; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
mw.widgets.datetime.DateTimeFormatter.prototype.parseSpecValue = function ( v ) { |
| 409 |
let k; |
| 410 |
|
| 411 |
if ( v === '' ) { |
| 412 |
return null; |
| 413 |
} |
| 414 |
|
| 415 |
if ( !this.values ) { |
| 416 |
v = +v; |
| 417 |
if ( this.type === 'boolean' || this.type === 'toggleLocal' ) { |
| 418 |
return isNaN( v ) ? undefined : !!v; |
| 419 |
} else { |
| 420 |
return isNaN( v ) ? undefined : v; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
if ( v.normalize ) { |
| 425 |
v = v.normalize(); |
| 426 |
} |
| 427 |
|
| 428 |
const re = new RegExp( '^\\s*' + mw.util.escapeRegExp( v ), 'i' ); |
| 429 |
for ( k in this.values ) { |
| 430 |
k = +k; |
| 431 |
if ( !isNaN( k ) && re.test( this.values[ k ] ) ) { |
| 432 |
if ( this.type === 'boolean' || this.type === 'toggleLocal' ) { |
| 433 |
return !!k; |
| 434 |
} else { |
| 435 |
return k; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
return undefined; |
| 440 |
}; |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
mw.widgets.datetime.DateTimeFormatter.prototype.getComponentsFromDate = function ( date ) { |
| 455 |
|
| 456 |
return { |
| 457 |
zone: this.local ? date.getTimezoneOffset() : 0 |
| 458 |
}; |
| 459 |
}; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
mw.widgets.datetime.DateTimeFormatter.prototype.getDateFromComponents = function ( ) { |
| 468 |
|
| 469 |
return new Date(); |
| 470 |
}; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
mw.widgets.datetime.DateTimeFormatter.prototype.adjustComponent = function ( date ) { |
| 485 |
|
| 486 |
return date; |
| 487 |
}; |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
mw.widgets.datetime.DateTimeFormatter.prototype.getCalendarHeadings = function () { |
| 499 |
|
| 500 |
return []; |
| 501 |
}; |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
mw.widgets.datetime.DateTimeFormatter.prototype.sameCalendarGrid = function ( date1, date2 ) { |
| 512 |
|
| 513 |
return date1.getTime() === date2.getTime(); |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
mw.widgets.datetime.DateTimeFormatter.prototype.datePartIsEqual = function ( date1, date2 ) { |
| 524 |
if ( this.local ) { |
| 525 |
return ( |
| 526 |
date1.getFullYear() === date2.getFullYear() && |
| 527 |
date1.getMonth() === date2.getMonth() && |
| 528 |
date1.getDate() === date2.getDate() |
| 529 |
); |
| 530 |
} else { |
| 531 |
return ( |
| 532 |
date1.getUTCFullYear() === date2.getUTCFullYear() && |
| 533 |
date1.getUTCMonth() === date2.getUTCMonth() && |
| 534 |
date1.getUTCDate() === date2.getUTCDate() |
| 535 |
); |
| 536 |
} |
| 537 |
}; |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
mw.widgets.datetime.DateTimeFormatter.prototype.timePartIsEqual = function ( date1, date2 ) { |
| 547 |
if ( this.local ) { |
| 548 |
return ( |
| 549 |
date1.getHours() === date2.getHours() && |
| 550 |
date1.getMinutes() === date2.getMinutes() && |
| 551 |
date1.getSeconds() === date2.getSeconds() && |
| 552 |
date1.getMilliseconds() === date2.getMilliseconds() |
| 553 |
); |
| 554 |
} else { |
| 555 |
return ( |
| 556 |
date1.getUTCHours() === date2.getUTCHours() && |
| 557 |
date1.getUTCMinutes() === date2.getUTCMinutes() && |
| 558 |
date1.getUTCSeconds() === date2.getUTCSeconds() && |
| 559 |
date1.getUTCMilliseconds() === date2.getUTCMilliseconds() |
| 560 |
); |
| 561 |
} |
| 562 |
}; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
mw.widgets.datetime.DateTimeFormatter.prototype.localChangesDatePart = function ( date ) { |
| 571 |
return ( |
| 572 |
date.getUTCFullYear() !== date.getFullYear() || |
| 573 |
date.getUTCMonth() !== date.getMonth() || |
| 574 |
date.getUTCDate() !== date.getDate() |
| 575 |
); |
| 576 |
}; |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
mw.widgets.datetime.DateTimeFormatter.prototype.mergeDateAndTime = function ( datepart, timepart ) { |
| 587 |
const ret = new Date( datepart.getTime() ); |
| 588 |
|
| 589 |
if ( this.local ) { |
| 590 |
ret.setHours( |
| 591 |
timepart.getHours(), |
| 592 |
timepart.getMinutes(), |
| 593 |
timepart.getSeconds(), |
| 594 |
timepart.getMilliseconds() |
| 595 |
); |
| 596 |
} else { |
| 597 |
ret.setUTCHours( |
| 598 |
timepart.getUTCHours(), |
| 599 |
timepart.getUTCMinutes(), |
| 600 |
timepart.getUTCSeconds(), |
| 601 |
timepart.getUTCMilliseconds() |
| 602 |
); |
| 603 |
} |
| 604 |
|
| 605 |
return ret; |
| 606 |
}; |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
. |
| 612 |
. |
| 613 |
e |
| 614 |
f |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
mw.widgets.datetime.DateTimeFormatter.prototype.getCalendarData = function ( ) { |
| 635 |
|
| 636 |
return { |
| 637 |
header: '', |
| 638 |
monthComponent: 'month', |
| 639 |
dayComponent: 'day', |
| 640 |
rows: [] |
| 641 |
}; |
| 642 |
}; |
| 643 |
|
| 644 |
}() ); |