Debian Codegraph @

dojox/mobile/app/_FormWidget.js

raw symbols SCIP
1 dojo.provide("dojox.mobile.app._FormWidget");
2 dojo.experimental("dojox.mobile.app._FormWidget");
3
4 dojo.require("dojo.window");
5
6 dojo.require("dijit._WidgetBase");
7 dojo.require("dijit.focus"); // dijit.focus()
8
9 dojo.declare("dojox.mobile.app._FormWidget", dijit._WidgetBase, {
10 // summary:
11 // Base class for widgets corresponding to native HTML elements such as `<checkbox>` or `<button>`,
12 // which can be children of a `<form>` node or a `dojox.mobile.app.Form` widget.
13 //
14 // description:
15 // Represents a single HTML element.
16 // All these widgets should have these attributes just like native HTML input elements.
17 // You can set them during widget construction or afterwards, via `dijit._WidgetBase.attr`.
18 //
19 // They also share some common methods.
20
21 // name: String
22 // Name used when submitting form; same as "name" attribute or plain HTML elements
23 name: "",
24
25 // alt: String
26 // Corresponds to the native HTML `<input>` element's attribute.
27 alt: "",
28
29 // value: String
30 // Corresponds to the native HTML `<input>` element's attribute.
31 value: "",
32
33 // type: String
34 // Corresponds to the native HTML `<input>` element's attribute.
35 type: "text",
36
37 // disabled: Boolean
38 // Should this widget respond to user input?
39 // In markup, this is specified as "disabled='disabled'", or just "disabled".
40 disabled: false,
41
42 // intermediateChanges: Boolean
43 // Fires onChange for each value change or only on demand
44 intermediateChanges: false,
45
46 // scrollOnFocus: Boolean
47 // On focus, should this widget scroll into view?
48 scrollOnFocus: false,
49
50 // These mixins assume that the focus node is an INPUT, as many but not all _FormWidgets are.
51 attributeMap: dojo.delegate(dijit._WidgetBase.prototype.attributeMap, {
52 value: "focusNode",
53 id: "focusNode",
54 alt: "focusNode",
55 title: "focusNode"
56 }),
57
58 postMixInProperties: function(){
59 // Setup name=foo string to be referenced from the template (but only if a name has been specified)
60 // Unfortunately we can't use attributeMap to set the name due to IE limitations, see #8660
61 // Regarding escaping, see heading "Attribute values" in
62 // http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.3.2
63 this.nameAttrSetting = this.name ? ('name="' + this.name.replace(/'/g, "&quot;") + '"') : '';
64 this.inherited(arguments);
65 },
66
67 postCreate: function(){
68 this.inherited(arguments);
69 this.connect(this.domNode, "onmousedown", "_onMouseDown");
70 },
71
72 _setDisabledAttr: function(/*Boolean*/ value){
73 this.disabled = value;
74 dojo.attr(this.focusNode, 'disabled', value);
75 if(this.valueNode){
76 dojo.attr(this.valueNode, 'disabled', value);
77 }
78 },
79
80 _onFocus: function(e){
81 if(this.scrollOnFocus){
82 dojo.window.scrollIntoView(this.domNode);
83 }
84 this.inherited(arguments);
85 },
86
87 isFocusable: function(){
88 // summary:
89 // Tells if this widget is focusable or not. Used internally by dijit.
90 // tags:
91 // protected
92 return !this.disabled && !this.readOnly
93 && this.focusNode && (dojo.style(this.domNode, "display") != "none");
94 },
95
96 focus: function(){
97 // summary:
98 // Put focus on this widget
99 this.focusNode.focus();
100 },
101
102 compare: function(/*anything*/val1, /*anything*/val2){
103 // summary:
104 // Compare 2 values (as returned by attr('value') for this widget).
105 // tags:
106 // protected
107 if(typeof val1 == "number" && typeof val2 == "number"){
108 return (isNaN(val1) && isNaN(val2)) ? 0 : val1 - val2;
109 }else if(val1 > val2){
110 return 1;
111 }else if(val1 < val2){
112 return -1;
113 }else{
114 return 0;
115 }
116 },
117
118 onChange: function(newValue){
119 // summary:
120 // Callback when this widget's value is changed.
121 // tags:
122 // callback
123 },
124
125 // _onChangeActive: [private] Boolean
126 // Indicates that changes to the value should call onChange() callback.
127 // This is false during widget initialization, to avoid calling onChange()
128 // when the initial value is set.
129 _onChangeActive: false,
130
131 _handleOnChange: function(/*anything*/ newValue, /* Boolean? */ priorityChange){
132 // summary:
133 // Called when the value of the widget is set. Calls onChange() if appropriate
134 // newValue:
135 // the new value
136 // priorityChange:
137 // For a slider, for example, dragging the slider is priorityChange==false,
138 // but on mouse up, it's priorityChange==true. If intermediateChanges==true,
139 // onChange is only called form priorityChange=true events.
140 // tags:
141 // private
142 this._lastValue = newValue;
143 if(this._lastValueReported == undefined && (priorityChange === null || !this._onChangeActive)){
144 // this block executes not for a change, but during initialization,
145 // and is used to store away the original value (or for ToggleButton, the original checked state)
146 this._resetValue = this._lastValueReported = newValue;
147 }
148 if((this.intermediateChanges || priorityChange || priorityChange === undefined) &&
149 ((typeof newValue != typeof this._lastValueReported) ||
150 this.compare(newValue, this._lastValueReported) != 0)){
151 this._lastValueReported = newValue;
152 if(this._onChangeActive){
153 if(this._onChangeHandle){
154 clearTimeout(this._onChangeHandle);
155 }
156 // setTimout allows hidden value processing to run and
157 // also the onChange handler can safely adjust focus, etc
158 this._onChangeHandle = setTimeout(dojo.hitch(this,
159 function(){
160 this._onChangeHandle = null;
161 this.onChange(newValue);
162 }), 0); // try to collapse multiple onChange's fired faster than can be processed
163 }
164 }
165 },
166
167 create: function(){
168 // Overrides _Widget.create()
169 this.inherited(arguments);
170 this._onChangeActive = true;
171 },
172
173 destroy: function(){
174 if(this._onChangeHandle){ // destroy called before last onChange has fired
175 clearTimeout(this._onChangeHandle);
176 this.onChange(this._lastValueReported);
177 }
178 this.inherited(arguments);
179 },
180
181 _onMouseDown: function(e){
182 // If user clicks on the button, even if the mouse is released outside of it,
183 // this button should get focus (to mimics native browser buttons).
184 // This is also needed on chrome because otherwise buttons won't get focus at all,
185 // which leads to bizarre focus restore on Dialog close etc.
186 if(this.isFocusable()){
187 // Set a global event to handle mouseup, so it fires properly
188 // even if the cursor leaves this.domNode before the mouse up event.
189 var mouseUpConnector = this.connect(dojo.body(), "onmouseup", function(){
190 if(this.isFocusable()){
191 this.focus();
192 }
193 this.disconnect(mouseUpConnector);
194 });
195 }
196 },
197
198 selectInputText: function(/*DomNode*/element, /*Number?*/ start, /*Number?*/ stop){
199 // summary:
200 // Select text in the input element argument, from start (default 0), to stop (default end).
201
202 // TODO: use functions in _editor/selection.js?
203 var _window = dojo.global;
204 var _document = dojo.doc;
205 element = dojo.byId(element);
206 if(isNaN(start)){ start = 0; }
207 if(isNaN(stop)){ stop = element.value ? element.value.length : 0; }
208 dijit.focus(element);
209
210 if(_window["getSelection"] && element.setSelectionRange){
211 element.setSelectionRange(start, stop);
212 }
213 }
214 });
215
216 dojo.declare("dojox.mobile.app._FormValueWidget", dojox.mobile.app._FormWidget,
217 {
218 // summary:
219 // Base class for widgets corresponding to native HTML elements such as `<input>` or `<select>`
220 // that have user changeable values.
221 // description:
222 // Each _FormValueWidget represents a single input value, and has a (possibly hidden) `<input>` element,
223 // to which it serializes it's input value, so that form submission (either normal submission or via FormBind?)
224 // works as expected.
225
226 // Don't attempt to mixin the 'type', 'name' attributes here programatically -- they must be declared
227 // directly in the template as read by the parser in order to function. IE is known to specifically
228 // require the 'name' attribute at element creation time. See #8484, #8660.
229 // TODO: unclear what that {value: ""} is for; FormWidget.attributeMap copies value to focusNode,
230 // so maybe {value: ""} is so the value *doesn't* get copied to focusNode?
231 // Seems like we really want value removed from attributeMap altogether
232 // (although there's no easy way to do that now)
233
234 // readOnly: Boolean
235 // Should this widget respond to user input?
236 // In markup, this is specified as "readOnly".
237 // Similar to disabled except readOnly form values are submitted.
238 readOnly: false,
239
240 attributeMap: dojo.delegate(dojox.mobile.app._FormWidget.prototype.attributeMap, {
241 value: "",
242 readOnly: "focusNode"
243 }),
244
245 _setReadOnlyAttr: function(/*Boolean*/ value){
246 this.readOnly = value;
247 dojo.attr(this.focusNode, 'readOnly', value);
248 },
249
250 postCreate: function(){
251 this.inherited(arguments);
252
253 // Update our reset value if it hasn't yet been set (because this.set()
254 // is only called when there *is* a value)
255 if(this._resetValue === undefined){
256 this._resetValue = this.value;
257 }
258 },
259
260 _setValueAttr: function(/*anything*/ newValue, /*Boolean, optional*/ priorityChange){
261 // summary:
262 // Hook so attr('value', value) works.
263 // description:
264 // Sets the value of the widget.
265 // If the value has changed, then fire onChange event, unless priorityChange
266 // is specified as null (or false?)
267 this.value = newValue;
268 this._handleOnChange(newValue, priorityChange);
269 },
270
271 _getValueAttr: function(){
272 // summary:
273 // Hook so attr('value') works.
274 return this._lastValue;
275 },
276
277 undo: function(){
278 // summary:
279 // Restore the value to the last value passed to onChange
280 this._setValueAttr(this._lastValueReported, false);
281 },
282
283 reset: function(){
284 // summary:
285 // Reset the widget's value to what it was at initialization time
286 this._hasBeenBlurred = false;
287 this._setValueAttr(this._resetValue, true);
288 }
289 });