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