-
Notifications
You must be signed in to change notification settings - Fork 28
/
FormWidget.js
367 lines (330 loc) · 11.6 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/** @module delite/FormWidget */
define([
"dcl/dcl",
"./Widget"
], function (dcl, Widget) {
// Detect if specified instance or its prototypes, not including HTMLElement, have defined the specified property.
function overridesProperty (instance, prop) {
for (var proto = Object.getPrototypeOf(instance);
proto && proto !== instance._BaseHTMLElement.prototype;
proto = Object.getPrototypeOf(proto)
) {
var descriptor = Object.getOwnPropertyDescriptor(proto, prop);
if (descriptor) {
return true;
}
}
}
/**
* 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# */ {
declaredClass: "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,
/**
* Array of names 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 {Array|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,
/**
* Sets the `required` property of the focus nodes, or their `aria-required` attribute if they do not support
* the `required` property.
* @member {boolean}
* @default false
*/
required: false,
/**
* If set to true, the widget move all aria-* attributes found on the root DOM element of this widget and
* apply those to the focusNode using the overridden setAttribute method.
* @member {boolean}
* @default true
*/
moveAriaAttributes: true,
/**
* 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
*/
/**
* 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
*/
_mapAttributes: dcl.superCall(function (sup) {
return function () {
var input = this.querySelector("input");
if (input) {
// Get value and name from embedded <input> node.
if (input.value) {
this.setAttribute("value", input.value);
}
if (input.name) {
this.setAttribute("name", input.name);
}
} else {
// Create this.valueNode as a convenience, but don't add to the DOM because that breaks widgets like
// deliteful/Checkbox that unconditionally create their own this.valueNode:
// You end up with two embedded <input> nodes.
input = this.ownerDocument.createElement("input");
}
this.valueNode = input;
return sup.call(this);
};
}),
refreshRendering: function (oldValues) {
// Handle disabled, required and tabIndex, across the tabStops and root node.
// No special processing is needed for tabStops other than just to refresh disabled, required 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.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 || "required" in oldValues || "id" in oldValues) {
var inputIdUnset = true;
var inputId;
if ("id" in oldValues && this.id) {
inputId = this.id + "-input";
}
this.forEachFocusNode(function (node) {
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;
// Set the disabled property for native elements like <input>, and also custom elements with a
// disabled property. Otherwise set aria-disabled. Note that on IE every element has a
// disabled property, so it's hard to test if it's real or not
if (/^(button|fieldset|input|keygen|optgroup|option|select|textarea)$/i.test(node.tagName) ||
(node._ctor && overridesProperty(node, "disabled"))) {
node.disabled = this.disabled;
} else {
node.setAttribute("aria-disabled", "" + this.disabled);
}
// Likewise for aria-required.
if (/^(input|select|textarea)$/i.test(node.tagName) ||
(node._ctor && overridesProperty(node, "required"))) {
node.required = this.required;
} else if (/^(combobox|gridcell|listbox|radiogroup|spinbutton|textbox|tree)$/i.test(
node.getAttribute("role"))) {
node.setAttribute("aria-required", "" + this.required);
}
// Set the focus node's id.
if (/^(input|textarea|select|button|keygen)$/i.test(node.tagName) && inputId && inputIdUnset) {
inputIdUnset = false;
if (!node.id) {
node.id = inputId;
}
}
});
}
// Set properties on valueNode.
var valueNode = this.valueNode !== this && this.valueNode;
if (valueNode) {
if ("value" in oldValues && valueNode.value !== this.value) {
// Update <input>'s value if necessary, but don't update the value because the user
// typed a character into the <input> as that will move the caret to the end of the
// <input>.
valueNode.value = this.value;
}
if ("disabled" in oldValues) {
valueNode.disabled = this.disabled; // prevent submit
}
if ("name" in oldValues) {
valueNode.name = this.name;
}
if ("required" in oldValues) {
valueNode.required = this.required;
}
}
},
/**
* 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[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.forEach(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.focusNode && this.moveAriaAttributes) {
this.focusNode.setAttribute(name, value);
} else {
sup.call(this, name, value);
}
};
}),
getAttribute: dcl.superCall(function (sup) {
return function (name) {
if (/^aria-/.test(name) && this.focusNode && this.moveAriaAttributes) {
return this.focusNode.getAttribute(name);
} else {
return sup.call(this, name);
}
};
}),
hasAttribute: dcl.superCall(function (sup) {
return function (name) {
if (/^aria-/.test(name) && this.focusNode && this.moveAriaAttributes) {
return this.focusNode.hasAttribute(name);
} else {
return sup.call(this, name);
}
};
}),
removeAttribute: dcl.superCall(function (sup) {
return function (name) {
if (/^aria-/.test(name) && this.focusNode && this.moveAriaAttributes) {
this.focusNode.removeAttribute(name);
} else {
sup.call(this, name);
}
};
}),
afterInitializeRendering: function () {
this._moveAriaAttributes();
},
connectedCallback: function () {
// If the widget is in a form, reset the initial value of the widget when the form is reset.
for (var form = this.parentNode; form; form = form.parentNode) {
if (/^form$/i.test(form.tagName)) {
this.on("reset", function () {
this.defer(function () {
this.afterFormResetCallback();
});
}.bind(this), form);
break;
}
}
},
/**
* Callback after `<form>` containing this widget is reset.
* By the time this callback executes, `this.valueNode.value` will have already been reset according to
* the form's original value.
*
* @protected
*/
afterFormResetCallback: function () {
if (this.checked !== this.valueNode.checked) {
this.checked = this.valueNode.checked;
}
},
/**
* Move all initially specified aria-* attributes to focus node.
*
* @protected
*/
_moveAriaAttributes: function () {
if (this.focusNode && this.moveAriaAttributes) {
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);
}
}
}
}
});
});