]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/form/_DateTimeTextBox.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dijit / form / _DateTimeTextBox.js
1 if(!dojo._hasResource["dijit.form._DateTimeTextBox"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit.form._DateTimeTextBox"] = true;
3 dojo.provide("dijit.form._DateTimeTextBox");
4
5 dojo.require("dojo.date");
6 dojo.require("dojo.date.locale");
7 dojo.require("dojo.date.stamp");
8 dojo.require("dijit.form.ValidationTextBox");
9
10 /*=====
11 dojo.declare(
12         "dijit.form._DateTimeTextBox.__Constraints",
13         [dijit.form.RangeBoundTextBox.__Constraints, dojo.date.locale.__FormatOptions]
14 );
15 =====*/
16
17 dojo.declare(
18         "dijit.form._DateTimeTextBox",
19         dijit.form.RangeBoundTextBox,
20         {
21                 // summary:
22                 //              A validating, serializable, range-bound date or time text box.
23                 //
24                 // constraints: dijit.form._DateTimeTextBox.__Constraints 
25
26                 /*=====
27                 constraints: {},
28                 ======*/
29                 regExpGen: dojo.date.locale.regexp,
30                 compare: dojo.date.compare,
31                 format: function(/*Date*/ value, /*dojo.date.locale.__FormatOptions*/ constraints){
32                         //      summary: formats the value as a Date, according to constraints
33                         if(!value){ return ''; }
34                         return dojo.date.locale.format(value, constraints);
35                 },
36                 parse: function(/*String*/ value, /*dojo.date.locale.__FormatOptions*/ constraints){
37                         //      summary: parses the value as a Date, according to constraints
38                         return dojo.date.locale.parse(value, constraints) || undefined; /* can't return null to getValue since that's special */
39                 },
40
41                 serialize: dojo.date.stamp.toISOString,
42
43                 //      value: Date
44                 //              The value of this widget as a JavaScript Date object.  Use `getValue`/`setValue` to manipulate.
45                 //              When passed to the parser in markup, must be specified according to `dojo.date.stamp.fromISOString`
46                 value: new Date(""),    // value.toString()="NaN"
47
48                 //      popupClass: String
49                 //              Name of the popup widget class used to select a date/time
50                 popupClass: "", // default is no popup = text only
51                 _selector: "",
52
53                 postMixInProperties: function(){
54                         //dijit.form.RangeBoundTextBox.prototype.postMixInProperties.apply(this, arguments);
55                         this.inherited(arguments);
56                         if(!this.value || this.value.toString() == dijit.form._DateTimeTextBox.prototype.value.toString()){
57                                 this.value = undefined;
58                         }
59                         var constraints = this.constraints;
60                         constraints.selector = this._selector;
61                         constraints.fullYear = true; // see #5465 - always format with 4-digit years
62                         var fromISO = dojo.date.stamp.fromISOString;
63                         if(typeof constraints.min == "string"){ constraints.min = fromISO(constraints.min); }
64                         if(typeof constraints.max == "string"){ constraints.max = fromISO(constraints.max); }
65                 },
66
67                 _onFocus: function(/*Event*/ evt){
68                         // summary: open the TimePicker popup
69                         this._open();
70                 },
71
72                 setValue: function(/*Date*/ value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
73                         // summary:
74                         //      Sets the date on this textbox.  Note that `value` must be a Javascript Date object.
75                         this.inherited(arguments);
76                         if(this._picker){
77                                 // #3948: fix blank date on popup only
78                                 if(!value){value=new Date();}
79                                 this._picker.setValue(value);
80                         }
81                 },
82
83                 _open: function(){
84                         // summary:
85                         //      opens the TimePicker, and sets the onValueSelected value
86
87                         if(this.disabled || this.readOnly || !this.popupClass){return;}
88
89                         var textBox = this;
90
91                         if(!this._picker){
92                                 var PopupProto=dojo.getObject(this.popupClass, false);
93                                 this._picker = new PopupProto({
94                                         onValueSelected: function(value){
95
96                                                 textBox.focus(); // focus the textbox before the popup closes to avoid reopening the popup
97                                                 setTimeout(dojo.hitch(textBox, "_close"), 1); // allow focus time to take
98
99                                                 // this will cause InlineEditBox and other handlers to do stuff so make sure it's last
100                                                 dijit.form._DateTimeTextBox.superclass.setValue.call(textBox, value, true);
101                                         },
102                                         lang: textBox.lang,
103                                         constraints: textBox.constraints,
104                                         isDisabledDate: function(/*Date*/ date){
105                                                 // summary:
106                                                 //      disables dates outside of the min/max of the _DateTimeTextBox
107                                                 var compare = dojo.date.compare;
108                                                 var constraints = textBox.constraints;
109                                                 return constraints && (constraints.min && (compare(constraints.min, date, "date") > 0) || 
110                                                         (constraints.max && compare(constraints.max, date, "date") < 0));
111                                         }
112                                 });
113                                 this._picker.setValue(this.getValue() || new Date());
114                         }
115                         if(!this._opened){
116                                 dijit.popup.open({
117                                         parent: this,
118                                         popup: this._picker,
119                                         around: this.domNode,
120                                         onCancel: dojo.hitch(this, this._close),
121                                         onClose: function(){ textBox._opened=false; }
122                                 });
123                                 this._opened=true;
124                         }
125                         
126                         dojo.marginBox(this._picker.domNode,{ w:this.domNode.offsetWidth });
127                 },
128
129                 _close: function(){
130                         if(this._opened){
131                                 dijit.popup.close(this._picker);
132                                 this._opened=false;
133                         }                       
134                 },
135
136                 _onBlur: function(){
137                         // summary: called magically when focus has shifted away from this widget and it's dropdown
138                         this._close();
139                         if(this._picker){
140                                 // teardown so that constraints will be rebuilt next time (redundant reference: #6002)
141                                 this._picker.destroy();
142                                 delete this._picker;
143                         }
144                         this.inherited(arguments);
145                         // don't focus on <input>.  the user has explicitly focused on something else.
146                 },
147
148                 getDisplayedValue:function(){
149                         return this.textbox.value;
150                 },
151
152                 setDisplayedValue:function(/*String*/ value, /*Boolean?*/ priorityChange){
153                         this.setValue(this.parse(value, this.constraints), priorityChange, value);
154                 },
155
156                 destroy: function(){
157                         if(this._picker){
158                                 this._picker.destroy();
159                                 delete this._picker;
160                         }
161                         this.inherited(arguments);
162                 },
163
164                 _onKeyPress: function(/*Event*/e){
165                         if(dijit.form._DateTimeTextBox.superclass._onKeyPress.apply(this, arguments)){
166                                 if(this._opened && e.keyCode == dojo.keys.ESCAPE && !e.shiftKey && !e.ctrlKey && !e.altKey){
167                                         this._close();
168                                         dojo.stopEvent(e);
169                                 }
170                         }
171                 }
172         }
173 );
174
175 }