2 * MediaWiki Widgets – DateInputWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
11 * @classdesc Date input widget.
14 * // Date input widget showcase
15 * let fieldset = new OO.ui.FieldsetLayout( {
17 * new OO.ui.FieldLayout(
18 * new mw.widgets.DateInputWidget(),
21 * label: 'Select date'
24 * new OO.ui.FieldLayout(
25 * new mw.widgets.DateInputWidget( { precision: 'month' } ),
28 * label: 'Select month'
31 * new OO.ui.FieldLayout(
32 * new mw.widgets.DateInputWidget( {
33 * inputFormat: 'DD.MM.YYYY',
34 * displayFormat: 'Do [of] MMMM [anno Domini] YYYY'
38 * label: 'Select date (custom formats)'
43 * $( document.body ).append( fieldset.$element );
45 * // The value is stored in 'YYYY-MM-DD' or 'YYYY-MM' format
48 * // Accessing values in a date input widget
49 * let dateInput = new mw.widgets.DateInputWidget();
50 * let $label = $( '<p>' );
51 * $( document.body ).append( $label, dateInput.$element );
52 * dateInput.on( 'change', function () {
53 * // The value will always be a valid date or empty string, malformed input is ignored
54 * let date = dateInput.getValue();
55 * $label.text( 'Selected date: ' + ( date || '(none)' ) );
59 * @extends OO.ui.TextInputWidget
60 * @mixes OO.ui.mixin.IndicatorElement
63 * @description Create an mw.widgets.DateInputWidget object.
64 * @param {Object} [config] Configuration options
65 * @param {string} [config.precision='day'] Date precision to use, 'day' or 'month'
66 * @param {string} [config.value] Day or month date (depending on `precision`), in the format 'YYYY-MM-DD'
67 * or 'YYYY-MM'. If not given or empty string, no date is selected.
68 * @param {string} [config.inputFormat] Date format string to use for the textual input field. Displayed
69 * while the widget is active, and the user can type in a date in this format. Should be short
70 * and easy to type. When not given, defaults to 'YYYY-MM-DD' or 'YYYY-MM', depending on
72 * @param {string} [config.displayFormat] Date format string to use for the clickable label. Displayed
73 * while the widget is inactive. Should be as unambiguous as possible (for example, prefer to
74 * spell out the month, rather than rely on the order), even if that makes it longer. When not
75 * given, the default is language-specific.
76 * @param {boolean} [config.longDisplayFormat=false] If a custom displayFormat is not specified, use
77 * unabbreviated day of the week and month names in the default language-specific displayFormat.
78 * @param {string} [config.placeholderLabel=No date selected] Placeholder text shown when the widget is not
79 * selected. Default text taken from message `mw-widgets-dateinput-no-date`.
80 * @param {string} [config.placeholderDateFormat] User-visible date format string displayed in the textual input
81 * field when it's empty. Should be the same as `inputFormat`, but translated to the user's
82 * language. When not given, defaults to a translated version of 'YYYY-MM-DD' or 'YYYY-MM',
83 * depending on `precision`.
84 * @param {boolean} [config.required=false] Mark the field as required. Implies `indicator: 'required'`.
85 * @param {string} [config.mustBeAfter] Validates the date to be after this. In the 'YYYY-MM-DD' format.
86 * @param {string} [config.mustBeBefore] Validates the date to be before this. In the 'YYYY-MM-DD' format.
87 * @param {jQuery} [config.$overlay] Render the calendar into a separate layer. This configuration is
88 * useful in cases where the expanded calendar is larger than its container. The specified
89 * overlay layer is usually on top of the container and has a larger area. By default, the
90 * calendar uses relative positioning.
91 * @param {Object} [config.calendar] Configuration options for the this input's
92 * {@link mw.widgets.CalendarWidget CalendarWidget}.
94 mw.widgets.DateInputWidget = function MWWDateInputWidget( config ) {
95 // Config initialization
96 config = Object.assign( {
98 longDisplayFormat: false,
100 placeholderLabel: mw.msg( 'mw-widgets-dateinput-no-date' )
102 if ( config.required ) {
103 if ( config.indicator === undefined ) {
104 config.indicator = 'required';
108 let placeholderDateFormat;
109 if ( config.placeholderDateFormat ) {
110 placeholderDateFormat = config.placeholderDateFormat;
111 } else if ( config.inputFormat ) {
112 // We have no way to display a translated placeholder for custom formats
113 placeholderDateFormat = '';
115 // The following messages are used here:
116 // * mw-widgets-dateinput-placeholder-day
117 // * mw-widgets-dateinput-placeholder-month
118 placeholderDateFormat = mw.msg( 'mw-widgets-dateinput-placeholder-' + config.precision );
121 // Properties (must be set before parent constructor, which calls #setValue)
122 this.$handle = $( '<div>' );
123 this.innerLabel = new OO.ui.LabelWidget();
124 this.textInput = new OO.ui.TextInputWidget( {
125 required: config.required,
126 placeholder: placeholderDateFormat,
127 validate: this.validateDate.bind( this )
129 this.calendar = new mw.widgets.CalendarWidget( Object.assign( {
130 lazyInitOnToggle: true,
131 // Can't pass `$floatableContainer: this.$element` here, the latter is not set yet.
132 // Instead we call setFloatableContainer() below.
133 precision: config.precision
134 }, config.calendar ) );
136 this.inTextInput = 0;
137 this.closing = false;
138 this.inputFormat = config.inputFormat;
139 this.displayFormat = config.displayFormat;
140 this.longDisplayFormat = config.longDisplayFormat;
141 this.required = config.required;
142 this.placeholderLabel = config.placeholderLabel;
143 // Validate and set min and max dates as properties
145 if ( config.mustBeAfter !== undefined ) {
146 const mustBeAfter = moment( config.mustBeAfter, 'YYYY-MM-DD' );
147 if ( mustBeAfter.isValid() ) {
148 this.mustBeAfter = mustBeAfter;
151 if ( config.mustBeBefore !== undefined ) {
152 const mustBeBefore = moment( config.mustBeBefore, 'YYYY-MM-DD' );
153 if ( mustBeBefore.isValid() ) {
154 this.mustBeBefore = mustBeBefore;
157 // Parent constructor
158 mw.widgets.DateInputWidget.super.call( this, config );
160 // Mixin constructors
161 OO.ui.mixin.IndicatorElement.call( this, config );
164 this.calendar.connect( this, {
165 change: 'onCalendarChange'
167 this.textInput.connect( this, {
169 change: 'onTextInputChange'
172 focusout: this.onBlur.bind( this )
174 this.calendar.$element.on( {
175 focusout: this.onBlur.bind( this ),
176 click: this.onCalendarClick.bind( this ),
177 keypress: this.onCalendarKeyPress.bind( this )
180 click: this.onClick.bind( this ),
181 keypress: this.onKeyPress.bind( this ),
182 focus: this.onFocus.bind( this )
186 // Move 'tabindex' from this.$input (which is invisible) to the visible handle
187 if ( !this.isDisabled() ) {
188 this.setTabIndexedElement( this.$handle );
191 .append( this.innerLabel.$element, this.$indicator )
192 .addClass( 'mw-widget-dateInputWidget-handle' );
193 this.calendar.$element
194 .addClass( 'mw-widget-dateInputWidget-calendar' );
196 .addClass( 'mw-widget-dateInputWidget' )
197 .append( this.$handle, this.textInput.$element, this.calendar.$element );
199 const $overlay = config.$overlay === true ? OO.ui.getDefaultOverlay() : config.$overlay;
202 this.calendar.setFloatableContainer( this.$element );
203 $overlay.append( this.calendar.$element );
205 // The text input and calendar are not in DOM order, so fix up focus transitions.
206 this.textInput.$input.on( 'keydown', ( e ) => {
207 if ( e.which === OO.ui.Keys.TAB ) {
209 // Tabbing backward from text input: normal browser behavior
211 // Tabbing forward from text input: just focus the calendar
212 this.calendar.$element.trigger( 'focus' );
217 this.calendar.$element.on( 'keydown', ( e ) => {
218 if ( e.which === OO.ui.Keys.TAB ) {
220 // Tabbing backward from calendar: just focus the text input
221 this.textInput.$input.trigger( 'focus' );
224 // Tabbing forward from calendar: focus the text input, then allow normal browser
225 // behavior to move focus to next focusable after it
226 this.textInput.$input.trigger( 'focus' );
232 // Set handle label and hide stuff
234 this.textInput.toggle( false );
235 this.calendar.toggle( false );
237 // See InputWidget#reusePreInfuseDOM about config.$input
238 if ( config.$input ) {
239 // Hide unused <input> from PHP after infusion is done
240 config.$input.addClass( 'oo-ui-element-hidden' );
246 OO.inheritClass( mw.widgets.DateInputWidget, OO.ui.TextInputWidget );
247 OO.mixinClass( mw.widgets.DateInputWidget, OO.ui.mixin.IndicatorElement );
252 * Fired when the widget is deactivated (when the calendar is closed). This can happen because
253 * the user selected a value, or because the user blurred the widget.
255 * @event mw.widgets.DateInputWidget.deactivate
256 * @param {boolean} userSelected Whether the deactivation happened because the user selected a value
264 mw.widgets.DateInputWidget.static.reusePreInfuseDOM = function ( node, config ) {
265 config = mw.widgets.DateInputWidget.super.static.reusePreInfuseDOM( node, config );
266 if ( config.$input ) {
267 // Ignore the extra field from PendingTextInputWidget (T382344)
268 config.$input = config.$input.first();
279 mw.widgets.DateInputWidget.prototype.getInputElement = function () {
280 return $( '<input>' ).attr( 'type', 'hidden' );
284 * Respond to calendar date change events.
288 mw.widgets.DateInputWidget.prototype.onCalendarChange = function () {
290 if ( !this.inTextInput ) {
291 // If this is caused by user typing in the input field, do not set anything.
292 // The value may be invalid (see #onTextInputChange), but displayable on the calendar.
293 this.setValue( this.calendar.getDate() );
299 * Respond to text input value change events.
303 mw.widgets.DateInputWidget.prototype.onTextInputChange = function () {
305 value = this.textInput.getValue(),
306 mom = moment( value, this.getInputFormat() ),
307 valid = this.isValidDate( value );
310 if ( value === '' ) {
313 } else if ( valid ) {
314 // Well-formed date value, parse and set it
315 // Use English locale to avoid number formatting
316 this.setValue( mom.locale( 'en' ).format( this.getInternalFormat() ) );
318 // Not well-formed, but possibly partial? Try updating the calendar, but do not set the
319 // internal value. Generally this only makes sense when 'inputFormat' is little-endian (e.g.
320 // 'YYYY-MM-DD'), but that's hard to check for, and might be difficult to handle the parsing
321 // right for weird formats. So limit this trick to only when we're using the default
322 // 'inputFormat', which is the same as the internal format, 'YYYY-MM-DD'.
323 if ( this.getInputFormat() === this.getInternalFormat() ) {
324 this.calendar.setMoment( mom );
334 mw.widgets.DateInputWidget.prototype.setValue = function ( value ) {
335 const oldValue = this.value;
337 if ( !moment( value, this.getInternalFormat() ).isValid() ) {
341 mw.widgets.DateInputWidget.super.prototype.setValue.call( this, value );
343 if ( this.value !== oldValue ) {
345 this.setValidityFlag();
352 * Handle text input and calendar blur events.
356 mw.widgets.DateInputWidget.prototype.onBlur = function () {
358 const $focussed = $( ':focus' );
359 // Deactivate unless the focus moved to something else inside this widget
361 !OO.ui.contains( this.$element[ 0 ], $focussed[ 0 ], true ) &&
362 // Calendar might be in an $overlay
363 !OO.ui.contains( this.calendar.$element[ 0 ], $focussed[ 0 ], true )
373 mw.widgets.DateInputWidget.prototype.focus = function () {
381 mw.widgets.DateInputWidget.prototype.blur = function () {
387 * Update the contents of the label, text input and status of calendar to reflect selected value.
391 mw.widgets.DateInputWidget.prototype.updateUI = function () {
392 if ( this.getValue() === '' ) {
393 this.textInput.setValue( '' );
394 this.calendar.setDate( null );
395 this.innerLabel.setLabel( this.placeholderLabel );
396 this.$element.addClass( 'mw-widget-dateInputWidget-empty' );
398 const moment = this.getMoment();
399 if ( !this.inTextInput ) {
400 this.textInput.setValue( moment.format( this.getInputFormat() ) );
402 if ( !this.inCalendar ) {
403 this.calendar.setDate( this.getValue() );
405 this.innerLabel.setLabel( moment.format( this.getDisplayFormat() ) );
406 this.$element.removeClass( 'mw-widget-dateInputWidget-empty' );
411 * Deactivate this input field for data entry. Closes the calendar and hides the text field.
414 * @param {boolean} [userSelected] Whether we are deactivating because the user selected a value
416 mw.widgets.DateInputWidget.prototype.deactivate = function ( userSelected ) {
417 this.$element.removeClass( 'mw-widget-dateInputWidget-active' );
419 this.textInput.toggle( false );
420 this.calendar.toggle( false );
421 this.setValidityFlag();
423 if ( userSelected ) {
424 // Prevent focusing the handle from reopening the calendar
426 this.$handle.trigger( 'focus' );
427 this.closing = false;
430 this.emit( 'deactivate', !!userSelected );
434 * Activate this input field for data entry. Opens the calendar and shows the text field.
438 mw.widgets.DateInputWidget.prototype.activate = function () {
439 this.calendar.resetUI();
440 this.$element.addClass( 'mw-widget-dateInputWidget-active' );
442 this.textInput.toggle( true );
443 this.calendar.toggle( true );
445 this.textInput.$input.trigger( 'focus' );
449 * Get the date format to be used for handle label when the input is inactive.
452 * @return {string} Format string
454 mw.widgets.DateInputWidget.prototype.getDisplayFormat = function () {
455 if ( this.displayFormat !== undefined ) {
456 return this.displayFormat;
459 if ( this.calendar.getPrecision() === 'month' ) {
462 // The formats Moment.js provides:
463 // * ll: Month name, day of month, year
464 // * lll: Month name, day of month, year, time
465 // * llll: Month name, day of month, day of week, year, time
467 // The format we want:
468 // * ????: Month name, day of month, day of week, year
470 // We try to construct it as 'llll - (lll - ll)' and hope for the best.
471 // This seems to work well for many languages (maybe even all?).
473 const localeData = moment.localeData( moment.locale() );
474 const llll = localeData.longDateFormat( 'llll' );
475 const lll = localeData.longDateFormat( 'lll' );
476 const ll = localeData.longDateFormat( 'll' );
477 let format = llll.replace( lll.replace( ll, '' ), '' );
479 if ( this.longDisplayFormat ) {
480 // Replace MMM to MMMM and ddd to dddd but don't change MMMM and dddd
481 format = format.replace( /MMMM?/, 'MMMM' ).replace( /dddd?/, 'dddd' );
489 * Get the date format to be used for the text field when the input is active.
492 * @return {string} Format string
494 mw.widgets.DateInputWidget.prototype.getInputFormat = function () {
495 if ( this.inputFormat !== undefined ) {
496 return this.inputFormat;
502 }[ this.calendar.getPrecision() ];
506 * Get the date format to be used internally for the value. This is not configurable in any way,
507 * and always either 'YYYY-MM-DD' or 'YYYY-MM'.
510 * @return {string} Format string
512 mw.widgets.DateInputWidget.prototype.getInternalFormat = function () {
516 }[ this.calendar.getPrecision() ];
520 * Get the Moment object for current value.
522 * @return {Object} Moment object
524 mw.widgets.DateInputWidget.prototype.getMoment = function () {
525 return moment( this.getValue(), this.getInternalFormat() );
529 * Handle mouse click events.
532 * @param {jQuery.Event} e Mouse click event
533 * @return {boolean} False to cancel the default event
535 mw.widgets.DateInputWidget.prototype.onClick = function ( e ) {
536 if ( !this.isDisabled() && !this.isReadOnly() && e.which === 1 ) {
543 * Handle key press events.
546 * @param {jQuery.Event} e Key press event
547 * @return {boolean|undefined} False to cancel the default event
549 mw.widgets.DateInputWidget.prototype.onKeyPress = function ( e ) {
550 if ( !this.isDisabled() && !this.isReadOnly() &&
551 ( e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
559 * Handle focus events.
563 mw.widgets.DateInputWidget.prototype.onFocus = function () {
564 if ( !this.isDisabled() && !this.isReadOnly() && !this.closing ) {
570 * Handle calendar key press events.
573 * @param {jQuery.Event} e Key press event
574 * @return {boolean|undefined} False to cancel the default event
576 mw.widgets.DateInputWidget.prototype.onCalendarKeyPress = function ( e ) {
577 if ( !this.isDisabled() && e.which === OO.ui.Keys.ENTER ) {
578 this.deactivate( true );
584 * Handle calendar click events.
587 * @param {jQuery.Event} e Mouse click event
588 * @return {boolean|undefined} False to cancel the default event
590 mw.widgets.DateInputWidget.prototype.onCalendarClick = function ( e ) {
591 const targetClass = this.calendar.getPrecision() === 'month' ?
592 'mw-widget-calendarWidget-month' :
593 'mw-widget-calendarWidget-day';
595 !this.isDisabled() &&
597 // eslint-disable-next-line no-jquery/no-class-state
598 $( e.target ).hasClass( targetClass )
600 this.deactivate( true );
606 * Handle text input enter events.
610 mw.widgets.DateInputWidget.prototype.onEnter = function () {
611 this.deactivate( true );
616 * @param {string} date Date string, to be valid, must be in 'YYYY-MM-DD' or 'YYYY-MM' format or
617 * (unless the field is required) empty
620 mw.widgets.DateInputWidget.prototype.validateDate = function ( date ) {
623 isValid = !this.required;
625 isValid = this.isValidDate( date ) && this.isInRange( date );
632 * @param {string} date Date string, to be valid, must be in 'YYYY-MM-DD' or 'YYYY-MM' format
635 mw.widgets.DateInputWidget.prototype.isValidDate = function ( date ) {
636 // "Half-strict mode": for example, for the format 'YYYY-MM-DD', 2015-1-3 instead of 2015-01-03
637 // is okay, but 2015-01 isn't, and neither is 2015-01-foo. Use Moment's "fuzzy" mode and check
638 // parsing flags for the details (stolen from implementation of moment#isValid).
640 mom = moment( date, this.getInputFormat() ),
641 flags = mom.parsingFlags();
643 return mom.isValid() && flags.charsLeftOver === 0 && flags.unusedTokens.length === 0;
647 * Validates if the date is within the range configured with {@link #cfg-mustBeAfter}
648 * and {@link #cfg-mustBeBefore}.
651 * @param {string} date Date string, to be valid, must be empty (no date selected) or in
652 * 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
655 mw.widgets.DateInputWidget.prototype.isInRange = function ( date ) {
656 if ( this.mustBeAfter === undefined && this.mustBeBefore === undefined ) {
659 const momentDate = moment( date, 'YYYY-MM-DD' );
660 const isAfter = ( this.mustBeAfter === undefined || momentDate.isAfter( this.mustBeAfter ) );
661 const isBefore = ( this.mustBeBefore === undefined || momentDate.isBefore( this.mustBeBefore ) );
662 return isAfter && isBefore;
666 * Get the validity of current value.
668 * This method returns a promise that resolves if the value is valid and rejects if
669 * it isn't. Uses {@link #validateDate}.
671 * @return {jQuery.Promise} A promise that resolves if the value is valid, rejects if not.
673 mw.widgets.DateInputWidget.prototype.getValidity = function () {
674 const isValid = this.validateDate( this.getValue() );
677 return $.Deferred().resolve().promise();
679 return $.Deferred().reject().promise();
684 * Sets the 'invalid' flag appropriately.
686 * @param {boolean} [isValid] Optionally override validation result
688 mw.widgets.DateInputWidget.prototype.setValidityFlag = function ( isValid ) {
689 const setFlag = ( valid ) => {
691 this.$input.attr( 'aria-invalid', 'true' );
693 this.$input.removeAttr( 'aria-invalid' );
695 this.setFlags( { invalid: !valid } );
698 if ( isValid !== undefined ) {
701 this.getValidity().then( () => {