-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCaptionsTogglePlugin.js
132 lines (118 loc) · 3.34 KB
/
CaptionsTogglePlugin.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
import { SavedData } from '../SavedData';
import { ButtonPlugin } from '../base-plugins';
import { Button } from '../ui-elements';
/**
* @export
* @class CaptionsTogglePlugin
* @property {Button[]} _captionsButtons An array of caption mute buttons
* @property {boolean} _captionsMuted True if captions are muted
* @property {number} captionsButtonLength The length of the captionsButtons array
* @extends {ButtonPlugin}
*/
export class CaptionsTogglePlugin extends ButtonPlugin {
/**
*Creates an instance of CaptionsTogglePlugin.
* @param {string | HTMLElement} captionsButtons selector string for one or more captions mute buttons
* @memberof CaptionsTogglePlugin
*/
constructor(captionsButtons) {
super('Caption-Button-Plugin');
this.sendAllProperties = this.sendAllProperties.bind(this);
this._captionsButtons = [];
if ( captionsButtons instanceof HTMLElement ) {
this._captionsButtons[0] = new Button({
button: captionsButtons,
onClick: this.captionsButtonClick.bind(this),
channel: 'captions'
});
} else {
document.querySelectorAll(captionsButtons).forEach((button) => {
this._captionsButtons.push(new Button({
button: button,
onClick: this.captionsButtonClick.bind(this),
channel: 'captions'
}));
});
}
this._captionsMuted = false;
this.captionsButtonLength = this._captionsButtons.length;
if (0 >= this.captionsButtonLength) {
this.warn(
'Plugin was not provided any valid button or input elements'
);
return;
}
}
/**
* @memberof CaptionsTogglePlugin
*/
init() {
// Handle the features request
this.client.on(
'features',
function($event) {
for (let i = 0; i < this.captionsButtonLength; i ++) {
this._captionsButtons[i].displayButton($event.data);
}
if (null === SavedData.read(CaptionsTogglePlugin.captionsToggleKey)) {
return;
}
this._captionsMuted = !!SavedData.read(CaptionsTogglePlugin.captionsToggleKey);
this._setMuteProp('captionsMuted', this._captionsButtons, this._captionsMuted, true);
}.bind(this)
);
}
/**
* @memberof CaptionsTogglePlugin
*/
start() {
for (let i = 0; i < this.captionsButtonsLength; i++) {
this.captionsButtons[i].enableButton();
}
this.client.on('loaded', this.sendAllProperties);
this.client.on('loadDone', this.sendAllProperties);
}
/**
*
* Sends initial caption properties to the application
* @memberof CaptionsTogglePlugin
*/
sendAllProperties() {
this.sendProperty(CaptionsTogglePlugin.captionsToggleKey, this.captionsMuted);
}
/**
* @memberof CaptionsTogglePlugin
*/
captionsButtonClick() {
this.captionsMuted = !this.captionsMuted;
}
/**
* @readonly
* @memberof CaptionsTogglePlugin
*/
get captionsMuted() {
return this._captionsMuted;
}
/**
* @param {boolean} muted
* @memberof CaptionsTogglePlugin
*/
set captionsMuted(muted) {
this._captionsMuted = muted;
this._setMuteProp(
'captionsMuted',
this._captionsButtons,
this._captionsMuted
);
}
/**
* Get CaptionToggle Key
* @readonly
* @static
* @memberof captionsToggleKey
* @returns {string}
*/
static get captionsToggleKey() {
return 'captionsMuted';
}
}