c8093bb83316df84ea23798bb86de8f43c9593a4
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 * Creates an mw.widgets.DateInputWidget object.
14 * // Date input widget showcase
15 * var 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 * $( '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 * var dateInput = new mw.widgets.DateInputWidget();
50 * var $label = $( '<p>' );
51 * $( '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 * var date = dateInput.getValue();
55 * $label.text( 'Selected date: ' + ( date || '(none)' ) );
59 * @extends OO.ui.InputWidget
62 * @param {Object} [config] Configuration options
63 * @cfg {string} [precision='day'] Date precision to use, 'day' or 'month'
64 * @cfg {string} [value] Day or month date (depending on `precision`), in the format 'YYYY-MM-DD'
65 * or 'YYYY-MM'. If not given or empty string, no date is selected.
66 * @cfg {string} [inputFormat] Date format string to use for the textual input field. Displayed
67 * while the widget is active, and the user can type in a date in this format. Should be short
68 * and easy to type. When not given, defaults to 'YYYY-MM-DD' or 'YYYY-MM', depending on
70 * @cfg {string} [displayFormat] Date format string to use for the clickable label. Displayed
71 * while the widget is inactive. Should be as unambiguous as possible (for example, prefer to
72 * spell out the month, rather than rely on the order), even if that makes it longer. When not
73 * given, the default is language-specific.
74 * @cfg {string} [placeholder] User-visible date format string displayed in the textual input
75 * field when it's empty. Should be the same as `inputFormat`, but translated to the user's
76 * language. When not given, defaults to a translated version of 'YYYY-MM-DD' or 'YYYY-MM',
77 * depending on `precision`.
78 * @cfg {boolean} [required=false] Mark the field as required. Implies `indicator: 'required'`.
80 mw
.widgets
.DateInputWidget
= function MWWDateInputWidget( config
) {
81 // Config initialization
82 config
= $.extend( { precision
: 'day' }, config
);
83 if ( config
.required
) {
84 if ( config
.indicator
=== undefined ) {
85 config
.indicator
= 'required';
90 if ( config
.placeholder
) {
91 placeholder
= config
.placeholder
;
92 } else if ( config
.inputFormat
) {
93 // We have no way to display a translated placeholder for custom formats
96 // Messages: mw-widgets-dateinput-placeholder-day, mw-widgets-dateinput-placeholder-month
97 placeholder
= mw
.msg( 'mw-widgets-dateinput-placeholder-' + config
.precision
);
100 // Properties (must be set before parent constructor, which calls #setValue)
101 this.handle
= new OO
.ui
.LabelWidget();
102 this.textInput
= new OO
.ui
.TextInputWidget( {
103 placeholder
: placeholder
,
104 validate
: this.validateDate
.bind( this )
106 this.calendar
= new mw
.widgets
.CalendarWidget( {
107 precision
: config
.precision
110 this.inTextInput
= 0;
111 this.inputFormat
= config
.inputFormat
;
112 this.displayFormat
= config
.displayFormat
;
114 // Parent constructor
115 mw
.widgets
.DateInputWidget
.parent
.call( this, config
);
118 this.calendar
.connect( this, {
119 change
: 'onCalendarChange'
121 this.textInput
.connect( this, {
123 change
: 'onTextInputChange'
126 focusout
: this.onBlur
.bind( this )
128 this.calendar
.$element
.on( {
129 keypress
: this.onCalendarKeyPress
.bind( this )
131 this.handle
.$element
.on( {
132 click
: this.onClick
.bind( this ),
133 keypress
: this.onKeyPress
.bind( this )
137 if ( config
.required
) {
138 this.$input
.attr( 'required', 'required' );
139 this.$input
.attr( 'aria-required', 'true' );
141 // Move 'tabindex' from this.$input (which is invisible) to the visible handle
142 this.setTabIndexedElement( this.handle
.$element
);
144 .addClass( 'mw-widget-dateInputWidget-handle' );
146 .addClass( 'mw-widget-dateInputWidget' )
147 .append( this.handle
.$element
, this.textInput
.$element
, this.calendar
.$element
);
148 // Set handle label and hide stuff
155 OO
.inheritClass( mw
.widgets
.DateInputWidget
, OO
.ui
.InputWidget
);
163 mw
.widgets
.DateInputWidget
.prototype.getInputElement = function () {
164 return $( '<input type="hidden">' );
168 * Respond to calendar date change events.
172 mw
.widgets
.DateInputWidget
.prototype.onCalendarChange = function () {
174 if ( !this.inTextInput
) {
175 // If this is caused by user typing in the input field, do not set anything.
176 // The value may be invalid (see #onTextInputChange), but displayable on the calendar.
177 this.setValue( this.calendar
.getDate() );
183 * Respond to text input value change events.
187 mw
.widgets
.DateInputWidget
.prototype.onTextInputChange = function () {
190 value
= this.textInput
.getValue();
192 this.textInput
.isValid().done( function ( valid
) {
193 if ( value
=== '' ) {
195 widget
.setValue( '' );
196 } else if ( valid
) {
197 // Well-formed date value, parse and set it
198 var mom
= moment( value
, widget
.getInputFormat() );
199 // Use English locale to avoid number formatting
200 widget
.setValue( mom
.locale( 'en' ).format( widget
.getInternalFormat() ) );
202 // Not well-formed, but possibly partial? Try updating the calendar, but do not set the
203 // internal value. Generally this only makes sense when 'inputFormat' is little-endian (e.g.
204 // 'YYYY-MM-DD'), but that's hard to check for, and might be difficult to handle the parsing
205 // right for weird formats. So limit this trick to only when we're using the default
206 // 'inputFormat', which is the same as the internal format, 'YYYY-MM-DD'.
207 if ( widget
.getInputFormat() === widget
.getInternalFormat() ) {
208 widget
.calendar
.setDate( widget
.textInput
.getValue() );
211 widget
.inTextInput
--;
218 mw
.widgets
.DateInputWidget
.prototype.setValue = function ( value
) {
219 var oldValue
= this.value
;
221 if ( !moment( value
, this.getInternalFormat() ).isValid() ) {
225 mw
.widgets
.DateInputWidget
.parent
.prototype.setValue
.call( this, value
);
227 if ( this.value
!== oldValue
) {
235 * Handle text input and calendar blur events.
239 mw
.widgets
.DateInputWidget
.prototype.onBlur = function () {
241 setTimeout( function () {
242 var $focussed
= $( ':focus' );
243 // Deactivate unless the focus moved to something else inside this widget
244 if ( !OO
.ui
.contains( widget
.$element
[ 0 ], $focussed
[ 0 ], true ) ) {
253 mw
.widgets
.DateInputWidget
.prototype.focus = function () {
261 mw
.widgets
.DateInputWidget
.prototype.blur = function () {
267 * Update the contents of the label, text input and status of calendar to reflect selected value.
271 mw
.widgets
.DateInputWidget
.prototype.updateUI = function () {
272 if ( this.getValue() === '' ) {
273 this.textInput
.setValue( '' );
274 this.calendar
.setDate( null );
275 this.handle
.setLabel( mw
.msg( 'mw-widgets-dateinput-no-date' ) );
276 this.$element
.addClass( 'mw-widget-dateInputWidget-empty' );
278 if ( !this.inTextInput
) {
279 this.textInput
.setValue( this.getMoment().format( this.getInputFormat() ) );
281 if ( !this.inCalendar
) {
282 this.calendar
.setDate( this.getValue() );
284 this.handle
.setLabel( this.getMoment().format( this.getDisplayFormat() ) );
285 this.$element
.removeClass( 'mw-widget-dateInputWidget-empty' );
290 * Deactivate this input field for data entry. Closes the calendar and hides the text field.
294 mw
.widgets
.DateInputWidget
.prototype.deactivate = function () {
295 this.$element
.removeClass( 'mw-widget-dateInputWidget-active' );
296 this.handle
.toggle( true );
297 this.textInput
.toggle( false );
298 this.calendar
.toggle( false );
302 * Activate this input field for data entry. Opens the calendar and shows the text field.
306 mw
.widgets
.DateInputWidget
.prototype.activate = function () {
307 this.$element
.addClass( 'mw-widget-dateInputWidget-active' );
308 this.handle
.toggle( false );
309 this.textInput
.toggle( true );
310 this.calendar
.toggle( true );
312 this.textInput
.$input
.focus();
316 * Get the date format to be used for handle label when the input is inactive.
319 * @return {string} Format string
321 mw
.widgets
.DateInputWidget
.prototype.getDisplayFormat = function () {
322 if ( this.displayFormat
!== undefined ) {
323 return this.displayFormat
;
326 if ( this.calendar
.getPrecision() === 'month' ) {
329 // The formats Moment.js provides:
330 // * ll: Month name, day of month, year
331 // * lll: Month name, day of month, year, time
332 // * llll: Month name, day of month, day of week, year, time
334 // The format we want:
335 // * ????: Month name, day of month, day of week, year
337 // We try to construct it as 'llll - (lll - ll)' and hope for the best.
338 // This seems to work well for many languages (maybe even all?).
340 var localeData
= moment
.localeData( moment
.locale() ),
341 llll
= localeData
.longDateFormat( 'llll' ),
342 lll
= localeData
.longDateFormat( 'lll' ),
343 ll
= localeData
.longDateFormat( 'll' ),
344 format
= llll
.replace( lll
.replace( ll
, '' ), '' );
351 * Get the date format to be used for the text field when the input is active.
354 * @return {string} Format string
356 mw
.widgets
.DateInputWidget
.prototype.getInputFormat = function () {
357 if ( this.inputFormat
!== undefined ) {
358 return this.inputFormat
;
364 }[ this.calendar
.getPrecision() ];
368 * Get the date format to be used internally for the value. This is not configurable in any way,
369 * and always either 'YYYY-MM-DD' or 'YYYY-MM'.
372 * @return {string} Format string
374 mw
.widgets
.DateInputWidget
.prototype.getInternalFormat = function () {
378 }[ this.calendar
.getPrecision() ];
382 * Get the Moment object for current value.
384 * @return {Object} Moment object
386 mw
.widgets
.DateInputWidget
.prototype.getMoment = function () {
387 return moment( this.getValue(), this.getInternalFormat() );
391 * Handle mouse click events.
394 * @param {jQuery.Event} e Mouse click event
396 mw
.widgets
.DateInputWidget
.prototype.onClick = function ( e
) {
397 if ( !this.isDisabled() && e
.which
=== 1 ) {
404 * Handle key press events.
407 * @param {jQuery.Event} e Key press event
409 mw
.widgets
.DateInputWidget
.prototype.onKeyPress = function ( e
) {
410 if ( !this.isDisabled() &&
411 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
419 * Handle calendar key press events.
422 * @param {jQuery.Event} e Key press event
424 mw
.widgets
.DateInputWidget
.prototype.onCalendarKeyPress = function ( e
) {
425 if ( !this.isDisabled() && e
.which
=== OO
.ui
.Keys
.ENTER
) {
427 this.handle
.$element
.focus();
433 * Handle text input enter events.
437 mw
.widgets
.DateInputWidget
.prototype.onEnter = function () {
439 this.handle
.$element
.focus();
444 * @param {string} date Date string, to be valid, must be empty (no date selected) or in
445 * 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
447 mw
.widgets
.DateInputWidget
.prototype.validateDate = function ( date
) {
452 // "Half-strict mode": for example, for the format 'YYYY-MM-DD', 2015-1-3 instead of 2015-01-03
453 // is okay, but 2015-01 isn't, and neither is 2015-01-foo. Use Moment's "fuzzy" mode and check
454 // parsing flags for the details (stoled from implementation of #isValid).
456 mom
= moment( date
, this.getInputFormat() ),
457 flags
= mom
.parsingFlags();
459 return mom
.isValid() && flags
.charsLeftOver
=== 0 && flags
.unusedTokens
.length
=== 0;
462 }( jQuery
, mediaWiki
) );