Debian Codegraph @

dijit/form/_FormWidgetMixin.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach
3 "dojo/_base/declare", // declare
4 "dojo/dom-attr", // domAttr.set
5 "dojo/dom-style", // domStyle.get
6 "dojo/_base/lang", // lang.hitch lang.isArray
7 "dojo/mouse", // mouse.isLeft
8 "dojo/on",
9 "dojo/sniff", // has("webkit")
10 "dojo/window", // winUtils.scrollIntoView
11 "../a11y" // a11y.hasDefaultTabStop
12 ], function(array, declare, domAttr, domStyle, lang, mouse, on, has, winUtils, a11y){
13
14 // module:
15 // dijit/form/_FormWidgetMixin
16
17 return declare("dijit.form._FormWidgetMixin", null, {
18 // summary:
19 // Mixin for widgets corresponding to native HTML elements such as `<checkbox>` or `<button>`,
20 // which can be children of a `<form>` node or a `dijit/form/Form` widget.
21 //
22 // description:
23 // Represents a single HTML element.
24 // All these widgets should have these attributes just like native HTML input elements.
25 // You can set them during widget construction or afterwards, via `dijit/_WidgetBase.set()`.
26 //
27 // They also share some common methods.
28
29 // name: [const] String
30 // Name used when submitting form; same as "name" attribute or plain HTML elements
31 name: "",
32
33 // alt: String
34 // Corresponds to the native HTML `<input>` element's attribute.
35 alt: "",
36
37 // value: String
38 // Corresponds to the native HTML `<input>` element's attribute.
39 value: "",
40
41 // type: [const] String
42 // Corresponds to the native HTML `<input>` element's attribute.
43 type: "text",
44
45 // type: String
46 // Apply aria-label in markup to the widget's focusNode
47 "aria-label": "focusNode",
48
49 // tabIndex: String
50 // Order fields are traversed when user hits the tab key
51 tabIndex: "0",
52 _setTabIndexAttr: "focusNode", // force copy even when tabIndex default value, needed since Button is <span>
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 // scrollOnFocus: Boolean
64 // On focus, should this widget scroll into view?
65 scrollOnFocus: true,
66
67 // Override _WidgetBase mapping id to this.domNode, needs to be on focusNode so <label> etc.
68 // works with screen reader
69 _setIdAttr: "focusNode",
70
71 _setDisabledAttr: function(/*Boolean*/ value){
72 this._set("disabled", value);
73
74 // Set disabled property if focusNode is an <input>, but aria-disabled attribute if focusNode is a <span>.
75 // Can't use "disabled" in this.focusNode as a test because on IE, that's true for all nodes.
76 if(/^(button|input|select|textarea|optgroup|option|fieldset)$/i.test(this.focusNode.tagName)){
77 domAttr.set(this.focusNode, 'disabled', value);
78 // IE has a Caret Browsing mode (hit F7 to activate) where disabled textboxes can be modified
79 // textboxes marked readonly if disabled to avoid this issue.
80 if (has('trident') && 'readOnly' in this) {
81 domAttr.set(this.focusNode, 'readonly', value || this.readOnly);
82 }
83 }else{
84 this.focusNode.setAttribute("aria-disabled", value ? "true" : "false");
85 }
86
87 // And also set disabled on the hidden <input> node
88 if(this.valueNode){
89 domAttr.set(this.valueNode, 'disabled', value);
90 }
91
92 if(value){
93 // reset these, because after the domNode is disabled, we can no longer receive
94 // mouse related events, see #4200
95 this._set("hovering", false);
96 this._set("active", false);
97
98 // clear tab stop(s) on this widget's focusable node(s) (ComboBox has two focusable nodes)
99 var attachPointNames = "tabIndex" in this.attributeMap ? this.attributeMap.tabIndex :
100 ("_setTabIndexAttr" in this) ? this._setTabIndexAttr : "focusNode";
101 array.forEach(lang.isArray(attachPointNames) ? attachPointNames : [attachPointNames], function(attachPointName){
102 var node = this[attachPointName];
103 // complex code because tabIndex=-1 on a <div> doesn't work on FF
104 if(has("webkit") || a11y.hasDefaultTabStop(node)){ // see #11064 about webkit bug
105 node.setAttribute('tabIndex', "-1");
106 }else{
107 node.removeAttribute('tabIndex');
108 }
109 }, this);
110 }else{
111 if(this.tabIndex != ""){
112 this.set('tabIndex', this.tabIndex);
113 }
114 }
115 },
116
117 _onFocus: function(/*String*/ by){
118 // If user clicks on the widget, even if the mouse is released outside of it,
119 // this widget's focusNode should get focus (to mimic native browser behavior).
120 // Browsers often need help to make sure the focus via mouse actually gets to the focusNode.
121 // TODO: consider removing all of this for 2.0 or sooner, see #16622 etc.
122 if(by == "mouse" && this.isFocusable()){
123 // IE exhibits strange scrolling behavior when refocusing a node so only do it when !focused.
124 var focusHandle = this.own(on(this.focusNode, "focus", function(){
125 mouseUpHandle.remove();
126 focusHandle.remove();
127 }))[0];
128 // Set a global event to handle mouseup, so it fires properly
129 // even if the cursor leaves this.domNode before the mouse up event.
130 var event = has("pointer-events") ? "pointerup" : has("MSPointer") ? "MSPointerUp" :
131 has("touch-events") ? "touchend, mouseup" : // seems like overkill but see #16622, #16725
132 "mouseup";
133 var mouseUpHandle = this.own(on(this.ownerDocumentBody, event, lang.hitch(this, function(evt){
134 mouseUpHandle.remove();
135 focusHandle.remove();
136 // if here, then the mousedown did not focus the focusNode as the default action
137 if(this.focused){
138 if(evt.type == "touchend"){
139 this.defer("focus"); // native focus hasn't occurred yet
140 }else{
141 this.focus(); // native focus already occurred on mousedown
142 }
143 }
144 })))[0];
145 }
146 if(this.scrollOnFocus){
147 this.defer(function(){
148 winUtils.scrollIntoView(this.domNode);
149 }); // without defer, the input caret position can change on mouse click
150 }
151 this.inherited(arguments);
152 },
153
154 isFocusable: function(){
155 // summary:
156 // Tells if this widget is focusable or not. Used internally by dijit.
157 // tags:
158 // protected
159 return !this.disabled && this.focusNode && (domStyle.get(this.domNode, "display") != "none");
160 },
161
162 focus: function(){
163 // summary:
164 // Put focus on this widget
165 if(!this.disabled && this.focusNode.focus){
166 try{
167 this.focusNode.focus();
168 }catch(e){
169 }
170 /*squelch errors from hidden nodes*/
171 }
172 },
173
174 compare: function(/*anything*/ val1, /*anything*/ val2){
175 // summary:
176 // Compare 2 values (as returned by get('value') for this widget).
177 // tags:
178 // protected
179 if(typeof val1 == "number" && typeof val2 == "number"){
180 return (isNaN(val1) && isNaN(val2)) ? 0 : val1 - val2;
181 }else if(val1 > val2){
182 return 1;
183 }else if(val1 < val2){
184 return -1;
185 }else{
186 return 0;
187 }
188 },
189
190 onChange: function(/*===== newValue =====*/){
191 // summary:
192 // Callback when this widget's value is changed.
193 // tags:
194 // callback
195 },
196
197 // _onChangeActive: [private] Boolean
198 // Indicates that changes to the value should call onChange() callback.
199 // This is false during widget initialization, to avoid calling onChange()
200 // when the initial value is set.
201 _onChangeActive: false,
202
203 _handleOnChange: function(/*anything*/ newValue, /*Boolean?*/ priorityChange){
204 // summary:
205 // Called when the value of the widget is set. Calls onChange() if appropriate
206 // newValue:
207 // the new value
208 // priorityChange:
209 // For a slider, for example, dragging the slider is priorityChange==false,
210 // but on mouse up, it's priorityChange==true. If intermediateChanges==false,
211 // onChange is only called form priorityChange=true events.
212 // tags:
213 // private
214 if(this._lastValueReported == undefined && (priorityChange === null || !this._onChangeActive)){
215 // this block executes not for a change, but during initialization,
216 // and is used to store away the original value (or for ToggleButton, the original checked state)
217 this._resetValue = this._lastValueReported = newValue;
218 }
219 this._pendingOnChange = this._pendingOnChange
220 || (typeof newValue != typeof this._lastValueReported)
221 || (this.compare(newValue, this._lastValueReported) != 0);
222 if((this.intermediateChanges || priorityChange || priorityChange === undefined) && this._pendingOnChange){
223 this._lastValueReported = newValue;
224 this._pendingOnChange = false;
225 if(this._onChangeActive){
226 if(this._onChangeHandle){
227 this._onChangeHandle.remove();
228 }
229 // defer allows hidden value processing to run and
230 // also the onChange handler can safely adjust focus, etc
231 this._onChangeHandle = this.defer(
232 function(){
233 this._onChangeHandle = null;
234 this.onChange(newValue);
235 }); // try to collapse multiple onChange's fired faster than can be processed
236 }
237 }
238 },
239
240 create: function(){
241 // Overrides _Widget.create()
242 this.inherited(arguments);
243 this._onChangeActive = true;
244 },
245
246 destroy: function(){
247 if(this._onChangeHandle){ // destroy called before last onChange has fired
248 this._onChangeHandle.remove();
249 this.onChange(this._lastValueReported);
250 }
251 this.inherited(arguments);
252 }
253 });
254 });