3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.form.TimeField
9 * @extends Ext.form.ComboBox
10 * Provides a time input field with a time dropdown and automatic time validation. Example usage:
12 new Ext.form.TimeField({
19 * Create a new TimeField
20 * @param {Object} config
23 Ext.form.TimeField = Ext.extend(Ext.form.ComboBox, {
25 * @cfg {Date/String} minValue
26 * The minimum allowed time. Can be either a Javascript date object with a valid time value or a string
27 * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).
31 * @cfg {Date/String} maxValue
32 * The maximum allowed time. Can be either a Javascript date object with a valid time value or a string
33 * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).
37 * @cfg {String} minText
38 * The error text to display when the date in the cell is before minValue (defaults to
39 * 'The time in this field must be equal to or after {0}').
41 minText : "The time in this field must be equal to or after {0}",
43 * @cfg {String} maxText
44 * The error text to display when the time is after maxValue (defaults to
45 * 'The time in this field must be equal to or before {0}').
47 maxText : "The time in this field must be equal to or before {0}",
49 * @cfg {String} invalidText
50 * The error text to display when the time in the field is invalid (defaults to
51 * '{value} is not a valid time').
53 invalidText : "{0} is not a valid time",
55 * @cfg {String} format
56 * The default time format string which can be overriden for localization support. The format must be
57 * valid according to {@link Date#parseDate} (defaults to 'g:i A', e.g., '3:15 PM'). For 24-hour time
58 * format try 'H:i' instead.
62 * @cfg {String} altFormats
63 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
64 * 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').
66 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",
68 * @cfg {Number} increment
69 * The number of minutes between each time value in the list (defaults to 15).
80 // private - This is the date to use when generating time values in the absence of either minValue
81 // or maxValue. Using the current date causes DST issues on DST boundary dates, so this is an
82 // arbitrary "safe" date that can be any date aside from DST boundary dates.
85 initDateFormat: 'j/n/Y',
88 initComponent : function(){
89 if(Ext.isDefined(this.minValue)){
90 this.setMinValue(this.minValue, true);
92 if(Ext.isDefined(this.maxValue)){
93 this.setMaxValue(this.maxValue, true);
96 this.generateStore(true);
98 Ext.form.TimeField.superclass.initComponent.call(this);
102 * Replaces any existing {@link #minValue} with the new time and refreshes the store.
103 * @param {Date/String} value The minimum time that can be selected
105 setMinValue: function(value, /* private */ initial){
106 this.setLimit(value, true, initial);
111 * Replaces any existing {@link #maxValue} with the new time and refreshes the store.
112 * @param {Date/String} value The maximum time that can be selected
114 setMaxValue: function(value, /* private */ initial){
115 this.setLimit(value, false, initial);
120 generateStore: function(initial){
121 var min = this.minValue || new Date(this.initDate).clearTime(),
122 max = this.maxValue || new Date(this.initDate).clearTime().add('mi', (24 * 60) - 1),
126 times.push(min.dateFormat(this.format));
127 min = min.add('mi', this.increment);
129 this.bindStore(times, initial);
133 setLimit: function(value, isMin, initial){
135 if(Ext.isString(value)){
136 d = this.parseDate(value);
137 }else if(Ext.isDate(value)){
141 var val = new Date(this.initDate).clearTime();
142 val.setHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
143 this[isMin ? 'minValue' : 'maxValue'] = val;
145 this.generateStore();
151 getValue : function(){
152 var v = Ext.form.TimeField.superclass.getValue.call(this);
153 return this.formatDate(this.parseDate(v)) || '';
157 setValue : function(value){
158 return Ext.form.TimeField.superclass.setValue.call(this, this.formatDate(this.parseDate(value)));
162 validateValue : Ext.form.DateField.prototype.validateValue,
164 formatDate : Ext.form.DateField.prototype.formatDate,
166 parseDate: function(value) {
167 if (!value || Ext.isDate(value)) {
171 var id = this.initDate + ' ',
172 idf = this.initDateFormat + ' ',
173 v = Date.parseDate(id + value, idf + this.format), // *** handle DST. note: this.format is a TIME-only format
174 af = this.altFormats;
177 if (!this.altFormatsArray) {
178 this.altFormatsArray = af.split("|");
180 for (var i = 0, afa = this.altFormatsArray, len = afa.length; i < len && !v; i++) {
181 v = Date.parseDate(id + value, idf + afa[i]);
188 Ext.reg('timefield', Ext.form.TimeField);