244 lines
No EOL
11 KiB
HTML
244 lines
No EOL
11 KiB
HTML
<html><head><title>DateField.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>DateField.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Ext.form.DateField
|
|
* @extends Ext.form.TriggerField
|
|
* Provides a date input field <b>with</b> a {@link Ext.DatePicker} dropdown and automatic date validation.
|
|
* @constructor
|
|
* Create a <b>new</b> DateField
|
|
* @param {Object} config
|
|
*/</i>
|
|
Ext.form.DateField = <b>function</b>(config){
|
|
Ext.form.DateField.superclass.constructor.call(<b>this</b>, config);
|
|
<b>if</b>(typeof <b>this</b>.minValue == "string") <b>this</b>.minValue = <b>this</b>.parseDate(<b>this</b>.minValue);
|
|
<b>if</b>(typeof <b>this</b>.maxValue == "string") <b>this</b>.maxValue = <b>this</b>.parseDate(<b>this</b>.maxValue);
|
|
<b>this</b>.ddMatch = null;
|
|
<b>if</b>(this.disabledDates){
|
|
<b>var</b> dd = <b>this</b>.disabledDates;
|
|
<b>var</b> re = "(?:";
|
|
<b>for</b>(var i = 0; i < dd.length; i++){
|
|
re += dd[i];
|
|
<b>if</b>(i != dd.length-1) re += "|";
|
|
}
|
|
<b>this</b>.ddMatch = <b>new</b> RegExp(re + ")");
|
|
}
|
|
};
|
|
|
|
Ext.extend(Ext.form.DateField, Ext.form.TriggerField, {
|
|
<i>/**
|
|
* @cfg {String} format
|
|
* The <b>default</b> date format string which can be overriden <b>for</b> localization support. The format must be
|
|
* valid according to {@link Date#parseDate} (defaults to 'm/d/y').
|
|
*/</i>
|
|
format : "m/d/y",
|
|
<i>/**
|
|
* @cfg {Array} disabledDays
|
|
* An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
|
|
*/</i>
|
|
disabledDays : null,
|
|
<i>/**
|
|
* @cfg {String} disabledDaysText
|
|
* The tooltip to display when the date falls on a disabled day (defaults to 'Disabled')
|
|
*/</i>
|
|
disabledDaysText : "Disabled",
|
|
<i>/**
|
|
* @cfg {Array} disabledDates
|
|
* An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
|
|
* expression so they are very powerful. Some examples:
|
|
* <ul>
|
|
* <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
|
|
* <li>["03/08", "09/16"] would disable those days <b>for</b> every year</li>
|
|
* <li>["^03/08"] would only match the beginning (useful <b>if</b> you are using short years)</li>
|
|
* <li>["03/../2006"] would disable every day <b>in</b> March 2006</li>
|
|
* <li>["^03"] would disable every day <b>in</b> every March</li>
|
|
* </ul>
|
|
* In order to support regular expressions, <b>if</b> you are using a date format that has "." <b>in</b> it, you will have to
|
|
* escape the dot when restricting dates. For example: ["03\\.08\\.03"].
|
|
*/</i>
|
|
disabledDates : null,
|
|
<i>/**
|
|
* @cfg {String} disabledDatesText
|
|
* The tooltip text to display when the date falls on a disabled date (defaults to 'Disabled')
|
|
*/</i>
|
|
disabledDatesText : "Disabled",
|
|
<i>/**
|
|
* @cfg {Date/String} minValue
|
|
* The minimum allowed date. Can be either a Javascript date object or a string date <b>in</b> a
|
|
* valid format (defaults to null).
|
|
*/</i>
|
|
minValue : null,
|
|
<i>/**
|
|
* @cfg {Date/String} maxValue
|
|
* The maximum allowed date. Can be either a Javascript date object or a string date <b>in</b> a
|
|
* valid format (defaults to null).
|
|
*/</i>
|
|
maxValue : null,
|
|
<i>/**
|
|
* @cfg {String} minText
|
|
* The error text to display when the date <b>in</b> the cell is before minValue (defaults to
|
|
* 'The date <b>in</b> this field must be after {minValue}').
|
|
*/</i>
|
|
minText : "The date <b>in</b> this field must be after {0}",
|
|
<i>/**
|
|
* @cfg {String} maxText
|
|
* The error text to display when the date <b>in</b> the cell is before maxValue (defaults to
|
|
* 'The date <b>in</b> this field must be before {maxValue}').
|
|
*/</i>
|
|
maxText : "The date <b>in</b> this field must be before {0}",
|
|
<i>/**
|
|
* @cfg {String} invalidText
|
|
* The error to display when the date <b>in</b> the field is invalid (defaults to
|
|
* '{value} is not a valid date - it must be <b>in</b> the format {format}').
|
|
*/</i>
|
|
invalidText : "{0} is not a valid date - it must be <b>in</b> the format {1}",
|
|
<i>/**
|
|
* @cfg {String} triggerClass
|
|
* An additional CSS class used to style the trigger button. The trigger will always get the
|
|
* class 'x-form-trigger' and triggerClass will be <b>appended</b> <b>if</b> specified (defaults to 'x-form-date-trigger'
|
|
* which displays a calendar icon).
|
|
*/</i>
|
|
triggerClass : 'x-form-date-trigger',
|
|
<i>/**
|
|
* @cfg {String/Object} autoCreate
|
|
* A DomHelper element spec, or true <b>for</b> a <b>default</b> element spec (defaults to
|
|
* {tag: "input", type: "text", size: "10", autocomplete: "off"})
|
|
*/</i>
|
|
|
|
<i>// private</i>
|
|
defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
|
|
|
|
<i>// private</i>
|
|
validateValue : <b>function</b>(value){
|
|
value = <b>this</b>.formatDate(value);
|
|
<b>if</b>(!Ext.form.DateField.superclass.validateValue.call(<b>this</b>, value)){
|
|
<b>return</b> false;
|
|
}
|
|
<b>if</b>(value.length < 1){ <i>// <b>if</b> it's blank and textfield didn't flag it then it's valid</i>
|
|
<b>return</b> true;
|
|
}
|
|
<b>var</b> svalue = value;
|
|
value = <b>this</b>.parseDate(value);
|
|
<b>if</b>(!value){
|
|
<b>this</b>.markInvalid(String.format(<b>this</b>.invalidText, svalue, <b>this</b>.format));
|
|
<b>return</b> false;
|
|
}
|
|
<b>var</b> time = value.getTime();
|
|
<b>if</b>(this.minValue && time < <b>this</b>.minValue.getTime()){
|
|
<b>this</b>.markInvalid(String.format(<b>this</b>.minText, <b>this</b>.formatDate(<b>this</b>.minValue)));
|
|
<b>return</b> false;
|
|
}
|
|
<b>if</b>(this.maxValue && time > <b>this</b>.maxValue.getTime()){
|
|
<b>this</b>.markInvalid(String.format(<b>this</b>.maxText, <b>this</b>.formatDate(<b>this</b>.maxValue)));
|
|
<b>return</b> false;
|
|
}
|
|
<b>if</b>(this.disabledDays){
|
|
<b>var</b> day = value.getDay();
|
|
<b>for</b>(var i = 0; i < <b>this</b>.disabledDays.length; i++) {
|
|
<b>if</b>(day === <b>this</b>.disabledDays[i]){
|
|
<b>this</b>.markInvalid(<b>this</b>.disabledDaysText);
|
|
<b>return</b> false;
|
|
}
|
|
}
|
|
}
|
|
<b>var</b> fvalue = <b>this</b>.formatDate(value);
|
|
<b>if</b>(this.ddMatch && <b>this</b>.ddMatch.test(fvalue)){
|
|
<b>this</b>.markInvalid(String.format(<b>this</b>.disabledDatesText, fvalue));
|
|
<b>return</b> false;
|
|
}
|
|
<b>return</b> true;
|
|
},
|
|
|
|
<i>// private</i>
|
|
<i>// Provides logic to override the <b>default</b> TriggerField.validateBlur which just returns true</i>
|
|
validateBlur : <b>function</b>(){
|
|
<b>return</b> !<b>this</b>.menu || !<b>this</b>.menu.isVisible();
|
|
},
|
|
|
|
<i>/**
|
|
* Returns the current date value of the date field
|
|
* @<b>return</b> {Date} value The date value
|
|
*/</i>
|
|
getValue : <b>function</b>(){
|
|
<b>return</b> this.parseDate(Ext.form.DateField.superclass.getValue.call(<b>this</b>)) || "";
|
|
},
|
|
|
|
<i>/**
|
|
* Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid
|
|
* date, using DateField.format as the date format, according to the same rules as {@link Date#parseDate}
|
|
* (the <b>default</b> format used is "m/d/y").
|
|
* <br />Usage:
|
|
* <pre><code>
|
|
<i>//All of these calls set the same date value (May 4, 2006)</i>
|
|
|
|
<i>//Pass a date object:</i>
|
|
<b>var</b> dt = <b>new</b> Date('5/4/06');
|
|
dateField.setValue(dt);
|
|
|
|
<i>//Pass a date string (<b>default</b> format):</i>
|
|
dateField.setValue('5/4/06');
|
|
|
|
<i>//Pass a date string (custom format):</i>
|
|
dateField.format = 'Y-m-d';
|
|
dateField.setValue('2006-5-4');
|
|
</code></pre>
|
|
* @param {String/Date} date The date or valid date string
|
|
*/</i>
|
|
setValue : <b>function</b>(date){
|
|
Ext.form.DateField.superclass.setValue.call(<b>this</b>, <b>this</b>.formatDate(<b>this</b>.parseDate(date)));
|
|
},
|
|
|
|
<i>// private</i>
|
|
parseDate : <b>function</b>(value){
|
|
<b>return</b> (!value || value instanceof Date) ?
|
|
value : Date.parseDate(value, <b>this</b>.format);
|
|
},
|
|
|
|
<i>// private</i>
|
|
formatDate : <b>function</b>(date){
|
|
<b>return</b> (!date || !(date instanceof Date)) ?
|
|
date : date.dateFormat(<b>this</b>.format);
|
|
},
|
|
|
|
<i>// private</i>
|
|
menuListeners : {
|
|
select: <b>function</b>(m, d){
|
|
<b>this</b>.setValue(d);
|
|
},
|
|
show : <b>function</b>(){ <i>// retain focus styling</i>
|
|
<b>this</b>.onFocus();
|
|
},
|
|
hide : <b>function</b>(){
|
|
<b>this</b>.focus();
|
|
<b>var</b> ml = <b>this</b>.menuListeners;
|
|
<b>this</b>.menu.un("select", ml.select, <b>this</b>);
|
|
<b>this</b>.menu.un("show", ml.show, <b>this</b>);
|
|
<b>this</b>.menu.un("hide", ml.hide, <b>this</b>);
|
|
}
|
|
},
|
|
|
|
<i>// private</i>
|
|
<i>// Implements the <b>default</b> empty TriggerField.onTriggerClick <b>function</b> to display the DatePicker</i>
|
|
onTriggerClick : <b>function</b>(){
|
|
<b>if</b>(this.disabled){
|
|
<b>return</b>;
|
|
}
|
|
<b>if</b>(this.menu == null){
|
|
<b>this</b>.menu = <b>new</b> Ext.menu.DateMenu();
|
|
}
|
|
Ext.apply(<b>this</b>.menu.picker, {
|
|
minDate : <b>this</b>.minValue,
|
|
maxDate : <b>this</b>.maxValue,
|
|
disabledDatesRE : <b>this</b>.ddMatch,
|
|
disabledDatesText : <b>this</b>.disabledDatesText,
|
|
disabledDays : <b>this</b>.disabledDays,
|
|
disabledDaysText : <b>this</b>.disabledDaysText,
|
|
format : <b>this</b>.format,
|
|
minText : String.format(<b>this</b>.minText, <b>this</b>.formatDate(<b>this</b>.minValue)),
|
|
maxText : String.format(<b>this</b>.maxText, <b>this</b>.formatDate(<b>this</b>.maxValue))
|
|
});
|
|
<b>this</b>.menu.on(Ext.apply({}, <b>this</b>.menuListeners, {
|
|
scope:<b>this</b>
|
|
}));
|
|
<b>this</b>.menu.picker.setValue(<b>this</b>.getValue() || <b>new</b> Date());
|
|
<b>this</b>.menu.show(<b>this</b>.el, "tl-bl?");
|
|
}
|
|
});</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |