3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * Provides a time input field with a time dropdown and automatic time validation.
18 * This field recognizes and uses JavaScript Date objects as its main {@link #value} type (only the time portion of the
19 * date is used; the month/day/year are ignored). In addition, it recognizes string values which are parsed according to
20 * the {@link #format} and/or {@link #altFormats} configs. These may be reconfigured to use time formats appropriate for
23 * The field may be limited to a certain range of times by using the {@link #minValue} and {@link #maxValue} configs,
24 * and the interval between time options in the dropdown can be changed with the {@link #increment} config.
29 * Ext.create('Ext.form.Panel', {
33 * renderTo: Ext.getBody(),
37 * fieldLabel: 'Time In',
38 * minValue: '6:00 AM',
39 * maxValue: '8:00 PM',
45 * fieldLabel: 'Time Out',
46 * minValue: '6:00 AM',
47 * maxValue: '8:00 PM',
53 Ext.define('Ext.form.field.Time', {
54 extend:'Ext.form.field.Picker',
55 alias: 'widget.timefield',
56 requires: ['Ext.form.field.Date', 'Ext.picker.Time', 'Ext.view.BoundListKeyNav', 'Ext.Date'],
57 alternateClassName: ['Ext.form.TimeField', 'Ext.form.Time'],
60 * @cfg {String} triggerCls
61 * An additional CSS class used to style the trigger button. The trigger will always get the {@link #triggerBaseCls}
62 * by default and triggerCls will be **appended** if specified. Defaults to 'x-form-time-trigger' for the Time field
65 triggerCls: Ext.baseCSSPrefix + 'form-time-trigger',
68 * @cfg {Date/String} minValue
69 * The minimum allowed time. Can be either a Javascript date object with a valid time value or a string time in a
70 * valid format -- see {@link #format} and {@link #altFormats}.
74 * @cfg {Date/String} maxValue
75 * The maximum allowed time. Can be either a Javascript date object with a valid time value or a string time in a
76 * valid format -- see {@link #format} and {@link #altFormats}.
80 * @cfg {String} minText
81 * The error text to display when the entered time is before {@link #minValue}.
83 minText : "The time in this field must be equal to or after {0}",
86 * @cfg {String} maxText
87 * The error text to display when the entered time is after {@link #maxValue}.
89 maxText : "The time in this field must be equal to or before {0}",
92 * @cfg {String} invalidText
93 * The error text to display when the time in the field is invalid.
95 invalidText : "{0} is not a valid time",
98 * @cfg {String} format
99 * The default time format string which can be overriden for localization support. The format must be valid
100 * according to {@link Ext.Date#parse} (defaults to 'g:i A', e.g., '3:15 PM'). For 24-hour time format try 'H:i'
106 * @cfg {String} submitFormat
107 * The date format string which will be submitted to the server. The format must be valid according to {@link
108 * Ext.Date#parse} (defaults to {@link #format}).
112 * @cfg {String} altFormats
113 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
116 altFormats : "g:ia|g:iA|g:i a|g:i A|h:i|g:i|H:i|ga|ha|gA|h a|g a|g A|gi|hi|gia|hia|g|H|gi a|hi a|giA|hiA|gi A|hi A",
119 * @cfg {Number} increment
120 * The number of minutes between each time value in the list.
125 * @cfg {Number} pickerMaxHeight
126 * The maximum height of the {@link Ext.picker.Time} dropdown.
128 pickerMaxHeight: 300,
131 * @cfg {Boolean} selectOnTab
132 * Whether the Tab key should select the currently highlighted item.
138 * This is the date to use when generating time values in the absence of either minValue
139 * or maxValue. Using the current date causes DST issues on DST boundary dates, so this is an
140 * arbitrary "safe" date that can be any date aside from DST boundary dates.
142 initDate: '1/1/2008',
143 initDateFormat: 'j/n/Y',
146 initComponent: function() {
159 initValue: function() {
163 // If a String value was supplied, try to convert it to a proper Date object
164 if (Ext.isString(value)) {
165 me.value = me.rawToValue(value);
172 * Replaces any existing {@link #minValue} with the new time and refreshes the picker's range.
173 * @param {Date/String} value The minimum time that can be selected
175 setMinValue: function(value) {
178 me.setLimit(value, true);
180 picker.setMinValue(me.minValue);
185 * Replaces any existing {@link #maxValue} with the new time and refreshes the picker's range.
186 * @param {Date/String} value The maximum time that can be selected
188 setMaxValue: function(value) {
191 me.setLimit(value, false);
193 picker.setMaxValue(me.maxValue);
199 * Updates either the min or max value. Converts the user's value into a Date object whose
200 * year/month/day is set to the {@link #initDate} so that only the time fields are significant.
202 setLimit: function(value, isMin) {
205 if (Ext.isString(value)) {
206 d = me.parseDate(value);
208 else if (Ext.isDate(value)) {
212 val = Ext.Date.clearTime(new Date(me.initDate));
213 val.setHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
214 me[isMin ? 'minValue' : 'maxValue'] = val;
218 rawToValue: function(rawValue) {
219 return this.parseDate(rawValue) || rawValue || null;
222 valueToRaw: function(value) {
223 return this.formatDate(this.parseDate(value));
227 * Runs all of Time's validations and returns an array of any errors. Note that this first runs Text's validations,
228 * so the returned array is an amalgamation of all field errors. The additional validation checks are testing that
229 * the time format is valid, that the chosen time is within the {@link #minValue} and {@link #maxValue} constraints
231 * @param {Object} [value] The value to get errors for (defaults to the current field value)
232 * @return {String[]} All validation errors for this field
234 getErrors: function(value) {
236 format = Ext.String.format,
237 errors = me.callParent(arguments),
238 minValue = me.minValue,
239 maxValue = me.maxValue,
242 value = me.formatDate(value || me.processRawValue(me.getRawValue()));
244 if (value === null || value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
248 date = me.parseDate(value);
250 errors.push(format(me.invalidText, value, me.format));
254 if (minValue && date < minValue) {
255 errors.push(format(me.minText, me.formatDate(minValue)));
258 if (maxValue && date > maxValue) {
259 errors.push(format(me.maxText, me.formatDate(maxValue)));
265 formatDate: function() {
266 return Ext.form.field.Date.prototype.formatDate.apply(this, arguments);
271 * Parses an input value into a valid Date object.
272 * @param {String/Date} value
274 parseDate: function(value) {
275 if (!value || Ext.isDate(value)) {
280 val = me.safeParse(value, me.format),
281 altFormats = me.altFormats,
282 altFormatsArray = me.altFormatsArray,
286 if (!val && altFormats) {
287 altFormatsArray = altFormatsArray || altFormats.split('|');
288 len = altFormatsArray.length;
289 for (; i < len && !val; ++i) {
290 val = me.safeParse(value, altFormatsArray[i]);
296 safeParse: function(value, format){
302 if (utilDate.formatContainsDateInfo(format)) {
303 // assume we've been given a full date
304 result = utilDate.parse(value, format);
306 // Use our initial safe date
307 parsedDate = utilDate.parse(me.initDate + ' ' + value, me.initDateFormat + ' ' + format);
316 getSubmitValue: function() {
318 format = me.submitFormat || me.format,
319 value = me.getValue();
321 return value ? Ext.Date.format(value, format) : null;
326 * Creates the {@link Ext.picker.Time}
328 createPicker: function() {
330 picker = Ext.create('Ext.picker.Time', {
337 minValue: me.minValue,
338 maxValue: me.maxValue,
339 increment: me.increment,
341 ownerCt: this.ownerCt,
342 renderTo: document.body,
343 maxHeight: me.pickerMaxHeight,
344 focusOnToFront: false
347 me.mon(picker.getSelectionModel(), {
348 selectionchange: me.onListSelect,
357 * Enables the key nav for the Time picker when it is expanded.
358 * TODO this is largely the same logic as ComboBox, should factor out.
360 onExpand: function() {
362 keyNav = me.pickerKeyNav,
363 selectOnTab = me.selectOnTab,
364 picker = me.getPicker(),
365 lastSelected = picker.getSelectionModel().lastSelected,
369 keyNav = me.pickerKeyNav = Ext.create('Ext.view.BoundListKeyNav', this.inputEl, {
374 if(me.picker.highlightedItem) {
375 this.selectHighlighted(e);
381 // Tab key event is allowed to propagate to field
385 // stop tab monitoring from Ext.form.field.Trigger so it doesn't short-circuit selectOnTab
387 me.ignoreMonitorTab = true;
390 Ext.defer(keyNav.enable, 1, keyNav); //wait a bit so it doesn't react to the down arrow opening the picker
392 // Highlight the last selected item and scroll it into view
394 itemNode = picker.getNode(lastSelected);
396 picker.highlightItem(itemNode);
397 picker.el.scrollChildIntoView(itemNode, false);
404 * Disables the key nav for the Time picker when it is collapsed.
406 onCollapse: function() {
408 keyNav = me.pickerKeyNav;
411 me.ignoreMonitorTab = false;
417 * Clears the highlighted item in the picker on change.
418 * This prevents the highlighted item from being selected instead of the custom typed in value when the tab key is pressed.
420 onChange: function() {
424 me.callParent(arguments);
426 picker.clearHighlight();
432 * Handles a time being selected from the Time picker.
434 onListSelect: function(list, recordArray) {
436 record = recordArray[0],
437 val = record ? record.get('date') : null;
439 me.fireEvent('select', me, val);
440 me.picker.clearHighlight();