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 if ( $.isPlainObject( config
.formatter
) && config
.formatter
.format
=== undefined ) {
55 config
.formatter
.format
= '@' + config
.type
;
59 this.type
= config
.type
;
62 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].call( this, config
);
65 OO
.ui
.mixin
.IconElement
.call( this, config
);
66 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
67 OO
.ui
.mixin
.PendingElement
.call( this, config
);
70 this.$handle
= $( '<span>' );
71 this.$fields
= $( '<span>' );
73 this.clearable
= !!config
.clearable
;
74 this.required
= !!config
.required
;
76 if ( typeof config
.min
=== 'string' ) {
77 config
.min
= this.parseDateValue( config
.min
);
79 if ( config
.min
instanceof Date
&& config
.min
.getTime() >= -62167219200000 ) {
80 this.min
= config
.min
;
82 this.min
= new Date( -62167219200000 ); // 0000-01-01T00:00:00.000Z
85 if ( typeof config
.max
=== 'string' ) {
86 config
.max
= this.parseDateValue( config
.max
);
88 if ( config
.max
instanceof Date
&& config
.max
.getTime() <= 253402300799999 ) {
89 this.max
= config
.max
;
91 this.max
= new Date( 253402300799999 ); // 9999-12-31T12:59:59.999Z
94 switch ( this.type
) {
96 this.min
.setUTCHours( 0, 0, 0, 0 );
97 this.max
.setUTCHours( 23, 59, 59, 999 );
100 this.min
.setUTCFullYear( 1970, 0, 1 );
101 this.max
.setUTCFullYear( 1970, 0, 1 );
104 if ( this.min
> this.max
) {
106 '"min" (' + this.min
.toISOString() + ') must not be greater than ' +
107 '"max" (' + this.max
.toISOString() + ')'
111 if ( config
.formatter
instanceof mw
.widgets
.datetime
.DateTimeFormatter
) {
112 this.formatter
= config
.formatter
;
113 } else if ( $.isPlainObject( config
.formatter
) ) {
114 this.formatter
= new mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter( config
.formatter
);
116 throw new Error( '"formatter" must be an mw.widgets.datetime.DateTimeFormatter or a plain object' );
119 if ( this.type
=== 'time' || config
.calendar
=== null ) {
120 this.calendar
= null;
122 config
.calendar
= $.extend( {}, config
.calendar
, {
123 formatter
: this.formatter
,
128 this.calendar
= new mw
.widgets
.datetime
.CalendarWidget( config
.calendar
);
133 click
: this.onHandleClick
.bind( this )
135 this.connect( this, {
138 this.formatter
.connect( this, {
141 if ( this.calendar
) {
142 this.calendar
.connect( this, {
143 change
: 'onCalendarChange'
148 this.setTabIndex( -1 );
150 this.$fields
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-fields' );
154 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-handle' )
155 .append( this.$icon
, this.$indicator
, this.$fields
);
158 .addClass( 'mw-widgets-datetime-dateTimeInputWidget' )
159 .append( this.$handle
);
161 if ( this.calendar
) {
162 this.$element
.append( this.calendar
.$element
);
168 OO
.inheritClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.InputWidget
);
169 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IconElement
);
170 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IndicatorElement
);
171 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.PendingElement
);
173 /* Static properties */
175 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'static' ].supportsSimpleLabel
= false;
182 * Convert a date string to a Date
185 * @param {string} value
186 * @return {Date|null}
188 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.parseDateValue = function ( value
) {
191 value
= String( value
);
192 switch ( this.type
) {
194 value
= value
+ 'T00:00:00Z';
197 value
= '1970-01-01T' + value
+ 'Z';
200 m
= /^(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec( value
);
203 while ( m
[ 7 ].length
< 3 ) {
210 date
.setUTCFullYear( m
[ 1 ], m
[ 2 ] - 1, m
[ 3 ] );
211 date
.setUTCHours( m
[ 4 ], m
[ 5 ], m
[ 6 ], m
[ 7 ] );
212 if ( date
.getTime() < -62167219200000 || date
.getTime() > 253402300799999 ||
213 date
.getUTCFullYear() !== +m
[ 1 ] ||
214 date
.getUTCMonth() + 1 !== +m
[ 2 ] ||
215 date
.getUTCDate() !== +m
[ 3 ] ||
216 date
.getUTCHours() !== +m
[ 4 ] ||
217 date
.getUTCMinutes() !== +m
[ 5 ] ||
218 date
.getUTCSeconds() !== +m
[ 6 ] ||
219 date
.getUTCMilliseconds() !== +m
[ 7 ]
233 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.cleanUpValue = function ( value
) {
236 if ( value
=== '' ) {
240 if ( value
instanceof Date
) {
243 date
= this.parseDateValue( value
);
246 if ( date
instanceof Date
) {
247 pad = function ( v
, l
) {
249 while ( v
.length
< l
) {
255 switch ( this.type
) {
257 value
= pad( date
.getUTCFullYear(), 4 ) +
258 '-' + pad( date
.getUTCMonth() + 1, 2 ) +
259 '-' + pad( date
.getUTCDate(), 2 );
263 value
= pad( date
.getUTCHours(), 2 ) +
264 ':' + pad( date
.getUTCMinutes(), 2 ) +
265 ':' + pad( date
.getUTCSeconds(), 2 ) +
266 '.' + pad( date
.getUTCMilliseconds(), 3 );
267 value
= value
.replace( /\.?0+$/, '' );
271 value
= date
.toISOString();
282 * Get the value of the input as a Date object
284 * @return {Date|null}
286 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getValueAsDate = function () {
287 return this.parseDateValue( this.getValue() );
291 * Set up the UI fields
295 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setupFields = function () {
296 var i
, $field
, spec
, placeholder
, sz
, maxlength
,
297 spanValFunc = function ( v
) {
298 if ( v
=== undefined ) {
299 return this.data( 'mw-widgets-datetime-dateTimeInputWidget-value' );
302 this.data( 'mw-widgets-datetime-dateTimeInputWidget-value', v
);
304 v
= this.data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder' );
310 reduceFunc = function ( k
, v
) {
311 maxlength
= Math
.max( maxlength
, v
);
313 disabled
= this.isDisabled(),
314 specs
= this.formatter
.getFieldSpec();
316 this.$fields
.empty();
317 this.clearButton
= null;
320 for ( i
= 0; i
< specs
.length
; i
++ ) {
322 if ( typeof spec
=== 'string' ) {
324 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
326 .appendTo( this.$fields
);
331 while ( placeholder
.length
< spec
.size
) {
335 if ( spec
.type
=== 'number' ) {
336 // Numbers ''should'' be the same width. But we need some extra for
338 sz
= ( spec
.size
* 1.15 ) + 'ch';
340 // Add a little for padding
341 sz
= ( spec
.size
* 1.15 ) + 'ch';
343 if ( spec
.editable
&& spec
.type
!== 'static' ) {
344 if ( spec
.type
=== 'boolean' || spec
.type
=== 'toggleLocal' ) {
345 $field
= $( '<span>' )
347 tabindex
: disabled
? -1 : 0
350 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
352 keydown
: this.onFieldKeyDown
.bind( this, $field
),
353 focus
: this.onFieldFocus
.bind( this, $field
),
354 click
: this.onFieldClick
.bind( this, $field
),
355 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
357 $field
.val
= spanValFunc
;
359 maxlength
= spec
.size
;
360 if ( spec
.intercalarySize
) {
361 $.each( spec
.intercalarySize
, reduceFunc
);
363 $field
= $( '<input>' ).attr( 'type', 'text' )
365 tabindex
: disabled
? -1 : 0,
371 placeholder
: placeholder
375 keydown
: this.onFieldKeyDown
.bind( this, $field
),
376 click
: this.onFieldClick
.bind( this, $field
),
377 focus
: this.onFieldFocus
.bind( this, $field
),
378 blur
: this.onFieldBlur
.bind( this, $field
),
379 change
: this.onFieldChange
.bind( this, $field
),
380 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
383 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
385 $field
= $( '<span>' )
387 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
388 if ( spec
.type
=== 'static' ) {
389 $field
.text( spec
.value
);
391 $field
.val
= spanValFunc
;
395 this.fields
.push( $field
);
397 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
398 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec
)
399 .appendTo( this.$fields
);
402 if ( this.clearable
) {
403 this.clearButton
= new OO
.ui
.ButtonWidget( {
404 classes
: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
409 click
: 'onClearClick'
411 this.$fields
.append( this.clearButton
.$element
);
414 this.updateFieldsFromValue();
418 * Update the UI fields from the current value
422 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateFieldsFromValue = function () {
423 var i
, $field
, spec
, intercalary
, sz
,
424 date
= this.getValueAsDate();
426 if ( date
=== null ) {
427 this.components
= null;
429 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
430 $field
= this.fields
[ i
];
431 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
434 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
437 if ( spec
.intercalarySize
) {
438 if ( spec
.type
=== 'number' ) {
439 // Numbers ''should'' be the same width. But we need some extra for
441 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
443 // Add a little for padding
444 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
449 this.setFlags( { invalid
: this.required
} );
451 this.components
= this.formatter
.getComponentsFromDate( date
);
452 intercalary
= this.components
.intercalary
;
454 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
455 $field
= this.fields
[ i
];
456 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
457 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
458 if ( spec
.type
!== 'static' ) {
459 $field
.val( spec
.formatValue( this.components
[ spec
.component
] ) );
461 if ( spec
.intercalarySize
) {
462 if ( intercalary
&& spec
.intercalarySize
[ intercalary
] !== undefined ) {
463 sz
= spec
.intercalarySize
[ intercalary
];
467 $field
.toggleClass( 'oo-ui-element-hidden', sz
<= 0 );
468 if ( spec
.type
=== 'number' ) {
469 // Numbers ''should'' be the same width. But we need some extra for
471 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
473 // Add a little for padding
474 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
479 this.setFlags( { invalid
: date
< this.min
|| date
> this.max
} );
482 this.$element
.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date
=== null );
486 * Update the value with data from the UI fields
490 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateValueFromFields = function () {
491 var i
, v
, $field
, spec
, curDate
, newDate
,
497 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
498 $field
= this.fields
[ i
];
499 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
500 if ( spec
.editable
) {
501 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
504 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
508 v
= spec
.parseValue( v
);
509 if ( v
=== undefined ) {
510 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
513 components
[ spec
.component
] = v
;
520 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
521 this.fields
[ i
].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
523 } else if ( anyEmpty
) {
528 curDate
= this.getValueAsDate();
529 newDate
= this.formatter
.getDateFromComponents( components
);
530 if ( !curDate
|| !newDate
|| curDate
.getTime() !== newDate
.getTime() ) {
531 this.setValue( newDate
);
537 * Handle change event
541 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onChange = function () {
544 this.updateFieldsFromValue();
546 if ( this.calendar
) {
547 date
= this.getValueAsDate();
548 this.calendar
.setSelected( date
);
550 this.calendar
.setFocusedDate( date
);
556 * Handle clear button click event
560 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onClearClick = function () {
566 * Handle click on the widget background
569 * @param {jQuery.Event} e Click event
571 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onHandleClick = function () {
576 * Handle key down events on our field inputs.
579 * @param {jQuery} $field
580 * @param {jQuery.Event} e Key down event
582 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldKeyDown = function ( $field
, e
) {
583 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
585 if ( !this.isDisabled() ) {
587 case OO
.ui
.Keys
.ENTER
:
588 case OO
.ui
.Keys
.SPACE
:
589 if ( spec
.type
=== 'boolean' ) {
591 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
594 } else if ( spec
.type
=== 'toggleLocal' ) {
595 this.formatter
.toggleLocal();
600 case OO
.ui
.Keys
.DOWN
:
601 if ( spec
.type
=== 'toggleLocal' ) {
602 this.formatter
.toggleLocal();
605 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
,
606 e
.keyCode
=== OO
.ui
.Keys
.UP
? -1 : 1, 'wrap' )
609 if ( $field
.is( ':input' ) ) {
618 * Handle focus events on our field inputs.
621 * @param {jQuery} $field
622 * @param {jQuery.Event} e Focus event
624 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldFocus = function ( $field
) {
625 if ( !this.isDisabled() ) {
626 if ( this.getValueAsDate() === null ) {
627 this.setValue( this.formatter
.getDefaultDate() );
629 if ( $field
.is( ':input' ) ) {
633 if ( this.calendar
) {
634 this.calendar
.toggle( true );
640 * Handle click events on our field inputs.
643 * @param {jQuery} $field
644 * @param {jQuery.Event} e Click event
646 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldClick = function ( $field
) {
647 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
649 if ( !this.isDisabled() ) {
650 if ( spec
.type
=== 'boolean' ) {
652 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
654 } else if ( spec
.type
=== 'toggleLocal' ) {
655 this.formatter
.toggleLocal();
661 * Handle blur events on our field inputs.
664 * @param {jQuery} $field
665 * @param {jQuery.Event} e Blur event
667 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldBlur = function ( $field
) {
669 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
671 this.updateValueFromFields();
674 date
= this.getValueAsDate();
678 v
= spec
.formatValue( this.formatter
.getComponentsFromDate( date
)[ spec
.component
] );
679 if ( v
!== $field
.val() ) {
686 * Handle change events on our field inputs.
689 * @param {jQuery} $field
690 * @param {jQuery.Event} e Change event
692 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldChange = function () {
693 this.updateValueFromFields();
697 * Handle wheel events on our field inputs.
700 * @param {jQuery} $field
701 * @param {jQuery.Event} e Change event
703 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldWheel = function ( $field
, e
) {
705 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
707 if ( this.isDisabled() ) {
711 // Standard 'wheel' event
712 if ( e
.originalEvent
.deltaMode
!== undefined ) {
713 this.sawWheelEvent
= true;
715 if ( e
.originalEvent
.deltaY
) {
716 delta
= -e
.originalEvent
.deltaY
;
717 } else if ( e
.originalEvent
.deltaX
) {
718 delta
= e
.originalEvent
.deltaX
;
721 // Non-standard events
722 if ( !this.sawWheelEvent
) {
723 if ( e
.originalEvent
.wheelDeltaX
) {
724 delta
= -e
.originalEvent
.wheelDeltaX
;
725 } else if ( e
.originalEvent
.wheelDeltaY
) {
726 delta
= e
.originalEvent
.wheelDeltaY
;
727 } else if ( e
.originalEvent
.wheelDelta
) {
728 delta
= e
.originalEvent
.wheelDelta
;
729 } else if ( e
.originalEvent
.detail
) {
730 delta
= -e
.originalEvent
.detail
;
734 if ( delta
&& spec
) {
735 if ( spec
.type
=== 'toggleLocal' ) {
736 this.formatter
.toggleLocal();
739 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, delta
< 0 ? -1 : 1, 'wrap' )
747 * Handle calendar change event
751 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onCalendarChange = function () {
752 var curDate
= this.getValueAsDate(),
753 newDate
= this.calendar
.getSelected()[ 0 ];
756 if ( !curDate
|| newDate
.getTime() !== curDate
.getTime() ) {
757 this.setValue( newDate
);
766 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getInputElement = function () {
767 return $( '<input>' ).attr( 'type', 'hidden' );
773 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setDisabled = function ( disabled
) {
774 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
776 // Flag all our fields as disabled
777 if ( this.$fields
) {
778 this.$fields
.find( 'input' ).prop( 'disabled', this.isDisabled() );
779 this.$fields
.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
782 if ( this.clearButton
) {
783 this.clearButton
.setDisabled( disabled
);
792 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.focus = function () {
793 if ( !this.$fields
.find( document
.activeElement
).length
) {
794 this.$fields
.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
802 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.blur = function () {
803 this.$fields
.find( document
.activeElement
).blur();
810 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.simulateLabelClick = function () {
814 }( jQuery
, mediaWiki
) );