3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.form.DateField"></div>/**
16 * @class Ext.form.DateField
17 * @extends Ext.form.TriggerField
18 * Provides a date input field with a {@link Ext.DatePicker} dropdown and automatic date validation.
20 * Create a new DateField
21 * @param {Object} config
24 Ext.form.DateField = Ext.extend(Ext.form.TriggerField, {
25 <div id="cfg-Ext.form.DateField-format"></div>/**
26 * @cfg {String} format
27 * The default date format string which can be overriden for localization support. The format must be
28 * valid according to {@link Date#parseDate} (defaults to <tt>'m/d/Y'</tt>).
31 <div id="cfg-Ext.form.DateField-altFormats"></div>/**
32 * @cfg {String} altFormats
33 * Multiple date formats separated by "<tt>|</tt>" to try when parsing a user input value and it
34 * does not match the defined format (defaults to
35 * <tt>'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'</tt>).
37 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",
38 <div id="cfg-Ext.form.DateField-disabledDaysText"></div>/**
39 * @cfg {String} disabledDaysText
40 * The tooltip to display when the date falls on a disabled day (defaults to <tt>'Disabled'</tt>)
42 disabledDaysText : "Disabled",
43 <div id="cfg-Ext.form.DateField-disabledDatesText"></div>/**
44 * @cfg {String} disabledDatesText
45 * The tooltip text to display when the date falls on a disabled date (defaults to <tt>'Disabled'</tt>)
47 disabledDatesText : "Disabled",
48 <div id="cfg-Ext.form.DateField-minText"></div>/**
49 * @cfg {String} minText
50 * The error text to display when the date in the cell is before <tt>{@link #minValue}</tt> (defaults to
51 * <tt>'The date in this field must be after {minValue}'</tt>).
53 minText : "The date in this field must be equal to or after {0}",
54 <div id="cfg-Ext.form.DateField-maxText"></div>/**
55 * @cfg {String} maxText
56 * The error text to display when the date in the cell is after <tt>{@link #maxValue}</tt> (defaults to
57 * <tt>'The date in this field must be before {maxValue}'</tt>).
59 maxText : "The date in this field must be equal to or before {0}",
60 <div id="cfg-Ext.form.DateField-invalidText"></div>/**
61 * @cfg {String} invalidText
62 * The error text to display when the date in the field is invalid (defaults to
63 * <tt>'{value} is not a valid date - it must be in the format {format}'</tt>).
65 invalidText : "{0} is not a valid date - it must be in the format {1}",
66 <div id="cfg-Ext.form.DateField-triggerClass"></div>/**
67 * @cfg {String} triggerClass
68 * An additional CSS class used to style the trigger button. The trigger will always get the
69 * class <tt>'x-form-trigger'</tt> and <tt>triggerClass</tt> will be <b>appended</b> if specified
70 * (defaults to <tt>'x-form-date-trigger'</tt> which displays a calendar icon).
72 triggerClass : 'x-form-date-trigger',
73 <div id="cfg-Ext.form.DateField-showToday"></div>/**
74 * @cfg {Boolean} showToday
75 * <tt>false</tt> to hide the footer area of the DatePicker containing the Today button and disable
76 * the keyboard handler for spacebar that selects the current date (defaults to <tt>true</tt>).
79 <div id="cfg-Ext.form.DateField-minValue"></div>/**
80 * @cfg {Date/String} minValue
81 * The minimum allowed date. Can be either a Javascript date object or a string date in a
82 * valid format (defaults to null).
84 <div id="cfg-Ext.form.DateField-maxValue"></div>/**
85 * @cfg {Date/String} maxValue
86 * The maximum allowed date. Can be either a Javascript date object or a string date in a
87 * valid format (defaults to null).
89 <div id="cfg-Ext.form.DateField-disabledDays"></div>/**
90 * @cfg {Array} disabledDays
91 * An array of days to disable, 0 based (defaults to null). Some examples:<pre><code>
92 // disable Sunday and Saturday:
95 disabledDays: [1,2,3,4,5]
98 <div id="cfg-Ext.form.DateField-disabledDates"></div>/**
99 * @cfg {Array} disabledDates
100 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
101 * expression so they are very powerful. Some examples:<pre><code>
102 // disable these exact dates:
103 disabledDates: ["03/08/2003", "09/16/2003"]
104 // disable these days for every year:
105 disabledDates: ["03/08", "09/16"]
106 // only match the beginning (useful if you are using short years):
107 disabledDates: ["^03/08"]
108 // disable every day in March 2006:
109 disabledDates: ["03/../2006"]
110 // disable every day in every March:
111 disabledDates: ["^03"]
113 * Note that the format of the dates included in the array should exactly match the {@link #format} config.
114 * In order to support regular expressions, if you are using a {@link #format date format} that has "." in
115 * it, you will have to escape the dot when restricting dates. For example: <tt>["03\\.08\\.03"]</tt>.
117 <div id="cfg-Ext.form.DateField-autoCreate"></div>/**
118 * @cfg {String/Object} autoCreate
119 * A {@link Ext.DomHelper DomHelper element specification object}, or <tt>true</tt> for the default element
120 * specification object:<pre><code>
121 * autoCreate: {tag: "input", type: "text", size: "10", autocomplete: "off"}
126 defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
128 // in the absence of a time value, a default value of 12 noon will be used
129 // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
130 initTime: '12', // 24 hour format
134 // PUBLIC -- to be documented
135 safeParse : function(value, format) {
136 if (/[gGhH]/.test(format.replace(/(\\.)/g, ''))) {
137 // if parse format contains hour information, no DST adjustment is necessary
138 return Date.parseDate(value, format);
140 // set time to 12 noon, then clear the time
141 var parsedDate = Date.parseDate(value + ' ' + this.initTime, format + ' ' + this.initTimeFormat);
143 if (parsedDate) return parsedDate.clearTime();
147 initComponent : function(){
148 Ext.form.DateField.superclass.initComponent.call(this);
151 <div id="event-Ext.form.DateField-select"></div>/**
153 * Fires when a date is selected via the date picker.
154 * @param {Ext.form.DateField} this
155 * @param {Date} date The date that was selected
160 if(Ext.isString(this.minValue)){
161 this.minValue = this.parseDate(this.minValue);
163 if(Ext.isString(this.maxValue)){
164 this.maxValue = this.parseDate(this.maxValue);
166 this.disabledDatesRE = null;
167 this.initDisabledDays();
170 initEvents: function() {
171 Ext.form.DateField.superclass.initEvents.call(this);
172 this.keyNav = new Ext.KeyNav(this.el, {
173 "down": function(e) {
174 this.onTriggerClick();
183 initDisabledDays : function(){
184 if(this.disabledDates){
185 var dd = this.disabledDates,
189 Ext.each(dd, function(d, i){
190 re += Ext.isDate(d) ? '^' + Ext.escapeRe(d.dateFormat(this.format)) + '$' : dd[i];
195 this.disabledDatesRE = new RegExp(re + ')');
199 <div id="method-Ext.form.DateField-setDisabledDates"></div>/**
200 * Replaces any existing disabled dates with new values and refreshes the DatePicker.
201 * @param {Array} disabledDates An array of date strings (see the <tt>{@link #disabledDates}</tt> config
202 * for details on supported values) used to disable a pattern of dates.
204 setDisabledDates : function(dd){
205 this.disabledDates = dd;
206 this.initDisabledDays();
208 this.menu.picker.setDisabledDates(this.disabledDatesRE);
212 <div id="method-Ext.form.DateField-setDisabledDays"></div>/**
213 * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the DatePicker.
214 * @param {Array} disabledDays An array of disabled day indexes. See the <tt>{@link #disabledDays}</tt>
215 * config for details on supported values.
217 setDisabledDays : function(dd){
218 this.disabledDays = dd;
220 this.menu.picker.setDisabledDays(dd);
224 <div id="method-Ext.form.DateField-setMinValue"></div>/**
225 * Replaces any existing <tt>{@link #minValue}</tt> with the new value and refreshes the DatePicker.
226 * @param {Date} value The minimum date that can be selected
228 setMinValue : function(dt){
229 this.minValue = (Ext.isString(dt) ? this.parseDate(dt) : dt);
231 this.menu.picker.setMinDate(this.minValue);
235 <div id="method-Ext.form.DateField-setMaxValue"></div>/**
236 * Replaces any existing <tt>{@link #maxValue}</tt> with the new value and refreshes the DatePicker.
237 * @param {Date} value The maximum date that can be selected
239 setMaxValue : function(dt){
240 this.maxValue = (Ext.isString(dt) ? this.parseDate(dt) : dt);
242 this.menu.picker.setMaxDate(this.maxValue);
246 <div id="method-Ext.form.DateField-getErrors"></div>/**
247 * Runs all of NumberFields validations and returns an array of any errors. Note that this first
248 * runs TextField's validations, so the returned array is an amalgamation of all field errors.
249 * The additional validation checks are testing that the date format is valid, that the chosen
250 * date is within the min and max date constraints set, that the date chosen is not in the disabledDates
251 * regex and that the day chosed is not one of the disabledDays.
252 * @param {Mixed} value The value to get errors for (defaults to the current field value)
253 * @return {Array} All validation errors for this field
255 getErrors: function(value) {
256 var errors = Ext.form.DateField.superclass.getErrors.apply(this, arguments);
258 value = this.formatDate(value || this.processValue(this.getRawValue()));
260 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
265 value = this.parseDate(value);
267 errors.push(String.format(this.invalidText, svalue, this.format));
271 var time = value.getTime();
272 if (this.minValue && time < this.minValue.getTime()) {
273 errors.push(String.format(this.minText, this.formatDate(this.minValue)));
276 if (this.maxValue && time > this.maxValue.getTime()) {
277 errors.push(String.format(this.maxText, this.formatDate(this.maxValue)));
280 if (this.disabledDays) {
281 var day = value.getDay();
283 for(var i = 0; i < this.disabledDays.length; i++) {
284 if (day === this.disabledDays[i]) {
285 errors.push(this.disabledDaysText);
291 var fvalue = this.formatDate(value);
292 if (this.disabledDatesRE && this.disabledDatesRE.test(fvalue)) {
293 errors.push(String.format(this.disabledDatesText, fvalue));
300 // Provides logic to override the default TriggerField.validateBlur which just returns true
301 validateBlur : function(){
302 return !this.menu || !this.menu.isVisible();
305 <div id="method-Ext.form.DateField-getValue"></div>/**
306 * Returns the current date value of the date field.
307 * @return {Date} The date value
309 getValue : function(){
310 return this.parseDate(Ext.form.DateField.superclass.getValue.call(this)) || "";
313 <div id="method-Ext.form.DateField-setValue"></div>/**
314 * Sets the value of the date field. You can pass a date object or any string that can be
315 * parsed into a valid date, using <tt>{@link #format}</tt> as the date format, according
316 * to the same rules as {@link Date#parseDate} (the default format used is <tt>"m/d/Y"</tt>).
319 //All of these calls set the same date value (May 4, 2006)
321 //Pass a date object:
322 var dt = new Date('5/4/2006');
323 dateField.setValue(dt);
325 //Pass a date string (default format):
326 dateField.setValue('05/04/2006');
328 //Pass a date string (custom format):
329 dateField.format = 'Y-m-d';
330 dateField.setValue('2006-05-04');
332 * @param {String/Date} date The date or valid date string
333 * @return {Ext.form.Field} this
335 setValue : function(date){
336 return Ext.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
340 parseDate : function(value) {
341 if(!value || Ext.isDate(value)){
345 var v = this.safeParse(value, this.format),
346 af = this.altFormats,
347 afa = this.altFormatsArray;
350 afa = afa || af.split("|");
352 for (var i = 0, len = afa.length; i < len && !v; i++) {
353 v = this.safeParse(value, afa[i]);
360 onDestroy : function(){
361 Ext.destroy(this.menu, this.keyNav);
362 Ext.form.DateField.superclass.onDestroy.call(this);
366 formatDate : function(date){
367 return Ext.isDate(date) ? date.dateFormat(this.format) : date;
370 <div id="method-Ext.form.DateField-onTriggerClick"></div>/**
371 * @method onTriggerClick
375 // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
376 onTriggerClick : function(){
380 if(this.menu == null){
381 this.menu = new Ext.menu.DateMenu({
387 Ext.apply(this.menu.picker, {
388 minDate : this.minValue,
389 maxDate : this.maxValue,
390 disabledDatesRE : this.disabledDatesRE,
391 disabledDatesText : this.disabledDatesText,
392 disabledDays : this.disabledDays,
393 disabledDaysText : this.disabledDaysText,
394 format : this.format,
395 showToday : this.showToday,
396 minText : String.format(this.minText, this.formatDate(this.minValue)),
397 maxText : String.format(this.maxText, this.formatDate(this.maxValue))
399 this.menu.picker.setValue(this.getValue() || new Date());
400 this.menu.show(this.el, "tl-bl?");
401 this.menuEvents('on');
405 menuEvents: function(method){
406 this.menu[method]('select', this.onSelect, this);
407 this.menu[method]('hide', this.onMenuHide, this);
408 this.menu[method]('show', this.onFocus, this);
411 onSelect: function(m, d){
413 this.fireEvent('select', this, d);
417 onMenuHide: function(){
418 this.focus(false, 60);
419 this.menuEvents('un');
423 beforeBlur : function(){
424 var v = this.parseDate(this.getRawValue());
430 <div id="cfg-Ext.form.DateField-grow"></div>/**
431 * @cfg {Boolean} grow @hide
433 <div id="cfg-Ext.form.DateField-growMin"></div>/**
434 * @cfg {Number} growMin @hide
436 <div id="cfg-Ext.form.DateField-growMax"></div>/**
437 * @cfg {Number} growMax @hide
439 <div id="method-Ext.form.DateField-autoSize"></div>/**
444 Ext.reg('datefield', Ext.form.DateField);</pre>