2150 |
mathias |
1 |
if(!dojo._hasResource["dijit.form._FormWidget"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dijit.form._FormWidget"] = true;
|
|
|
3 |
dojo.provide("dijit.form._FormWidget");
|
|
|
4 |
|
|
|
5 |
dojo.require("dijit._Widget");
|
|
|
6 |
dojo.require("dijit._Templated");
|
|
|
7 |
|
|
|
8 |
dojo.declare("dijit.form._FormWidget", [dijit._Widget, dijit._Templated],
|
|
|
9 |
{
|
|
|
10 |
/*
|
|
|
11 |
Summary:
|
|
|
12 |
FormElement widgets correspond to native HTML elements such as <input> or <button> or <select>.
|
|
|
13 |
Each FormElement represents a single input value, and has a (possibly hidden) <input> element,
|
|
|
14 |
to which it serializes its input value, so that form submission (either normal submission or via FormBind?)
|
|
|
15 |
works as expected.
|
|
|
16 |
|
|
|
17 |
All these widgets should have these attributes just like native HTML input elements.
|
|
|
18 |
You can set them during widget construction, but after that they are read only.
|
|
|
19 |
|
|
|
20 |
They also share some common methods.
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
// baseClass: String
|
|
|
24 |
// Root CSS class of the widget (ex: dijitTextBox), used to add CSS classes of widget
|
|
|
25 |
// (ex: "dijitTextBox dijitTextBoxInvalid dijitTextBoxFocused dijitTextBoxInvalidFocused")
|
|
|
26 |
// See _setStateClass().
|
|
|
27 |
baseClass: "",
|
|
|
28 |
|
|
|
29 |
// value: String
|
|
|
30 |
// Corresponds to the native HTML <input> element's attribute.
|
|
|
31 |
value: "",
|
|
|
32 |
|
|
|
33 |
// name: String
|
|
|
34 |
// Name used when submitting form; same as "name" attribute or plain HTML elements
|
|
|
35 |
name: "",
|
|
|
36 |
|
|
|
37 |
// id: String
|
|
|
38 |
// Corresponds to the native HTML <input> element's attribute.
|
|
|
39 |
// Also becomes the id for the widget.
|
|
|
40 |
id: "",
|
|
|
41 |
|
|
|
42 |
// alt: String
|
|
|
43 |
// Corresponds to the native HTML <input> element's attribute.
|
|
|
44 |
alt: "",
|
|
|
45 |
|
|
|
46 |
// type: String
|
|
|
47 |
// Corresponds to the native HTML <input> element's attribute.
|
|
|
48 |
type: "text",
|
|
|
49 |
|
|
|
50 |
// tabIndex: Integer
|
|
|
51 |
// Order fields are traversed when user hits the tab key
|
|
|
52 |
tabIndex: "0",
|
|
|
53 |
|
|
|
54 |
// disabled: Boolean
|
|
|
55 |
// Should this widget respond to user input?
|
|
|
56 |
// In markup, this is specified as "disabled='disabled'", or just "disabled".
|
|
|
57 |
disabled: false,
|
|
|
58 |
|
|
|
59 |
// intermediateChanges: Boolean
|
|
|
60 |
// Fires onChange for each value change or only on demand
|
|
|
61 |
intermediateChanges: false,
|
|
|
62 |
|
|
|
63 |
// These mixins assume that the focus node is an INPUT, as many but not all _FormWidgets are.
|
|
|
64 |
// Don't attempt to mixin the 'type', 'name' attributes here programatically -- they must be declared
|
|
|
65 |
// directly in the template as read by the parser in order to function. IE is known to specifically
|
|
|
66 |
// require the 'name' attribute at element creation time.
|
|
|
67 |
attributeMap: dojo.mixin(dojo.clone(dijit._Widget.prototype.attributeMap),
|
|
|
68 |
{id:"focusNode", tabIndex:"focusNode", alt:"focusNode"}),
|
|
|
69 |
|
|
|
70 |
setDisabled: function(/*Boolean*/ disabled){
|
|
|
71 |
// summary:
|
|
|
72 |
// Set disabled state of widget.
|
|
|
73 |
|
|
|
74 |
this.domNode.disabled = this.disabled = disabled;
|
|
|
75 |
if(this.focusNode){
|
|
|
76 |
this.focusNode.disabled = disabled;
|
|
|
77 |
}
|
|
|
78 |
if(disabled){
|
|
|
79 |
//reset those, because after the domNode is disabled, we can no longer receive
|
|
|
80 |
//mouse related events, see #4200
|
|
|
81 |
this._hovering = false;
|
|
|
82 |
this._active = false;
|
|
|
83 |
}
|
|
|
84 |
dijit.setWaiState(this.focusNode || this.domNode, "disabled", disabled);
|
|
|
85 |
this._setStateClass();
|
|
|
86 |
},
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
_onMouse : function(/*Event*/ event){
|
|
|
90 |
// summary:
|
|
|
91 |
// Sets _hovering, _active, and stateModifier properties depending on mouse state,
|
|
|
92 |
// then calls setStateClass() to set appropriate CSS classes for this.domNode.
|
|
|
93 |
//
|
|
|
94 |
// To get a different CSS class for hover, send onmouseover and onmouseout events to this method.
|
|
|
95 |
// To get a different CSS class while mouse button is depressed, send onmousedown to this method.
|
|
|
96 |
|
|
|
97 |
var mouseNode = event.target;
|
|
|
98 |
if(mouseNode && mouseNode.getAttribute){
|
|
|
99 |
this.stateModifier = mouseNode.getAttribute("stateModifier") || "";
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if(!this.disabled){
|
|
|
103 |
switch(event.type){
|
|
|
104 |
case "mouseenter" :
|
|
|
105 |
case "mouseover" :
|
|
|
106 |
this._hovering = true;
|
|
|
107 |
break;
|
|
|
108 |
|
|
|
109 |
case "mouseout" :
|
|
|
110 |
case "mouseleave" :
|
|
|
111 |
this._hovering = false;
|
|
|
112 |
break;
|
|
|
113 |
|
|
|
114 |
case "mousedown" :
|
|
|
115 |
this._active = true;
|
|
|
116 |
// set a global event to handle mouseup, so it fires properly
|
|
|
117 |
// even if the cursor leaves the button
|
|
|
118 |
var self = this;
|
|
|
119 |
// #2685: use this.connect and disconnect so destroy works properly
|
|
|
120 |
var mouseUpConnector = this.connect(dojo.body(), "onmouseup", function(){
|
|
|
121 |
self._active = false;
|
|
|
122 |
self._setStateClass();
|
|
|
123 |
self.disconnect(mouseUpConnector);
|
|
|
124 |
});
|
|
|
125 |
break;
|
|
|
126 |
}
|
|
|
127 |
this._setStateClass();
|
|
|
128 |
}
|
|
|
129 |
},
|
|
|
130 |
|
|
|
131 |
isFocusable: function(){
|
|
|
132 |
return !this.disabled && (dojo.style(this.domNode, "display") != "none");
|
|
|
133 |
},
|
|
|
134 |
|
|
|
135 |
focus: function(){
|
|
|
136 |
dijit.focus(this.focusNode);
|
|
|
137 |
},
|
|
|
138 |
|
|
|
139 |
_setStateClass: function(){
|
|
|
140 |
// summary
|
|
|
141 |
// Update the visual state of the widget by setting the css classes on this.domNode
|
|
|
142 |
// (or this.stateNode if defined) by combining this.baseClass with
|
|
|
143 |
// various suffixes that represent the current widget state(s).
|
|
|
144 |
//
|
|
|
145 |
// In the case where a widget has multiple
|
|
|
146 |
// states, it sets the class based on all possible
|
|
|
147 |
// combinations. For example, an invalid form widget that is being hovered
|
|
|
148 |
// will be "dijitInput dijitInputInvalid dijitInputHover dijitInputInvalidHover".
|
|
|
149 |
//
|
|
|
150 |
// For complex widgets with multiple regions, there can be various hover/active states,
|
|
|
151 |
// such as "Hover" or "CloseButtonHover" (for tab buttons).
|
|
|
152 |
// This is controlled by a stateModifier="CloseButton" attribute on the close button node.
|
|
|
153 |
//
|
|
|
154 |
// The widget may have one or more of the following states, determined
|
|
|
155 |
// by this.state, this.checked, this.valid, and this.selected:
|
|
|
156 |
// Error - ValidationTextBox sets this.state to "Error" if the current input value is invalid
|
|
|
157 |
// Checked - ex: a checkmark or a ToggleButton in a checked state, will have this.checked==true
|
|
|
158 |
// Selected - ex: currently selected tab will have this.selected==true
|
|
|
159 |
//
|
|
|
160 |
// In addition, it may have at most one of the following states,
|
|
|
161 |
// based on this.disabled and flags set in _onMouse (this._active, this._hovering, this._focused):
|
|
|
162 |
// Disabled - if the widget is disabled
|
|
|
163 |
// Active - if the mouse (or space/enter key?) is being pressed down
|
|
|
164 |
// Focused - if the widget has focus
|
|
|
165 |
// Hover - if the mouse is over the widget
|
|
|
166 |
//
|
|
|
167 |
// (even if multiple af the above conditions are true we only pick the first matching one)
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
// Get original (non state related, non baseClass related) class specified in template
|
|
|
171 |
if(!("staticClass" in this)){
|
|
|
172 |
this.staticClass = (this.stateNode||this.domNode).className;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
// Compute new set of classes
|
|
|
176 |
var classes = [ this.baseClass ];
|
|
|
177 |
|
|
|
178 |
function multiply(modifier){
|
|
|
179 |
classes=classes.concat(dojo.map(classes, function(c){ return c+modifier; }));
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
if(this.checked){
|
|
|
183 |
multiply("Checked");
|
|
|
184 |
}
|
|
|
185 |
if(this.state){
|
|
|
186 |
multiply(this.state);
|
|
|
187 |
}
|
|
|
188 |
if(this.selected){
|
|
|
189 |
multiply("Selected");
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// Only one of these four can be applied.
|
|
|
193 |
// Active trumps Focused, Focused trumps Hover, and Disabled trumps all.
|
|
|
194 |
if(this.disabled){
|
|
|
195 |
multiply("Disabled");
|
|
|
196 |
}else if(this._active){
|
|
|
197 |
multiply(this.stateModifier+"Active");
|
|
|
198 |
}else{
|
|
|
199 |
if(this._focused){
|
|
|
200 |
multiply("Focused");
|
|
|
201 |
}
|
|
|
202 |
if((this.stateModifier || !this._focused) && this._hovering){
|
|
|
203 |
multiply(this.stateModifier+"Hover");
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
(this.stateNode || this.domNode).className = this.staticClass + " " + classes.join(" ");
|
|
|
207 |
},
|
|
|
208 |
|
|
|
209 |
onChange: function(newValue){
|
|
|
210 |
// summary: callback when value is changed
|
|
|
211 |
},
|
|
|
212 |
|
|
|
213 |
postCreate: function(){
|
|
|
214 |
this.setValue(this.value, null); // null reserved for initial value
|
|
|
215 |
this.setDisabled(this.disabled);
|
|
|
216 |
this._setStateClass();
|
|
|
217 |
},
|
|
|
218 |
|
|
|
219 |
setValue: function(/*anything*/ newValue, /*Boolean, optional*/ priorityChange){
|
|
|
220 |
// summary: set the value of the widget.
|
|
|
221 |
this._lastValue = newValue;
|
|
|
222 |
dijit.setWaiState(this.focusNode || this.domNode, "valuenow", this.forWaiValuenow());
|
|
|
223 |
if(priorityChange === undefined){ priorityChange = true; } // setValue with value only should fire onChange
|
|
|
224 |
if(this._lastValueReported == undefined && priorityChange === null){ // don't report the initial value
|
|
|
225 |
this._lastValueReported = newValue;
|
|
|
226 |
}
|
|
|
227 |
if((this.intermediateChanges || priorityChange) &&
|
|
|
228 |
((newValue && newValue.toString)?newValue.toString():newValue) !== ((this._lastValueReported && this._lastValueReported.toString)?this._lastValueReported.toString():this._lastValueReported)){
|
|
|
229 |
this._lastValueReported = newValue;
|
|
|
230 |
this.onChange(newValue);
|
|
|
231 |
}
|
|
|
232 |
},
|
|
|
233 |
|
|
|
234 |
getValue: function(){
|
|
|
235 |
// summary: get the value of the widget.
|
|
|
236 |
return this._lastValue;
|
|
|
237 |
},
|
|
|
238 |
|
|
|
239 |
undo: function(){
|
|
|
240 |
// summary: restore the value to the last value passed to onChange
|
|
|
241 |
this.setValue(this._lastValueReported, false);
|
|
|
242 |
},
|
|
|
243 |
|
|
|
244 |
_onKeyPress: function(e){
|
|
|
245 |
if(e.keyCode == dojo.keys.ESCAPE && !e.shiftKey && !e.ctrlKey && !e.altKey){
|
|
|
246 |
var v = this.getValue();
|
|
|
247 |
var lv = this._lastValueReported;
|
|
|
248 |
// Equality comparison of objects such as dates are done by reference so
|
|
|
249 |
// two distinct objects are != even if they have the same data. So use
|
|
|
250 |
// toStrings in case the values are objects.
|
|
|
251 |
if((typeof lv != "undefined") && ((v!==null && v.toString)?v.toString():null) !== lv.toString()){
|
|
|
252 |
this.undo();
|
|
|
253 |
dojo.stopEvent(e);
|
|
|
254 |
return false;
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
return true;
|
|
|
258 |
},
|
|
|
259 |
|
|
|
260 |
forWaiValuenow: function(){
|
|
|
261 |
// summary: returns a value, reflecting the current state of the widget,
|
|
|
262 |
// to be used for the ARIA valuenow.
|
|
|
263 |
// This method may be overridden by subclasses that want
|
|
|
264 |
// to use something other than this.getValue() for valuenow
|
|
|
265 |
return this.getValue();
|
|
|
266 |
}
|
|
|
267 |
});
|
|
|
268 |
|
|
|
269 |
}
|