forked from ibm-js/delite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormWidget.js
244 lines (221 loc) · 7.56 KB
/
FormWidget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/** @module delite/FormWidget */
define([
"dcl/dcl",
"./Widget"
], function (dcl, Widget) {
/**
* Base class for widgets that extend `HTMLElement`, but conceptually correspond to form elements.
*
* Most form widgets should extend FormValueWidget rather than extending FormWidget directly, but
* FormWidget should be the base class for form widgets that *don't* have an end user settable value,
* for example checkboxes and buttons. Note that clicking a checkbox changes its state (i.e. the value of
* its `checked` property), but does not change its `value` property.
*
* Also note that both this widget and KeyNav define the `focus()` method, so if your widget extends both classes,
* take care that the `focus()` method you want takes precedence in the inheritance hierarchy.
*
* @mixin module:delite/FormWidget
* @augments module:delite/Widget
*/
return dcl(Widget, /** @lends module:delite/FormWidget# */ {
/**
* Name used when submitting form; same as "name" attribute or plain HTML elements.
* @member {string}
*/
name: "",
/**
* Corresponds to the native HTML `<input>` element's attribute.
* @member {string}
*/
alt: "",
/**
* Corresponds to the native HTML `<input>` element's attribute.
*
* For widgets that directly extend FormWidget (ex: checkboxes), the value is set programatically when the
* widget is created, and the end user merely changes the widget's state, i.e. the `checked` property.
*
* For widgets that extend FormValueWidget, the end user can interactively change the `value` property via
* mouse, keyboard, touch, etc.
*
* @member {string}
*/
value: "",
/**
* The order in which fields are traversed when user presses the tab key.
* @member {number}
* @default 0
*/
tabIndex: 0,
/**
* Comma separated list of tabbable nodes, i.e. comma separated list of widget properties that reference
* the widget DOM nodes that receive focus during tab operations.
*
* Aria roles are applied to these nodes rather than the widget root node.
*
* Note that FormWidget requires that all of the tabbable nodes be sub-nodes of the widget, rather than the
* root node. This is because of its processing of `tabIndex`.
*
* @member {string}
* @default "focusNode"
*/
tabStops: "focusNode",
/**
* If set to true, the widget will not respond to user input and will not be included in form submission.
* FormWidget automatically updates `valueNode`'s and `focusNode`'s `disabled` property to match the widget's
* `disabled` property.
* @member {boolean}
* @default false
*/
disabled: false,
/**
* For widgets with a single tab stop, the Element within the widget, often an `<input>`,
* that gets the focus. Widgets with multiple tab stops, such as a range slider, should set `tabStops`
* rather than setting `focusNode`.
*
* @member {HTMLElement} module:delite/FormWidget#focusNode
* @protected
*/
postRender: function () {
this.notifyCurrentValue("tabStops");
},
/**
* A form element, typically an `<input>`, embedded within the widget, and likely hidden.
* It is used to represent the widget's state/value during form submission.
*
* Subclasses of FormWidget like checkboxes and radios should update `valueNode`'s `checked` property.
*
* @member {HTMLElement} module:delite/FormWidget#valueNode
* @protected
* @default undefined
*/
refreshRendering: function (oldValues) {
// Handle disabled and tabIndex, across the tabStops and root node.
// No special processing is needed for tabStops other than just to refresh disabled and tabIndex.
// If the tab stops have changed then start by removing the tabIndex from all the old tab stops.
if ("tabStops" in oldValues) {
oldValues.tabStops.split(/, */).forEach(function (nodeName) {
var node = this[nodeName];
node.tabIndex = "-1"; // backup plan in case next line of code ineffective
node.removeAttribute("tabindex"); // works for <div> etc. but not <input>
}, this);
}
// Set tabIndex etc. for all tabbable nodes.
// To keep things simple, if anything has changed then reapply all the properties.
if ("tabStops" in oldValues || "tabIndex" in oldValues || "disabled" in oldValues || "alt" in oldValues) {
this.forEachFocusNode(function (node) {
node.disabled = this.disabled;
if (this.disabled) {
node.tabIndex = "-1"; // backup plan in case next line of code ineffective
node.removeAttribute("tabindex"); // works for <div> etc. but not <input>
} else {
node.tabIndex = this._get("tabIndex");
}
node.alt = this.alt;
node.setAttribute("aria-disabled", "" + this.disabled); // let JAWS know
});
}
// Set properties on valueNode.
var valueNode = this.valueNode !== this && this.valueNode;
if (valueNode) {
if ("value" in oldValues) {
valueNode.value = this.value;
}
if ("disabled" in oldValues) {
valueNode.disabled = this.disabled; // prevent submit
}
if ("name" in oldValues) {
valueNode.name = this.name;
}
}
},
/**
* Put focus on this widget.
*/
focus: function () {
var focusNode = this.firstFocusNode();
if (!this.disabled && focusNode.focus) {
try {
focusNode.focus();
} catch (e) {
// squelch errors from hidden nodes
}
}
},
/**
* Helper method to get the first focusable node, usually `this.focusNode`.
*
* @protected
*/
firstFocusNode: function () {
return this[this.tabStops.split(/, */)[0]];
},
/**
* Helper method to execute callback for each focusable node in the widget.
* Typically the callback is just called once, for `this.focusNode`.
* @param {Function} callback - The callback function.
* @protected
*/
forEachFocusNode: function (callback) {
this.tabStops.split(/, */).map(function (nodeName) {
var node = this[nodeName];
if (node !== this) { // guard against hard to debug infinite recursion
callback.call(this, node);
}
}, this);
},
////////////////////////////////////////////////////////////////////////////////////////////
// Override setAttribute() etc. to put aria-label etc. onto the focus node rather than the root
// node, so that screen readers work properly.
setAttribute: dcl.superCall(function (sup) {
return function (name, value) {
if (/^aria-/.test(name)) {
this.forEachFocusNode(function (node) {
node.setAttribute(name, value);
});
} else {
sup.call(this, name, value);
}
};
}),
getAttribute: dcl.superCall(function (sup) {
return function (name) {
if (/^aria-/.test(name)) {
return this.firstFocusNode().getAttribute(name);
} else {
return sup.call(this, name);
}
};
}),
hasAttribute: dcl.superCall(function (sup) {
return function (name) {
if (/^aria-/.test(name)) {
return this.firstFocusNode().hasAttribute(name);
} else {
return sup.call(this, name);
}
};
}),
removeAttribute: dcl.superCall(function (sup) {
return function (name) {
if (/^aria-/.test(name)) {
this.forEachFocusNode(function (node) {
node.removeAttribute(name);
});
} else {
sup.call(this, name);
}
};
}),
createdCallback: function () {
// Move all initially specified aria- attributes to focus node(s).
var attr, idx = 0;
while ((attr = this.attributes[idx++])) {
if (/^aria-/.test(attr.name)) {
this.setAttribute(attr.name, attr.value);
// force remove from root node not focus nodes
HTMLElement.prototype.removeAttribute.call(this, attr.name);
}
}
}
});
});