Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / widgets / form / TimeField.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.form.TimeField\r
9  * @extends Ext.form.ComboBox\r
10  * Provides a time input field with a time dropdown and automatic time validation.  Example usage:\r
11  * <pre><code>\r
12 new Ext.form.TimeField({\r
13     minValue: '9:00 AM',\r
14     maxValue: '6:00 PM',\r
15     increment: 30\r
16 });\r
17 </code></pre>\r
18  * @constructor\r
19  * Create a new TimeField\r
20  * @param {Object} config\r
21  * @xtype timefield\r
22  */\r
23 Ext.form.TimeField = Ext.extend(Ext.form.ComboBox, {\r
24     /**\r
25      * @cfg {Date/String} minValue\r
26      * The minimum allowed time. Can be either a Javascript date object with a valid time value or a string \r
27      * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).\r
28      */\r
29     minValue : undefined,\r
30     /**\r
31      * @cfg {Date/String} maxValue\r
32      * The maximum allowed time. Can be either a Javascript date object with a valid time value or a string \r
33      * time in a valid format -- see {@link #format} and {@link #altFormats} (defaults to undefined).\r
34      */\r
35     maxValue : undefined,\r
36     /**\r
37      * @cfg {String} minText\r
38      * The error text to display when the date in the cell is before minValue (defaults to\r
39      * 'The time in this field must be equal to or after {0}').\r
40      */\r
41     minText : "The time in this field must be equal to or after {0}",\r
42     /**\r
43      * @cfg {String} maxText\r
44      * The error text to display when the time is after maxValue (defaults to\r
45      * 'The time in this field must be equal to or before {0}').\r
46      */\r
47     maxText : "The time in this field must be equal to or before {0}",\r
48     /**\r
49      * @cfg {String} invalidText\r
50      * The error text to display when the time in the field is invalid (defaults to\r
51      * '{value} is not a valid time').\r
52      */\r
53     invalidText : "{0} is not a valid time",\r
54     /**\r
55      * @cfg {String} format\r
56      * The default time format string which can be overriden for localization support.  The format must be\r
57      * valid according to {@link Date#parseDate} (defaults to 'g:i A', e.g., '3:15 PM').  For 24-hour time\r
58      * format try 'H:i' instead.\r
59      */\r
60     format : "g:i A",\r
61     /**\r
62      * @cfg {String} altFormats\r
63      * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined\r
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').\r
65      */\r
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",\r
67     /**\r
68      * @cfg {Number} increment\r
69      * The number of minutes between each time value in the list (defaults to 15).\r
70      */\r
71     increment: 15,\r
72 \r
73     // private override\r
74     mode: 'local',\r
75     // private override\r
76     triggerAction: 'all',\r
77     // private override\r
78     typeAhead: false,\r
79     \r
80     // private - This is the date to use when generating time values in the absence of either minValue\r
81     // or maxValue.  Using the current date causes DST issues on DST boundary dates, so this is an \r
82     // arbitrary "safe" date that can be any date aside from DST boundary dates.\r
83     initDate: '1/1/2008',\r
84 \r
85     // private\r
86     initComponent : function(){\r
87         if(Ext.isDefined(this.minValue)){\r
88             this.setMinValue(this.minValue, true);\r
89         }\r
90         if(Ext.isDefined(this.maxValue)){\r
91             this.setMaxValue(this.maxValue, true);\r
92         }\r
93         if(!this.store){\r
94             this.generateStore(true);\r
95         }\r
96         Ext.form.TimeField.superclass.initComponent.call(this);\r
97     },\r
98     \r
99     /**\r
100      * Replaces any existing {@link #minValue} with the new time and refreshes the store.\r
101      * @param {Date/String} value The minimum time that can be selected\r
102      */\r
103     setMinValue: function(value, /* private */ initial){\r
104         this.setLimit(value, true, initial);\r
105         return this;\r
106     },\r
107 \r
108     /**\r
109      * Replaces any existing {@link #maxValue} with the new time and refreshes the store.\r
110      * @param {Date/String} value The maximum time that can be selected\r
111      */\r
112     setMaxValue: function(value, /* private */ initial){\r
113         this.setLimit(value, false, initial);\r
114         return this;\r
115     },\r
116     \r
117     // private\r
118     generateStore: function(initial){\r
119         var min = this.minValue || new Date(this.initDate).clearTime(),\r
120             max = this.maxValue || new Date(this.initDate).clearTime().add('mi', (24 * 60) - 1),\r
121             times = [];\r
122             \r
123         while(min <= max){\r
124             times.push(min.dateFormat(this.format));\r
125             min = min.add('mi', this.increment);\r
126         }\r
127         this.bindStore(times, initial);\r
128     },\r
129 \r
130     // private\r
131     setLimit: function(value, isMin, initial){\r
132         var d;\r
133         if(Ext.isString(value)){\r
134             d = this.parseDate(value);\r
135         }else if(Ext.isDate(value)){\r
136             d = value;\r
137         }\r
138         if(d){\r
139             var val = new Date(this.initDate).clearTime();\r
140             val.setHours(d.getHours(), d.getMinutes(), isMin ? 0 : 59, 0);\r
141             this[isMin ? 'minValue' : 'maxValue'] = val;\r
142             if(!initial){\r
143                 this.generateStore();\r
144             }\r
145         }\r
146     },\r
147     \r
148     // inherited docs\r
149     getValue : function(){\r
150         var v = Ext.form.TimeField.superclass.getValue.call(this);\r
151         return this.formatDate(this.parseDate(v)) || '';\r
152     },\r
153 \r
154     // inherited docs\r
155     setValue : function(value){\r
156         return Ext.form.TimeField.superclass.setValue.call(this, this.formatDate(this.parseDate(value)));\r
157     },\r
158 \r
159     // private overrides\r
160     validateValue : Ext.form.DateField.prototype.validateValue,\r
161     parseDate : Ext.form.DateField.prototype.parseDate,\r
162     formatDate : Ext.form.DateField.prototype.formatDate,\r
163 \r
164     // private\r
165     beforeBlur : function(){\r
166         var v = this.parseDate(this.getRawValue());\r
167         if(v){\r
168             this.setValue(v.dateFormat(this.format));\r
169         }\r
170         Ext.form.TimeField.superclass.beforeBlur.call(this);\r
171     }\r
172 \r
173     /**\r
174      * @cfg {Boolean} grow @hide\r
175      */\r
176     /**\r
177      * @cfg {Number} growMin @hide\r
178      */\r
179     /**\r
180      * @cfg {Number} growMax @hide\r
181      */\r
182     /**\r
183      * @hide\r
184      * @method autoSize\r
185      */\r
186 });\r
187 Ext.reg('timefield', Ext.form.TimeField);