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 [OOjs UI 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/OOjs_UI/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 * Convert a date string to a Date
190 * @param {string} value
191 * @return {Date|null}
193 mw.widgets.datetime.DateTimeInputWidget.prototype.parseDateValue = function ( value ) {
196 value = String( value );
197 switch ( this.type ) {
199 value = value + 'T00:00:00Z';
202 value = '1970-01-01T' + value + 'Z';
205 m = /^(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec( value );
208 while ( m[ 7 ].length < 3 ) {
215 date.setUTCFullYear( m[ 1 ], m[ 2 ] - 1, m[ 3 ] );
216 date.setUTCHours( m[ 4 ], m[ 5 ], m[ 6 ], m[ 7 ] );
217 if ( date.getTime() < -62167219200000 || date.getTime() > 253402300799999 ||
218 date.getUTCFullYear() !== +m[ 1 ] ||
219 date.getUTCMonth() + 1 !== +m[ 2 ] ||
220 date.getUTCDate() !== +m[ 3 ] ||
221 date.getUTCHours() !== +m[ 4 ] ||
222 date.getUTCMinutes() !== +m[ 5 ] ||
223 date.getUTCSeconds() !== +m[ 6 ] ||
224 date.getUTCMilliseconds() !== +m[ 7 ]
238 mw.widgets.datetime.DateTimeInputWidget.prototype.cleanUpValue = function ( value ) {
241 if ( value === '' ) {
245 if ( value instanceof Date ) {
248 date = this.parseDateValue( value );
251 if ( date instanceof Date ) {
252 pad = function ( v, l ) {
254 while ( v.length < l ) {
260 switch ( this.type ) {
262 value = pad( date.getUTCFullYear(), 4 ) +
263 '-' + pad( date.getUTCMonth() + 1, 2 ) +
264 '-' + pad( date.getUTCDate(), 2 );
268 value = pad( date.getUTCHours(), 2 ) +
269 ':' + pad( date.getUTCMinutes(), 2 ) +
270 ':' + pad( date.getUTCSeconds(), 2 ) +
271 '.' + pad( date.getUTCMilliseconds(), 3 );
272 value = value.replace( /\.?0+$/, '' );
276 value = date.toISOString();
287 * Get the value of the input as a Date object
289 * @return {Date|null}
291 mw.widgets.datetime.DateTimeInputWidget.prototype.getValueAsDate = function () {
292 return this.parseDateValue( this.getValue() );
296 * Set up the UI fields
300 mw.widgets.datetime.DateTimeInputWidget.prototype.setupFields = function () {
301 var i, $field, spec, placeholder, sz, maxlength,
302 spanValFunc = function ( v ) {
303 if ( v === undefined ) {
304 return this.data( 'mw-widgets-datetime-dateTimeInputWidget-value' );
307 this.data( 'mw-widgets-datetime-dateTimeInputWidget-value', v );
309 v = this.data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder' );
315 reduceFunc = function ( k, v ) {
316 maxlength = Math.max( maxlength, v );
318 disabled = this.isDisabled(),
319 specs = this.formatter.getFieldSpec();
321 this.$fields.empty();
322 this.clearButton = null;
325 for ( i = 0; i < specs.length; i++ ) {
327 if ( typeof spec === 'string' ) {
329 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
331 .appendTo( this.$fields );
336 while ( placeholder.length < spec.size ) {
340 if ( spec.type === 'number' ) {
341 // Numbers ''should'' be the same width. But we need some extra for
343 sz = ( spec.size * 1.15 ) + 'ch';
345 // Add a little for padding
346 sz = ( spec.size * 1.15 ) + 'ch';
348 if ( spec.editable && spec.type !== 'static' ) {
349 if ( spec.type === 'boolean' || spec.type === 'toggleLocal' ) {
350 $field = $( '<span>' )
352 tabindex: disabled ? -1 : 0
355 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder );
357 keydown: this.onFieldKeyDown.bind( this, $field ),
358 focus: this.onFieldFocus.bind( this, $field ),
359 click: this.onFieldClick.bind( this, $field ),
360 'wheel mousewheel DOMMouseScroll': this.onFieldWheel.bind( this, $field )
362 $field.val = spanValFunc;
364 maxlength = spec.size;
365 if ( spec.intercalarySize ) {
366 $.each( spec.intercalarySize, reduceFunc );
368 $field = $( '<input>' ).attr( 'type', 'text' )
370 tabindex: disabled ? -1 : 0,
376 placeholder: placeholder
380 keydown: this.onFieldKeyDown.bind( this, $field ),
381 click: this.onFieldClick.bind( this, $field ),
382 focus: this.onFieldFocus.bind( this, $field ),
383 blur: this.onFieldBlur.bind( this, $field ),
384 change: this.onFieldChange.bind( this, $field ),
385 'wheel mousewheel DOMMouseScroll': this.onFieldWheel.bind( this, $field )
388 $field.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
390 $field = $( '<span>' )
392 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder );
393 if ( spec.type === 'static' ) {
394 $field.text( spec.value );
396 $field.val = spanValFunc;
400 this.fields.push( $field );
402 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
403 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec )
404 .appendTo( this.$fields );
407 if ( this.clearable ) {
408 this.clearButton = new OO.ui.ButtonWidget( {
409 classes: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
414 click: 'onClearClick'
416 this.$fields.append( this.clearButton.$element );
419 this.updateFieldsFromValue();
423 * Update the UI fields from the current value
427 mw.widgets.datetime.DateTimeInputWidget.prototype.updateFieldsFromValue = function () {
428 var i, $field, spec, intercalary, sz,
429 date = this.getValueAsDate();
431 if ( date === null ) {
432 this.components = null;
434 for ( i = 0; i < this.fields.length; i++ ) {
435 $field = this.fields[ i ];
436 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
439 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
442 if ( spec.intercalarySize ) {
443 if ( spec.type === 'number' ) {
444 // Numbers ''should'' be the same width. But we need some extra for
446 $field.width( ( spec.size * 1.15 ) + 'ch' );
448 // Add a little for padding
449 $field.width( ( spec.size * 1.15 ) + 'ch' );
454 this.setFlags( { invalid: this.required } );
456 this.components = this.formatter.getComponentsFromDate( date );
457 intercalary = this.components.intercalary;
459 for ( i = 0; i < this.fields.length; i++ ) {
460 $field = this.fields[ i ];
461 $field.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
462 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
463 if ( spec.type !== 'static' ) {
464 $field.val( spec.formatValue( this.components[ spec.component ] ) );
466 if ( spec.intercalarySize ) {
467 if ( intercalary && spec.intercalarySize[ intercalary ] !== undefined ) {
468 sz = spec.intercalarySize[ intercalary ];
472 $field.toggleClass( 'oo-ui-element-hidden', sz <= 0 );
473 if ( spec.type === 'number' ) {
474 // Numbers ''should'' be the same width. But we need some extra for
476 this.fields[ i ].width( ( sz * 1.15 ) + 'ch' );
478 // Add a little for padding
479 this.fields[ i ].width( ( sz * 1.15 ) + 'ch' );
484 this.setFlags( { invalid: date < this.min || date > this.max } );
487 this.$element.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date === null );
491 * Update the value with data from the UI fields
495 mw.widgets.datetime.DateTimeInputWidget.prototype.updateValueFromFields = function () {
496 var i, v, $field, spec, curDate, newDate,
502 for ( i = 0; i < this.fields.length; i++ ) {
503 $field = this.fields[ i ];
504 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
505 if ( spec.editable ) {
506 $field.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
509 $field.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
513 v = spec.parseValue( v );
514 if ( v === undefined ) {
515 $field.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
518 components[ spec.component ] = v;
525 for ( i = 0; i < this.fields.length; i++ ) {
526 this.fields[ i ].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
528 } else if ( anyEmpty ) {
533 curDate = this.getValueAsDate();
534 newDate = this.formatter.getDateFromComponents( components );
535 if ( !curDate || !newDate || curDate.getTime() !== newDate.getTime() ) {
536 this.setValue( newDate );
542 * Handle change event
546 mw.widgets.datetime.DateTimeInputWidget.prototype.onChange = function () {
549 this.updateFieldsFromValue();
551 if ( this.calendar ) {
552 date = this.getValueAsDate();
553 this.calendar.setSelected( date );
555 this.calendar.setFocusedDate( date );
561 * Handle clear button click event
565 mw.widgets.datetime.DateTimeInputWidget.prototype.onClearClick = function () {
571 * Handle click on the widget background
574 * @param {jQuery.Event} e Click event
576 mw.widgets.datetime.DateTimeInputWidget.prototype.onHandleClick = function () {
581 * Handle key down events on our field inputs.
584 * @param {jQuery} $field
585 * @param {jQuery.Event} e Key down event
586 * @return {boolean} False to cancel the default event
588 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldKeyDown = function ( $field, e ) {
589 var spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
591 if ( !this.isDisabled() ) {
593 case OO.ui.Keys.ENTER:
594 case OO.ui.Keys.SPACE:
595 if ( spec.type === 'boolean' ) {
597 this.formatter.adjustComponent( this.getValueAsDate(), spec.component, 1, 'wrap' )
600 } else if ( spec.type === 'toggleLocal' ) {
601 this.formatter.toggleLocal();
606 case OO.ui.Keys.DOWN:
607 if ( spec.type === 'toggleLocal' ) {
608 this.formatter.toggleLocal();
611 this.formatter.adjustComponent( this.getValueAsDate(), spec.component,
612 e.keyCode === OO.ui.Keys.UP ? -1 : 1, 'wrap' )
615 if ( $field.is( ':input' ) ) {
624 * Handle focus events on our field inputs.
627 * @param {jQuery} $field
628 * @param {jQuery.Event} e Focus event
630 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldFocus = function ( $field ) {
631 if ( !this.isDisabled() ) {
632 if ( this.getValueAsDate() === null ) {
633 this.setValue( this.formatter.getDefaultDate() );
635 if ( $field.is( ':input' ) ) {
639 if ( this.calendar ) {
640 this.calendar.toggle( true );
646 * Handle click events on our field inputs.
649 * @param {jQuery} $field
650 * @param {jQuery.Event} e Click event
652 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldClick = function ( $field ) {
653 var spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
655 if ( !this.isDisabled() ) {
656 if ( spec.type === 'boolean' ) {
658 this.formatter.adjustComponent( this.getValueAsDate(), spec.component, 1, 'wrap' )
660 } else if ( spec.type === 'toggleLocal' ) {
661 this.formatter.toggleLocal();
667 * Handle blur events on our field inputs.
670 * @param {jQuery} $field
671 * @param {jQuery.Event} e Blur event
673 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldBlur = function ( $field ) {
675 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
677 this.updateValueFromFields();
680 date = this.getValueAsDate();
684 v = spec.formatValue( this.formatter.getComponentsFromDate( date )[ spec.component ] );
685 if ( v !== $field.val() ) {
692 * Handle change events on our field inputs.
695 * @param {jQuery} $field
696 * @param {jQuery.Event} e Change event
698 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldChange = function () {
699 this.updateValueFromFields();
703 * Handle wheel events on our field inputs.
706 * @param {jQuery} $field
707 * @param {jQuery.Event} e Change event
708 * @return {boolean} False to cancel the default event
710 mw.widgets.datetime.DateTimeInputWidget.prototype.onFieldWheel = function ( $field, e ) {
712 spec = $field.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
714 if ( this.isDisabled() ) {
718 // Standard 'wheel' event
719 if ( e.originalEvent.deltaMode !== undefined ) {
720 this.sawWheelEvent = true;
722 if ( e.originalEvent.deltaY ) {
723 delta = -e.originalEvent.deltaY;
724 } else if ( e.originalEvent.deltaX ) {
725 delta = e.originalEvent.deltaX;
728 // Non-standard events
729 if ( !this.sawWheelEvent ) {
730 if ( e.originalEvent.wheelDeltaX ) {
731 delta = -e.originalEvent.wheelDeltaX;
732 } else if ( e.originalEvent.wheelDeltaY ) {
733 delta = e.originalEvent.wheelDeltaY;
734 } else if ( e.originalEvent.wheelDelta ) {
735 delta = e.originalEvent.wheelDelta;
736 } else if ( e.originalEvent.detail ) {
737 delta = -e.originalEvent.detail;
741 if ( delta && spec ) {
742 if ( spec.type === 'toggleLocal' ) {
743 this.formatter.toggleLocal();
746 this.formatter.adjustComponent( this.getValueAsDate(), spec.component, delta < 0 ? -1 : 1, 'wrap' )
754 * Handle calendar change event
758 mw.widgets.datetime.DateTimeInputWidget.prototype.onCalendarChange = function () {
759 var curDate = this.getValueAsDate(),
760 newDate = this.calendar.getSelected()[ 0 ];
763 if ( !curDate || newDate.getTime() !== curDate.getTime() ) {
764 this.setValue( newDate );
773 mw.widgets.datetime.DateTimeInputWidget.prototype.getInputElement = function () {
774 return $( '<input>' ).attr( 'type', 'hidden' );
780 mw.widgets.datetime.DateTimeInputWidget.prototype.setDisabled = function ( disabled ) {
781 mw.widgets.datetime.DateTimeInputWidget[ 'super' ].prototype.setDisabled.call( this, disabled );
783 // Flag all our fields as disabled
784 if ( this.$fields ) {
785 this.$fields.find( 'input' ).prop( 'disabled', this.isDisabled() );
786 this.$fields.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
789 if ( this.clearButton ) {
790 this.clearButton.setDisabled( disabled );
799 mw.widgets.datetime.DateTimeInputWidget.prototype.focus = function () {
800 if ( !this.$fields.find( document.activeElement ).length ) {
801 this.$fields.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
809 mw.widgets.datetime.DateTimeInputWidget.prototype.blur = function () {
810 this.$fields.find( document.activeElement ).blur();
817 mw.widgets.datetime.DateTimeInputWidget.prototype.simulateLabelClick = function () {
821 }( jQuery, mediaWiki ) );