Debian Codegraph @

resources/src/mediawiki.widgets.datetime/DateTimeInputWidget.js

raw symbols SCIP
1 ( function () {
2
3 /**
4 * @classdesc DateTimeInputWidgets can be used to input a date, a time, or
5 * a date and time, in either UTC or the user's local timezone.
6 * Please see the [OOUI documentation on MediaWiki](https://www.mediawiki.org/wiki/OOUI/Widgets/Inputs)
7 * for more information and examples.
8 *
9 * This widget can be used inside a HTML form, such as a OO.ui.FormLayout.
10 *
11 * @example
12 * // Example of a text input widget
13 * var dateTimeInput = new mw.widgets.datetime.DateTimeInputWidget( {} )
14 * $( document.body ).append( dateTimeInput.$element );
15 *
16 * @class
17 * @extends OO.ui.InputWidget
18 * @mixes OO.ui.mixin.IconElement
19 * @mixes OO.ui.mixin.IndicatorElement
20 * @mixes OO.ui.mixin.PendingElement
21 * @mixes OO.ui.mixin.FlaggedElement
22 *
23 * @constructor
24 * @description Create an instance of `mw.widgets.datetime.DateTimeInputWidget`.
25 * @param {Object} [config] Configuration options
26 * @param {string} [config.type='datetime'] Whether to act like a 'date', 'time', or 'datetime' input.
27 * Affects values stored in the relevant `<input>` and the formatting and
28 * interpretation of values passed to/from
29 * {@link https://doc.wikimedia.org/oojs-ui/master/js/OO.ui.InputWidget.html#getValue getValue()} and
30 * {@link https://doc.wikimedia.org/oojs-ui/master/js/OO.ui.InputWidget.html#setValue setValue()}.
31 * It's up to the user to configure the DateTimeFormatter correctly.
32 * @param {Object|mw.widgets.datetime.DateTimeFormatter} [config.formatter={}] Configuration options for
33 * {@link mw.widgets.datetime.ProlepticGregorianDateTimeFormatter} (with 'format' defaulting to
34 * '@date', '@time', or '@datetime' depending on 'type'), or an
35 * {@link mw.widgets.datetime.DateTimeFormatter} instance to use.
36 * @param {Object|null} [config.calendar={}] Configuration options for
37 * {@link mw.widgets.datetime.CalendarWidget}; note certain settings will be forced based on the
38 * settings passed to this widget. Set null to disable the calendar.
39 * @param {boolean} [config.required=false] Whether a value is required.
40 * @param {boolean} [config.clearable=true] Whether to provide for blanking the value.
41 * @param {Date|null} [config.value=null] Default value for the widget
42 * @param {Date|string|null} [config.min=null] Minimum allowed date
43 * @param {Date|string|null} [config.max=null] Maximum allowed date
44 */
45 mw.widgets.datetime.DateTimeInputWidget = function MwWidgetsDatetimeDateTimeInputWidget( config ) {
46 // Configuration initialization
47 config = Object.assign( {
48 type: 'datetime',
49 clearable: true,
50 required: false,
51 min: null,
52 max: null,
53 formatter: {},
54 calendar: {}
55 }, config );
56
57 // See InputWidget#reusePreInfuseDOM about config.$input
58 if ( config.$input ) {
59 config.$input.addClass( 'oo-ui-element-hidden' );
60 }
61
62 if ( $.isPlainObject( config.formatter ) && config.formatter.format === undefined ) {
63 config.formatter.format = '@' + config.type;
64 }
65
66 // Early properties
67 this.type = config.type;
68
69 // Parent constructor
70 mw.widgets.datetime.DateTimeInputWidget.super.call( this, config );
71
72 // Mixin constructors
73 OO.ui.mixin.IconElement.call( this, config );
74 OO.ui.mixin.IndicatorElement.call( this, config );
75 OO.ui.mixin.PendingElement.call( this, config );
76 OO.ui.mixin.FlaggedElement.call( this, config );
77
78 // Properties
79 this.$handle = $( '<span>' );
80 this.$fields = $( '<span>' );
81 this.fields = [];
82 this.clearable = !!config.clearable;
83 this.required = !!config.required;
84
85 if ( typeof config.min === 'string' ) {
86 config.min = this.parseDateValue( config.min );
87 }
88 if ( config.min instanceof Date && config.min.getTime() >= -62167219200000 ) {
89 this.min = config.min;
90 } else {
91 this.min = new Date( -62167219200000 ); // 0000-01-01T00:00:00.000Z
92 }
93
94 if ( typeof config.max === 'string' ) {
95 config.max = this.parseDateValue( config.max );
96 }
97 if ( config.max instanceof Date && config.max.getTime() <= 253402300799999 ) {
98 this.max = config.max;
99 } else {
100 this.max = new Date( 253402300799999 ); // 9999-12-31T12:59:59.999Z
101 }
102
103 switch ( this.type ) {
104 case 'date':
105 this.min.setUTCHours( 0, 0, 0, 0 );
106 this.max.setUTCHours( 23, 59, 59, 999 );
107 break;
108 case 'time':
109 this.min.setUTCFullYear( 1970, 0, 1 );
110 this.max.setUTCFullYear( 1970, 0, 1 );
111 break;
112 }
113 if ( this.min > this.max ) {
114 throw new Error(
115 '"min" (' + this.min.toISOString() + ') must not be greater than ' +
116 '"max" (' + this.max.toISOString() + ')'
117 );
118 }
119
120 if ( config.formatter instanceof mw.widgets.datetime.DateTimeFormatter ) {
121 this.formatter = config.formatter;
122 } else if ( $.isPlainObject( config.formatter ) ) {
123 this.formatter = new mw.widgets.datetime.ProlepticGregorianDateTimeFormatter( config.formatter );
124 } else {
125 throw new Error( '"formatter" must be an mw.widgets.datetime.DateTimeFormatter or a plain object' );
126 }
127
128 if ( this.type === 'time' || config.calendar === null ) {
129 this.calendar = null;
130 } else {
131 config.calendar = Object.assign( {}, config.calendar, {
132 formatter: this.formatter,
133 widget: this,
134 min: this.min,
135 max: this.max
136 } );
137 this.calendar = new mw.widgets.datetime.CalendarWidget( config.calendar );
138 }
139
140 // Events
141 this.$handle.on( {
142 click: this.onHandleClick.bind( this )
143 } );
144 this.connect( this, {
145 change: 'onChange'
146 } );
147 this.formatter.connect( this, {
148 local: 'onChange'
149 } );
150 if ( this.calendar ) {
151 this.calendar.connect( this, {
152 change: 'onCalendarChange'
153 } );
154 }
155
156 // Initialization
157 this.setTabIndex( -1 );
158
159 this.$fields.addClass( 'mw-widgets-datetime-dateTimeInputWidget-fields' );
160 this.setupFields();
161
162 this.$handle
163 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-handle' )
164 .append( this.$icon, this.$indicator, this.$fields );
165
166 this.$element
167 .addClass( 'mw-widgets-datetime-dateTimeInputWidget' )
168 .append( this.$handle );
169
170 if ( this.calendar ) {
171 const date = this.getValueAsDate();
172 this.calendar.setSelected( date );
173 if ( date ) {
174 this.calendar.setFocusedDate( date );
175 }
176 this.$element.append( this.calendar.$element );
177 }
178 };
179
180 /* Setup */
181
182 OO.inheritClass( mw.widgets.datetime.DateTimeInputWidget, OO.ui.InputWidget );
183 OO.mixinClass( mw.widgets.datetime.DateTimeInputWidget, OO.ui.mixin.IconElement );
184 OO.mixinClass( mw.widgets.datetime.DateTimeInputWidget, OO.ui.mixin.IndicatorElement );
185 OO.mixinClass( mw.widgets.datetime.DateTimeInputWidget, OO.ui.mixin.PendingElement );
186 OO.mixinClass( mw.widgets.datetime.DateTimeInputWidget, OO.ui.mixin.FlaggedElement );
187
188 /* Static properties */
189
190 mw.widgets.datetime.DateTimeInputWidget.static.supportsSimpleLabel = false;
191
192 /* Events */
193
194 /* Methods */
195
196 /**
197 * Get the currently focused field, if any.
198 *
199 * @private
200 * @return {jQuery}
201 */
202 mw.widgets.datetime.DateTimeInputWidget.prototype.getFocusedField = function () {
203 return this.$fields.find( this.getElementDocument().activeElement );
204 };
205
206 /**
207 * Convert a date string to a Date.
208 *
209 * @private
210 * @param {string} value
211 * @return {Date|null}
212 */
213 mw.widgets.datetime.DateTimeInputWidget.prototype.parseDateValue = function ( value ) {
214 value = String( value );
215 switch ( this.type ) {
216 case 'date':
217 value = value + 'T00:00:00Z';
218 break;
219 case 'time':
220 value = '1970-01-01T' + value + 'Z';
221 break;
222 }
223
224 let date;
225
226 const m = /^(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec( value );
227 if ( m ) {
228 if ( m[ 7 ] ) {
229 while ( m[ 7 ].length < 3 ) {
230 m[ 7 ] += '0';
231 }
232 } else {
233 m[ 7 ] = 0;
234 }
235 date = new Date();
236 date.setUTCFullYear( m[ 1 ], m[ 2 ] - 1, m[ 3 ] );
237 date.setUTCHours( m[ 4 ], m[ 5 ], m[ 6 ], m[ 7 ] );
238 if ( date.getTime() < -62167219200000 || date.getTime() > 253402300799999 ||
239 date.getUTCFullYear() !== +m[ 1 ] ||
240 date.getUTCMonth() + 1 !== +m[ 2 ] ||
241 date.getUTCDate() !== +m[ 3 ] ||
242 date.getUTCHours() !== +m[ 4 ] ||
243 date.getUTCMinutes() !== +m[ 5 ] ||
244 date.getUTCSeconds() !== +m[ 6 ] ||
245 date.getUTCMilliseconds() !== +m[ 7 ]
246 ) {
247 date = null;
248 }
249 } else {
250 date = null;
251 }
252
253 return date;
254 };
255
256 /**
257 * @inheritdoc
258 */
259 mw.widgets.datetime.DateTimeInputWidget.prototype.cleanUpValue = function ( value ) {
260 let date, pad;
261
262 if ( value === '' ) {
263 return '';
264 }
265
266 if ( value instanceof Date ) {
267 date = value;
268 } else {
269 date = this.parseDateValue( value );
270 }
271
272 if ( date instanceof Date ) {
273 pad = function ( v, l ) {
274 v = String( v );
275 while ( v.length < l ) {
276 v = '0' + v;
277 }
278 return v;
279 };
280
281 switch ( this.type ) {
282 case 'date':
283 value = pad( date.getUTCFullYear(), 4 ) +
284 '-' + pad( date.getUTCMonth() + 1, 2 ) +
285 '-' + pad( date.getUTCDate(), 2 );
286 break;
287
288 case 'time':
289 value = pad( date.getUTCHours(), 2 ) +
290 ':' + pad( date.getUTCMinutes(), 2 ) +
291 ':' + pad( date.getUTCSeconds(), 2 ) +
292 '.' + pad( date.getUTCMilliseconds(), 3 );
293 value = value.replace( /\.?0+$/, '' );
294 break;
295
296 default:
297 value = date.toISOString();
298 break;
299 }
300 } else {
301 value = '';
302 }
303
304 return value;
305 };
306
307 /**
308 * Get the value of the input as a Date object.
309 *
310 * @return {Date|null}
311 */
312 mw.widgets.datetime.DateTimeInputWidget.prototype.getValueAsDate = function () {
313 return this.parseDateValue( this.getValue() );
314 };
315
316 /**
317 * Set up the UI fields.
318 *
319 * @private
320 */
321 mw.widgets.datetime.DateTimeInputWidget.prototype.setupFields = function () {
322 let i, $field, spec, placeholder, sz, maxlength;
323
324 const
325 spanValFunc = function ( v ) {
326 if ( v === undefined ) {
327 return this.data( 'mw-widgets-datetime-dateTimeInputWidget-value' );
328 } else {
329 v = String( v );
330 this.data( 'mw-widgets-datetime-dateTimeInputWidget-value', v );
331 if ( v === '' ) {
332 v = this.data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder' );
333 }
334 this.text( v );
335 return this;
336 }
337 },
338 reduceFunc = function ( k, v ) {
339 maxlength = Math.max( maxlength, v );
340 },
341 disabled = this.isDisabled(),
342 specs = this.formatter.getFieldSpec();
343
344 this.$fields.empty();
345 this.clearButton = null;
346 this.fields = [];
347
348 for ( i = 0; i < specs.length; i++ ) {
349 spec = specs[ i ];
350 if ( typeof spec === 'string' ) {
351 $( '<span>' )
352 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
353 .text( spec )
354 .appendTo( this.$fields );
355 continue;
356 }
357
358 placeholder = '';
359 while ( placeholder.length < spec.size ) {
360 placeholder += '_';
361 }
362
363 if ( spec.type === 'number' ) {
364 sz = spec.size + 'ch';
365 } else {
366 // Add a little for padding
367 sz = ( spec.size * 1.25 ) + 'ch';
368 }
369 if ( spec.editable && spec.type !== 'static' ) {
370 if ( spec.type === 'boolean' || spec.type === 'toggleLocal' ) {
371 $field = $( '<span>' )
372 .attr( {
373 tabindex: disabled ? -1 : 0
374 } )
375 .width( sz )
376 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder );
377 $field.on( {
378 keydown: this.onFieldKeyDown.bind( this, $field ),
379 focus: this.onFieldFocus.bind( this, $field ),
380 click: this.onFieldClick.bind( this, $field ),
381 'wheel mousewheel DOMMouseScroll': this.onFieldWheel.bind( this, $field )
382 } );
383 $field.val = spanValFunc;
384 } else {
385 maxlength = spec.size;
386 if ( spec.intercalarySize ) {
387 // eslint-disable-next-line no-jquery/no-each-util
388 $.each( spec.intercalarySize, reduceFunc );
389 }
390 $field = $( '<input>' ).attr( 'type', 'text' )
391 .attr( {
392 tabindex: disabled ? -1 : 0,
393 size: spec.size,
394 maxlength: maxlength
395 } )
396 .prop( {
397 disabled: disabled,
398 placeholder: placeholder
399 } )
400 .width( sz );
401 $field.on( {
402 keydown: this.onFieldKeyDown.bind( this, $field ),
403 click: this.onFieldClick.bind( this, $field ),
404 focus: this.onFieldFocus.bind( this, $field ),
405 blur: this.onFieldBlur.bind( this, $field ),
406 change: this.onFieldChange.bind( this, $field ),
407 'wheel mousewheel DOMMouseScroll': this.onFieldWheel.bind( this, $field )
408 } );
409 }
410 $field.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
411 } else {
412 $field = $( '<span>' )
413 .width( sz )
414 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder );
415 if ( spec.type !== 'static' ) {
416 $field.prop( 'tabIndex', -1 );
417 $field.on( 'focus', this.onFieldFocus.bind( this, $field ) );
418 }
419 if ( spec.type === 'static' ) {
420 $field.text( spec.value );
421 } else {
422 $field.val = spanValFunc;
423 }
424 }
425
426 this.fields.push( $field );
427 $field
428 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
429 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec )
430 .appendTo( this.$fields );
431 }
432
433 if ( this.clearable ) {
434 this.clearButton = new OO.ui.ButtonWidget( {
435 classes: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
436 framed: false,
437 icon: 'clear',
438 disabled: disabled
439 } ).connect( this, {
440 click: 'onClearClick'
441 } );
442 this.$fields.append( this.clearButton.$element );
443 }
444
445 this.updateFieldsFromValue();
446 };
447
448 /**
449 * Update the UI fields from the current value.
450 *
451 * @private
452 */
453 mw.widgets.datetime.DateTimeInputWidget.prototype.updateFieldsFromValue = function () {
454 let i, $field, spec, intercalary, sz;
455
456 const date = this.getValueAsDate();
457
458 if ( date === null ) {
459 this.components = null;
460
461 for ( i = 0; i < this.fields.length; i++ ) {
462 $field = this.fields[ i ];
463 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
464
465 $field
466 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
467 .val( '' );
468
469 if ( spec.intercalarySize ) {
470 if ( spec.type === 'number' ) {
471 $field.width( spec.size + 'ch' );
472 } else {
473 // Add a little for padding
474 $field.width( ( spec.size * 1.25 ) + 'ch' );
475 }
476 }
477 }
478
479 this.setFlags( { invalid: this.required } );
480 } else {
481 this.components = this.formatter.getComponentsFromDate( date );
482 intercalary = this.components.intercalary;
483
484 for ( i = 0; i < this.fields.length; i++ ) {
485 $field = this.fields[ i ];
486 $field.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
487 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
488 if ( spec.type !== 'static' ) {
489 $field.val( spec.formatValue( this.components[ spec.component ] ) );
490 }
491 if ( spec.intercalarySize ) {
492 if ( intercalary && spec.intercalarySize[ intercalary ] !== undefined ) {
493 sz = spec.intercalarySize[ intercalary ];
494 } else {
495 sz = spec.size;
496 }
497 $field.toggleClass( 'oo-ui-element-hidden', sz <= 0 );
498 if ( spec.type === 'number' ) {
499 this.fields[ i ].width( sz + 'ch' );
500 } else {
501 // Add a little for padding
502 this.fields[ i ].width( ( sz * 1.25 ) + 'ch' );
503 }
504 }
505 }
506
507 this.setFlags( { invalid: date < this.min || date > this.max } );
508 }
509
510 this.$element.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date === null );
511 };
512
513 /**
514 * Update the value with data from the UI fields.
515 *
516 * @private
517 */
518 mw.widgets.datetime.DateTimeInputWidget.prototype.updateValueFromFields = function () {
519 let i, v, $field, spec, curDate, newDate,
520 anyInvalid = false,
521 anyEmpty = false,
522 allEmpty = true;
523
524 const components = {};
525
526 for ( i = 0; i < this.fields.length; i++ ) {
527 $field = this.fields[ i ];
528 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
529 if ( spec.editable ) {
530 $field.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
531 v = $field.val();
532 if ( v === '' ) {
533 $field.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
534 anyEmpty = true;
535 } else {
536 allEmpty = false;
537 v = spec.parseValue( v );
538 if ( v === undefined ) {
539 $field.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
540 anyInvalid = true;
541 } else {
542 components[ spec.component ] = v;
543 }
544 }
545 }
546 }
547
548 if ( allEmpty ) {
549 for ( i = 0; i < this.fields.length; i++ ) {
550 this.fields[ i ].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
551 }
552 } else if ( anyEmpty ) {
553 anyInvalid = true;
554 }
555
556 if ( !anyInvalid ) {
557 curDate = this.getValueAsDate();
558 newDate = this.formatter.getDateFromComponents( components );
559 if ( !curDate || !newDate || curDate.getTime() !== newDate.getTime() ) {
560 this.setValue( newDate );
561 }
562 }
563 };
564
565 /**
566 * Handle change event.
567 *
568 * @private
569 */
570 mw.widgets.datetime.DateTimeInputWidget.prototype.onChange = function () {
571 let date;
572
573 this.updateFieldsFromValue();
574
575 if ( this.calendar ) {
576 date = this.getValueAsDate();
577 this.calendar.setSelected( date );
578 if ( date ) {
579 this.calendar.setFocusedDate( date );
580 }
581 }
582 };
583
584 /**
585 * Handle clear button click event.
586 *
587 * @private
588 */
589 mw.widgets.datetime.DateTimeInputWidget.prototype.onClearClick = function () {
590 this.blur().setValue( '' );
591 };
592
593 /**
594 * Handle click on the widget background.
595 *
596 * @private
597 * @param {jQuery.Event} e Click event
598 */
599 mw.widgets.datetime.DateTimeInputWidget.prototype.onHandleClick = function () {
600 this.focus();
601 };
602
603 /**
604 * Handle key down events on our field inputs.
605 *
606 * @private
607 * @param {jQuery} $field
608 * @param {jQuery.Event} e Key down event
609 * @return {boolean|undefined} False to cancel the default event
610 */
611 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldKeyDown = function ( $field, e ) {
612 const spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
613
614 if ( !this.isDisabled() ) {
615 switch ( e.which ) {
616 case OO.ui.Keys.ENTER:
617 case OO.ui.Keys.SPACE:
618 if ( spec.type === 'boolean' ) {
619 this.setValue(
620 this.formatter.adjustComponent( this.getValueAsDate(), spec.component, 1, 'wrap' )
621 );
622 return false;
623 } else if ( spec.type === 'toggleLocal' ) {
624 this.formatter.toggleLocal();
625 }
626 break;
627
628 case OO.ui.Keys.UP:
629 case OO.ui.Keys.DOWN:
630 if ( spec.type === 'toggleLocal' ) {
631 this.formatter.toggleLocal();
632 } else {
633 this.setValue(
634 this.formatter.adjustComponent( this.getValueAsDate(), spec.component,
635 e.keyCode === OO.ui.Keys.UP ? -1 : 1, 'wrap' )
636 );
637 }
638 if ( $field.is( 'input' ) ) {
639 $field.trigger( 'select' );
640 }
641 return false;
642 }
643 }
644 };
645
646 /**
647 * Handle focus events on our field inputs.
648 *
649 * @private
650 * @param {jQuery} $field
651 * @param {jQuery.Event} e Focus event
652 */
653 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldFocus = function ( $field ) {
654 const spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
655
656 if ( !this.isDisabled() ) {
657 if ( this.getValueAsDate() === null ) {
658 this.setValue( this.formatter.getDefaultDate() );
659 }
660 if ( $field.is( 'input' ) ) {
661 $field.trigger( 'select' );
662 }
663
664 if ( this.calendar ) {
665 this.calendar.toggle( !!spec.calendarComponent );
666 }
667 }
668 };
669
670 /**
671 * Handle click events on our field inputs.
672 *
673 * @private
674 * @param {jQuery} $field
675 * @param {jQuery.Event} e Click event
676 */
677 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldClick = function ( $field ) {
678 const spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
679
680 if ( !this.isDisabled() ) {
681 if ( spec.type === 'boolean' ) {
682 this.setValue(
683 this.formatter.adjustComponent( this.getValueAsDate(), spec.component, 1, 'wrap' )
684 );
685 } else if ( spec.type === 'toggleLocal' ) {
686 this.formatter.toggleLocal();
687 }
688 }
689 };
690
691 /**
692 * Handle blur events on our field inputs.
693 *
694 * @private
695 * @param {jQuery} $field
696 * @param {jQuery.Event} e Blur event
697 */
698 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldBlur = function ( $field ) {
699 let v;
700 const spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
701
702 this.updateValueFromFields();
703
704 // Normalize
705 const date = this.getValueAsDate();
706 if ( !date ) {
707 $field.val( '' );
708 } else {
709 v = spec.formatValue( this.formatter.getComponentsFromDate( date )[ spec.component ] );
710 if ( v !== $field.val() ) {
711 $field.val( v );
712 }
713 }
714 };
715
716 /**
717 * Handle change events on our field inputs.
718 *
719 * @private
720 * @param {jQuery} $field
721 * @param {jQuery.Event} e Change event
722 */
723 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldChange = function () {
724 this.updateValueFromFields();
725 };
726
727 /**
728 * Handle wheel events on our field inputs.
729 *
730 * @private
731 * @param {jQuery} $field
732 * @param {jQuery.Event} e Change event
733 * @return {boolean|undefined} False to cancel the default event
734 */
735 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldWheel = function ( $field, e ) {
736 let delta = 0;
737 const spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
738
739 if ( this.isDisabled() || !this.getFocusedField().length ) {
740 return;
741 }
742
743 // Standard 'wheel' event
744 if ( e.originalEvent.deltaMode !== undefined ) {
745 this.sawWheelEvent = true;
746 }
747 if ( e.originalEvent.deltaY ) {
748 delta = -e.originalEvent.deltaY;
749 } else if ( e.originalEvent.deltaX ) {
750 delta = e.originalEvent.deltaX;
751 }
752
753 // Non-standard events
754 if ( !this.sawWheelEvent ) {
755 if ( e.originalEvent.wheelDeltaX ) {
756 delta = -e.originalEvent.wheelDeltaX;
757 } else if ( e.originalEvent.wheelDeltaY ) {
758 delta = e.originalEvent.wheelDeltaY;
759 } else if ( e.originalEvent.wheelDelta ) {
760 delta = e.originalEvent.wheelDelta;
761 } else if ( e.originalEvent.detail ) {
762 delta = -e.originalEvent.detail;
763 }
764 }
765
766 if ( delta && spec ) {
767 if ( spec.type === 'toggleLocal' ) {
768 this.formatter.toggleLocal();
769 } else {
770 this.setValue(
771 this.formatter.adjustComponent( this.getValueAsDate(), spec.component, delta < 0 ? -1 : 1, 'wrap' )
772 );
773 }
774 return false;
775 }
776 };
777
778 /**
779 * Handle calendar change event.
780 *
781 * @private
782 */
783 mw.widgets.datetime.DateTimeInputWidget.prototype.onCalendarChange = function () {
784 const curDate = this.getValueAsDate(),
785 newDate = this.calendar.getSelected()[ 0 ];
786
787 if ( newDate ) {
788 if ( !curDate || newDate.getTime() !== curDate.getTime() ) {
789 this.setValue( newDate );
790 }
791 }
792 };
793
794 /**
795 * @inheritdoc
796 * @private
797 */
798 mw.widgets.datetime.DateTimeInputWidget.prototype.getInputElement = function () {
799 return $( '<input>' ).attr( 'type', 'hidden' );
800 };
801
802 /**
803 * @inheritdoc
804 */
805 mw.widgets.datetime.DateTimeInputWidget.prototype.setDisabled = function ( disabled ) {
806 mw.widgets.datetime.DateTimeInputWidget.super.prototype.setDisabled.call( this, disabled );
807
808 // Flag all our fields as disabled
809 if ( this.$fields ) {
810 this.$fields.find( 'input' ).prop( 'disabled', this.isDisabled() );
811 this.$fields.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
812 }
813
814 if ( this.clearButton ) {
815 this.clearButton.setDisabled( disabled );
816 }
817
818 return this;
819 };
820
821 /**
822 * @inheritdoc
823 */
824 mw.widgets.datetime.DateTimeInputWidget.prototype.focus = function () {
825 if ( !this.getFocusedField().length ) {
826 this.$fields.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().trigger( 'focus' );
827 }
828 return this;
829 };
830
831 /**
832 * @inheritdoc
833 */
834 mw.widgets.datetime.DateTimeInputWidget.prototype.blur = function () {
835 this.getFocusedField().blur();
836 return this;
837 };
838
839 /**
840 * @inheritdoc
841 */
842 mw.widgets.datetime.DateTimeInputWidget.prototype.simulateLabelClick = function () {
843 this.focus();
844 };
845
846 }() );