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-method-constructor'><span id='Ext-form-field-Time'>/**
19 </span></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 * Create a new Time field
57 * @param {Object} config
60 Ext.define('Ext.form.field.Time', {
61 extend:'Ext.form.field.Picker',
62 alias: 'widget.timefield',
63 requires: ['Ext.form.field.Date', 'Ext.picker.Time', 'Ext.view.BoundListKeyNav', 'Ext.Date'],
64 alternateClassName: ['Ext.form.TimeField', 'Ext.form.Time'],
66 <span id='Ext-form-field-Time-cfg-triggerCls'> /**
67 </span> * @cfg {String} triggerCls
68 * An additional CSS class used to style the trigger button. The trigger will always get the
69 * {@link #triggerBaseCls} by default and <tt>triggerCls</tt> will be <b>appended</b> if specified.
70 * Defaults to <tt>'x-form-time-trigger'</tt> for the Time field trigger.
72 triggerCls: Ext.baseCSSPrefix + 'form-time-trigger',
74 <span id='Ext-form-field-Time-cfg-minValue'> /**
75 </span> * @cfg {Date/String} minValue
76 * The minimum allowed time. Can be either a Javascript date object with a valid time value or a string
77 * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).
80 <span id='Ext-form-field-Time-cfg-maxValue'> /**
81 </span> * @cfg {Date/String} maxValue
82 * The maximum allowed time. Can be either a Javascript date object with a valid time value or a string
83 * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).
86 <span id='Ext-form-field-Time-cfg-minText'> /**
87 </span> * @cfg {String} minText
88 * The error text to display when the entered time is before {@link #minValue} (defaults to
89 * 'The time in this field must be equal to or after {0}').
91 minText : "The time in this field must be equal to or after {0}",
93 <span id='Ext-form-field-Time-cfg-maxText'> /**
94 </span> * @cfg {String} maxText
95 * The error text to display when the entered time is after {@link #maxValue} (defaults to
96 * 'The time in this field must be equal to or before {0}').
98 maxText : "The time in this field must be equal to or before {0}",
100 <span id='Ext-form-field-Time-cfg-invalidText'> /**
101 </span> * @cfg {String} invalidText
102 * The error text to display when the time in the field is invalid (defaults to
103 * '{value} is not a valid time').
105 invalidText : "{0} is not a valid time",
107 <span id='Ext-form-field-Time-cfg-format'> /**
108 </span> * @cfg {String} format
109 * The default time format string which can be overriden for localization support. The format must be
110 * valid according to {@link Ext.Date#parse} (defaults to 'g:i A', e.g., '3:15 PM'). For 24-hour time
111 * format try 'H:i' instead.
113 format : "g:i A",
115 <span id='Ext-form-field-Time-cfg-submitFormat'> /**
116 </span> * @cfg {String} submitFormat The date format string which will be submitted to the server.
117 * The format must be valid according to {@link Ext.Date#parse} (defaults to <tt>{@link #format}</tt>).
120 <span id='Ext-form-field-Time-cfg-altFormats'> /**
121 </span> * @cfg {String} altFormats
122 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
123 * 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').
125 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",
127 <span id='Ext-form-field-Time-cfg-increment'> /**
128 </span> * @cfg {Number} increment
129 * The number of minutes between each time value in the list (defaults to 15).
133 <span id='Ext-form-field-Time-cfg-pickerMaxHeight'> /**
134 </span> * @cfg {Number} pickerMaxHeight
135 * The maximum height of the {@link Ext.picker.Time} dropdown. Defaults to 300.
137 pickerMaxHeight: 300,
139 <span id='Ext-form-field-Time-cfg-selectOnTab'> /**
140 </span> * @cfg {Boolean} selectOnTab
141 * Whether the Tab key should select the currently highlighted item. Defaults to <tt>true</tt>.
145 <span id='Ext-form-field-Time-property-initDate'> /**
147 * This is the date to use when generating time values in the absence of either minValue
148 * or maxValue. Using the current date causes DST issues on DST boundary dates, so this is an
149 * arbitrary "safe" date that can be any date aside from DST boundary dates.
151 initDate: '1/1/2008',
152 initDateFormat: 'j/n/Y',
155 initComponent: function() {
168 initValue: function() {
172 // If a String value was supplied, try to convert it to a proper Date object
173 if (Ext.isString(value)) {
174 me.value = me.rawToValue(value);
180 <span id='Ext-form-field-Time-method-setMinValue'> /**
181 </span> * Replaces any existing {@link #minValue} with the new time and refreshes the picker's range.
182 * @param {Date/String} value The minimum time that can be selected
184 setMinValue: function(value) {
187 me.setLimit(value, true);
189 picker.setMinValue(me.minValue);
193 <span id='Ext-form-field-Time-method-setMaxValue'> /**
194 </span> * Replaces any existing {@link #maxValue} with the new time and refreshes the picker's range.
195 * @param {Date/String} value The maximum time that can be selected
197 setMaxValue: function(value) {
200 me.setLimit(value, false);
202 picker.setMaxValue(me.maxValue);
206 <span id='Ext-form-field-Time-method-setLimit'> /**
208 * Updates either the min or max value. Converts the user's value into a Date object whose
209 * year/month/day is set to the {@link #initDate} so that only the time fields are significant.
211 setLimit: function(value, isMin) {
214 if (Ext.isString(value)) {
215 d = me.parseDate(value);
217 else if (Ext.isDate(value)) {
221 val = Ext.Date.clearTime(new Date(me.initDate));
222 val.setHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
223 me[isMin ? 'minValue' : 'maxValue'] = val;
227 rawToValue: function(rawValue) {
228 return this.parseDate(rawValue) || rawValue || null;
231 valueToRaw: function(value) {
232 return this.formatDate(this.parseDate(value));
235 <span id='Ext-form-field-Time-method-getErrors'> /**
236 </span> * Runs all of Time's validations and returns an array of any errors. Note that this first
237 * runs Text's validations, so the returned array is an amalgamation of all field errors.
238 * The additional validation checks are testing that the time format is valid, that the chosen
239 * time is within the {@link #minValue} and {@link #maxValue} constraints set.
240 * @param {Mixed} value The value to get errors for (defaults to the current field value)
241 * @return {Array} All validation errors for this field
243 getErrors: function(value) {
245 format = Ext.String.format,
246 errors = me.callParent(arguments),
247 minValue = me.minValue,
248 maxValue = me.maxValue,
251 value = me.formatDate(value || me.processRawValue(me.getRawValue()));
253 if (value === null || value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
257 date = me.parseDate(value);
259 errors.push(format(me.invalidText, value, me.format));
263 if (minValue && date < minValue) {
264 errors.push(format(me.minText, me.formatDate(minValue)));
267 if (maxValue && date > maxValue) {
268 errors.push(format(me.maxText, me.formatDate(maxValue)));
274 formatDate: function() {
275 return Ext.form.field.Date.prototype.formatDate.apply(this, arguments);
278 <span id='Ext-form-field-Time-method-parseDate'> /**
280 * Parses an input value into a valid Date object.
281 * @param {String/Date} value
283 parseDate: function(value) {
284 if (!value || Ext.isDate(value)) {
289 val = me.safeParse(value, me.format),
290 altFormats = me.altFormats,
291 altFormatsArray = me.altFormatsArray,
295 if (!val && altFormats) {
296 altFormatsArray = altFormatsArray || altFormats.split('|');
297 len = altFormatsArray.length;
298 for (; i < len && !val; ++i) {
299 val = me.safeParse(value, altFormatsArray[i]);
305 safeParse: function(value, format){
311 if (utilDate.formatContainsDateInfo(format)) {
312 // assume we've been given a full date
313 result = utilDate.parse(value, format);
315 // Use our initial safe date
316 parsedDate = utilDate.parse(me.initDate + ' ' + value, me.initDateFormat + ' ' + format);
325 getSubmitValue: function() {
327 format = me.submitFormat || me.format,
328 value = me.getValue();
330 return value ? Ext.Date.format(value, format) : null;
333 <span id='Ext-form-field-Time-method-createPicker'> /**
335 * Creates the {@link Ext.picker.Time}
337 createPicker: function() {
339 picker = Ext.create('Ext.picker.Time', {
345 minValue: me.minValue,
346 maxValue: me.maxValue,
347 increment: me.increment,
349 ownerCt: this.ownerCt,
350 renderTo: document.body,
351 maxHeight: me.pickerMaxHeight,
352 focusOnToFront: false
355 me.mon(picker.getSelectionModel(), {
356 selectionchange: me.onListSelect,
363 <span id='Ext-form-field-Time-method-onExpand'> /**
365 * Enables the key nav for the Time picker when it is expanded.
366 * TODO this is largely the same logic as ComboBox, should factor out.
368 onExpand: function() {
370 keyNav = me.pickerKeyNav,
371 selectOnTab = me.selectOnTab,
372 picker = me.getPicker(),
373 lastSelected = picker.getSelectionModel().lastSelected,
377 keyNav = me.pickerKeyNav = Ext.create('Ext.view.BoundListKeyNav', this.inputEl, {
382 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-onListSelect'> /**
421 * Handles a time being selected from the Time picker.
423 onListSelect: function(list, recordArray) {
425 record = recordArray[0],
426 val = record ? record.get('date') : null;
428 me.fireEvent('select', me, val);
429 me.picker.clearHighlight();