4 * DateTimeInputWidgets can be used to input a date, a time, or a date and
5 * time, in either UTC or the user's local timezone.
6 * Please see the [OOUI documentation on MediaWiki] [1] for more information and examples.
8 * This widget can be used inside a HTML form, such as a OO.ui.FormLayout.
11 * // Example of a text input widget
12 * var dateTimeInput = new mw.widgets.datetime.DateTimeInputWidget( {} )
13 * $( 'body' ).append( dateTimeInput.$element );
15 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Inputs
18 * @extends OO.ui.InputWidget
19 * @mixins OO.ui.mixin.IconElement
20 * @mixins OO.ui.mixin.IndicatorElement
21 * @mixins OO.ui.mixin.PendingElement
24 * @param {Object} [config] Configuration options
25 * @cfg {string} [type='datetime'] Whether to act like a 'date', 'time', or 'datetime' input.
26 * Affects values stored in the relevant <input> and the formatting and
27 * interpretation of values passed to/from getValue() and setValue(). It's up
28 * to the user to configure the DateTimeFormatter correctly.
29 * @cfg {Object|mw.widgets.datetime.DateTimeFormatter} [formatter={}] Configuration options for
30 * mw.widgets.datetime.ProlepticGregorianDateTimeFormatter (with 'format' defaulting to
31 * '@date', '@time', or '@datetime' depending on 'type'), or an
32 * mw.widgets.datetime.DateTimeFormatter instance to use.
33 * @cfg {Object|null} [calendar={}] Configuration options for
34 * mw.widgets.datetime.CalendarWidget; note certain settings will be forced based on the
35 * settings passed to this widget. Set null to disable the calendar.
36 * @cfg {boolean} [required=false] Whether a value is required.
37 * @cfg {boolean} [clearable=true] Whether to provide for blanking the value.
38 * @cfg {Date|null} [value=null] Default value for the widget
39 * @cfg {Date|string|null} [min=null] Minimum allowed date
40 * @cfg {Date|string|null} [max=null] Maximum allowed date
42 mw
.widgets
.datetime
.DateTimeInputWidget
= function MwWidgetsDatetimeDateTimeInputWidget( config
) {
43 // Configuration initialization
54 // See InputWidget#reusePreInfuseDOM about config.$input
55 if ( config
.$input
) {
56 config
.$input
.addClass( 'oo-ui-element-hidden' );
59 if ( $.isPlainObject( config
.formatter
) && config
.formatter
.format
=== undefined ) {
60 config
.formatter
.format
= '@' + config
.type
;
64 this.type
= config
.type
;
67 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].call( this, config
);
70 OO
.ui
.mixin
.IconElement
.call( this, config
);
71 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
72 OO
.ui
.mixin
.PendingElement
.call( this, config
);
75 this.$handle
= $( '<span>' );
76 this.$fields
= $( '<span>' );
78 this.clearable
= !!config
.clearable
;
79 this.required
= !!config
.required
;
81 if ( typeof config
.min
=== 'string' ) {
82 config
.min
= this.parseDateValue( config
.min
);
84 if ( config
.min
instanceof Date
&& config
.min
.getTime() >= -62167219200000 ) {
85 this.min
= config
.min
;
87 this.min
= new Date( -62167219200000 ); // 0000-01-01T00:00:00.000Z
90 if ( typeof config
.max
=== 'string' ) {
91 config
.max
= this.parseDateValue( config
.max
);
93 if ( config
.max
instanceof Date
&& config
.max
.getTime() <= 253402300799999 ) {
94 this.max
= config
.max
;
96 this.max
= new Date( 253402300799999 ); // 9999-12-31T12:59:59.999Z
99 switch ( this.type
) {
101 this.min
.setUTCHours( 0, 0, 0, 0 );
102 this.max
.setUTCHours( 23, 59, 59, 999 );
105 this.min
.setUTCFullYear( 1970, 0, 1 );
106 this.max
.setUTCFullYear( 1970, 0, 1 );
109 if ( this.min
> this.max
) {
111 '"min" (' + this.min
.toISOString() + ') must not be greater than ' +
112 '"max" (' + this.max
.toISOString() + ')'
116 if ( config
.formatter
instanceof mw
.widgets
.datetime
.DateTimeFormatter
) {
117 this.formatter
= config
.formatter
;
118 } else if ( $.isPlainObject( config
.formatter
) ) {
119 this.formatter
= new mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter( config
.formatter
);
121 throw new Error( '"formatter" must be an mw.widgets.datetime.DateTimeFormatter or a plain object' );
124 if ( this.type
=== 'time' || config
.calendar
=== null ) {
125 this.calendar
= null;
127 config
.calendar
= $.extend( {}, config
.calendar
, {
128 formatter
: this.formatter
,
133 this.calendar
= new mw
.widgets
.datetime
.CalendarWidget( config
.calendar
);
138 click
: this.onHandleClick
.bind( this )
140 this.connect( this, {
143 this.formatter
.connect( this, {
146 if ( this.calendar
) {
147 this.calendar
.connect( this, {
148 change
: 'onCalendarChange'
153 this.setTabIndex( -1 );
155 this.$fields
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-fields' );
159 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-handle' )
160 .append( this.$icon
, this.$indicator
, this.$fields
);
163 .addClass( 'mw-widgets-datetime-dateTimeInputWidget' )
164 .append( this.$handle
);
166 if ( this.calendar
) {
167 this.$element
.append( this.calendar
.$element
);
173 OO
.inheritClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.InputWidget
);
174 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IconElement
);
175 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IndicatorElement
);
176 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.PendingElement
);
178 /* Static properties */
180 mw
.widgets
.datetime
.DateTimeInputWidget
.static.supportsSimpleLabel
= false;
187 * Get the currently focused field, if any
192 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getFocusedField = function () {
193 return this.$fields
.find( this.getElementDocument().activeElement
);
197 * Convert a date string to a Date
200 * @param {string} value
201 * @return {Date|null}
203 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.parseDateValue = function ( value
) {
206 value
= String( value
);
207 switch ( this.type
) {
209 value
= value
+ 'T00:00:00Z';
212 value
= '1970-01-01T' + value
+ 'Z';
215 m
= /^(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec( value
);
218 while ( m
[ 7 ].length
< 3 ) {
225 date
.setUTCFullYear( m
[ 1 ], m
[ 2 ] - 1, m
[ 3 ] );
226 date
.setUTCHours( m
[ 4 ], m
[ 5 ], m
[ 6 ], m
[ 7 ] );
227 if ( date
.getTime() < -62167219200000 || date
.getTime() > 253402300799999 ||
228 date
.getUTCFullYear() !== +m
[ 1 ] ||
229 date
.getUTCMonth() + 1 !== +m
[ 2 ] ||
230 date
.getUTCDate() !== +m
[ 3 ] ||
231 date
.getUTCHours() !== +m
[ 4 ] ||
232 date
.getUTCMinutes() !== +m
[ 5 ] ||
233 date
.getUTCSeconds() !== +m
[ 6 ] ||
234 date
.getUTCMilliseconds() !== +m
[ 7 ]
248 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.cleanUpValue = function ( value
) {
251 if ( value
=== '' ) {
255 if ( value
instanceof Date
) {
258 date
= this.parseDateValue( value
);
261 if ( date
instanceof Date
) {
262 pad = function ( v
, l
) {
264 while ( v
.length
< l
) {
270 switch ( this.type
) {
272 value
= pad( date
.getUTCFullYear(), 4 ) +
273 '-' + pad( date
.getUTCMonth() + 1, 2 ) +
274 '-' + pad( date
.getUTCDate(), 2 );
278 value
= pad( date
.getUTCHours(), 2 ) +
279 ':' + pad( date
.getUTCMinutes(), 2 ) +
280 ':' + pad( date
.getUTCSeconds(), 2 ) +
281 '.' + pad( date
.getUTCMilliseconds(), 3 );
282 value
= value
.replace( /\.?0+$/, '' );
286 value
= date
.toISOString();
297 * Get the value of the input as a Date object
299 * @return {Date|null}
301 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getValueAsDate = function () {
302 return this.parseDateValue( this.getValue() );
306 * Set up the UI fields
310 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setupFields = function () {
311 var i
, $field
, spec
, placeholder
, sz
, maxlength
,
312 spanValFunc = function ( v
) {
313 if ( v
=== undefined ) {
314 return this.data( 'mw-widgets-datetime-dateTimeInputWidget-value' );
317 this.data( 'mw-widgets-datetime-dateTimeInputWidget-value', v
);
319 v
= this.data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder' );
325 reduceFunc = function ( k
, v
) {
326 maxlength
= Math
.max( maxlength
, v
);
328 disabled
= this.isDisabled(),
329 specs
= this.formatter
.getFieldSpec();
331 this.$fields
.empty();
332 this.clearButton
= null;
335 for ( i
= 0; i
< specs
.length
; i
++ ) {
337 if ( typeof spec
=== 'string' ) {
339 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
341 .appendTo( this.$fields
);
346 while ( placeholder
.length
< spec
.size
) {
350 if ( spec
.type
=== 'number' ) {
351 // Numbers ''should'' be the same width. But we need some extra for
353 sz
= ( spec
.size
* 1.15 ) + 'ch';
355 // Add a little for padding
356 sz
= ( spec
.size
* 1.25 ) + 'ch';
358 if ( spec
.editable
&& spec
.type
!== 'static' ) {
359 if ( spec
.type
=== 'boolean' || spec
.type
=== 'toggleLocal' ) {
360 $field
= $( '<span>' )
362 tabindex
: disabled
? -1 : 0
365 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
367 keydown
: this.onFieldKeyDown
.bind( this, $field
),
368 focus
: this.onFieldFocus
.bind( this, $field
),
369 click
: this.onFieldClick
.bind( this, $field
),
370 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
372 $field
.val
= spanValFunc
;
374 maxlength
= spec
.size
;
375 if ( spec
.intercalarySize
) {
376 // eslint-disable-next-line no-restricted-properties
377 $.each( spec
.intercalarySize
, reduceFunc
);
379 $field
= $( '<input>' ).attr( 'type', 'text' )
381 tabindex
: disabled
? -1 : 0,
387 placeholder
: placeholder
391 keydown
: this.onFieldKeyDown
.bind( this, $field
),
392 click
: this.onFieldClick
.bind( this, $field
),
393 focus
: this.onFieldFocus
.bind( this, $field
),
394 blur
: this.onFieldBlur
.bind( this, $field
),
395 change
: this.onFieldChange
.bind( this, $field
),
396 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
399 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
401 $field
= $( '<span>' )
403 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
404 if ( spec
.type
!== 'static' ) {
405 $field
.prop( 'tabIndex', -1 );
406 $field
.on( 'focus', this.onFieldFocus
.bind( this, $field
) );
408 if ( spec
.type
=== 'static' ) {
409 $field
.text( spec
.value
);
411 $field
.val
= spanValFunc
;
415 this.fields
.push( $field
);
417 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
418 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec
)
419 .appendTo( this.$fields
);
422 if ( this.clearable
) {
423 this.clearButton
= new OO
.ui
.ButtonWidget( {
424 classes
: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
429 click
: 'onClearClick'
431 this.$fields
.append( this.clearButton
.$element
);
434 this.updateFieldsFromValue();
438 * Update the UI fields from the current value
442 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateFieldsFromValue = function () {
443 var i
, $field
, spec
, intercalary
, sz
,
444 date
= this.getValueAsDate();
446 if ( date
=== null ) {
447 this.components
= null;
449 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
450 $field
= this.fields
[ i
];
451 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
454 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
457 if ( spec
.intercalarySize
) {
458 if ( spec
.type
=== 'number' ) {
459 // Numbers ''should'' be the same width. But we need some extra for
461 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
463 // Add a little for padding
464 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
469 this.setFlags( { invalid
: this.required
} );
471 this.components
= this.formatter
.getComponentsFromDate( date
);
472 intercalary
= this.components
.intercalary
;
474 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
475 $field
= this.fields
[ i
];
476 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
477 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
478 if ( spec
.type
!== 'static' ) {
479 $field
.val( spec
.formatValue( this.components
[ spec
.component
] ) );
481 if ( spec
.intercalarySize
) {
482 if ( intercalary
&& spec
.intercalarySize
[ intercalary
] !== undefined ) {
483 sz
= spec
.intercalarySize
[ intercalary
];
487 $field
.toggleClass( 'oo-ui-element-hidden', sz
<= 0 );
488 if ( spec
.type
=== 'number' ) {
489 // Numbers ''should'' be the same width. But we need some extra for
491 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
493 // Add a little for padding
494 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
499 this.setFlags( { invalid
: date
< this.min
|| date
> this.max
} );
502 this.$element
.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date
=== null );
506 * Update the value with data from the UI fields
510 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateValueFromFields = function () {
511 var i
, v
, $field
, spec
, curDate
, newDate
,
517 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
518 $field
= this.fields
[ i
];
519 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
520 if ( spec
.editable
) {
521 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
524 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
528 v
= spec
.parseValue( v
);
529 if ( v
=== undefined ) {
530 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
533 components
[ spec
.component
] = v
;
540 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
541 this.fields
[ i
].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
543 } else if ( anyEmpty
) {
548 curDate
= this.getValueAsDate();
549 newDate
= this.formatter
.getDateFromComponents( components
);
550 if ( !curDate
|| !newDate
|| curDate
.getTime() !== newDate
.getTime() ) {
551 this.setValue( newDate
);
557 * Handle change event
561 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onChange = function () {
564 this.updateFieldsFromValue();
566 if ( this.calendar
) {
567 date
= this.getValueAsDate();
568 this.calendar
.setSelected( date
);
570 this.calendar
.setFocusedDate( date
);
576 * Handle clear button click event
580 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onClearClick = function () {
586 * Handle click on the widget background
589 * @param {jQuery.Event} e Click event
591 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onHandleClick = function () {
596 * Handle key down events on our field inputs.
599 * @param {jQuery} $field
600 * @param {jQuery.Event} e Key down event
601 * @return {boolean} False to cancel the default event
603 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldKeyDown = function ( $field
, e
) {
604 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
606 if ( !this.isDisabled() ) {
608 case OO
.ui
.Keys
.ENTER
:
609 case OO
.ui
.Keys
.SPACE
:
610 if ( spec
.type
=== 'boolean' ) {
612 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
615 } else if ( spec
.type
=== 'toggleLocal' ) {
616 this.formatter
.toggleLocal();
621 case OO
.ui
.Keys
.DOWN
:
622 if ( spec
.type
=== 'toggleLocal' ) {
623 this.formatter
.toggleLocal();
626 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
,
627 e
.keyCode
=== OO
.ui
.Keys
.UP
? -1 : 1, 'wrap' )
630 if ( $field
.is( ':input' ) ) {
639 * Handle focus events on our field inputs.
642 * @param {jQuery} $field
643 * @param {jQuery.Event} e Focus event
645 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldFocus = function ( $field
) {
646 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
648 if ( !this.isDisabled() ) {
649 if ( this.getValueAsDate() === null ) {
650 this.setValue( this.formatter
.getDefaultDate() );
652 if ( $field
.is( ':input' ) ) {
656 if ( this.calendar
) {
657 this.calendar
.toggle( !!spec
.calendarComponent
);
663 * Handle click events on our field inputs.
666 * @param {jQuery} $field
667 * @param {jQuery.Event} e Click event
669 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldClick = function ( $field
) {
670 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
672 if ( !this.isDisabled() ) {
673 if ( spec
.type
=== 'boolean' ) {
675 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
677 } else if ( spec
.type
=== 'toggleLocal' ) {
678 this.formatter
.toggleLocal();
684 * Handle blur events on our field inputs.
687 * @param {jQuery} $field
688 * @param {jQuery.Event} e Blur event
690 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldBlur = function ( $field
) {
692 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
694 this.updateValueFromFields();
697 date
= this.getValueAsDate();
701 v
= spec
.formatValue( this.formatter
.getComponentsFromDate( date
)[ spec
.component
] );
702 if ( v
!== $field
.val() ) {
709 * Handle change events on our field inputs.
712 * @param {jQuery} $field
713 * @param {jQuery.Event} e Change event
715 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldChange = function () {
716 this.updateValueFromFields();
720 * Handle wheel events on our field inputs.
723 * @param {jQuery} $field
724 * @param {jQuery.Event} e Change event
725 * @return {boolean} False to cancel the default event
727 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldWheel = function ( $field
, e
) {
729 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
731 if ( this.isDisabled() || !this.getFocusedField().length
) {
735 // Standard 'wheel' event
736 if ( e
.originalEvent
.deltaMode
!== undefined ) {
737 this.sawWheelEvent
= true;
739 if ( e
.originalEvent
.deltaY
) {
740 delta
= -e
.originalEvent
.deltaY
;
741 } else if ( e
.originalEvent
.deltaX
) {
742 delta
= e
.originalEvent
.deltaX
;
745 // Non-standard events
746 if ( !this.sawWheelEvent
) {
747 if ( e
.originalEvent
.wheelDeltaX
) {
748 delta
= -e
.originalEvent
.wheelDeltaX
;
749 } else if ( e
.originalEvent
.wheelDeltaY
) {
750 delta
= e
.originalEvent
.wheelDeltaY
;
751 } else if ( e
.originalEvent
.wheelDelta
) {
752 delta
= e
.originalEvent
.wheelDelta
;
753 } else if ( e
.originalEvent
.detail
) {
754 delta
= -e
.originalEvent
.detail
;
758 if ( delta
&& spec
) {
759 if ( spec
.type
=== 'toggleLocal' ) {
760 this.formatter
.toggleLocal();
763 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, delta
< 0 ? -1 : 1, 'wrap' )
771 * Handle calendar change event
775 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onCalendarChange = function () {
776 var curDate
= this.getValueAsDate(),
777 newDate
= this.calendar
.getSelected()[ 0 ];
780 if ( !curDate
|| newDate
.getTime() !== curDate
.getTime() ) {
781 this.setValue( newDate
);
790 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getInputElement = function () {
791 return $( '<input>' ).attr( 'type', 'hidden' );
797 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setDisabled = function ( disabled
) {
798 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
800 // Flag all our fields as disabled
801 if ( this.$fields
) {
802 this.$fields
.find( 'input' ).prop( 'disabled', this.isDisabled() );
803 this.$fields
.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
806 if ( this.clearButton
) {
807 this.clearButton
.setDisabled( disabled
);
816 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.focus = function () {
817 if ( !this.getFocusedField().length
) {
818 this.$fields
.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
826 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.blur = function () {
827 this.getFocusedField().blur();
834 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.simulateLabelClick = function () {