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.15 ) + '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 $.each( spec
.intercalarySize
, reduceFunc
);
378 $field
= $( '<input>' ).attr( 'type', 'text' )
380 tabindex
: disabled
? -1 : 0,
386 placeholder
: placeholder
390 keydown
: this.onFieldKeyDown
.bind( this, $field
),
391 click
: this.onFieldClick
.bind( this, $field
),
392 focus
: this.onFieldFocus
.bind( this, $field
),
393 blur
: this.onFieldBlur
.bind( this, $field
),
394 change
: this.onFieldChange
.bind( this, $field
),
395 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
398 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
400 $field
= $( '<span>' )
402 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
403 if ( spec
.type
!== 'static' ) {
404 $field
.prop( 'tabIndex', -1 );
405 $field
.on( 'focus', this.onFieldFocus
.bind( this, $field
) );
407 if ( spec
.type
=== 'static' ) {
408 $field
.text( spec
.value
);
410 $field
.val
= spanValFunc
;
414 this.fields
.push( $field
);
416 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
417 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec
)
418 .appendTo( this.$fields
);
421 if ( this.clearable
) {
422 this.clearButton
= new OO
.ui
.ButtonWidget( {
423 classes
: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
428 click
: 'onClearClick'
430 this.$fields
.append( this.clearButton
.$element
);
433 this.updateFieldsFromValue();
437 * Update the UI fields from the current value
441 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateFieldsFromValue = function () {
442 var i
, $field
, spec
, intercalary
, sz
,
443 date
= this.getValueAsDate();
445 if ( date
=== null ) {
446 this.components
= null;
448 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
449 $field
= this.fields
[ i
];
450 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
453 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
456 if ( spec
.intercalarySize
) {
457 if ( spec
.type
=== 'number' ) {
458 // Numbers ''should'' be the same width. But we need some extra for
460 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
462 // Add a little for padding
463 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
468 this.setFlags( { invalid
: this.required
} );
470 this.components
= this.formatter
.getComponentsFromDate( date
);
471 intercalary
= this.components
.intercalary
;
473 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
474 $field
= this.fields
[ i
];
475 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
476 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
477 if ( spec
.type
!== 'static' ) {
478 $field
.val( spec
.formatValue( this.components
[ spec
.component
] ) );
480 if ( spec
.intercalarySize
) {
481 if ( intercalary
&& spec
.intercalarySize
[ intercalary
] !== undefined ) {
482 sz
= spec
.intercalarySize
[ intercalary
];
486 $field
.toggleClass( 'oo-ui-element-hidden', sz
<= 0 );
487 if ( spec
.type
=== 'number' ) {
488 // Numbers ''should'' be the same width. But we need some extra for
490 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
492 // Add a little for padding
493 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
498 this.setFlags( { invalid
: date
< this.min
|| date
> this.max
} );
501 this.$element
.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date
=== null );
505 * Update the value with data from the UI fields
509 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateValueFromFields = function () {
510 var i
, v
, $field
, spec
, curDate
, newDate
,
516 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
517 $field
= this.fields
[ i
];
518 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
519 if ( spec
.editable
) {
520 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
523 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
527 v
= spec
.parseValue( v
);
528 if ( v
=== undefined ) {
529 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
532 components
[ spec
.component
] = v
;
539 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
540 this.fields
[ i
].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
542 } else if ( anyEmpty
) {
547 curDate
= this.getValueAsDate();
548 newDate
= this.formatter
.getDateFromComponents( components
);
549 if ( !curDate
|| !newDate
|| curDate
.getTime() !== newDate
.getTime() ) {
550 this.setValue( newDate
);
556 * Handle change event
560 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onChange = function () {
563 this.updateFieldsFromValue();
565 if ( this.calendar
) {
566 date
= this.getValueAsDate();
567 this.calendar
.setSelected( date
);
569 this.calendar
.setFocusedDate( date
);
575 * Handle clear button click event
579 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onClearClick = function () {
585 * Handle click on the widget background
588 * @param {jQuery.Event} e Click event
590 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onHandleClick = function () {
595 * Handle key down events on our field inputs.
598 * @param {jQuery} $field
599 * @param {jQuery.Event} e Key down event
600 * @return {boolean} False to cancel the default event
602 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldKeyDown = function ( $field
, e
) {
603 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
605 if ( !this.isDisabled() ) {
607 case OO
.ui
.Keys
.ENTER
:
608 case OO
.ui
.Keys
.SPACE
:
609 if ( spec
.type
=== 'boolean' ) {
611 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
614 } else if ( spec
.type
=== 'toggleLocal' ) {
615 this.formatter
.toggleLocal();
620 case OO
.ui
.Keys
.DOWN
:
621 if ( spec
.type
=== 'toggleLocal' ) {
622 this.formatter
.toggleLocal();
625 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
,
626 e
.keyCode
=== OO
.ui
.Keys
.UP
? -1 : 1, 'wrap' )
629 if ( $field
.is( ':input' ) ) {
638 * Handle focus events on our field inputs.
641 * @param {jQuery} $field
642 * @param {jQuery.Event} e Focus event
644 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldFocus = function ( $field
) {
645 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
647 if ( !this.isDisabled() ) {
648 if ( this.getValueAsDate() === null ) {
649 this.setValue( this.formatter
.getDefaultDate() );
651 if ( $field
.is( ':input' ) ) {
655 if ( this.calendar
) {
656 this.calendar
.toggle( !!spec
.calendarComponent
);
662 * Handle click events on our field inputs.
665 * @param {jQuery} $field
666 * @param {jQuery.Event} e Click event
668 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldClick = function ( $field
) {
669 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
671 if ( !this.isDisabled() ) {
672 if ( spec
.type
=== 'boolean' ) {
674 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
676 } else if ( spec
.type
=== 'toggleLocal' ) {
677 this.formatter
.toggleLocal();
683 * Handle blur events on our field inputs.
686 * @param {jQuery} $field
687 * @param {jQuery.Event} e Blur event
689 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldBlur = function ( $field
) {
691 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
693 this.updateValueFromFields();
696 date
= this.getValueAsDate();
700 v
= spec
.formatValue( this.formatter
.getComponentsFromDate( date
)[ spec
.component
] );
701 if ( v
!== $field
.val() ) {
708 * Handle change events on our field inputs.
711 * @param {jQuery} $field
712 * @param {jQuery.Event} e Change event
714 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldChange = function () {
715 this.updateValueFromFields();
719 * Handle wheel events on our field inputs.
722 * @param {jQuery} $field
723 * @param {jQuery.Event} e Change event
724 * @return {boolean} False to cancel the default event
726 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldWheel = function ( $field
, e
) {
728 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
730 if ( this.isDisabled() || !this.getFocusedField().length
) {
734 // Standard 'wheel' event
735 if ( e
.originalEvent
.deltaMode
!== undefined ) {
736 this.sawWheelEvent
= true;
738 if ( e
.originalEvent
.deltaY
) {
739 delta
= -e
.originalEvent
.deltaY
;
740 } else if ( e
.originalEvent
.deltaX
) {
741 delta
= e
.originalEvent
.deltaX
;
744 // Non-standard events
745 if ( !this.sawWheelEvent
) {
746 if ( e
.originalEvent
.wheelDeltaX
) {
747 delta
= -e
.originalEvent
.wheelDeltaX
;
748 } else if ( e
.originalEvent
.wheelDeltaY
) {
749 delta
= e
.originalEvent
.wheelDeltaY
;
750 } else if ( e
.originalEvent
.wheelDelta
) {
751 delta
= e
.originalEvent
.wheelDelta
;
752 } else if ( e
.originalEvent
.detail
) {
753 delta
= -e
.originalEvent
.detail
;
757 if ( delta
&& spec
) {
758 if ( spec
.type
=== 'toggleLocal' ) {
759 this.formatter
.toggleLocal();
762 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, delta
< 0 ? -1 : 1, 'wrap' )
770 * Handle calendar change event
774 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onCalendarChange = function () {
775 var curDate
= this.getValueAsDate(),
776 newDate
= this.calendar
.getSelected()[ 0 ];
779 if ( !curDate
|| newDate
.getTime() !== curDate
.getTime() ) {
780 this.setValue( newDate
);
789 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getInputElement = function () {
790 return $( '<input>' ).attr( 'type', 'hidden' );
796 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setDisabled = function ( disabled
) {
797 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
799 // Flag all our fields as disabled
800 if ( this.$fields
) {
801 this.$fields
.find( 'input' ).prop( 'disabled', this.isDisabled() );
802 this.$fields
.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
805 if ( this.clearButton
) {
806 this.clearButton
.setDisabled( disabled
);
815 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.focus = function () {
816 if ( !this.getFocusedField().length
) {
817 this.$fields
.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
825 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.blur = function () {
826 this.getFocusedField().blur();
833 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.simulateLabelClick = function () {
837 }( jQuery
, mediaWiki
) );