forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
275 lines (250 loc) · 9.05 KB
/
input.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shakaDemo.BoolInput');
goog.provide('shakaDemo.DatalistInput');
goog.provide('shakaDemo.Input');
goog.provide('shakaDemo.NumberInput');
goog.provide('shakaDemo.SelectInput');
goog.provide('shakaDemo.TextInput');
goog.require('shakaDemo.MessageIds');
goog.requireType('shakaDemo.InputContainer');
/**
* Creates and contains the MDL elements of a type of input.
*/
shakaDemo.Input = class {
/**
* @param {!shakaDemo.InputContainer} parentContainer
* @param {string} inputType The element type for the input object.
* @param {string} containerType The element type for the container containing
* the input object.
* @param {string} extraType The element type for the "sibling element" to the
* input object. If null, it adds no such element.
* @param {function(!HTMLInputElement, !shakaDemo.Input)} onChange
*/
constructor(parentContainer, inputType, containerType, extraType, onChange) {
/** @private {!Element} */
this.container_ = document.createElement(containerType);
parentContainer.latestElementContainer.appendChild(this.container_);
/** @private {!HTMLInputElement} */
this.input_ =
/** @type {!HTMLInputElement} */(document.createElement(inputType));
this.input_.onchange = () => {
onChange(this.input_, this);
};
// <textarea> elements need to also react to 'input' events.
if (inputType == 'textarea') {
this.input_.oninput = () => {
onChange(this.input_, this);
};
}
this.input_.id = shakaDemo.Input.generateNewId_('input');
this.container_.appendChild(this.input_);
if (parentContainer.latestTooltip) {
// Since the row isn't focusable, add the tooltip information into the
// accessibility data of the input, so it can be accessed by users using
// screen readers.
const extraInfo = document.createElement('span');
extraInfo.textContent = parentContainer.latestTooltip;
extraInfo.classList.add('hidden');
extraInfo.id = shakaDemo.Input.generateNewId_('extra-info');
this.container_.appendChild(extraInfo);
this.input_.setAttribute('aria-describedby', extraInfo.id);
}
/**
* Most MDL inputs require some sort of "sibling element" that exists at
* the same level as the input itself. These other elements are used to
* create various visual effects, such as the ripple effect.
* @private {?Element}
*/
this.extra_ = null;
if (extraType) {
this.extra_ = document.createElement(extraType);
this.container_.appendChild(this.extra_);
}
}
/** @return {!HTMLInputElement} */
input() {
return this.input_;
}
/** @return {!Element} */
container() {
return this.container_;
}
/** @return {?Element} */
extra() {
return this.extra_;
}
/** @param {boolean} valid */
setValid(valid) {
if (valid) {
this.input_.setCustomValidity(''); // valid
this.container_.classList.remove('is-invalid');
} else {
this.input_.setCustomValidity('invalid'); // any message will do
this.container_.parentElement.classList.add('is-invalid');
}
}
/**
* @param {string} prefix
* @return {string}
* @private
*/
static generateNewId_(prefix) {
const idNumber = shakaDemo.Input.lastId_;
shakaDemo.Input.lastId_ += 1;
return prefix + '-labeled-' + idNumber;
}
};
/** @private {number} */
shakaDemo.Input.lastId_ = 0;
/**
* Creates and contains the MDL elements of a select input.
*/
shakaDemo.SelectInput = class extends shakaDemo.Input {
/**
* @param {!shakaDemo.InputContainer} parentContainer
* @param {?shakaDemo.MessageIds} name
* @param {function(!HTMLInputElement, !shakaDemo.Input)} onChange
* @param {!Object.<string, string>} values
*/
constructor(parentContainer, name, onChange, values) {
super(parentContainer, 'select', 'div', 'label', onChange);
this.container_.classList.add('mdl-textfield');
this.container_.classList.add('mdl-js-textfield');
this.container_.classList.add('mdl-textfield--floating-label');
this.input_.classList.add('mdl-textfield__input');
this.extra_.classList.add('mdl-textfield__label');
this.extra_.setAttribute('for', this.input_.id);
if (name) {
this.extra_.textContent = shakaDemoMain.getLocalizedString(name);
}
for (const value of Object.keys(values)) {
const option =
/** @type {!HTMLOptionElement} */(document.createElement('option'));
option.textContent = values[value];
option.value = value;
this.input_.appendChild(option);
}
}
};
/**
* Creates and contains the MDL elements of a bool input.
*/
shakaDemo.BoolInput = class extends shakaDemo.Input {
/**
* @param {!shakaDemo.InputContainer} parentContainer
* @param {string} name
* @param {function(!HTMLInputElement, !shakaDemo.Input)} onChange
*/
constructor(parentContainer, name, onChange) {
super(parentContainer, 'input', 'label', 'span', onChange);
this.input_.type = 'checkbox';
this.container_.classList.add('mdl-switch');
this.container_.classList.add('mdl-js-switch');
this.container_.classList.add('mdl-js-ripple-effect');
this.container_.setAttribute('for', this.input_.id);
this.input_.classList.add('mdl-switch__input');
this.extra_.classList.add('mdl-switch__label');
}
};
/**
* Creates and contains the MDL elements of a text input.
*/
shakaDemo.TextInput = class extends shakaDemo.Input {
/**
* @param {!shakaDemo.InputContainer} parentContainer
* @param {string} name
* @param {function(!HTMLInputElement, !shakaDemo.Input)} onChange
* @param {boolean=} isTextArea
*/
constructor(parentContainer, name, onChange, isTextArea) {
super(parentContainer, isTextArea ? 'textarea' : 'input', 'div', 'label',
onChange);
this.container_.classList.add('mdl-textfield');
this.container_.classList.add('mdl-js-textfield');
this.container_.classList.add('mdl-textfield--floating-label');
this.input_.classList.add('mdl-textfield__input');
this.extra_.classList.add('mdl-textfield__label');
this.extra_.setAttribute('for', this.input_.id);
}
};
/**
* Creates and contains the MDL elements of a datalist input.
*/
shakaDemo.DatalistInput = class extends shakaDemo.TextInput {
/**
* @param {!shakaDemo.InputContainer} parentContainer
* @param {string} name
* @param {function(!HTMLInputElement, !shakaDemo.Input)} onChange
* @param {!Array.<string>} values
*/
constructor(parentContainer, name, onChange, values) {
super(parentContainer, name, onChange, /* isTextArea= */ false);
// This element is not literally a datalist, as those are not supported on
// all platforms (and they also have no MDL style support).
// Instead, this is using the third-party "awesomplete" module, which acts
// as a text field with autocomplete selection.
const awesomplete = new Awesomplete(this.input_);
awesomplete.list = values.slice(); // Make a local copy of the values list.
awesomplete.minChars = 0;
this.input_.addEventListener('focus', () => {
// By default, awesomplete does not show suggestions on focusing on the
// input, only on typing something.
// This manually updates the suggestions, so that they will show up.
awesomplete.evaluate();
});
this.input_.addEventListener('awesomplete-selectcomplete', () => {
onChange(this.input_, this);
});
}
};
/**
* Creates and contains the MDL elements of a number input.
*/
shakaDemo.NumberInput = class extends shakaDemo.TextInput {
/**
* @param {!shakaDemo.InputContainer} parentContainer
* @param {string} name
* @param {function(!HTMLInputElement, !shakaDemo.Input)} onChange
* @param {boolean} canBeDecimal
* @param {boolean} canBeZero
* @param {boolean} canBeUnset
*/
constructor(
parentContainer, name, onChange, canBeDecimal, canBeZero, canBeUnset) {
super(parentContainer, name, onChange, /* isTextArea= */ false);
const error = document.createElement('span');
error.classList.add('mdl-textfield__error');
this.container_.appendChild(error);
const MessageIds = shakaDemo.MessageIds;
const localize = (name) => shakaDemoMain.getLocalizedString(name);
if (canBeZero && canBeDecimal) {
error.textContent = localize(MessageIds.NUMBER_DECIMAL_WARNING);
} else if (canBeZero) {
error.textContent = localize(MessageIds.NUMBER_INTEGER_WARNING);
} else if (canBeDecimal) {
error.textContent =
localize(MessageIds.NUMBER_NONZERO_DECIMAL_WARNING);
} else {
error.textContent =
localize(MessageIds.NUMBER_NONZERO_INTEGER_WARNING);
}
this.input_.pattern = '(Infinity|';
if (canBeZero) {
this.input_.pattern += '[0-9]*';
} else {
this.input_.pattern += '[0-9]*[1-9][0-9]*';
}
if (canBeDecimal) {
// TODO: Handle commas as decimal delimeters, for appropriate regions?
this.input_.pattern += '(.[0-9]+)?';
}
this.input_.pattern += ')';
if (canBeUnset) {
this.input_.pattern += '?';
}
}
};