91f797dad088fdc9939d5fa2765f55a933934a41
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'
147 // Our form input *should* be type="hidden". But if we're infusing from
149 if ( this.$input
.attr( 'type' ) !== 'hidden' ) {
151 this.$input
.attr( 'type', 'hidden' );
154 // IE <= 8, and IE 9 in quirks mode, doesn't allow changing the
155 // type, so just hide the field with CSS. IE 9 in quirks mode
156 // doesn't even throw an error, so do that unconditionally. Sigh.
157 this.$input
.css( 'display', 'none' );
161 this.setTabIndex( -1 );
163 this.$fields
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-fields' );
167 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-handle' )
168 .append( this.$icon
, this.$indicator
, this.$fields
);
171 .addClass( 'mw-widgets-datetime-dateTimeInputWidget' )
172 .append( this.$handle
);
174 if ( this.calendar
) {
175 this.$element
.append( this.calendar
.$element
);
181 OO
.inheritClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.InputWidget
);
182 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IconElement
);
183 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.IndicatorElement
);
184 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeInputWidget
, OO
.ui
.mixin
.PendingElement
);
186 /* Static properties */
188 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'static' ].supportsSimpleLabel
= false;
195 * Convert a date string to a Date
198 * @param {string} value
199 * @return {Date|null}
201 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.parseDateValue = function ( value
) {
204 value
= String( value
);
205 switch ( this.type
) {
207 value
= value
+ 'T00:00:00Z';
210 value
= '1970-01-01T' + value
+ 'Z';
213 m
= /^(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec( value
);
216 while ( m
[ 7 ].length
< 3 ) {
223 date
.setUTCFullYear( m
[ 1 ], m
[ 2 ] - 1, m
[ 3 ] );
224 date
.setUTCHours( m
[ 4 ], m
[ 5 ], m
[ 6 ], m
[ 7 ] );
225 if ( date
.getTime() < -62167219200000 || date
.getTime() > 253402300799999 ||
226 date
.getUTCFullYear() !== +m
[ 1 ] ||
227 date
.getUTCMonth() + 1 !== +m
[ 2 ] ||
228 date
.getUTCDate() !== +m
[ 3 ] ||
229 date
.getUTCHours() !== +m
[ 4 ] ||
230 date
.getUTCMinutes() !== +m
[ 5 ] ||
231 date
.getUTCSeconds() !== +m
[ 6 ] ||
232 date
.getUTCMilliseconds() !== +m
[ 7 ]
246 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.cleanUpValue = function ( value
) {
249 if ( value
=== '' ) {
253 if ( value
instanceof Date
) {
256 date
= this.parseDateValue( value
);
259 if ( date
instanceof Date
) {
260 pad = function ( v
, l
) {
262 while ( v
.length
< l
) {
268 switch ( this.type
) {
270 value
= pad( date
.getUTCFullYear(), 4 ) +
271 '-' + pad( date
.getUTCMonth() + 1, 2 ) +
272 '-' + pad( date
.getUTCDate(), 2 );
276 value
= pad( date
.getUTCHours(), 2 ) +
277 ':' + pad( date
.getUTCMinutes(), 2 ) +
278 ':' + pad( date
.getUTCSeconds(), 2 ) +
279 '.' + pad( date
.getUTCMilliseconds(), 3 );
280 value
= value
.replace( /\.?0+$/, '' );
284 value
= date
.toISOString();
295 * Get the value of the input as a Date object
297 * @return {Date|null}
299 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getValueAsDate = function () {
300 return this.parseDateValue( this.getValue() );
304 * Set up the UI fields
308 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setupFields = function () {
309 var i
, $field
, spec
, placeholder
, sz
, maxlength
,
310 spanValFunc = function ( v
) {
311 if ( v
=== undefined ) {
312 return this.data( 'mw-widgets-datetime-dateTimeInputWidget-value' );
315 this.data( 'mw-widgets-datetime-dateTimeInputWidget-value', v
);
317 v
= this.data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder' );
323 reduceFunc = function ( k
, v
) {
324 maxlength
= Math
.max( maxlength
, v
);
326 disabled
= this.isDisabled(),
327 specs
= this.formatter
.getFieldSpec();
329 this.$fields
.empty();
330 this.clearButton
= null;
333 for ( i
= 0; i
< specs
.length
; i
++ ) {
335 if ( typeof spec
=== 'string' ) {
337 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
339 .appendTo( this.$fields
);
344 while ( placeholder
.length
< spec
.size
) {
348 if ( spec
.type
=== 'number' ) {
349 // Numbers ''should'' be the same width. But we need some extra for
351 sz
= ( spec
.size
* 1.15 ) + 'ch';
353 // Add a little for padding
354 sz
= ( spec
.size
* 1.15 ) + 'ch';
356 if ( spec
.editable
&& spec
.type
!== 'static' ) {
357 if ( spec
.type
=== 'boolean' || spec
.type
=== 'toggleLocal' ) {
358 $field
= $( '<span>' )
360 tabindex
: disabled
? -1 : 0
363 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
365 keydown
: this.onFieldKeyDown
.bind( this, $field
),
366 focus
: this.onFieldFocus
.bind( this, $field
),
367 click
: this.onFieldClick
.bind( this, $field
),
368 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
370 $field
.val
= spanValFunc
;
372 maxlength
= spec
.size
;
373 if ( spec
.intercalarySize
) {
374 $.each( spec
.intercalarySize
, reduceFunc
);
376 $field
= $( '<input>' ).attr( 'type', 'text' )
378 tabindex
: disabled
? -1 : 0,
384 placeholder
: placeholder
388 keydown
: this.onFieldKeyDown
.bind( this, $field
),
389 click
: this.onFieldClick
.bind( this, $field
),
390 focus
: this.onFieldFocus
.bind( this, $field
),
391 blur
: this.onFieldBlur
.bind( this, $field
),
392 change
: this.onFieldChange
.bind( this, $field
),
393 'wheel mousewheel DOMMouseScroll': this.onFieldWheel
.bind( this, $field
)
396 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-editField' );
398 $field
= $( '<span>' )
400 .data( 'mw-widgets-datetime-dateTimeInputWidget-placeholder', placeholder
);
401 if ( spec
.type
=== 'static' ) {
402 $field
.text( spec
.value
);
404 $field
.val
= spanValFunc
;
408 this.fields
.push( $field
);
410 .addClass( 'mw-widgets-datetime-dateTimeInputWidget-field' )
411 .data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec', spec
)
412 .appendTo( this.$fields
);
415 if ( this.clearable
) {
416 this.clearButton
= new OO
.ui
.ButtonWidget( {
417 classes
: [ 'mw-widgets-datetime-dateTimeInputWidget-field', 'mw-widgets-datetime-dateTimeInputWidget-clearButton' ],
422 click
: 'onClearClick'
424 this.$fields
.append( this.clearButton
.$element
);
427 this.updateFieldsFromValue();
431 * Update the UI fields from the current value
435 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateFieldsFromValue = function () {
436 var i
, $field
, spec
, intercalary
, sz
,
437 date
= this.getValueAsDate();
439 if ( date
=== null ) {
440 this.components
= null;
442 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
443 $field
= this.fields
[ i
];
444 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
447 .removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid oo-ui-element-hidden' )
450 if ( spec
.intercalarySize
) {
451 if ( spec
.type
=== 'number' ) {
452 // Numbers ''should'' be the same width. But we need some extra for
454 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
456 // Add a little for padding
457 $field
.width( ( spec
.size
* 1.15 ) + 'ch' );
462 this.setFlags( { invalid
: this.required
} );
464 this.components
= this.formatter
.getComponentsFromDate( date
);
465 intercalary
= this.components
.intercalary
;
467 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
468 $field
= this.fields
[ i
];
469 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
470 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
471 if ( spec
.type
!== 'static' ) {
472 $field
.val( spec
.formatValue( this.components
[ spec
.component
] ) );
474 if ( spec
.intercalarySize
) {
475 if ( intercalary
&& spec
.intercalarySize
[ intercalary
] !== undefined ) {
476 sz
= spec
.intercalarySize
[ intercalary
];
480 $field
.toggleClass( 'oo-ui-element-hidden', sz
<= 0 );
481 if ( spec
.type
=== 'number' ) {
482 // Numbers ''should'' be the same width. But we need some extra for
484 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
486 // Add a little for padding
487 this.fields
[ i
].width( ( sz
* 1.15 ) + 'ch' );
492 this.setFlags( { invalid
: date
< this.min
|| date
> this.max
} );
495 this.$element
.toggleClass( 'mw-widgets-datetime-dateTimeInputWidget-empty', date
=== null );
499 * Update the value with data from the UI fields
503 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.updateValueFromFields = function () {
504 var i
, v
, $field
, spec
, curDate
, newDate
,
510 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
511 $field
= this.fields
[ i
];
512 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
513 if ( spec
.editable
) {
514 $field
.removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
517 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
521 v
= spec
.parseValue( v
);
522 if ( v
=== undefined ) {
523 $field
.addClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
526 components
[ spec
.component
] = v
;
533 for ( i
= 0; i
< this.fields
.length
; i
++ ) {
534 this.fields
[ i
].removeClass( 'mw-widgets-datetime-dateTimeInputWidget-invalid' );
536 } else if ( anyEmpty
) {
541 curDate
= this.getValueAsDate();
542 newDate
= this.formatter
.getDateFromComponents( components
);
543 if ( !curDate
|| !newDate
|| curDate
.getTime() !== newDate
.getTime() ) {
544 this.setValue( newDate
);
550 * Handle change event
554 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onChange = function () {
557 this.updateFieldsFromValue();
559 if ( this.calendar
) {
560 date
= this.getValueAsDate();
561 this.calendar
.setSelected( date
);
563 this.calendar
.setFocusedDate( date
);
569 * Handle clear button click event
573 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onClearClick = function () {
579 * Handle click on the widget background
582 * @param {jQuery.Event} e Click event
584 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onHandleClick = function () {
589 * Handle key down events on our field inputs.
592 * @param {jQuery} $field
593 * @param {jQuery.Event} e Key down event
595 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldKeyDown = function ( $field
, e
) {
596 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
598 if ( !this.isDisabled() ) {
600 case OO
.ui
.Keys
.ENTER
:
601 case OO
.ui
.Keys
.SPACE
:
602 if ( spec
.type
=== 'boolean' ) {
604 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
607 } else if ( spec
.type
=== 'toggleLocal' ) {
608 this.formatter
.toggleLocal();
613 case OO
.ui
.Keys
.DOWN
:
614 if ( spec
.type
=== 'toggleLocal' ) {
615 this.formatter
.toggleLocal();
618 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
,
619 e
.keyCode
=== OO
.ui
.Keys
.UP
? -1 : 1, 'wrap' )
622 if ( $field
.is( ':input' ) ) {
631 * Handle focus events on our field inputs.
634 * @param {jQuery} $field
635 * @param {jQuery.Event} e Focus event
637 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldFocus = function ( $field
) {
638 if ( !this.isDisabled() ) {
639 if ( this.getValueAsDate() === null ) {
640 this.setValue( this.formatter
.getDefaultDate() );
642 if ( $field
.is( ':input' ) ) {
646 if ( this.calendar
) {
647 this.calendar
.toggle( true );
653 * Handle click events on our field inputs.
656 * @param {jQuery} $field
657 * @param {jQuery.Event} e Click event
659 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldClick = function ( $field
) {
660 var spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
662 if ( !this.isDisabled() ) {
663 if ( spec
.type
=== 'boolean' ) {
665 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, 1, 'wrap' )
667 } else if ( spec
.type
=== 'toggleLocal' ) {
668 this.formatter
.toggleLocal();
674 * Handle blur events on our field inputs.
677 * @param {jQuery} $field
678 * @param {jQuery.Event} e Blur event
680 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldBlur = function ( $field
) {
682 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
684 this.updateValueFromFields();
687 date
= this.getValueAsDate();
691 v
= spec
.formatValue( this.formatter
.getComponentsFromDate( date
)[ spec
.component
] );
692 if ( v
!== $field
.val() ) {
699 * Handle change events on our field inputs.
702 * @param {jQuery} $field
703 * @param {jQuery.Event} e Change event
705 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldChange = function () {
706 this.updateValueFromFields();
710 * Handle wheel events on our field inputs.
713 * @param {jQuery} $field
714 * @param {jQuery.Event} e Change event
716 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onFieldWheel = function ( $field
, e
) {
718 spec
= $field
.data( 'mw-widgets-datetime-dateTimeInputWidget-fieldSpec' );
720 if ( this.isDisabled() ) {
724 // Standard 'wheel' event
725 if ( e
.originalEvent
.deltaMode
!== undefined ) {
726 this.sawWheelEvent
= true;
728 if ( e
.originalEvent
.deltaY
) {
729 delta
= -e
.originalEvent
.deltaY
;
730 } else if ( e
.originalEvent
.deltaX
) {
731 delta
= e
.originalEvent
.deltaX
;
734 // Non-standard events
735 if ( !this.sawWheelEvent
) {
736 if ( e
.originalEvent
.wheelDeltaX
) {
737 delta
= -e
.originalEvent
.wheelDeltaX
;
738 } else if ( e
.originalEvent
.wheelDeltaY
) {
739 delta
= e
.originalEvent
.wheelDeltaY
;
740 } else if ( e
.originalEvent
.wheelDelta
) {
741 delta
= e
.originalEvent
.wheelDelta
;
742 } else if ( e
.originalEvent
.detail
) {
743 delta
= -e
.originalEvent
.detail
;
747 if ( delta
&& spec
) {
748 if ( spec
.type
=== 'toggleLocal' ) {
749 this.formatter
.toggleLocal();
752 this.formatter
.adjustComponent( this.getValueAsDate(), spec
.component
, delta
< 0 ? -1 : 1, 'wrap' )
760 * Handle calendar change event
764 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.onCalendarChange = function () {
765 var curDate
= this.getValueAsDate(),
766 newDate
= this.calendar
.getSelected()[ 0 ];
769 if ( !curDate
|| newDate
.getTime() !== curDate
.getTime() ) {
770 this.setValue( newDate
);
779 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.getInputElement = function () {
780 return $( '<input>' ).attr( 'type', 'hidden' );
786 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.setDisabled = function ( disabled
) {
787 mw
.widgets
.datetime
.DateTimeInputWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
789 // Flag all our fields as disabled
790 if ( this.$fields
) {
791 this.$fields
.find( 'input' ).prop( 'disabled', this.isDisabled() );
792 this.$fields
.find( '[tabindex]' ).attr( 'tabindex', this.isDisabled() ? -1 : 0 );
795 if ( this.clearButton
) {
796 this.clearButton
.setDisabled( disabled
);
805 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.focus = function () {
806 if ( !this.$fields
.find( document
.activeElement
).length
) {
807 this.$fields
.find( '.mw-widgets-datetime-dateTimeInputWidget-editField' ).first().focus();
815 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.blur = function () {
816 this.$fields
.find( document
.activeElement
).blur();
823 mw
.widgets
.datetime
.DateTimeInputWidget
.prototype.simulateLabelClick = function () {
827 }( jQuery
, mediaWiki
) );