/**
* Creates an mw.widgets.DateInputWidget object.
*
+ * @example
+ * // Date input widget showcase
+ * var fieldset = new OO.ui.FieldsetLayout( {
+ * items: [
+ * new OO.ui.FieldLayout(
+ * new mw.widgets.DateInputWidget(),
+ * {
+ * align: 'top',
+ * label: 'Select date'
+ * }
+ * ),
+ * new OO.ui.FieldLayout(
+ * new mw.widgets.DateInputWidget( { precision: 'month' } ),
+ * {
+ * align: 'top',
+ * label: 'Select month'
+ * }
+ * ),
+ * new OO.ui.FieldLayout(
+ * new mw.widgets.DateInputWidget( {
+ * inputFormat: 'DD.MM.YYYY',
+ * displayFormat: 'Do [of] MMMM [anno Domini] YYYY'
+ * } ),
+ * {
+ * align: 'top',
+ * label: 'Select date (custom formats)'
+ * }
+ * )
+ * ]
+ * } );
+ * $( 'body' ).append( fieldset.$element );
+ *
+ * The value is stored in 'YYYY-MM-DD' or 'YYYY-MM' format:
+ *
+ * @example
+ * // Accessing values in a date input widget
+ * var dateInput = new mw.widgets.DateInputWidget();
+ * var $label = $( '<p>' );
+ * $( 'body' ).append( $label, dateInput.$element );
+ * dateInput.on( 'change', function () {
+ * // The value will always be a valid date or empty string, malformed input is ignored
+ * var date = dateInput.getValue();
+ * $label.text( 'Selected date: ' + ( date || '(none)' ) );
+ * } );
+ *
* @class
* @extends OO.ui.InputWidget
*
* @constructor
* @param {Object} [config] Configuration options
* @cfg {string} [precision='day'] Date precision to use, 'day' or 'month'
- * @cfg {string|null} [date=null] Day or month date (depending on `precision`), in the
- * format 'YYYY-MM-DD' or 'YYYY-MM'. When null, defaults to current date.
+ * @cfg {string} [value] Day or month date (depending on `precision`), in the format 'YYYY-MM-DD'
+ * or 'YYYY-MM'. If not given or empty string, no date is selected.
* @cfg {string} [inputFormat] Date format string to use for the textual input field. Displayed
* while the widget is active, and the user can type in a date in this format. Should be short
* and easy to type. When not given, defaults to 'YYYY-MM-DD' or 'YYYY-MM', depending on
* while the widget is inactive. Should be as unambiguous as possible (for example, prefer to
* spell out the month, rather than rely on the order), even if that makes it longer. When not
* given, the default is language-specific.
+ * @cfg {string} [placeholder] User-visible date format string displayed in the textual input
+ * field when it's empty. Should be the same as `inputFormat`, but translated to the user's
+ * language. When not given, defaults to a translated version of 'YYYY-MM-DD' or 'YYYY-MM',
+ * depending on `precision`.
+ * @cfg {boolean} [required=false] Mark the field as required. Implies `indicator: 'required'`.
+ * @cfg {string} [mustBeAfter] Validates the date to be after this. In the 'YYYY-MM-DD' format.
+ * @cfg {string} [mustBeBefore] Validates the date to be before this. In the 'YYYY-MM-DD' format.
*/
mw.widgets.DateInputWidget = function MWWDateInputWidget( config ) {
// Config initialization
- config = config || {};
+ config = $.extend( { precision: 'day' }, config );
+ if ( config.required ) {
+ if ( config.indicator === undefined ) {
+ config.indicator = 'required';
+ }
+ }
+
+ var placeholder, mustBeAfter, mustBeBefore;
+ if ( config.placeholder ) {
+ placeholder = config.placeholder;
+ } else if ( config.inputFormat ) {
+ // We have no way to display a translated placeholder for custom formats
+ placeholder = '';
+ } else {
+ // Messages: mw-widgets-dateinput-placeholder-day, mw-widgets-dateinput-placeholder-month
+ placeholder = mw.msg( 'mw-widgets-dateinput-placeholder-' + config.precision );
+ }
// Properties (must be set before parent constructor, which calls #setValue)
this.handle = new OO.ui.LabelWidget();
this.textInput = new OO.ui.TextInputWidget( {
+ placeholder: placeholder,
validate: this.validateDate.bind( this )
} );
this.calendar = new mw.widgets.CalendarWidget( {
this.inputFormat = config.inputFormat;
this.displayFormat = config.displayFormat;
+ // Validate and set min and max dates as properties
+ mustBeAfter = moment( config.mustBeAfter, 'YYYY-MM-DD' );
+ mustBeBefore = moment( config.mustBeBefore, 'YYYY-MM-DD' );
+ if (
+ config.mustBeAfter !== undefined &&
+ mustBeAfter.isValid()
+ ) {
+ this.mustBeAfter = mustBeAfter;
+ }
+
+ if (
+ config.mustBeBefore !== undefined &&
+ mustBeBefore.isValid()
+ ) {
+ this.mustBeBefore = mustBeBefore;
+ }
+
// Parent constructor
mw.widgets.DateInputWidget.parent.call( this, config );
} );
// Initialization
+ if ( config.required ) {
+ this.$input.attr( 'required', 'required' );
+ this.$input.attr( 'aria-required', 'true' );
+ }
// Move 'tabindex' from this.$input (which is invisible) to the visible handle
this.setTabIndexedElement( this.handle.$element );
this.handle.$element
.addClass( 'mw-widget-dateInputWidget' )
.append( this.handle.$element, this.textInput.$element, this.calendar.$element );
// Set handle label and hide stuff
+ this.updateUI();
this.deactivate();
};
* @private
*/
mw.widgets.DateInputWidget.prototype.onTextInputChange = function () {
- var
+ var mom,
widget = this,
- value = this.textInput.getValue();
+ value = this.textInput.getValue(),
+ valid = this.isValidDate( value );
this.inTextInput++;
- this.textInput.isValid().done( function ( valid ) {
- if ( valid ) {
- // Well-formed date value, parse and set it
- var mom = moment( value, widget.getInputFormat() );
- // Use English locale to avoid number formatting
- widget.setValue( mom.locale( 'en' ).format( widget.getInternalFormat() ) );
- } else {
- // Not well-formed, but possibly partial? Try updating the calendar, but do not set the
- // internal value. Generally this only makes sense when 'inputFormat' is little-endian (e.g.
- // 'YYYY-MM-DD'), but that's hard to check for, and might be difficult to handle the parsing
- // right for weird formats. So limit this trick to only when we're using the default
- // 'inputFormat', which is the same as the internal format, 'YYYY-MM-DD'.
- if ( widget.getInputFormat() === widget.getInternalFormat() ) {
- widget.calendar.setDate( widget.textInput.getValue() );
- }
+
+ if ( value === '' ) {
+ // No date selected
+ widget.setValue( '' );
+ } else if ( valid ) {
+ // Well-formed date value, parse and set it
+ mom = moment( value, widget.getInputFormat() );
+ // Use English locale to avoid number formatting
+ widget.setValue( mom.locale( 'en' ).format( widget.getInternalFormat() ) );
+ } else {
+ // Not well-formed, but possibly partial? Try updating the calendar, but do not set the
+ // internal value. Generally this only makes sense when 'inputFormat' is little-endian (e.g.
+ // 'YYYY-MM-DD'), but that's hard to check for, and might be difficult to handle the parsing
+ // right for weird formats. So limit this trick to only when we're using the default
+ // 'inputFormat', which is the same as the internal format, 'YYYY-MM-DD'.
+ if ( widget.getInputFormat() === widget.getInternalFormat() ) {
+ widget.calendar.setDate( widget.textInput.getValue() );
}
- widget.inTextInput--;
- } );
+ }
+ widget.inTextInput--;
+
};
/**
* @inheritdoc
*/
mw.widgets.DateInputWidget.prototype.setValue = function ( value ) {
- if ( value === undefined || value === null ) {
- // Default to today
- value = this.calendar.getDate();
- }
-
var oldValue = this.value;
+ if ( !moment( value, this.getInternalFormat() ).isValid() ) {
+ value = '';
+ }
+
mw.widgets.DateInputWidget.parent.prototype.setValue.call( this, value );
if ( this.value !== oldValue ) {
- if ( !this.inCalendar ) {
- this.calendar.setDate( this.getValue() );
- }
- if ( !this.inTextInput ) {
- this.textInput.setValue( this.getMoment().format( this.getInputFormat() ) );
- }
+ this.updateUI();
}
return this;
setTimeout( function () {
var $focussed = $( ':focus' );
// Deactivate unless the focus moved to something else inside this widget
- if ( !OO.ui.contains( widget.$element[ 0 ], $focussed[0], true ) ) {
+ if ( !OO.ui.contains( widget.$element[ 0 ], $focussed[ 0 ], true ) ) {
widget.deactivate();
}
}, 0 );
};
/**
- * Deactivate this input field for data entry. Opens the calendar and shows the text field.
+ * @inheritdoc
+ */
+ mw.widgets.DateInputWidget.prototype.focus = function () {
+ this.activate();
+ return this;
+ };
+
+ /**
+ * @inheritdoc
+ */
+ mw.widgets.DateInputWidget.prototype.blur = function () {
+ this.deactivate();
+ return this;
+ };
+
+ /**
+ * Update the contents of the label, text input and status of calendar to reflect selected value.
*
* @private
*/
- mw.widgets.DateInputWidget.prototype.deactivate = function () {
- this.textInput.setValue( this.getMoment().format( this.getInputFormat() ) );
- this.calendar.setDate( this.getValue() );
- this.handle.setLabel( this.getMoment().format( this.getDisplayFormat() ) );
+ mw.widgets.DateInputWidget.prototype.updateUI = function () {
+ if ( this.getValue() === '' ) {
+ this.textInput.setValue( '' );
+ this.calendar.setDate( null );
+ this.handle.setLabel( mw.msg( 'mw-widgets-dateinput-no-date' ) );
+ this.$element.addClass( 'mw-widget-dateInputWidget-empty' );
+ } else {
+ if ( !this.inTextInput ) {
+ this.textInput.setValue( this.getMoment().format( this.getInputFormat() ) );
+ }
+ if ( !this.inCalendar ) {
+ this.calendar.setDate( this.getValue() );
+ }
+ this.handle.setLabel( this.getMoment().format( this.getDisplayFormat() ) );
+ this.$element.removeClass( 'mw-widget-dateInputWidget-empty' );
+ }
+ };
+ /**
+ * Deactivate this input field for data entry. Closes the calendar and hides the text field.
+ *
+ * @private
+ */
+ mw.widgets.DateInputWidget.prototype.deactivate = function () {
this.$element.removeClass( 'mw-widget-dateInputWidget-active' );
this.handle.toggle( true );
this.textInput.toggle( false );
};
/**
- * Activate this input field for data entry. Closes the calendar and hides the text field.
+ * Activate this input field for data entry. Opens the calendar and shows the text field.
*
* @private
*/
mw.widgets.DateInputWidget.prototype.activate = function () {
- this.setValue( this.getValue() );
-
+ this.calendar.resetUI();
this.$element.addClass( 'mw-widget-dateInputWidget-active' );
this.handle.toggle( false );
this.textInput.toggle( true );
/**
* @private
- * @param {string} date Date string, must be in 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
+ * @param {string} date Date string, to be valid, must be empty (no date selected) or in
+ * 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
+ * @returns {boolean}
*/
mw.widgets.DateInputWidget.prototype.validateDate = function ( date ) {
+ if ( date === '' ) {
+ return true;
+ }
+
+ var isValid = this.isValidDate( date ) && this.isInRange( date );
+ this.setValidityFlag( isValid );
+ return isValid;
+ };
+
+ /**
+ * @private
+ * @param {string} date Date string, to be valid, must be empty (no date selected) or in
+ * 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
+ * @returns {boolean}
+ */
+ mw.widgets.DateInputWidget.prototype.isValidDate = function ( date ) {
// "Half-strict mode": for example, for the format 'YYYY-MM-DD', 2015-1-3 instead of 2015-01-03
// is okay, but 2015-01 isn't, and neither is 2015-01-foo. Use Moment's "fuzzy" mode and check
// parsing flags for the details (stoled from implementation of #isValid).
return mom.isValid() && flags.charsLeftOver === 0 && flags.unusedTokens.length === 0;
};
+ /**
+ * Validates if the date is within the range configured with {@link #cfg-mustBeAfter}
+ * and {@link #cfg-mustBeBefore}.
+ *
+ * @private
+ * @param {string} date Date string, to be valid, must be empty (no date selected) or in
+ * 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
+ * @returns {boolean}
+ */
+ mw.widgets.DateInputWidget.prototype.isInRange = function ( date ) {
+ var momentDate = moment( date, 'YYYY-MM-DD' ),
+ isAfter = ( this.mustBeAfter === undefined || momentDate.isAfter( this.mustBeAfter ) ),
+ isBefore = ( this.mustBeBefore === undefined || momentDate.isBefore( this.mustBeBefore ) );
+
+ return isAfter && isBefore;
+ };
+
+ /**
+ * Get the validity of current value.
+ *
+ * This method returns a promise that resolves if the value is valid and rejects if
+ * it isn't. Uses {@link #validateDate}.
+ *
+ * @return {jQuery.Promise} A promise that resolves if the value is valid, rejects if not.
+ */
+ mw.widgets.DateInputWidget.prototype.getValidity = function () {
+ var isValid = this.validateDate( this.getValue() );
+
+ if ( isValid ) {
+ return $.Deferred().resolve().promise();
+ } else {
+ return $.Deferred().reject().promise();
+ }
+ };
+
+ /**
+ * Sets the 'invalid' flag appropriately.
+ *
+ * @param {boolean} [isValid] Optionally override validation result
+ */
+ mw.widgets.DateInputWidget.prototype.setValidityFlag = function ( isValid ) {
+ var widget = this,
+ setFlag = function ( valid ) {
+ if ( !valid ) {
+ widget.$input.attr( 'aria-invalid', 'true' );
+ } else {
+ widget.$input.removeAttr( 'aria-invalid' );
+ }
+ widget.setFlags( { invalid: !valid } );
+ };
+
+ if ( isValid !== undefined ) {
+ setFlag( isValid );
+ } else {
+ this.getValidity().then( function () {
+ setFlag( true );
+ }, function () {
+ setFlag( false );
+ } );
+ }
+ };
+
}( jQuery, mediaWiki ) );