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
.prop( 'tabIndex', -1 );
390 $field
.on( 'focus', this.onFieldFocus
.bind( this, $field
) );
392 if ( spec
.type
=== 'static' ) {
393 $field
.text( spec
.value
);
395 $field
.val
= spanValFunc
;
399 this.fields
.push( $field
);
401 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
402 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec
)
403 .appendTo( this.$fields
);
406 if ( this.clearable
) {
407 this.clearButton
= new OO
.ui
.ButtonWidget( {
408 classes
: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
413 click
: 'onClearClick'
415 this.$fields
.append( this.clearButton
.$element
);
418 this.updateFieldsFromValue();
422 * Update the UI fields from the current value
426 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateFieldsFromValue = function () {
427 var i
, $field
, spec
, intercalary
, sz
,
428 date
= this.getValueAsDate();
430 if ( date
=== null ) {
431 this.components
= null;
433 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
434 $field
= this.fields
[ i
];
435 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
438 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
441 if ( spec
.intercalarySize
) {
442 if ( spec
.type
=== 'number' ) {
443 // Numbers ''should'' be the same width. But we need some extra for
445 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
447 // Add a little for padding
448 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
453 this.setFlags( { invalid
: this.required
} );
455 this.components
= this.formatter
.getComponentsFromDate( date
);
456 intercalary
= this.components
.intercalary
;
458 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
459 $field
= this.fields
[ i
];
460 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
461 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
462 if ( spec
.type
!== 'static' ) {
463 $field
.val( spec
.formatValue( this.components
[ spec
.component
] ) );
465 if ( spec
.intercalarySize
) {
466 if ( intercalary
&& spec
.intercalarySize
[ intercalary
] !== undefined ) {
467 sz
= spec
.intercalarySize
[ intercalary
];
471 $field
.toggleClass( 'oo-ui-element-hidden', sz
<= 0 );
472 if ( spec
.type
=== 'number' ) {
473 // Numbers ''should'' be the same width. But we need some extra for
475 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
477 // Add a little for padding
478 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
483 this.setFlags( { invalid
: date
< this.min
|| date
> this.max
} );
486 this.$element
.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date
=== null );
490 * Update the value with data from the UI fields
494 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateValueFromFields = function () {
495 var i
, v
, $field
, spec
, curDate
, newDate
,
501 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
502 $field
= this.fields
[ i
];
503 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
504 if ( spec
.editable
) {
505 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
508 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
512 v
= spec
.parseValue( v
);
513 if ( v
=== undefined ) {
514 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
517 components
[ spec
.component
] = v
;
524 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
525 this.fields
[ i
].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
527 } else if ( anyEmpty
) {
532 curDate
= this.getValueAsDate();
533 newDate
= this.formatter
.getDateFromComponents( components
);
534 if ( !curDate
|| !newDate
|| curDate
.getTime() !== newDate
.getTime() ) {
535 this.setValue( newDate
);
541 * Handle change event
545 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onChange = function () {
548 this.updateFieldsFromValue();
550 if ( this.calendar
) {
551 date
= this.getValueAsDate();
552 this.calendar
.setSelected( date
);
554 this.calendar
.setFocusedDate( date
);
560 * Handle clear button click event
564 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onClearClick = function () {
570 * Handle click on the widget background
573 * @param {jQuery.Event} e Click event
575 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onHandleClick = function () {
580 * Handle key down events on our field inputs.
583 * @param {jQuery} $field
584 * @param {jQuery.Event} e Key down event
586 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldKeyDown = function ( $field
, e
) {
587 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
589 if ( !this.isDisabled() ) {
591 case OO
.ui
.Keys
.ENTER
:
592 case OO
.ui
.Keys
.SPACE
:
593 if ( spec
.type
=== 'boolean' ) {
595 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
598 } else if ( spec
.type
=== 'toggleLocal' ) {
599 this.formatter
.toggleLocal();
604 case OO
.ui
.Keys
.DOWN
:
605 if ( spec
.type
=== 'toggleLocal' ) {
606 this.formatter
.toggleLocal();
609 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
,
610 e
.keyCode
=== OO
.ui
.Keys
.UP
? -1 : 1, 'wrap' )
613 if ( $field
.is( ':input' ) ) {
622 * Handle focus events on our field inputs.
625 * @param {jQuery} $field
626 * @param {jQuery.Event} e Focus event
628 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldFocus = function ( $field
) {
629 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
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( !!spec
.calendarComponent
);
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
709 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldWheel = function ( $field
, e
) {
711 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
713 if ( this.isDisabled() ) {
717 // Standard 'wheel' event
718 if ( e
.originalEvent
.deltaMode
!== undefined ) {
719 this.sawWheelEvent
= true;
721 if ( e
.originalEvent
.deltaY
) {
722 delta
= -e
.originalEvent
.deltaY
;
723 } else if ( e
.originalEvent
.deltaX
) {
724 delta
= e
.originalEvent
.deltaX
;
727 // Non-standard events
728 if ( !this.sawWheelEvent
) {
729 if ( e
.originalEvent
.wheelDeltaX
) {
730 delta
= -e
.originalEvent
.wheelDeltaX
;
731 } else if ( e
.originalEvent
.wheelDeltaY
) {
732 delta
= e
.originalEvent
.wheelDeltaY
;
733 } else if ( e
.originalEvent
.wheelDelta
) {
734 delta
= e
.originalEvent
.wheelDelta
;
735 } else if ( e
.originalEvent
.detail
) {
736 delta
= -e
.originalEvent
.detail
;
740 if ( delta
&& spec
) {
741 if ( spec
.type
=== 'toggleLocal' ) {
742 this.formatter
.toggleLocal();
745 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, delta
< 0 ? -1 : 1, 'wrap' )
753 * Handle calendar change event
757 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onCalendarChange = function () {
758 var curDate
= this.getValueAsDate(),
759 newDate
= this.calendar
.getSelected()[ 0 ];
762 if ( !curDate
|| newDate
.getTime() !== curDate
.getTime() ) {
763 this.setValue( newDate
);
772 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getInputElement = function () {
773 return $( '<input>' ).attr( 'type', 'hidden' );
779 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setDisabled = function ( disabled
) {
780 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
782 // Flag all our fields as disabled
783 if ( this.$fields
) {
784 this.$fields
.find( 'input' ).prop( 'disabled', this.isDisabled() );
785 this.$fields
.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
788 if ( this.clearButton
) {
789 this.clearButton
.setDisabled( disabled
);
798 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.focus = function () {
799 if ( !this.$fields
.find( document
.activeElement
).length
) {
800 this.$fields
.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
808 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.blur = function () {
809 this.$fields
.find( document
.activeElement
).blur();
816 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.simulateLabelClick = function () {
820 }( jQuery
, mediaWiki
) );