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 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].call( this, config
);
62 OO
.ui
.mixin
.IconElement
.call( this, config
);
63 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
64 OO
.ui
.mixin
.PendingElement
.call( this, config
);
67 this.type
= config
.type
;
68 this.$handle
= $( '<span>' );
69 this.$fields
= $( '<span>' );
71 this.clearable
= !!config
.clearable
;
72 this.required
= !!config
.required
;
74 if ( typeof config
.min
=== 'string' ) {
75 config
.min
= this.parseDateValue( config
.min
);
77 if ( config
.min
instanceof Date
&& config
.min
.getTime() >= -62167219200000 ) {
78 this.min
= config
.min
;
80 this.min
= new Date( -62167219200000 ); // 0000-01-01T00:00:00.000Z
83 if ( typeof config
.max
=== 'string' ) {
84 config
.max
= this.parseDateValue( config
.max
);
86 if ( config
.max
instanceof Date
&& config
.max
.getTime() <= 253402300799999 ) {
87 this.max
= config
.max
;
89 this.max
= new Date( 253402300799999 ); // 9999-12-31T12:59:59.999Z
92 switch ( this.type
) {
94 this.min
.setUTCHours( 0, 0, 0, 0 );
95 this.max
.setUTCHours( 23, 59, 59, 999 );
98 this.min
.setUTCFullYear( 1970, 0, 1 );
99 this.max
.setUTCFullYear( 1970, 0, 1 );
102 if ( this.min
> this.max
) {
104 '"min" (' + this.min
.toISOString() + ') must not be greater than ' +
105 '"max" (' + this.max
.toISOString() + ')'
109 if ( config
.formatter
instanceof mw
.widgets
.datetime
.DateTimeFormatter
) {
110 this.formatter
= config
.formatter
;
111 } else if ( $.isPlainObject( config
.formatter
) ) {
112 this.formatter
= new mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter( config
.formatter
);
114 throw new Error( '"formatter" must be an mw.widgets.datetime.DateTimeFormatter or a plain object' );
117 if ( this.type
=== 'time' || config
.calendar
=== null ) {
118 this.calendar
= null;
120 config
.calendar
= $.extend( {}, config
.calendar
, {
121 formatter
: this.formatter
,
126 this.calendar
= new mw
.widgets
.datetime
.CalendarWidget( config
.calendar
);
131 click
: this.onHandleClick
.bind( this )
133 this.connect( this, {
136 this.formatter
.connect( this, {
139 if ( this.calendar
) {
140 this.calendar
.connect( this, {
141 change
: 'onCalendarChange'
146 this.setTabIndex( -1 );
148 this.$fields
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-fields' );
152 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-handle' )
153 .append( this.$icon
, this.$indicator
, this.$fields
);
156 .addClass( 'mw-widgets-datetime-dateTimeInputWidget' )
157 .append( this.$handle
);
159 if ( this.calendar
) {
160 this.$element
.append( this.calendar
.$element
);
166 OO
.inheritClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.InputWidget
);
167 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IconElement
);
168 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IndicatorElement
);
169 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.PendingElement
);
171 /* Static properties */
173 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'static' ].supportsSimpleLabel
= false;
180 * Convert a date string to a Date
183 * @param {string} value
184 * @return {Date|null}
186 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.parseDateValue = function ( value
) {
189 value
= String( value
);
190 switch ( this.type
) {
192 value
= value
+ 'T00:00:00Z';
195 value
= '1970-01-01T' + value
+ 'Z';
198 m
= /^(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec( value
);
201 while ( m
[ 7 ].length
< 3 ) {
208 date
.setUTCFullYear( m
[ 1 ], m
[ 2 ] - 1, m
[ 3 ] );
209 date
.setUTCHours( m
[ 4 ], m
[ 5 ], m
[ 6 ], m
[ 7 ] );
210 if ( date
.getTime() < -62167219200000 || date
.getTime() > 253402300799999 ||
211 date
.getUTCFullYear() !== +m
[ 1 ] ||
212 date
.getUTCMonth() + 1 !== +m
[ 2 ] ||
213 date
.getUTCDate() !== +m
[ 3 ] ||
214 date
.getUTCHours() !== +m
[ 4 ] ||
215 date
.getUTCMinutes() !== +m
[ 5 ] ||
216 date
.getUTCSeconds() !== +m
[ 6 ] ||
217 date
.getUTCMilliseconds() !== +m
[ 7 ]
231 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.cleanUpValue = function ( value
) {
234 if ( value
=== '' ) {
238 if ( value
instanceof Date
) {
241 date
= this.parseDateValue( value
);
244 if ( date
instanceof Date
) {
245 pad = function ( v
, l
) {
247 while ( v
.length
< l
) {
253 switch ( this.type
) {
255 value
= pad( date
.getUTCFullYear(), 4 ) +
256 '-' + pad( date
.getUTCMonth() + 1, 2 ) +
257 '-' + pad( date
.getUTCDate(), 2 );
261 value
= pad( date
.getUTCHours(), 2 ) +
262 ':' + pad( date
.getUTCMinutes(), 2 ) +
263 ':' + pad( date
.getUTCSeconds(), 2 ) +
264 '.' + pad( date
.getUTCMilliseconds(), 3 );
265 value
= value
.replace( /\.?0+$/, '' );
269 value
= date
.toISOString();
280 * Get the value of the input as a Date object
282 * @return {Date|null}
284 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getValueAsDate = function () {
285 return this.parseDateValue( this.getValue() );
289 * Set up the UI fields
293 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setupFields = function () {
294 var i
, $field
, spec
, placeholder
, sz
, maxlength
,
295 spanValFunc = function ( v
) {
296 if ( v
=== undefined ) {
297 return this.data( 'mw-widgets-datetime-dateTimeInputWidget-value' );
300 this.data( 'mw-widgets-datetime-dateTimeInputWidget-value', v
);
302 v
= this.data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder' );
308 reduceFunc = function ( k
, v
) {
309 maxlength
= Math
.max( maxlength
, v
);
311 disabled
= this.isDisabled(),
312 specs
= this.formatter
.getFieldSpec();
314 this.$fields
.empty();
315 this.clearButton
= null;
318 for ( i
= 0; i
< specs
.length
; i
++ ) {
320 if ( typeof spec
=== 'string' ) {
322 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
324 .appendTo( this.$fields
);
329 while ( placeholder
.length
< spec
.size
) {
333 if ( spec
.type
=== 'number' ) {
334 // Numbers ''should'' be the same width. But we need some extra for
336 sz
= ( spec
.size
* 1.15 ) + 'ch';
338 // Add a little for padding
339 sz
= ( spec
.size
* 1.15 ) + 'ch';
341 if ( spec
.editable
&& spec
.type
!== 'static' ) {
342 if ( spec
.type
=== 'boolean' || spec
.type
=== 'toggleLocal' ) {
343 $field
= $( '<span>' )
345 tabindex
: disabled
? -1 : 0
348 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
350 keydown
: this.onFieldKeyDown
.bind( this, $field
),
351 focus
: this.onFieldFocus
.bind( this, $field
),
352 click
: this.onFieldClick
.bind( this, $field
),
353 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
355 $field
.val
= spanValFunc
;
357 maxlength
= spec
.size
;
358 if ( spec
.intercalarySize
) {
359 $.each( spec
.intercalarySize
, reduceFunc
);
361 $field
= $( '<input type="text">' )
363 tabindex
: disabled
? -1 : 0,
369 placeholder
: placeholder
373 keydown
: this.onFieldKeyDown
.bind( this, $field
),
374 click
: this.onFieldClick
.bind( this, $field
),
375 focus
: this.onFieldFocus
.bind( this, $field
),
376 blur
: this.onFieldBlur
.bind( this, $field
),
377 change
: this.onFieldChange
.bind( this, $field
),
378 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
381 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
383 $field
= $( '<span>' )
385 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
386 if ( spec
.type
=== 'static' ) {
387 $field
.text( spec
.value
);
389 $field
.val
= spanValFunc
;
393 this.fields
.push( $field
);
395 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
396 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec
)
397 .appendTo( this.$fields
);
400 if ( this.clearable
) {
401 this.clearButton
= new OO
.ui
.ButtonWidget( {
402 classes
: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
407 click
: 'onClearClick'
409 this.$fields
.append( this.clearButton
.$element
);
412 this.updateFieldsFromValue();
416 * Update the UI fields from the current value
420 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateFieldsFromValue = function () {
421 var i
, $field
, spec
, intercalary
, sz
,
422 date
= this.getValueAsDate();
424 if ( date
=== null ) {
425 this.components
= null;
427 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
428 $field
= this.fields
[ i
];
429 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
432 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
435 if ( spec
.intercalarySize
) {
436 if ( spec
.type
=== 'number' ) {
437 // Numbers ''should'' be the same width. But we need some extra for
439 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
441 // Add a little for padding
442 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
447 this.setFlags( { invalid
: this.required
} );
449 this.components
= this.formatter
.getComponentsFromDate( date
);
450 intercalary
= this.components
.intercalary
;
452 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
453 $field
= this.fields
[ i
];
454 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
455 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
456 if ( spec
.type
!== 'static' ) {
457 $field
.val( spec
.formatValue( this.components
[ spec
.component
] ) );
459 if ( spec
.intercalarySize
) {
460 if ( intercalary
&& spec
.intercalarySize
[ intercalary
] !== undefined ) {
461 sz
= spec
.intercalarySize
[ intercalary
];
465 $field
.toggleClass( 'oo-ui-element-hidden', sz
<= 0 );
466 if ( spec
.type
=== 'number' ) {
467 // Numbers ''should'' be the same width. But we need some extra for
469 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
471 // Add a little for padding
472 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
477 this.setFlags( { invalid
: date
< this.min
|| date
> this.max
} );
480 this.$element
.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date
=== null );
484 * Update the value with data from the UI fields
488 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateValueFromFields = function () {
489 var i
, v
, $field
, spec
, curDate
, newDate
,
495 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
496 $field
= this.fields
[ i
];
497 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
498 if ( spec
.editable
) {
499 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
502 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
506 v
= spec
.parseValue( v
);
507 if ( v
=== undefined ) {
508 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
511 components
[ spec
.component
] = v
;
518 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
519 this.fields
[ i
].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
521 } else if ( anyEmpty
) {
526 curDate
= this.getValueAsDate();
527 newDate
= this.formatter
.getDateFromComponents( components
);
528 if ( !curDate
|| !newDate
|| curDate
.getTime() !== newDate
.getTime() ) {
529 this.setValue( newDate
);
535 * Handle change event
539 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onChange = function () {
542 this.updateFieldsFromValue();
544 if ( this.calendar
) {
545 date
= this.getValueAsDate();
546 this.calendar
.setSelected( date
);
548 this.calendar
.setFocusedDate( date
);
554 * Handle clear button click event
558 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onClearClick = function () {
564 * Handle click on the widget background
567 * @param {jQuery.Event} e Click event
569 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onHandleClick = function () {
574 * Handle key down events on our field inputs.
577 * @param {jQuery} $field
578 * @param {jQuery.Event} e Key down event
580 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldKeyDown = function ( $field
, e
) {
581 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
583 if ( !this.isDisabled() ) {
585 case OO
.ui
.Keys
.ENTER
:
586 case OO
.ui
.Keys
.SPACE
:
587 if ( spec
.type
=== 'boolean' ) {
589 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
592 } else if ( spec
.type
=== 'toggleLocal' ) {
593 this.formatter
.toggleLocal();
598 case OO
.ui
.Keys
.DOWN
:
599 if ( spec
.type
=== 'toggleLocal' ) {
600 this.formatter
.toggleLocal();
603 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
,
604 e
.keyCode
=== OO
.ui
.Keys
.UP
? -1 : 1, 'wrap' )
607 if ( $field
.is( ':input' ) ) {
616 * Handle focus events on our field inputs.
619 * @param {jQuery} $field
620 * @param {jQuery.Event} e Focus event
622 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldFocus = function ( $field
) {
623 if ( !this.isDisabled() ) {
624 if ( this.getValueAsDate() === null ) {
625 this.setValue( this.formatter
.getDefaultDate() );
627 if ( $field
.is( ':input' ) ) {
631 if ( this.calendar
) {
632 this.calendar
.toggle( true );
638 * Handle click events on our field inputs.
641 * @param {jQuery} $field
642 * @param {jQuery.Event} e Click event
644 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldClick = function ( $field
) {
645 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
647 if ( !this.isDisabled() ) {
648 if ( spec
.type
=== 'boolean' ) {
650 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
652 } else if ( spec
.type
=== 'toggleLocal' ) {
653 this.formatter
.toggleLocal();
659 * Handle blur events on our field inputs.
662 * @param {jQuery} $field
663 * @param {jQuery.Event} e Blur event
665 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldBlur = function ( $field
) {
667 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
669 this.updateValueFromFields();
672 date
= this.getValueAsDate();
676 v
= spec
.formatValue( this.formatter
.getComponentsFromDate( date
)[ spec
.component
] );
677 if ( v
!== $field
.val() ) {
684 * Handle change events on our field inputs.
687 * @param {jQuery} $field
688 * @param {jQuery.Event} e Change event
690 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldChange = function () {
691 this.updateValueFromFields();
695 * Handle wheel events on our field inputs.
698 * @param {jQuery} $field
699 * @param {jQuery.Event} e Change event
701 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldWheel = function ( $field
, e
) {
703 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
705 if ( this.isDisabled() ) {
709 // Standard 'wheel' event
710 if ( e
.originalEvent
.deltaMode
!== undefined ) {
711 this.sawWheelEvent
= true;
713 if ( e
.originalEvent
.deltaY
) {
714 delta
= -e
.originalEvent
.deltaY
;
715 } else if ( e
.originalEvent
.deltaX
) {
716 delta
= e
.originalEvent
.deltaX
;
719 // Non-standard events
720 if ( !this.sawWheelEvent
) {
721 if ( e
.originalEvent
.wheelDeltaX
) {
722 delta
= -e
.originalEvent
.wheelDeltaX
;
723 } else if ( e
.originalEvent
.wheelDeltaY
) {
724 delta
= e
.originalEvent
.wheelDeltaY
;
725 } else if ( e
.originalEvent
.wheelDelta
) {
726 delta
= e
.originalEvent
.wheelDelta
;
727 } else if ( e
.originalEvent
.detail
) {
728 delta
= -e
.originalEvent
.detail
;
732 if ( delta
&& spec
) {
733 if ( spec
.type
=== 'toggleLocal' ) {
734 this.formatter
.toggleLocal();
737 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, delta
< 0 ? -1 : 1, 'wrap' )
745 * Handle calendar change event
749 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onCalendarChange = function () {
750 var curDate
= this.getValueAsDate(),
751 newDate
= this.calendar
.getSelected()[ 0 ];
754 if ( !curDate
|| newDate
.getTime() !== curDate
.getTime() ) {
755 this.setValue( newDate
);
764 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getInputElement = function () {
765 return $( '<input type="hidden" />' );
771 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setDisabled = function ( disabled
) {
772 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
774 // Flag all our fields as disabled
775 if ( this.$fields
) {
776 this.$fields
.find( 'input' ).prop( 'disabled', this.isDisabled() );
777 this.$fields
.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
780 if ( this.clearButton
) {
781 this.clearButton
.setDisabled( disabled
);
790 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.focus = function () {
791 if ( !this.$fields
.find( document
.activeElement
).length
) {
792 this.$fields
.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
800 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.blur = function () {
801 this.$fields
.find( document
.activeElement
).blur();
808 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.simulateLabelClick = function () {
812 }( jQuery
, mediaWiki
) );