-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
text-track-settings.js
471 lines (408 loc) · 10.9 KB
/
text-track-settings.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* @file text-track-settings.js
*/
import window from 'global/window';
import Component from '../component';
import ModalDialog from '../modal-dialog';
import {createEl} from '../utils/dom';
import * as Obj from '../utils/obj';
import log from '../utils/log';
import TextTrackSettingsColors from './text-track-settings-colors';
import TextTrackSettingsFont from './text-track-settings-font';
import TrackSettingsControls from './text-track-settings-controls';
/** @import Player from '../player' */
const LOCAL_STORAGE_KEY = 'vjs-text-track-settings';
const COLOR_BLACK = ['#000', 'Black'];
const COLOR_BLUE = ['#00F', 'Blue'];
const COLOR_CYAN = ['#0FF', 'Cyan'];
const COLOR_GREEN = ['#0F0', 'Green'];
const COLOR_MAGENTA = ['#F0F', 'Magenta'];
const COLOR_RED = ['#F00', 'Red'];
const COLOR_WHITE = ['#FFF', 'White'];
const COLOR_YELLOW = ['#FF0', 'Yellow'];
const OPACITY_OPAQUE = ['1', 'Opaque'];
const OPACITY_SEMI = ['0.5', 'Semi-Transparent'];
const OPACITY_TRANS = ['0', 'Transparent'];
// Configuration for the various <select> elements in the DOM of this component.
//
// Possible keys include:
//
// `default`:
// The default option index. Only needs to be provided if not zero.
// `parser`:
// A function which is used to parse the value from the selected option in
// a customized way.
// `selector`:
// The selector used to find the associated <select> element.
const selectConfigs = {
backgroundColor: {
selector: '.vjs-bg-color > select',
id: 'captions-background-color-%s',
label: 'Color',
options: [
COLOR_BLACK,
COLOR_WHITE,
COLOR_RED,
COLOR_GREEN,
COLOR_BLUE,
COLOR_YELLOW,
COLOR_MAGENTA,
COLOR_CYAN
],
className: 'vjs-bg-color'
},
backgroundOpacity: {
selector: '.vjs-bg-opacity > select',
id: 'captions-background-opacity-%s',
label: 'Opacity',
options: [
OPACITY_OPAQUE,
OPACITY_SEMI,
OPACITY_TRANS
],
className: 'vjs-bg-opacity vjs-opacity'
},
color: {
selector: '.vjs-text-color > select',
id: 'captions-foreground-color-%s',
label: 'Color',
options: [
COLOR_WHITE,
COLOR_BLACK,
COLOR_RED,
COLOR_GREEN,
COLOR_BLUE,
COLOR_YELLOW,
COLOR_MAGENTA,
COLOR_CYAN
],
className: 'vjs-text-color'
},
edgeStyle: {
selector: '.vjs-edge-style > select',
id: '',
label: 'Text Edge Style',
options: [
['none', 'None'],
['raised', 'Raised'],
['depressed', 'Depressed'],
['uniform', 'Uniform'],
['dropshadow', 'Drop shadow']
]
},
fontFamily: {
selector: '.vjs-font-family > select',
id: '',
label: 'Font Family',
options: [
['proportionalSansSerif', 'Proportional Sans-Serif'],
['monospaceSansSerif', 'Monospace Sans-Serif'],
['proportionalSerif', 'Proportional Serif'],
['monospaceSerif', 'Monospace Serif'],
['casual', 'Casual'],
['script', 'Script'],
['small-caps', 'Small Caps']
]
},
fontPercent: {
selector: '.vjs-font-percent > select',
id: '',
label: 'Font Size',
options: [
['0.50', '50%'],
['0.75', '75%'],
['1.00', '100%'],
['1.25', '125%'],
['1.50', '150%'],
['1.75', '175%'],
['2.00', '200%'],
['3.00', '300%'],
['4.00', '400%']
],
default: 2,
parser: (v) => v === '1.00' ? null : Number(v)
},
textOpacity: {
selector: '.vjs-text-opacity > select',
id: 'captions-foreground-opacity-%s',
label: 'Opacity',
options: [
OPACITY_OPAQUE,
OPACITY_SEMI
],
className: 'vjs-text-opacity vjs-opacity'
},
// Options for this object are defined below.
windowColor: {
selector: '.vjs-window-color > select',
id: 'captions-window-color-%s',
label: 'Color',
className: 'vjs-window-color'
},
// Options for this object are defined below.
windowOpacity: {
selector: '.vjs-window-opacity > select',
id: 'captions-window-opacity-%s',
label: 'Opacity',
options: [
OPACITY_TRANS,
OPACITY_SEMI,
OPACITY_OPAQUE
],
className: 'vjs-window-opacity vjs-opacity'
}
};
selectConfigs.windowColor.options = selectConfigs.backgroundColor.options;
/**
* Get the actual value of an option.
*
* @param {string} value
* The value to get
*
* @param {Function} [parser]
* Optional function to adjust the value.
*
* @return {*}
* - Will be `undefined` if no value exists
* - Will be `undefined` if the given value is "none".
* - Will be the actual value otherwise.
*
* @private
*/
function parseOptionValue(value, parser) {
if (parser) {
value = parser(value);
}
if (value && value !== 'none') {
return value;
}
}
/**
* Gets the value of the selected <option> element within a <select> element.
*
* @param {Element} el
* the element to look in
*
* @param {Function} [parser]
* Optional function to adjust the value.
*
* @return {*}
* - Will be `undefined` if no value exists
* - Will be `undefined` if the given value is "none".
* - Will be the actual value otherwise.
*
* @private
*/
function getSelectedOptionValue(el, parser) {
const value = el.options[el.options.selectedIndex].value;
return parseOptionValue(value, parser);
}
/**
* Sets the selected <option> element within a <select> element based on a
* given value.
*
* @param {Element} el
* The element to look in.
*
* @param {string} value
* the property to look on.
*
* @param {Function} [parser]
* Optional function to adjust the value before comparing.
*
* @private
*/
function setSelectedOption(el, value, parser) {
if (!value) {
return;
}
for (let i = 0; i < el.options.length; i++) {
if (parseOptionValue(el.options[i].value, parser) === value) {
el.selectedIndex = i;
break;
}
}
}
/**
* Manipulate Text Tracks settings.
*
* @extends ModalDialog
*/
class TextTrackSettings extends ModalDialog {
/**
* Creates an instance of this class.
*
* @param {Player} player
* The `Player` that this class should be attached to.
*
* @param {Object} [options]
* The key/value store of player options.
*/
constructor(player, options) {
options.temporary = false;
super(player, options);
this.updateDisplay = this.updateDisplay.bind(this);
// fill the modal and pretend we have opened it
this.fill();
this.hasBeenOpened_ = this.hasBeenFilled_ = true;
this.renderModalComponents(player);
this.endDialog = createEl('p', {
className: 'vjs-control-text',
textContent: this.localize('End of dialog window.')
});
this.el().appendChild(this.endDialog);
this.setDefaults();
// Grab `persistTextTrackSettings` from the player options if not passed in child options
if (options.persistTextTrackSettings === undefined) {
this.options_.persistTextTrackSettings = this.options_.playerOptions.persistTextTrackSettings;
}
this.bindFunctionsToSelectsAndButtons();
if (this.options_.persistTextTrackSettings) {
this.restoreSettings();
}
}
renderModalComponents(player) {
const textTrackSettingsColors = new TextTrackSettingsColors(
player,
{
textTrackComponentid: this.id_,
selectConfigs,
fieldSets:
[
['color', 'textOpacity'],
['backgroundColor', 'backgroundOpacity'],
['windowColor', 'windowOpacity']
]
}
);
this.addChild(textTrackSettingsColors);
const textTrackSettingsFont = new TextTrackSettingsFont(
player,
{
textTrackComponentid: this.id_,
selectConfigs,
fieldSets:
[
['fontPercent'],
['edgeStyle'],
['fontFamily']
]
}
);
this.addChild(textTrackSettingsFont);
const trackSettingsControls = new TrackSettingsControls(player);
this.addChild(trackSettingsControls);
}
bindFunctionsToSelectsAndButtons() {
this.on(this.$('.vjs-done-button'), ['click', 'tap'], () => {
this.saveSettings();
this.close();
});
this.on(this.$('.vjs-default-button'), ['click', 'tap'], () => {
this.setDefaults();
this.updateDisplay();
});
Obj.each(selectConfigs, config => {
this.on(this.$(config.selector), 'change', this.updateDisplay);
});
}
dispose() {
this.endDialog = null;
super.dispose();
}
label() {
return this.localize('Caption Settings Dialog');
}
description() {
return this.localize('Beginning of dialog window. Escape will cancel and close the window.');
}
buildCSSClass() {
return super.buildCSSClass() + ' vjs-text-track-settings';
}
/**
* Gets an object of text track settings (or null).
*
* @return {Object}
* An object with config values parsed from the DOM or localStorage.
*/
getValues() {
return Obj.reduce(selectConfigs, (accum, config, key) => {
const value = getSelectedOptionValue(this.$(config.selector), config.parser);
if (value !== undefined) {
accum[key] = value;
}
return accum;
}, {});
}
/**
* Sets text track settings from an object of values.
*
* @param {Object} values
* An object with config values parsed from the DOM or localStorage.
*/
setValues(values) {
Obj.each(selectConfigs, (config, key) => {
setSelectedOption(this.$(config.selector), values[key], config.parser);
});
}
/**
* Sets all `<select>` elements to their default values.
*/
setDefaults() {
Obj.each(selectConfigs, (config) => {
const index = config.hasOwnProperty('default') ? config.default : 0;
this.$(config.selector).selectedIndex = index;
});
}
/**
* Restore texttrack settings from localStorage
*/
restoreSettings() {
let values;
try {
values = JSON.parse(window.localStorage.getItem(LOCAL_STORAGE_KEY));
} catch (err) {
log.warn(err);
}
if (values) {
this.setValues(values);
}
}
/**
* Save text track settings to localStorage
*/
saveSettings() {
if (!this.options_.persistTextTrackSettings) {
return;
}
const values = this.getValues();
try {
if (Object.keys(values).length) {
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(values));
} else {
window.localStorage.removeItem(LOCAL_STORAGE_KEY);
}
} catch (err) {
log.warn(err);
}
}
/**
* Update display of text track settings
*/
updateDisplay() {
const ttDisplay = this.player_.getChild('textTrackDisplay');
if (ttDisplay) {
ttDisplay.updateDisplay();
}
}
/**
* Repopulate dialog with new localizations on languagechange
*/
handleLanguagechange() {
this.fill();
this.renderModalComponents(this.player_);
this.bindFunctionsToSelectsAndButtons();
}
}
Component.registerComponent('TextTrackSettings', TextTrackSettings);
export default TextTrackSettings;