4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-field-Time'>/**
19 </span> * @class Ext.form.field.Time
20 * @extends Ext.form.field.Picker
21 * <p>Provides a time input field with a time dropdown and automatic time validation.</p>
22 * <p>This field recognizes and uses JavaScript Date objects as its main {@link #value} type (only the time
23 * portion of the date is used; the month/day/year are ignored). In addition, it recognizes string values which
24 * are parsed according to the {@link #format} and/or {@link #altFormats} configs. These may be reconfigured
25 * to use time formats appropriate for the user's locale.</p>
26 * <p>The field may be limited to a certain range of times by using the {@link #minValue} and {@link #maxValue}
27 * configs, and the interval between time options in the dropdown can be changed with the {@link #increment} config.</p>
28 * {@img Ext.form.Time/Ext.form.Time.png Ext.form.Time component}
29 * <p>Example usage:</p>
30 * <pre><code>
31 Ext.create('Ext.form.Panel', {
35 renderTo: Ext.getBody(),
39 fieldLabel: 'Time In',
47 fieldLabel: 'Time Out',
54 </code></pre>
56 Ext.define('Ext.form.field.Time', {
57 extend:'Ext.form.field.Picker',
58 alias: 'widget.timefield',
59 requires: ['Ext.form.field.Date', 'Ext.picker.Time', 'Ext.view.BoundListKeyNav', 'Ext.Date'],
60 alternateClassName: ['Ext.form.TimeField', 'Ext.form.Time'],
62 <span id='Ext-form-field-Time-cfg-triggerCls'> /**
63 </span> * @cfg {String} triggerCls
64 * An additional CSS class used to style the trigger button. The trigger will always get the
65 * {@link #triggerBaseCls} by default and <tt>triggerCls</tt> will be <b>appended</b> if specified.
66 * Defaults to <tt>'x-form-time-trigger'</tt> for the Time field trigger.
68 triggerCls: Ext.baseCSSPrefix + 'form-time-trigger',
70 <span id='Ext-form-field-Time-cfg-minValue'> /**
71 </span> * @cfg {Date/String} minValue
72 * The minimum allowed time. Can be either a Javascript date object with a valid time value or a string
73 * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).
76 <span id='Ext-form-field-Time-cfg-maxValue'> /**
77 </span> * @cfg {Date/String} maxValue
78 * The maximum allowed time. Can be either a Javascript date object with a valid time value or a string
79 * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).
82 <span id='Ext-form-field-Time-cfg-minText'> /**
83 </span> * @cfg {String} minText
84 * The error text to display when the entered time is before {@link #minValue} (defaults to
85 * 'The time in this field must be equal to or after {0}').
87 minText : "The time in this field must be equal to or after {0}",
89 <span id='Ext-form-field-Time-cfg-maxText'> /**
90 </span> * @cfg {String} maxText
91 * The error text to display when the entered time is after {@link #maxValue} (defaults to
92 * 'The time in this field must be equal to or before {0}').
94 maxText : "The time in this field must be equal to or before {0}",
96 <span id='Ext-form-field-Time-cfg-invalidText'> /**
97 </span> * @cfg {String} invalidText
98 * The error text to display when the time in the field is invalid (defaults to
99 * '{value} is not a valid time').
101 invalidText : "{0} is not a valid time",
103 <span id='Ext-form-field-Time-cfg-format'> /**
104 </span> * @cfg {String} format
105 * The default time format string which can be overriden for localization support. The format must be
106 * valid according to {@link Ext.Date#parse} (defaults to 'g:i A', e.g., '3:15 PM'). For 24-hour time
107 * format try 'H:i' instead.
109 format : "g:i A",
111 <span id='Ext-form-field-Time-cfg-submitFormat'> /**
112 </span> * @cfg {String} submitFormat The date format string which will be submitted to the server.
113 * The format must be valid according to {@link Ext.Date#parse} (defaults to <tt>{@link #format}</tt>).
116 <span id='Ext-form-field-Time-cfg-altFormats'> /**
117 </span> * @cfg {String} altFormats
118 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
119 * format (defaults to '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').
121 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",
123 <span id='Ext-form-field-Time-cfg-increment'> /**
124 </span> * @cfg {Number} increment
125 * The number of minutes between each time value in the list (defaults to 15).
129 <span id='Ext-form-field-Time-cfg-pickerMaxHeight'> /**
130 </span> * @cfg {Number} pickerMaxHeight
131 * The maximum height of the {@link Ext.picker.Time} dropdown. Defaults to 300.
133 pickerMaxHeight: 300,
135 <span id='Ext-form-field-Time-cfg-selectOnTab'> /**
136 </span> * @cfg {Boolean} selectOnTab
137 * Whether the Tab key should select the currently highlighted item. Defaults to <tt>true</tt>.
141 <span id='Ext-form-field-Time-property-initDate'> /**
143 * This is the date to use when generating time values in the absence of either minValue
144 * or maxValue. Using the current date causes DST issues on DST boundary dates, so this is an
145 * arbitrary "safe" date that can be any date aside from DST boundary dates.
147 initDate: '1/1/2008',
148 initDateFormat: 'j/n/Y',
151 initComponent: function() {
164 initValue: function() {
168 // If a String value was supplied, try to convert it to a proper Date object
169 if (Ext.isString(value)) {
170 me.value = me.rawToValue(value);
176 <span id='Ext-form-field-Time-method-setMinValue'> /**
177 </span> * Replaces any existing {@link #minValue} with the new time and refreshes the picker's range.
178 * @param {Date/String} value The minimum time that can be selected
180 setMinValue: function(value) {
183 me.setLimit(value, true);
185 picker.setMinValue(me.minValue);
189 <span id='Ext-form-field-Time-method-setMaxValue'> /**
190 </span> * Replaces any existing {@link #maxValue} with the new time and refreshes the picker's range.
191 * @param {Date/String} value The maximum time that can be selected
193 setMaxValue: function(value) {
196 me.setLimit(value, false);
198 picker.setMaxValue(me.maxValue);
202 <span id='Ext-form-field-Time-method-setLimit'> /**
204 * Updates either the min or max value. Converts the user's value into a Date object whose
205 * year/month/day is set to the {@link #initDate} so that only the time fields are significant.
207 setLimit: function(value, isMin) {
210 if (Ext.isString(value)) {
211 d = me.parseDate(value);
213 else if (Ext.isDate(value)) {
217 val = Ext.Date.clearTime(new Date(me.initDate));
218 val.setHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
219 me[isMin ? 'minValue' : 'maxValue'] = val;
223 rawToValue: function(rawValue) {
224 return this.parseDate(rawValue) || rawValue || null;
227 valueToRaw: function(value) {
228 return this.formatDate(this.parseDate(value));
231 <span id='Ext-form-field-Time-method-getErrors'> /**
232 </span> * Runs all of Time's validations and returns an array of any errors. Note that this first
233 * runs Text's validations, so the returned array is an amalgamation of all field errors.
234 * The additional validation checks are testing that the time format is valid, that the chosen
235 * time is within the {@link #minValue} and {@link #maxValue} constraints set.
236 * @param {Mixed} value The value to get errors for (defaults to the current field value)
237 * @return {Array} All validation errors for this field
239 getErrors: function(value) {
241 format = Ext.String.format,
242 errors = me.callParent(arguments),
243 minValue = me.minValue,
244 maxValue = me.maxValue,
247 value = me.formatDate(value || me.processRawValue(me.getRawValue()));
249 if (value === null || value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
253 date = me.parseDate(value);
255 errors.push(format(me.invalidText, value, me.format));
259 if (minValue && date < minValue) {
260 errors.push(format(me.minText, me.formatDate(minValue)));
263 if (maxValue && date > maxValue) {
264 errors.push(format(me.maxText, me.formatDate(maxValue)));
270 formatDate: function() {
271 return Ext.form.field.Date.prototype.formatDate.apply(this, arguments);
274 <span id='Ext-form-field-Time-method-parseDate'> /**
276 * Parses an input value into a valid Date object.
277 * @param {String/Date} value
279 parseDate: function(value) {
280 if (!value || Ext.isDate(value)) {
285 val = me.safeParse(value, me.format),
286 altFormats = me.altFormats,
287 altFormatsArray = me.altFormatsArray,
291 if (!val && altFormats) {
292 altFormatsArray = altFormatsArray || altFormats.split('|');
293 len = altFormatsArray.length;
294 for (; i < len && !val; ++i) {
295 val = me.safeParse(value, altFormatsArray[i]);
301 safeParse: function(value, format){
307 if (utilDate.formatContainsDateInfo(format)) {
308 // assume we've been given a full date
309 result = utilDate.parse(value, format);
311 // Use our initial safe date
312 parsedDate = utilDate.parse(me.initDate + ' ' + value, me.initDateFormat + ' ' + format);
321 getSubmitValue: function() {
323 format = me.submitFormat || me.format,
324 value = me.getValue();
326 return value ? Ext.Date.format(value, format) : null;
329 <span id='Ext-form-field-Time-method-createPicker'> /**
331 * Creates the {@link Ext.picker.Time}
333 createPicker: function() {
335 picker = Ext.create('Ext.picker.Time', {
341 minValue: me.minValue,
342 maxValue: me.maxValue,
343 increment: me.increment,
345 ownerCt: this.ownerCt,
346 renderTo: document.body,
347 maxHeight: me.pickerMaxHeight,
348 focusOnToFront: false
351 me.mon(picker.getSelectionModel(), {
352 selectionchange: me.onListSelect,
359 <span id='Ext-form-field-Time-method-onExpand'> /**
361 * Enables the key nav for the Time picker when it is expanded.
362 * TODO this is largely the same logic as ComboBox, should factor out.
364 onExpand: function() {
366 keyNav = me.pickerKeyNav,
367 selectOnTab = me.selectOnTab,
368 picker = me.getPicker(),
369 lastSelected = picker.getSelectionModel().lastSelected,
373 keyNav = me.pickerKeyNav = Ext.create('Ext.view.BoundListKeyNav', this.inputEl, {
378 if(me.picker.highlightedItem) {
379 this.selectHighlighted(e);
385 // Tab key event is allowed to propagate to field
389 // stop tab monitoring from Ext.form.field.Trigger so it doesn't short-circuit selectOnTab
391 me.ignoreMonitorTab = true;
394 Ext.defer(keyNav.enable, 1, keyNav); //wait a bit so it doesn't react to the down arrow opening the picker
396 // Highlight the last selected item and scroll it into view
398 itemNode = picker.getNode(lastSelected);
400 picker.highlightItem(itemNode);
401 picker.el.scrollChildIntoView(itemNode, false);
406 <span id='Ext-form-field-Time-method-onCollapse'> /**
408 * Disables the key nav for the Time picker when it is collapsed.
410 onCollapse: function() {
412 keyNav = me.pickerKeyNav;
415 me.ignoreMonitorTab = false;
419 <span id='Ext-form-field-Time-method-onChange'> /**
421 * Clears the highlighted item in the picker on change.
422 * This prevents the highlighted item from being selected instead of the custom typed in value when the tab key is pressed.
424 onChange: function() {
428 me.callParent(arguments);
430 picker.clearHighlight();
434 <span id='Ext-form-field-Time-method-onListSelect'> /**
436 * Handles a time being selected from the Time picker.
438 onListSelect: function(list, recordArray) {
440 record = recordArray[0],
441 val = record ? record.get('date') : null;
443 me.fireEvent('select', me, val);
444 me.picker.clearHighlight();