Debian Codegraph @

dijit/form/_DateTimeTextBox.js

raw symbols SCIP
1 define([
2 "dojo/date", // date date.compare
3 "dojo/date/locale", // locale.regexp
4 "dojo/date/stamp", // stamp.fromISOString stamp.toISOString
5 "dojo/_base/declare", // declare
6 "dojo/_base/lang", // lang.getObject
7 "./RangeBoundTextBox",
8 "../_HasDropDown",
9 "dojo/text!./templates/DropDownBox.html"
10 ], function(date, locale, stamp, declare, lang, RangeBoundTextBox, _HasDropDown, template){
11
12 // module:
13 // dijit/form/_DateTimeTextBox
14
15 new Date("X"); // workaround for #11279, new Date("") == NaN
16
17 var _DateTimeTextBox = declare("dijit.form._DateTimeTextBox", [RangeBoundTextBox, _HasDropDown], {
18 // summary:
19 // Base class for validating, serializable, range-bound date or time text box.
20
21 templateString: template,
22
23 // hasDownArrow: [const] Boolean
24 // Set this textbox to display a down arrow button, to open the drop down list.
25 hasDownArrow: true,
26
27 // Set classes like dijitDownArrowButtonHover depending on mouse action over button node
28 cssStateNodes: {
29 "_buttonNode": "dijitDownArrowButton"
30 },
31
32 /*=====
33 // constraints: _DateTimeTextBox.__Constraints
34 // Despite the name, this parameter specifies both constraints on the input
35 // (including starting/ending dates/times allowed) as well as
36 // formatting options like whether the date is displayed in long (ex: December 25, 2005)
37 // or short (ex: 12/25/2005) format. See `dijit/form/_DateTimeTextBox.__Constraints` for details.
38 constraints: {},
39 ======*/
40
41 // The constraints without the min/max properties. Used by the compare() method
42 _unboundedConstraints: {},
43
44 // Override ValidationTextBox.pattern.... we use a reg-ex generating function rather
45 // than a straight regexp to deal with locale (plus formatting options too?)
46 pattern: locale.regexp,
47
48 // datePackage: String
49 // JavaScript namespace to find calendar routines. If unspecified, uses Gregorian calendar routines
50 // at dojo/date and dojo/date/locale.
51 datePackage: "",
52 // TODO: for 2.0, replace datePackage with dateModule and dateLocalModule attributes specifying MIDs,
53 // or alternately just get rid of this completely and tell user to use module ID remapping
54 // via require
55
56 postMixInProperties: function(){
57 this.inherited(arguments);
58 this._set("type", "text"); // in case type="date"|"time" was specified which messes up parse/format
59 },
60
61 // Override _FormWidget.compare() to work for dates/times
62 compare: function(/*Date*/ val1, /*Date*/ val2){
63 var isInvalid1 = this._isInvalidDate(val1);
64 var isInvalid2 = this._isInvalidDate(val2);
65 if (isInvalid1 || isInvalid2){
66 return (isInvalid1 && isInvalid2) ? 0 : (!isInvalid1 ? 1 : -1);
67 }
68 // Format and parse the values before comparing them to make sure that only the parts of the
69 // date that will make the "round trip" get compared.
70 var fval1 = this.format(val1, this._unboundedConstraints),
71 fval2 = this.format(val2, this._unboundedConstraints),
72 pval1 = this.parse(fval1, this._unboundedConstraints),
73 pval2 = this.parse(fval2, this._unboundedConstraints);
74
75 return fval1 == fval2 ? 0 : date.compare(pval1, pval2, this._selector);
76 },
77
78 // flag to _HasDropDown to make drop down Calendar width == <input> width
79 autoWidth: true,
80
81 format: function(/*Date*/ value, /*locale.__FormatOptions*/ constraints){
82 // summary:
83 // Formats the value as a Date, according to specified locale (second argument)
84 // tags:
85 // protected
86 if(!value){ return ''; }
87 return this.dateLocaleModule.format(value, constraints);
88 },
89
90 "parse": function(/*String*/ value, /*locale.__FormatOptions*/ constraints){
91 // summary:
92 // Parses as string as a Date, according to constraints
93 // tags:
94 // protected
95
96 return this.dateLocaleModule.parse(value, constraints) || (this._isEmpty(value) ? null : undefined); // Date
97 },
98
99 // Overrides ValidationTextBox.serialize() to serialize a date in canonical ISO format.
100 serialize: function(/*anything*/ val, /*Object?*/ options){
101 if(val.toGregorian){
102 val = val.toGregorian();
103 }
104 return stamp.toISOString(val, options);
105 },
106
107 // dropDownDefaultValue: Date
108 // The default value to focus in the popupClass widget when the textbox value is empty.
109 dropDownDefaultValue : new Date(),
110
111 // value: Date
112 // The value of this widget as a JavaScript Date object. Use get("value") / set("value", val) to manipulate.
113 // When passed to the parser in markup, must be specified according to `dojo/date/stamp.fromISOString()`
114 value: new Date(""), // value.toString()="NaN"
115
116 _blankValue: null, // used by filter() when the textbox is blank
117
118 // popupClass: [protected extension] String
119 // Name of the popup widget class used to select a date/time.
120 // Subclasses should specify this.
121 popupClass: "", // default is no popup = text only
122
123
124 // _selector: [protected extension] String
125 // Specifies constraints.selector passed to dojo.date functions, should be either
126 // "date" or "time".
127 // Subclass must specify this.
128 _selector: "",
129
130 constructor: function(params /*===== , srcNodeRef =====*/){
131 // summary:
132 // Create the widget.
133 // params: Object|null
134 // Hash of initialization parameters for widget, including scalar values (like title, duration etc.)
135 // and functions, typically callbacks like onClick.
136 // The hash can contain any of the widget's properties, excluding read-only properties.
137 // srcNodeRef: DOMNode|String?
138 // If a srcNodeRef (DOM node) is specified, replace srcNodeRef with my generated DOM tree
139
140 params = params || {};
141 this.dateModule = params.datePackage ? lang.getObject(params.datePackage, false) : date;
142 this.dateClassObj = this.dateModule.Date || Date;
143 if(!(this.dateClassObj instanceof Date)){
144 this.value = new this.dateClassObj(this.value);
145 }
146 this.dateLocaleModule = params.datePackage ? lang.getObject(params.datePackage+".locale", false) : locale;
147 this._set('pattern', this.dateLocaleModule.regexp);
148 this._invalidDate = this.constructor.prototype.value.toString();
149 },
150
151 buildRendering: function(){
152 this.inherited(arguments);
153
154 if(!this.hasDownArrow){
155 this._buttonNode.style.display = "none";
156 }
157
158 // If hasDownArrow is false, we basically just want to treat the whole widget as the
159 // button.
160 if(!this.hasDownArrow){
161 this._buttonNode = this.domNode;
162 this.baseClass += " dijitComboBoxOpenOnClick";
163 }
164 },
165
166 _setConstraintsAttr: function(/*Object*/ constraints){
167 constraints.selector = this._selector;
168 constraints.fullYear = true; // see #5465 - always format with 4-digit years
169 var fromISO = stamp.fromISOString;
170 if(typeof constraints.min == "string"){
171 constraints.min = fromISO(constraints.min);
172 if(!(this.dateClassObj instanceof Date)){
173 constraints.min = new this.dateClassObj(constraints.min);
174 }
175 }
176 if(typeof constraints.max == "string"){
177 constraints.max = fromISO(constraints.max);
178 if(!(this.dateClassObj instanceof Date)){
179 constraints.max = new this.dateClassObj(constraints.max);
180 }
181 }
182 this.inherited(arguments);
183 this._unboundedConstraints = lang.mixin({}, this.constraints, {min: null, max: null});
184 },
185
186 _isDefinitelyOutOfRange: function(){
187 var returnValue = this.inherited(arguments);
188 var isOutOfRange = false;
189 var inputDate;
190 var inputYear;
191 var inputYearMax;
192 var inputYearMin;
193 var maxDate;
194 var minDate;
195
196 if(returnValue && (this.constraints.min || this.constraints.max)){
197 var dateRegExp = new RegExp(this._lastRegExp);
198 inputDate = dateRegExp.exec(this._lastInputEventValue);
199 if (inputDate != null) {
200 inputYear = inputDate[3];
201
202 if(this.constraints.min){
203 minDate = this.constraints.min instanceof Date ?
204 this.constraints.min : new Date(String(this.constraints.min));
205 minYear = minDate.getFullYear();
206 inputYearMax = parseInt((inputYear + '9999').substr(0, 4), 10);
207 isOutOfRange = inputYearMax < minYear;
208 }
209
210 if(!isOutOfRange && this.constraints.max){
211 maxDate = this.constraints.max instanceof Date ?
212 this.constraints.max : new Date(String(this.constraints.max));
213 maxYear = maxDate.getFullYear();
214 inputYearMin = parseInt((inputYear + '0000').substr(0, 4), 10);
215 isOutOfRange = inputYearMin > maxYear;
216 }
217
218 returnValue = isOutOfRange;
219 }
220 }
221
222 return returnValue;
223 },
224
225 _isInvalidDate: function(/*Date*/ value){
226 // summary:
227 // Runs various tests on the value, checking for invalid conditions
228 // tags:
229 // private
230 return !value || isNaN(value) || typeof value != "object" || value.toString() == this._invalidDate;
231 },
232
233 _setValueAttr: function(/*Date|String*/ value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
234 // summary:
235 // Sets the date on this textbox. Note: value can be a JavaScript Date literal or a string to be parsed.
236 if(value !== undefined){
237 if(typeof value == "string"){
238 value = stamp.fromISOString(value);
239 }
240 if(this._isInvalidDate(value)){
241 value = null;
242 }
243 if(value instanceof Date && !(this.dateClassObj instanceof Date)){
244 value = new this.dateClassObj(value);
245 }
246 }
247 this.inherited(arguments, [value, priorityChange, formattedValue]);
248 if(this.value instanceof Date){
249 this.filterString = "";
250 }
251
252 // Set the dropdown's value to match, unless we are being updated due to the user navigating the TimeTextBox
253 // dropdown via up/down arrow keys.
254 if(priorityChange !== false && this.dropDown){
255 this.dropDown.set('value', value, false);
256 }
257 },
258
259 _set: function(attr, value){
260 // Avoid spurious watch() notifications when value is changed to new Date object w/the same value
261 if(attr == "value"){
262 if(value instanceof Date && !(this.dateClassObj instanceof Date)){
263 value = new this.dateClassObj(value);
264 }
265 var oldValue = this._get("value");
266 if(oldValue instanceof this.dateClassObj && this.compare(value, oldValue) == 0){
267 return;
268 }
269 }
270 this.inherited(arguments);
271 },
272
273 _setDropDownDefaultValueAttr: function(/*Date*/ val){
274 if(this._isInvalidDate(val)){
275 // convert null setting into today's date, since there needs to be *some* default at all times.
276 val = new this.dateClassObj();
277 }
278 this._set("dropDownDefaultValue", val);
279 },
280
281 openDropDown: function(/*Function*/ callback){
282 // rebuild drop down every time, so that constraints get copied (#6002)
283 if(this.dropDown){
284 this.dropDown.destroy();
285 }
286 var PopupProto = lang.isString(this.popupClass) ? lang.getObject(this.popupClass, false) : this.popupClass,
287 textBox = this,
288 value = this.get("value");
289 this.dropDown = new PopupProto({
290 onChange: function(value){
291 // this will cause InlineEditBox and other handlers to do stuff so make sure it's last
292 textBox.set('value', value, true);
293 },
294 id: this.id + "_popup",
295 dir: textBox.dir,
296 lang: textBox.lang,
297 value: value,
298 textDir: textBox.textDir,
299 currentFocus: !this._isInvalidDate(value) ? value : this.dropDownDefaultValue,
300 constraints: textBox.constraints,
301 filterString: textBox.filterString, // for TimeTextBox, to filter times shown
302 datePackage: textBox.datePackage,
303 isDisabledDate: function(/*Date*/ date){
304 // summary:
305 // disables dates outside of the min/max of the _DateTimeTextBox
306 return !textBox.rangeCheck(date, textBox.constraints);
307 }
308 });
309
310 this.inherited(arguments);
311 },
312
313 _getDisplayedValueAttr: function(){
314 return this.textbox.value;
315 },
316
317 _setDisplayedValueAttr: function(/*String*/ value, /*Boolean?*/ priorityChange){
318 this._setValueAttr(this.parse(value, this.constraints), priorityChange, value);
319 }
320 });
321
322
323 /*=====
324 _DateTimeTextBox.__Constraints = declare([RangeBoundTextBox.__Constraints, locale.__FormatOptions], {
325 // summary:
326 // Specifies both the rules on valid/invalid values (first/last date/time allowed),
327 // and also formatting options for how the date/time is displayed.
328 // example:
329 // To restrict to dates within 2004, displayed in a long format like "December 25, 2005":
330 // | {min:'2004-01-01',max:'2004-12-31', formatLength:'long'}
331 });
332 =====*/
333
334 return _DateTimeTextBox;
335 });