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`.
79 mw
.widgets
.DateInputWidget
= function MWWDateInputWidget( config
) {
80 // Config initialization
81 config
= $.extend( { precision
: 'day' }, config
);
84 if ( config
.placeholder
) {
85 placeholder
= config
.placeholder
;
86 } else if ( config
.inputFormat
) {
87 // We have no way to display a translated placeholder for custom formats
90 // Messages: mw-widgets-dateinput-placeholder-day, mw-widgets-dateinput-placeholder-month
91 placeholder
= mw
.msg( 'mw-widgets-dateinput-placeholder-' + config
.precision
);
94 // Properties (must be set before parent constructor, which calls #setValue)
95 this.handle
= new OO
.ui
.LabelWidget();
96 this.textInput
= new OO
.ui
.TextInputWidget( {
97 placeholder
: placeholder
,
98 validate
: this.validateDate
.bind( this )
100 this.calendar
= new mw
.widgets
.CalendarWidget( {
101 precision
: config
.precision
104 this.inTextInput
= 0;
105 this.inputFormat
= config
.inputFormat
;
106 this.displayFormat
= config
.displayFormat
;
108 // Parent constructor
109 mw
.widgets
.DateInputWidget
.parent
.call( this, config
);
112 this.calendar
.connect( this, {
113 change
: 'onCalendarChange'
115 this.textInput
.connect( this, {
117 change
: 'onTextInputChange'
120 focusout
: this.onBlur
.bind( this )
122 this.calendar
.$element
.on( {
123 keypress
: this.onCalendarKeyPress
.bind( this )
125 this.handle
.$element
.on( {
126 click
: this.onClick
.bind( this ),
127 keypress
: this.onKeyPress
.bind( this )
131 // Move 'tabindex' from this.$input (which is invisible) to the visible handle
132 this.setTabIndexedElement( this.handle
.$element
);
134 .addClass( 'mw-widget-dateInputWidget-handle' );
136 .addClass( 'mw-widget-dateInputWidget' )
137 .append( this.handle
.$element
, this.textInput
.$element
, this.calendar
.$element
);
138 // Set handle label and hide stuff
145 OO
.inheritClass( mw
.widgets
.DateInputWidget
, OO
.ui
.InputWidget
);
153 mw
.widgets
.DateInputWidget
.prototype.getInputElement = function () {
154 return $( '<input type="hidden">' );
158 * Respond to calendar date change events.
162 mw
.widgets
.DateInputWidget
.prototype.onCalendarChange = function () {
164 if ( !this.inTextInput
) {
165 // If this is caused by user typing in the input field, do not set anything.
166 // The value may be invalid (see #onTextInputChange), but displayable on the calendar.
167 this.setValue( this.calendar
.getDate() );
173 * Respond to text input value change events.
177 mw
.widgets
.DateInputWidget
.prototype.onTextInputChange = function () {
180 value
= this.textInput
.getValue();
182 this.textInput
.isValid().done( function ( valid
) {
183 if ( value
=== '' ) {
185 widget
.setValue( '' );
186 } else if ( valid
) {
187 // Well-formed date value, parse and set it
188 var mom
= moment( value
, widget
.getInputFormat() );
189 // Use English locale to avoid number formatting
190 widget
.setValue( mom
.locale( 'en' ).format( widget
.getInternalFormat() ) );
192 // Not well-formed, but possibly partial? Try updating the calendar, but do not set the
193 // internal value. Generally this only makes sense when 'inputFormat' is little-endian (e.g.
194 // 'YYYY-MM-DD'), but that's hard to check for, and might be difficult to handle the parsing
195 // right for weird formats. So limit this trick to only when we're using the default
196 // 'inputFormat', which is the same as the internal format, 'YYYY-MM-DD'.
197 if ( widget
.getInputFormat() === widget
.getInternalFormat() ) {
198 widget
.calendar
.setDate( widget
.textInput
.getValue() );
201 widget
.inTextInput
--;
208 mw
.widgets
.DateInputWidget
.prototype.setValue = function ( value
) {
209 var oldValue
= this.value
;
211 if ( !moment( value
, this.getInternalFormat() ).isValid() ) {
215 mw
.widgets
.DateInputWidget
.parent
.prototype.setValue
.call( this, value
);
217 if ( this.value
!== oldValue
) {
225 * Handle text input and calendar blur events.
229 mw
.widgets
.DateInputWidget
.prototype.onBlur = function () {
231 setTimeout( function () {
232 var $focussed
= $( ':focus' );
233 // Deactivate unless the focus moved to something else inside this widget
234 if ( !OO
.ui
.contains( widget
.$element
[ 0 ], $focussed
[0], true ) ) {
243 mw
.widgets
.DateInputWidget
.prototype.focus = function () {
251 mw
.widgets
.DateInputWidget
.prototype.blur = function () {
257 * Update the contents of the label, text input and status of calendar to reflect selected value.
261 mw
.widgets
.DateInputWidget
.prototype.updateUI = function () {
262 if ( this.getValue() === '' ) {
263 this.textInput
.setValue( '' );
264 this.calendar
.setDate( null );
265 this.handle
.setLabel( mw
.msg( 'mw-widgets-dateinput-no-date' ) );
266 this.$element
.addClass( 'mw-widget-dateInputWidget-empty' );
268 if ( !this.inTextInput
) {
269 this.textInput
.setValue( this.getMoment().format( this.getInputFormat() ) );
271 if ( !this.inCalendar
) {
272 this.calendar
.setDate( this.getValue() );
274 this.handle
.setLabel( this.getMoment().format( this.getDisplayFormat() ) );
275 this.$element
.removeClass( 'mw-widget-dateInputWidget-empty' );
280 * Deactivate this input field for data entry. Closes the calendar and hides the text field.
284 mw
.widgets
.DateInputWidget
.prototype.deactivate = function () {
285 this.$element
.removeClass( 'mw-widget-dateInputWidget-active' );
286 this.handle
.toggle( true );
287 this.textInput
.toggle( false );
288 this.calendar
.toggle( false );
292 * Activate this input field for data entry. Opens the calendar and shows the text field.
296 mw
.widgets
.DateInputWidget
.prototype.activate = function () {
297 this.$element
.addClass( 'mw-widget-dateInputWidget-active' );
298 this.handle
.toggle( false );
299 this.textInput
.toggle( true );
300 this.calendar
.toggle( true );
302 this.textInput
.$input
.focus();
306 * Get the date format to be used for handle label when the input is inactive.
309 * @return {string} Format string
311 mw
.widgets
.DateInputWidget
.prototype.getDisplayFormat = function () {
312 if ( this.displayFormat
!== undefined ) {
313 return this.displayFormat
;
316 if ( this.calendar
.getPrecision() === 'month' ) {
319 // The formats Moment.js provides:
320 // * ll: Month name, day of month, year
321 // * lll: Month name, day of month, year, time
322 // * llll: Month name, day of month, day of week, year, time
324 // The format we want:
325 // * ????: Month name, day of month, day of week, year
327 // We try to construct it as 'llll - (lll - ll)' and hope for the best.
328 // This seems to work well for many languages (maybe even all?).
330 var localeData
= moment
.localeData( moment
.locale() ),
331 llll
= localeData
.longDateFormat( 'llll' ),
332 lll
= localeData
.longDateFormat( 'lll' ),
333 ll
= localeData
.longDateFormat( 'll' ),
334 format
= llll
.replace( lll
.replace( ll
, '' ), '' );
341 * Get the date format to be used for the text field when the input is active.
344 * @return {string} Format string
346 mw
.widgets
.DateInputWidget
.prototype.getInputFormat = function () {
347 if ( this.inputFormat
!== undefined ) {
348 return this.inputFormat
;
354 }[ this.calendar
.getPrecision() ];
358 * Get the date format to be used internally for the value. This is not configurable in any way,
359 * and always either 'YYYY-MM-DD' or 'YYYY-MM'.
362 * @return {string} Format string
364 mw
.widgets
.DateInputWidget
.prototype.getInternalFormat = function () {
368 }[ this.calendar
.getPrecision() ];
372 * Get the Moment object for current value.
374 * @return {Object} Moment object
376 mw
.widgets
.DateInputWidget
.prototype.getMoment = function () {
377 return moment( this.getValue(), this.getInternalFormat() );
381 * Handle mouse click events.
384 * @param {jQuery.Event} e Mouse click event
386 mw
.widgets
.DateInputWidget
.prototype.onClick = function ( e
) {
387 if ( !this.isDisabled() && e
.which
=== 1 ) {
394 * Handle key press events.
397 * @param {jQuery.Event} e Key press event
399 mw
.widgets
.DateInputWidget
.prototype.onKeyPress = function ( e
) {
400 if ( !this.isDisabled() &&
401 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
409 * Handle calendar key press events.
412 * @param {jQuery.Event} e Key press event
414 mw
.widgets
.DateInputWidget
.prototype.onCalendarKeyPress = function ( e
) {
415 if ( !this.isDisabled() && e
.which
=== OO
.ui
.Keys
.ENTER
) {
417 this.handle
.$element
.focus();
423 * Handle text input enter events.
427 mw
.widgets
.DateInputWidget
.prototype.onEnter = function () {
429 this.handle
.$element
.focus();
434 * @param {string} date Date string, to be valid, must be empty (no date selected) or in
435 * 'YYYY-MM-DD' or 'YYYY-MM' format to be valid
437 mw
.widgets
.DateInputWidget
.prototype.validateDate = function ( date
) {
442 // "Half-strict mode": for example, for the format 'YYYY-MM-DD', 2015-1-3 instead of 2015-01-03
443 // is okay, but 2015-01 isn't, and neither is 2015-01-foo. Use Moment's "fuzzy" mode and check
444 // parsing flags for the details (stoled from implementation of #isValid).
446 mom
= moment( date
, this.getInputFormat() ),
447 flags
= mom
.parsingFlags();
449 return mom
.isValid() && flags
.charsLeftOver
=== 0 && flags
.unusedTokens
.length
=== 0;
452 }( jQuery
, mediaWiki
) );