27 |
jpm |
1 |
/*
|
|
|
2 |
* Ext JS Library 2.0.2
|
|
|
3 |
* Copyright(c) 2006-2008, Ext JS, LLC.
|
|
|
4 |
* licensing@extjs.com
|
|
|
5 |
*
|
|
|
6 |
* http://extjs.com/license
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @class Ext.form.DateField
|
|
|
11 |
* @extends Ext.form.TriggerField
|
|
|
12 |
* Provides a date input field with a {@link Ext.DatePicker} dropdown and automatic date validation.
|
|
|
13 |
* @constructor
|
|
|
14 |
* Create a new DateField
|
|
|
15 |
* @param {Object} config
|
|
|
16 |
*/
|
|
|
17 |
Ext.form.DateField = Ext.extend(Ext.form.TriggerField, {
|
|
|
18 |
/**
|
|
|
19 |
* @cfg {String} format
|
|
|
20 |
* The default date format string which can be overriden for localization support. The format must be
|
|
|
21 |
* valid according to {@link Date#parseDate} (defaults to 'm/d/y').
|
|
|
22 |
*/
|
|
|
23 |
format : "m/d/y",
|
|
|
24 |
/**
|
|
|
25 |
* @cfg {String} altFormats
|
|
|
26 |
* Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
|
|
|
27 |
* format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
|
|
|
28 |
*/
|
|
|
29 |
altFormats : "m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d",
|
|
|
30 |
/**
|
|
|
31 |
* @cfg {Array} disabledDays
|
|
|
32 |
* An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
|
|
|
33 |
*/
|
|
|
34 |
disabledDays : null,
|
|
|
35 |
/**
|
|
|
36 |
* @cfg {String} disabledDaysText
|
|
|
37 |
* The tooltip to display when the date falls on a disabled day (defaults to 'Disabled')
|
|
|
38 |
*/
|
|
|
39 |
disabledDaysText : "Disabled",
|
|
|
40 |
/**
|
|
|
41 |
* @cfg {Array} disabledDates
|
|
|
42 |
* An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
|
|
|
43 |
* expression so they are very powerful. Some examples:
|
|
|
44 |
* <ul>
|
|
|
45 |
* <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
|
|
|
46 |
* <li>["03/08", "09/16"] would disable those days for every year</li>
|
|
|
47 |
* <li>["^03/08"] would only match the beginning (useful if you are using short years)</li>
|
|
|
48 |
* <li>["03/../2006"] would disable every day in March 2006</li>
|
|
|
49 |
* <li>["^03"] would disable every day in every March</li>
|
|
|
50 |
* </ul>
|
|
|
51 |
* In order to support regular expressions, if you are using a date format that has "." in it, you will have to
|
|
|
52 |
* escape the dot when restricting dates. For example: ["03\\.08\\.03"].
|
|
|
53 |
*/
|
|
|
54 |
disabledDates : null,
|
|
|
55 |
/**
|
|
|
56 |
* @cfg {String} disabledDatesText
|
|
|
57 |
* The tooltip text to display when the date falls on a disabled date (defaults to 'Disabled')
|
|
|
58 |
*/
|
|
|
59 |
disabledDatesText : "Disabled",
|
|
|
60 |
/**
|
|
|
61 |
* @cfg {Date/String} minValue
|
|
|
62 |
* The minimum allowed date. Can be either a Javascript date object or a string date in a
|
|
|
63 |
* valid format (defaults to null).
|
|
|
64 |
*/
|
|
|
65 |
minValue : null,
|
|
|
66 |
/**
|
|
|
67 |
* @cfg {Date/String} maxValue
|
|
|
68 |
* The maximum allowed date. Can be either a Javascript date object or a string date in a
|
|
|
69 |
* valid format (defaults to null).
|
|
|
70 |
*/
|
|
|
71 |
maxValue : null,
|
|
|
72 |
/**
|
|
|
73 |
* @cfg {String} minText
|
|
|
74 |
* The error text to display when the date in the cell is before minValue (defaults to
|
|
|
75 |
* 'The date in this field must be after {minValue}').
|
|
|
76 |
*/
|
|
|
77 |
minText : "The date in this field must be equal to or after {0}",
|
|
|
78 |
/**
|
|
|
79 |
* @cfg {String} maxText
|
|
|
80 |
* The error text to display when the date in the cell is after maxValue (defaults to
|
|
|
81 |
* 'The date in this field must be before {maxValue}').
|
|
|
82 |
*/
|
|
|
83 |
maxText : "The date in this field must be equal to or before {0}",
|
|
|
84 |
/**
|
|
|
85 |
* @cfg {String} invalidText
|
|
|
86 |
* The error text to display when the date in the field is invalid (defaults to
|
|
|
87 |
* '{value} is not a valid date - it must be in the format {format}').
|
|
|
88 |
*/
|
|
|
89 |
invalidText : "{0} is not a valid date - it must be in the format {1}",
|
|
|
90 |
/**
|
|
|
91 |
* @cfg {String} triggerClass
|
|
|
92 |
* An additional CSS class used to style the trigger button. The trigger will always get the
|
|
|
93 |
* class 'x-form-trigger' and triggerClass will be <b>appended</b> if specified (defaults to 'x-form-date-trigger'
|
|
|
94 |
* which displays a calendar icon).
|
|
|
95 |
*/
|
|
|
96 |
triggerClass : 'x-form-date-trigger',
|
|
|
97 |
/**
|
|
|
98 |
* @cfg {String/Object} autoCreate
|
|
|
99 |
* A DomHelper element spec, or true for a default element spec (defaults to
|
|
|
100 |
* {tag: "input", type: "text", size: "10", autocomplete: "off"})
|
|
|
101 |
*/
|
|
|
102 |
|
|
|
103 |
// private
|
|
|
104 |
defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
|
|
|
105 |
|
|
|
106 |
initComponent : function(){
|
|
|
107 |
Ext.form.DateField.superclass.initComponent.call(this);
|
|
|
108 |
if(typeof this.minValue == "string"){
|
|
|
109 |
this.minValue = this.parseDate(this.minValue);
|
|
|
110 |
}
|
|
|
111 |
if(typeof this.maxValue == "string"){
|
|
|
112 |
this.maxValue = this.parseDate(this.maxValue);
|
|
|
113 |
}
|
|
|
114 |
this.ddMatch = null;
|
|
|
115 |
if(this.disabledDates){
|
|
|
116 |
var dd = this.disabledDates;
|
|
|
117 |
var re = "(?:";
|
|
|
118 |
for(var i = 0; i < dd.length; i++){
|
|
|
119 |
re += dd[i];
|
|
|
120 |
if(i != dd.length-1) re += "|";
|
|
|
121 |
}
|
|
|
122 |
this.ddMatch = new RegExp(re + ")");
|
|
|
123 |
}
|
|
|
124 |
},
|
|
|
125 |
|
|
|
126 |
// private
|
|
|
127 |
validateValue : function(value){
|
|
|
128 |
value = this.formatDate(value);
|
|
|
129 |
if(!Ext.form.DateField.superclass.validateValue.call(this, value)){
|
|
|
130 |
return false;
|
|
|
131 |
}
|
|
|
132 |
if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
|
|
|
133 |
return true;
|
|
|
134 |
}
|
|
|
135 |
var svalue = value;
|
|
|
136 |
value = this.parseDate(value);
|
|
|
137 |
if(!value){
|
|
|
138 |
this.markInvalid(String.format(this.invalidText, svalue, this.format));
|
|
|
139 |
return false;
|
|
|
140 |
}
|
|
|
141 |
var time = value.getTime();
|
|
|
142 |
if(this.minValue && time < this.minValue.getTime()){
|
|
|
143 |
this.markInvalid(String.format(this.minText, this.formatDate(this.minValue)));
|
|
|
144 |
return false;
|
|
|
145 |
}
|
|
|
146 |
if(this.maxValue && time > this.maxValue.getTime()){
|
|
|
147 |
this.markInvalid(String.format(this.maxText, this.formatDate(this.maxValue)));
|
|
|
148 |
return false;
|
|
|
149 |
}
|
|
|
150 |
if(this.disabledDays){
|
|
|
151 |
var day = value.getDay();
|
|
|
152 |
for(var i = 0; i < this.disabledDays.length; i++) {
|
|
|
153 |
if(day === this.disabledDays[i]){
|
|
|
154 |
this.markInvalid(this.disabledDaysText);
|
|
|
155 |
return false;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
var fvalue = this.formatDate(value);
|
|
|
160 |
if(this.ddMatch && this.ddMatch.test(fvalue)){
|
|
|
161 |
this.markInvalid(String.format(this.disabledDatesText, fvalue));
|
|
|
162 |
return false;
|
|
|
163 |
}
|
|
|
164 |
return true;
|
|
|
165 |
},
|
|
|
166 |
|
|
|
167 |
// private
|
|
|
168 |
// Provides logic to override the default TriggerField.validateBlur which just returns true
|
|
|
169 |
validateBlur : function(){
|
|
|
170 |
return !this.menu || !this.menu.isVisible();
|
|
|
171 |
},
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Returns the current date value of the date field.
|
|
|
175 |
* @return {Date} The date value
|
|
|
176 |
*/
|
|
|
177 |
getValue : function(){
|
|
|
178 |
return this.parseDate(Ext.form.DateField.superclass.getValue.call(this)) || "";
|
|
|
179 |
},
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid
|
|
|
183 |
* date, using DateField.format as the date format, according to the same rules as {@link Date#parseDate}
|
|
|
184 |
* (the default format used is "m/d/y").
|
|
|
185 |
* <br />Usage:
|
|
|
186 |
* <pre><code>
|
|
|
187 |
//All of these calls set the same date value (May 4, 2006)
|
|
|
188 |
|
|
|
189 |
//Pass a date object:
|
|
|
190 |
var dt = new Date('5/4/06');
|
|
|
191 |
dateField.setValue(dt);
|
|
|
192 |
|
|
|
193 |
//Pass a date string (default format):
|
|
|
194 |
dateField.setValue('5/4/06');
|
|
|
195 |
|
|
|
196 |
//Pass a date string (custom format):
|
|
|
197 |
dateField.format = 'Y-m-d';
|
|
|
198 |
dateField.setValue('2006-5-4');
|
|
|
199 |
</code></pre>
|
|
|
200 |
* @param {String/Date} date The date or valid date string
|
|
|
201 |
*/
|
|
|
202 |
setValue : function(date){
|
|
|
203 |
Ext.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
|
|
|
204 |
},
|
|
|
205 |
|
|
|
206 |
// private
|
|
|
207 |
parseDate : function(value){
|
|
|
208 |
if(!value || Ext.isDate(value)){
|
|
|
209 |
return value;
|
|
|
210 |
}
|
|
|
211 |
var v = Date.parseDate(value, this.format);
|
|
|
212 |
if(!v && this.altFormats){
|
|
|
213 |
if(!this.altFormatsArray){
|
|
|
214 |
this.altFormatsArray = this.altFormats.split("|");
|
|
|
215 |
}
|
|
|
216 |
for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
|
|
|
217 |
v = Date.parseDate(value, this.altFormatsArray[i]);
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
return v;
|
|
|
221 |
},
|
|
|
222 |
|
|
|
223 |
// private
|
|
|
224 |
onDestroy : function(){
|
|
|
225 |
if(this.menu) {
|
|
|
226 |
this.menu.destroy();
|
|
|
227 |
}
|
|
|
228 |
if(this.wrap){
|
|
|
229 |
this.wrap.remove();
|
|
|
230 |
}
|
|
|
231 |
Ext.form.DateField.superclass.onDestroy.call(this);
|
|
|
232 |
},
|
|
|
233 |
|
|
|
234 |
// private
|
|
|
235 |
formatDate : function(date){
|
|
|
236 |
return Ext.isDate(date) ? date.dateFormat(this.format) : date;
|
|
|
237 |
},
|
|
|
238 |
|
|
|
239 |
// private
|
|
|
240 |
menuListeners : {
|
|
|
241 |
select: function(m, d){
|
|
|
242 |
this.setValue(d);
|
|
|
243 |
},
|
|
|
244 |
show : function(){ // retain focus styling
|
|
|
245 |
this.onFocus();
|
|
|
246 |
},
|
|
|
247 |
hide : function(){
|
|
|
248 |
this.focus.defer(10, this);
|
|
|
249 |
var ml = this.menuListeners;
|
|
|
250 |
this.menu.un("select", ml.select, this);
|
|
|
251 |
this.menu.un("show", ml.show, this);
|
|
|
252 |
this.menu.un("hide", ml.hide, this);
|
|
|
253 |
}
|
|
|
254 |
},
|
|
|
255 |
|
|
|
256 |
// private
|
|
|
257 |
// Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
|
|
|
258 |
onTriggerClick : function(){
|
|
|
259 |
if(this.disabled){
|
|
|
260 |
return;
|
|
|
261 |
}
|
|
|
262 |
if(this.menu == null){
|
|
|
263 |
this.menu = new Ext.menu.DateMenu();
|
|
|
264 |
}
|
|
|
265 |
Ext.apply(this.menu.picker, {
|
|
|
266 |
minDate : this.minValue,
|
|
|
267 |
maxDate : this.maxValue,
|
|
|
268 |
disabledDatesRE : this.ddMatch,
|
|
|
269 |
disabledDatesText : this.disabledDatesText,
|
|
|
270 |
disabledDays : this.disabledDays,
|
|
|
271 |
disabledDaysText : this.disabledDaysText,
|
|
|
272 |
format : this.format,
|
|
|
273 |
minText : String.format(this.minText, this.formatDate(this.minValue)),
|
|
|
274 |
maxText : String.format(this.maxText, this.formatDate(this.maxValue))
|
|
|
275 |
});
|
|
|
276 |
this.menu.on(Ext.apply({}, this.menuListeners, {
|
|
|
277 |
scope:this
|
|
|
278 |
}));
|
|
|
279 |
this.menu.picker.setValue(this.getValue() || new Date());
|
|
|
280 |
this.menu.show(this.el, "tl-bl?");
|
|
|
281 |
},
|
|
|
282 |
|
|
|
283 |
beforeBlur : function(){
|
|
|
284 |
var v = this.parseDate(this.getRawValue());
|
|
|
285 |
if(v){
|
|
|
286 |
this.setValue(v);
|
|
|
287 |
}
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
/**
|
|
|
291 |
* @cfg {Boolean} grow @hide
|
|
|
292 |
*/
|
|
|
293 |
/**
|
|
|
294 |
* @cfg {Number} growMin @hide
|
|
|
295 |
*/
|
|
|
296 |
/**
|
|
|
297 |
* @cfg {Number} growMax @hide
|
|
|
298 |
*/
|
|
|
299 |
/**
|
|
|
300 |
* @hide
|
|
|
301 |
* @method autoSize
|
|
|
302 |
*/
|
|
|
303 |
});
|
|
|
304 |
Ext.reg('datefield', Ext.form.DateField);
|