| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
mw.widgets.datetime.CalendarWidget = function MwWidgetsDatetimeCalendarWidget( config ) { |
| 32 |
|
| 33 |
config = Object.assign( { |
| 34 |
min: null, |
| 35 |
max: null, |
| 36 |
focusedDate: new Date(), |
| 37 |
selected: null, |
| 38 |
formatter: {} |
| 39 |
}, config ); |
| 40 |
|
| 41 |
|
| 42 |
mw.widgets.datetime.CalendarWidget.super.call( this, config ); |
| 43 |
|
| 44 |
|
| 45 |
OO.ui.mixin.TabIndexedElement.call( this, Object.assign( {}, config, { $tabIndexed: this.$element } ) ); |
| 46 |
|
| 47 |
|
| 48 |
if ( config.min instanceof Date && config.min.getTime() >= -62167219200000 ) { |
| 49 |
this.min = config.min; |
| 50 |
} else { |
| 51 |
this.min = new Date( -62167219200000 ); |
| 52 |
} |
| 53 |
if ( config.max instanceof Date && config.max.getTime() <= 253402300799999 ) { |
| 54 |
this.max = config.max; |
| 55 |
} else { |
| 56 |
this.max = new Date( 253402300799999 ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( config.focusedDate instanceof Date ) { |
| 60 |
this.focusedDate = config.focusedDate; |
| 61 |
} else { |
| 62 |
this.focusedDate = new Date(); |
| 63 |
} |
| 64 |
|
| 65 |
this.selected = []; |
| 66 |
|
| 67 |
if ( config.formatter instanceof mw.widgets.datetime.DateTimeFormatter ) { |
| 68 |
this.formatter = config.formatter; |
| 69 |
} else if ( $.isPlainObject( config.formatter ) ) { |
| 70 |
this.formatter = new mw.widgets.datetime.ProlepticGregorianDateTimeFormatter( config.formatter ); |
| 71 |
} else { |
| 72 |
throw new Error( '"formatter" must be an mw.widgets.datetime.DateTimeFormatter or a plain object' ); |
| 73 |
} |
| 74 |
|
| 75 |
this.calendarData = null; |
| 76 |
|
| 77 |
this.widget = config.widget; |
| 78 |
this.$widget = config.widget ? config.widget.$element : null; |
| 79 |
this.onDocumentMouseDownHandler = this.onDocumentMouseDown.bind( this ); |
| 80 |
|
| 81 |
this.$head = $( '<div>' ); |
| 82 |
this.$header = $( '<span>' ); |
| 83 |
this.$table = $( '<table>' ); |
| 84 |
this.cols = []; |
| 85 |
this.colNullable = []; |
| 86 |
this.headings = []; |
| 87 |
this.$tableBody = $( '<tbody>' ); |
| 88 |
this.rows = []; |
| 89 |
this.buttons = {}; |
| 90 |
this.minWidth = 1; |
| 91 |
this.daysPerWeek = 0; |
| 92 |
|
| 93 |
|
| 94 |
this.$element.on( { |
| 95 |
keydown: this.onKeyDown.bind( this ) |
| 96 |
} ); |
| 97 |
this.formatter.connect( this, { |
| 98 |
local: 'onLocalChange' |
| 99 |
} ); |
| 100 |
if ( this.$widget ) { |
| 101 |
this.checkFocusHandler = this.checkFocus.bind( this ); |
| 102 |
this.$element.on( { |
| 103 |
focusout: this.onFocusOut.bind( this ) |
| 104 |
} ); |
| 105 |
this.$widget.on( { |
| 106 |
focusout: this.onFocusOut.bind( this ) |
| 107 |
} ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
this.$head |
| 112 |
.addClass( 'mw-widgets-datetime-calendarWidget-heading' ) |
| 113 |
.append( |
| 114 |
new OO.ui.ButtonWidget( { |
| 115 |
icon: 'previous', |
| 116 |
framed: false, |
| 117 |
classes: [ 'mw-widgets-datetime-calendarWidget-previous' ], |
| 118 |
tabIndex: -1 |
| 119 |
} ).connect( this, { click: 'onPrevClick' } ).$element, |
| 120 |
new OO.ui.ButtonWidget( { |
| 121 |
icon: 'next', |
| 122 |
framed: false, |
| 123 |
classes: [ 'mw-widgets-datetime-calendarWidget-next' ], |
| 124 |
tabIndex: -1 |
| 125 |
} ).connect( this, { click: 'onNextClick' } ).$element, |
| 126 |
this.$header |
| 127 |
); |
| 128 |
const $colgroup = $( '<colgroup>' ); |
| 129 |
const $headTR = $( '<tr>' ); |
| 130 |
this.$table |
| 131 |
.addClass( 'mw-widgets-datetime-calendarWidget-grid' ) |
| 132 |
.append( $colgroup ) |
| 133 |
.append( $( '<thead>' ).append( $headTR ) ) |
| 134 |
.append( this.$tableBody ); |
| 135 |
|
| 136 |
const headings = this.formatter.getCalendarHeadings(); |
| 137 |
for ( let i = 0; i < headings.length; i++ ) { |
| 138 |
this.cols[ i ] = $( '<col>' ); |
| 139 |
this.headings[ i ] = $( '<th>' ); |
| 140 |
this.colNullable[ i ] = headings[ i ] === null; |
| 141 |
if ( headings[ i ] !== null ) { |
| 142 |
this.headings[ i ].text( headings[ i ] ); |
| 143 |
this.minWidth = Math.max( this.minWidth, headings[ i ].length ); |
| 144 |
this.daysPerWeek++; |
| 145 |
} |
| 146 |
$colgroup.append( this.cols[ i ] ); |
| 147 |
$headTR.append( this.headings[ i ] ); |
| 148 |
} |
| 149 |
|
| 150 |
this.setSelected( config.selected ); |
| 151 |
this.$element |
| 152 |
.addClass( 'mw-widgets-datetime-calendarWidget' ) |
| 153 |
.append( this.$head, this.$table ); |
| 154 |
|
| 155 |
if ( this.widget ) { |
| 156 |
this.$element.addClass( 'mw-widgets-datetime-calendarWidget-dependent' ); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
this.visible = false; |
| 162 |
this.$element.addClass( 'oo-ui-element-hidden' ); |
| 163 |
} else { |
| 164 |
this.updateUI(); |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
OO.inheritClass( mw.widgets.datetime.CalendarWidget, OO.ui.Widget ); |
| 171 |
OO.mixinClass( mw.widgets.datetime.CalendarWidget, OO.ui.mixin.TabIndexedElement ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
mw.widgets.datetime.CalendarWidget.prototype.getSelected = function () { |
| 204 |
return this.selected; |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
mw.widgets.datetime.CalendarWidget.prototype.setSelected = function ( dates ) { |
| 216 |
let i, changed = false; |
| 217 |
|
| 218 |
if ( dates instanceof Date ) { |
| 219 |
dates = [ dates ]; |
| 220 |
} else if ( Array.isArray( dates ) ) { |
| 221 |
dates = dates.filter( ( dt ) => dt instanceof Date ); |
| 222 |
dates.sort(); |
| 223 |
} else { |
| 224 |
dates = []; |
| 225 |
} |
| 226 |
|
| 227 |
if ( this.selected.length !== dates.length ) { |
| 228 |
changed = true; |
| 229 |
} else { |
| 230 |
for ( i = 0; i < dates.length; i++ ) { |
| 231 |
if ( dates[ i ].getTime() !== this.selected[ i ].getTime() ) { |
| 232 |
changed = true; |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if ( changed ) { |
| 239 |
this.selected = dates; |
| 240 |
this.emit( 'change', dates ); |
| 241 |
this.updateUI(); |
| 242 |
} |
| 243 |
|
| 244 |
return this; |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
mw.widgets.datetime.CalendarWidget.prototype.getFocusedDate = function () { |
| 253 |
return this.focusedDate; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
mw.widgets.datetime.CalendarWidget.prototype.setFocusedDate = function ( date ) { |
| 266 |
let changePage = false, |
| 267 |
updateUI = false; |
| 268 |
|
| 269 |
if ( this.focusedDate.getTime() === date.getTime() ) { |
| 270 |
return this; |
| 271 |
} |
| 272 |
|
| 273 |
if ( !this.formatter.sameCalendarGrid( this.focusedDate, date ) ) { |
| 274 |
changePage = true; |
| 275 |
updateUI = true; |
| 276 |
} else if ( |
| 277 |
!this.formatter.timePartIsEqual( this.focusedDate, date ) || |
| 278 |
!this.formatter.datePartIsEqual( this.focusedDate, date ) |
| 279 |
) { |
| 280 |
updateUI = true; |
| 281 |
} |
| 282 |
|
| 283 |
this.focusedDate = date; |
| 284 |
this.emit( 'focusChanged', this.focusedDate ); |
| 285 |
if ( changePage ) { |
| 286 |
this.emit( 'page', date ); |
| 287 |
} |
| 288 |
if ( updateUI ) { |
| 289 |
this.updateUI(); |
| 290 |
} |
| 291 |
|
| 292 |
return this; |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
mw.widgets.datetime.CalendarWidget.prototype.adjustDate = function ( date, component, delta ) { |
| 306 |
let newDate; |
| 307 |
|
| 308 |
const data = this.calendarData; |
| 309 |
|
| 310 |
if ( !data ) { |
| 311 |
return date; |
| 312 |
} |
| 313 |
|
| 314 |
switch ( component ) { |
| 315 |
case 'month': |
| 316 |
newDate = this.formatter.adjustComponent( date, data.monthComponent, delta, 'overflow' ); |
| 317 |
break; |
| 318 |
|
| 319 |
case 'week': |
| 320 |
if ( data.weekComponent === undefined ) { |
| 321 |
newDate = this.formatter.adjustComponent( |
| 322 |
date, data.dayComponent, delta * this.daysPerWeek, 'overflow' ); |
| 323 |
} else { |
| 324 |
newDate = this.formatter.adjustComponent( date, data.weekComponent, delta, 'overflow' ); |
| 325 |
} |
| 326 |
break; |
| 327 |
|
| 328 |
case 'day': |
| 329 |
newDate = this.formatter.adjustComponent( date, data.dayComponent, delta, 'overflow' ); |
| 330 |
break; |
| 331 |
|
| 332 |
default: |
| 333 |
throw new Error( 'Unknown component' ); |
| 334 |
} |
| 335 |
|
| 336 |
while ( newDate < this.min ) { |
| 337 |
newDate = this.formatter.adjustComponent( newDate, data.dayComponent, 1, 'overflow' ); |
| 338 |
} |
| 339 |
while ( newDate > this.max ) { |
| 340 |
newDate = this.formatter.adjustComponent( newDate, data.dayComponent, -1, 'overflow' ); |
| 341 |
} |
| 342 |
|
| 343 |
return newDate; |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
mw.widgets.datetime.CalendarWidget.prototype.updateUI = function () { |
| 352 |
let row, day, k, $cell, |
| 353 |
width = this.minWidth; |
| 354 |
|
| 355 |
const |
| 356 |
nullCols = [], |
| 357 |
focusedDate = this.getFocusedDate(), |
| 358 |
selected = this.getSelected(), |
| 359 |
datePartIsEqual = this.formatter.datePartIsEqual.bind( this.formatter ), |
| 360 |
isSelected = function ( dt ) { |
| 361 |
return datePartIsEqual( this, dt ); |
| 362 |
}; |
| 363 |
|
| 364 |
this.calendarData = this.formatter.getCalendarData( focusedDate ); |
| 365 |
|
| 366 |
this.$header.text( this.calendarData.header ); |
| 367 |
|
| 368 |
for ( let c = 0; c < this.colNullable.length; c++ ) { |
| 369 |
nullCols[ c ] = this.colNullable[ c ]; |
| 370 |
if ( nullCols[ c ] ) { |
| 371 |
for ( let r = 0; r < this.calendarData.rows.length; r++ ) { |
| 372 |
if ( this.calendarData.rows[ r ][ c ] ) { |
| 373 |
nullCols[ c ] = false; |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
this.$tableBody.children().detach(); |
| 381 |
for ( let r = 0; r < this.calendarData.rows.length; r++ ) { |
| 382 |
if ( !this.rows[ r ] ) { |
| 383 |
this.rows[ r ] = $( '<tr>' ); |
| 384 |
} else { |
| 385 |
this.rows[ r ].children().detach(); |
| 386 |
} |
| 387 |
this.$tableBody.append( this.rows[ r ] ); |
| 388 |
row = this.calendarData.rows[ r ]; |
| 389 |
for ( let c = 0; c < row.length; c++ ) { |
| 390 |
day = row[ c ]; |
| 391 |
if ( day === null ) { |
| 392 |
k = 'empty-' + r + '-' + c; |
| 393 |
if ( !this.buttons[ k ] ) { |
| 394 |
this.buttons[ k ] = $( '<td>' ); |
| 395 |
} |
| 396 |
$cell = this.buttons[ k ]; |
| 397 |
$cell.toggleClass( 'oo-ui-element-hidden', nullCols[ c ] ); |
| 398 |
} else { |
| 399 |
k = ( day.extra ? day.extra : '' ) + day.display; |
| 400 |
width = Math.max( width, day.display.length ); |
| 401 |
if ( !this.buttons[ k ] ) { |
| 402 |
this.buttons[ k ] = new OO.ui.ButtonWidget( { |
| 403 |
$element: $( '<td>' ), |
| 404 |
classes: [ |
| 405 |
'mw-widgets-datetime-calendarWidget-cell', |
| 406 |
day.extra ? 'mw-widgets-datetime-calendarWidget-extra' : '' |
| 407 |
], |
| 408 |
framed: true, |
| 409 |
label: day.display, |
| 410 |
tabIndex: -1 |
| 411 |
} ); |
| 412 |
this.buttons[ k ].connect( this, { click: [ 'onDayClick', this.buttons[ k ] ] } ); |
| 413 |
} |
| 414 |
this.buttons[ k ] |
| 415 |
.setData( day.date ) |
| 416 |
.setDisabled( day.date < this.min || day.date > this.max ); |
| 417 |
$cell = this.buttons[ k ].$element; |
| 418 |
$cell |
| 419 |
.toggleClass( 'mw-widgets-datetime-calendarWidget-focused', |
| 420 |
this.formatter.datePartIsEqual( focusedDate, day.date ) ) |
| 421 |
.toggleClass( 'mw-widgets-datetime-calendarWidget-selected', |
| 422 |
selected.some( isSelected, day.date ) ); |
| 423 |
} |
| 424 |
this.rows[ r ].append( $cell ); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
for ( let c = 0; c < this.cols.length; c++ ) { |
| 429 |
if ( nullCols[ c ] ) { |
| 430 |
this.cols[ c ].width( 0 ); |
| 431 |
} else { |
| 432 |
this.cols[ c ].width( width + 'em' ); |
| 433 |
} |
| 434 |
this.cols[ c ].toggleClass( 'oo-ui-element-hidden', nullCols[ c ] ); |
| 435 |
this.headings[ c ].toggleClass( 'oo-ui-element-hidden', nullCols[ c ] ); |
| 436 |
} |
| 437 |
}; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
mw.widgets.datetime.CalendarWidget.prototype.onLocalChange = function () { |
| 445 |
if ( this.formatter.localChangesDatePart( this.getFocusedDate() ) ) { |
| 446 |
this.emit( 'page', this.getFocusedDate() ); |
| 447 |
} |
| 448 |
|
| 449 |
this.updateUI(); |
| 450 |
}; |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
mw.widgets.datetime.CalendarWidget.prototype.onPrevClick = function () { |
| 458 |
this.setFocusedDate( this.adjustDate( this.getFocusedDate(), 'month', -1 ) ); |
| 459 |
if ( !this.$widget || OO.ui.contains( this.$element[ 0 ], document.activeElement, true ) ) { |
| 460 |
this.$element.trigger( 'focus' ); |
| 461 |
} |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
mw.widgets.datetime.CalendarWidget.prototype.onNextClick = function () { |
| 470 |
this.setFocusedDate( this.adjustDate( this.getFocusedDate(), 'month', 1 ) ); |
| 471 |
if ( !this.$widget || OO.ui.contains( this.$element[ 0 ], document.activeElement, true ) ) { |
| 472 |
this.$element.trigger( 'focus' ); |
| 473 |
} |
| 474 |
}; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
mw.widgets.datetime.CalendarWidget.prototype.onDayClick = function ( button ) { |
| 483 |
const data = button.getData(); |
| 484 |
this.setFocusedDate( data ); |
| 485 |
this.setSelected( [ data ] ); |
| 486 |
if ( !this.$widget || OO.ui.contains( this.$element[ 0 ], document.activeElement, true ) ) { |
| 487 |
this.$element.trigger( 'focus' ); |
| 488 |
} |
| 489 |
}; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
mw.widgets.datetime.CalendarWidget.prototype.onDocumentMouseDown = function ( e ) { |
| 498 |
if ( this.$widget && |
| 499 |
!OO.ui.contains( this.$element[ 0 ], e.target, true ) && |
| 500 |
!OO.ui.contains( this.$widget[ 0 ], e.target, true ) |
| 501 |
) { |
| 502 |
this.toggle( false ); |
| 503 |
} |
| 504 |
}; |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
mw.widgets.datetime.CalendarWidget.prototype.onKeyDown = function ( e ) { |
| 514 |
const focusedDate = this.getFocusedDate(); |
| 515 |
|
| 516 |
if ( !this.isDisabled() ) { |
| 517 |
switch ( e.which ) { |
| 518 |
case OO.ui.Keys.ENTER: |
| 519 |
case OO.ui.Keys.SPACE: |
| 520 |
this.setSelected( [ focusedDate ] ); |
| 521 |
return false; |
| 522 |
|
| 523 |
case OO.ui.Keys.LEFT: |
| 524 |
this.setFocusedDate( this.adjustDate( focusedDate, 'day', -1 ) ); |
| 525 |
return false; |
| 526 |
|
| 527 |
case OO.ui.Keys.RIGHT: |
| 528 |
this.setFocusedDate( this.adjustDate( focusedDate, 'day', 1 ) ); |
| 529 |
return false; |
| 530 |
|
| 531 |
case OO.ui.Keys.UP: |
| 532 |
this.setFocusedDate( this.adjustDate( focusedDate, 'week', -1 ) ); |
| 533 |
return false; |
| 534 |
|
| 535 |
case OO.ui.Keys.DOWN: |
| 536 |
this.setFocusedDate( this.adjustDate( focusedDate, 'week', 1 ) ); |
| 537 |
return false; |
| 538 |
|
| 539 |
case OO.ui.Keys.PAGEUP: |
| 540 |
this.setFocusedDate( this.adjustDate( focusedDate, 'month', -1 ) ); |
| 541 |
return false; |
| 542 |
|
| 543 |
case OO.ui.Keys.PAGEDOWN: |
| 544 |
this.setFocusedDate( this.adjustDate( focusedDate, 'month', 1 ) ); |
| 545 |
return false; |
| 546 |
} |
| 547 |
} |
| 548 |
}; |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
mw.widgets.datetime.CalendarWidget.prototype.onFocusOut = function () { |
| 556 |
setTimeout( this.checkFocusHandler ); |
| 557 |
}; |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
mw.widgets.datetime.CalendarWidget.prototype.checkFocus = function () { |
| 565 |
const containers = [ this.$element[ 0 ], this.$widget[ 0 ] ], |
| 566 |
activeElement = document.activeElement; |
| 567 |
|
| 568 |
if ( !activeElement || !OO.ui.contains( containers, activeElement, true ) ) { |
| 569 |
this.toggle( false ); |
| 570 |
} |
| 571 |
}; |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
mw.widgets.datetime.CalendarWidget.prototype.toggle = function ( visible ) { |
| 577 |
visible = ( visible === undefined ? !this.visible : !!visible ); |
| 578 |
const change = visible !== this.isVisible(); |
| 579 |
|
| 580 |
|
| 581 |
mw.widgets.datetime.CalendarWidget.super.prototype.toggle.call( this, visible ); |
| 582 |
|
| 583 |
if ( change ) { |
| 584 |
if ( visible ) { |
| 585 |
|
| 586 |
if ( this.$widget ) { |
| 587 |
this.getElementDocument().addEventListener( |
| 588 |
'mousedown', this.onDocumentMouseDownHandler, true |
| 589 |
); |
| 590 |
} |
| 591 |
this.updateUI(); |
| 592 |
} else { |
| 593 |
this.getElementDocument().removeEventListener( |
| 594 |
'mousedown', this.onDocumentMouseDownHandler, true |
| 595 |
); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
return this; |
| 600 |
}; |
| 601 |
|
| 602 |
}() ); |