Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / src / widgets / form / TimeField.js
1 /*!
2  * Ext JS Library 3.3.1
3  * Copyright(c) 2006-2010 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.com/license
6  */
7 /**
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:
11  * <pre><code>
12 new Ext.form.TimeField({
13     minValue: '9:00 AM',
14     maxValue: '6:00 PM',
15     increment: 30
16 });
17 </code></pre>
18  * @constructor
19  * Create a new TimeField
20  * @param {Object} config
21  * @xtype timefield
22  */
23 Ext.form.TimeField = Ext.extend(Ext.form.ComboBox, {
24     /**
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).
28      */
29     minValue : undefined,
30     /**
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).
34      */
35     maxValue : undefined,
36     /**
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}').
40      */
41     minText : "The time in this field must be equal to or after {0}",
42     /**
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}').
46      */
47     maxText : "The time in this field must be equal to or before {0}",
48     /**
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').
52      */
53     invalidText : "{0} is not a valid time",
54     /**
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.
59      */
60     format : "g:i A",
61     /**
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').
65      */
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",
67     /**
68      * @cfg {Number} increment
69      * The number of minutes between each time value in the list (defaults to 15).
70      */
71     increment: 15,
72
73     // private override
74     mode: 'local',
75     // private override
76     triggerAction: 'all',
77     // private override
78     typeAhead: false,
79
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.
83     initDate: '1/1/2008',
84
85     initDateFormat: 'j/n/Y',
86
87     // private
88     initComponent : function(){
89         if(Ext.isDefined(this.minValue)){
90             this.setMinValue(this.minValue, true);
91         }
92         if(Ext.isDefined(this.maxValue)){
93             this.setMaxValue(this.maxValue, true);
94         }
95         if(!this.store){
96             this.generateStore(true);
97         }
98         Ext.form.TimeField.superclass.initComponent.call(this);
99     },
100
101     /**
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
104      */
105     setMinValue: function(value, /* private */ initial){
106         this.setLimit(value, true, initial);
107         return this;
108     },
109
110     /**
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
113      */
114     setMaxValue: function(value, /* private */ initial){
115         this.setLimit(value, false, initial);
116         return this;
117     },
118
119     // private
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),
123             times = [];
124
125         while(min <= max){
126             times.push(min.dateFormat(this.format));
127             min = min.add('mi', this.increment);
128         }
129         this.bindStore(times, initial);
130     },
131
132     // private
133     setLimit: function(value, isMin, initial){
134         var d;
135         if(Ext.isString(value)){
136             d = this.parseDate(value);
137         }else if(Ext.isDate(value)){
138             d = value;
139         }
140         if(d){
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;
144             if(!initial){
145                 this.generateStore();
146             }
147         }
148     },
149
150     // inherited docs
151     getValue : function(){
152         var v = Ext.form.TimeField.superclass.getValue.call(this);
153         return this.formatDate(this.parseDate(v)) || '';
154     },
155
156     // inherited docs
157     setValue : function(value){
158         return Ext.form.TimeField.superclass.setValue.call(this, this.formatDate(this.parseDate(value)));
159     },
160
161     // private overrides
162     validateValue : Ext.form.DateField.prototype.validateValue,
163
164     formatDate : Ext.form.DateField.prototype.formatDate,
165
166     parseDate: function(value) {
167         if (!value || Ext.isDate(value)) {
168             return value;
169         }
170
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;
175
176         if (!v && af) {
177             if (!this.altFormatsArray) {
178                 this.altFormatsArray = af.split("|");
179             }
180             for (var i = 0, afa = this.altFormatsArray, len = afa.length; i < len && !v; i++) {
181                 v = Date.parseDate(id + value, idf + afa[i]);
182             }
183         }
184
185         return v;
186     }
187 });
188 Ext.reg('timefield', Ext.form.TimeField);