forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathad_statistics_button.js
More file actions
249 lines (201 loc) · 7.1 KB
/
ad_statistics_button.js
File metadata and controls
249 lines (201 loc) · 7.1 KB
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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.AdStatisticsButton');
goog.require('shaka.log');
goog.require('shaka.ads.Utils');
goog.require('shaka.ui.ContextMenu');
goog.require('shaka.ui.Controls');
goog.require('shaka.ui.Element');
goog.require('shaka.ui.Enums');
goog.require('shaka.ui.Locales');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.OverflowMenu');
goog.require('shaka.ui.Utils');
goog.require('shaka.util.Dom');
goog.require('shaka.util.Timer');
goog.requireType('shaka.ui.Controls');
/**
* @extends {shaka.ui.Element}
* @final
* @export
*/
shaka.ui.AdStatisticsButton = class extends shaka.ui.Element {
/**
* @param {!HTMLElement} parent
* @param {!shaka.ui.Controls} controls
*/
constructor(parent, controls) {
super(parent, controls);
/** @private {!HTMLButtonElement} */
this.button_ = shaka.util.Dom.createButton();
this.button_.classList.add('shaka-ad-statistics-button');
/** @private {!HTMLElement} */
this.icon_ = shaka.util.Dom.createHTMLElement('i');
this.icon_.classList.add('material-icons-round');
this.icon_.textContent =
shaka.ui.Enums.MaterialDesignIcons.STATISTICS_ON;
this.button_.appendChild(this.icon_);
const label = shaka.util.Dom.createHTMLElement('label');
label.classList.add('shaka-overflow-button-label');
/** @private {!HTMLElement} */
this.nameSpan_ = shaka.util.Dom.createHTMLElement('span');
label.appendChild(this.nameSpan_);
/** @private {!HTMLElement} */
this.stateSpan_ = shaka.util.Dom.createHTMLElement('span');
this.stateSpan_.classList.add('shaka-current-selection-span');
label.appendChild(this.stateSpan_);
this.button_.appendChild(label);
this.parent.appendChild(this.button_);
/** @private {!HTMLElement} */
this.container_ = shaka.util.Dom.createHTMLElement('div');
this.container_.classList.add('shaka-no-propagation');
this.container_.classList.add('shaka-show-controls-on-mouse-over');
this.container_.classList.add('shaka-ad-statistics-container');
this.container_.classList.add('shaka-hidden');
const controlsContainer = this.controls.getControlsContainer();
controlsContainer.appendChild(this.container_);
/** @private {!Array} */
this.statisticsList_ = [];
/** @private {!Object.<string, (number|!Array.<number>)>} */
this.currentStats_ = this.adManager.getStats();
/** @private {!Object.<string, HTMLElement>} */
this.displayedElements_ = {};
const parseLoadTimes = (name) => {
let totalTime = 0;
const loadTimes =
/** @type {!Array.<number>} */ (this.currentStats_[name]);
for (const loadTime of loadTimes) {
totalTime += parseFloat(loadTime);
}
return totalTime;
};
const showNumber = (name) => {
return this.currentStats_[name];
};
/** @private {!Object.<string, function(string):string>} */
this.parseFrom_ = {
'loadTimes': parseLoadTimes,
'averageLoadTime': showNumber,
'started': showNumber,
'playedCompletely': showNumber,
'skipped': showNumber,
'errors': showNumber,
};
/** @private {shaka.util.Timer} */
this.timer_ = new shaka.util.Timer(() => {
this.onTimerTick_();
});
this.updateLocalizedStrings_();
this.loadContainer_();
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
this.updateLocalizedStrings_();
});
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
this.updateLocalizedStrings_();
});
this.eventManager.listen(this.button_, 'click', () => {
this.onClick_();
this.updateLocalizedStrings_();
});
this.eventManager.listen(this.player, 'loading', () => {
shaka.ui.Utils.setDisplay(this.button_, false);
});
this.eventManager.listen(
this.adManager, shaka.ads.Utils.AD_STARTED, () => {
shaka.ui.Utils.setDisplay(this.button_, true);
});
}
/** @private */
onClick_() {
shaka.ui.Utils.setDisplay(this.parent, false);
if (this.container_.classList.contains('shaka-hidden')) {
this.icon_.textContent =
shaka.ui.Enums.MaterialDesignIcons.STATISTICS_OFF;
this.timer_.tickEvery(0.1);
shaka.ui.Utils.setDisplay(this.container_, true);
} else {
this.icon_.textContent =
shaka.ui.Enums.MaterialDesignIcons.STATISTICS_ON;
this.timer_.stop();
shaka.ui.Utils.setDisplay(this.container_, false);
}
}
/** @private */
updateLocalizedStrings_() {
const LocIds = shaka.ui.Locales.Ids;
this.nameSpan_.textContent =
this.localization.resolve(LocIds.AD_STATISTICS);
this.button_.ariaLabel = this.localization.resolve(LocIds.AD_STATISTICS);
const labelText = this.container_.classList.contains('shaka-hidden') ?
LocIds.OFF : LocIds.ON;
this.stateSpan_.textContent = this.localization.resolve(labelText);
}
/** @private */
generateComponent_(name) {
const section = shaka.util.Dom.createHTMLElement('div');
const label = shaka.util.Dom.createHTMLElement('label');
label.textContent = name + ':';
section.appendChild(label);
const value = shaka.util.Dom.createHTMLElement('span');
value.textContent = this.parseFrom_[name](name);
section.appendChild(value);
this.displayedElements_[name] = value;
return section;
}
/** @private */
loadContainer_() {
const closeElement = shaka.util.Dom.createHTMLElement('div');
closeElement.classList.add('shaka-no-propagation');
closeElement.classList.add('shaka-statistics-close');
const icon = shaka.util.Dom.createHTMLElement('i');
icon.classList.add('material-icons-round');
icon.textContent =
shaka.ui.Enums.MaterialDesignIcons.CLOSE;
closeElement.appendChild(icon);
this.container_.appendChild(closeElement);
this.eventManager.listen(icon, 'click', () => {
this.onClick_();
});
for (const name of this.controls.getConfig().adStatisticsList) {
if (name in this.currentStats_) {
this.container_.appendChild(this.generateComponent_(name));
this.statisticsList_.push(name);
} else {
shaka.log.alwaysWarn('Unrecognized ad statistic element:', name);
}
}
}
/** @private */
onTimerTick_() {
this.currentStats_ = this.adManager.getStats();
for (const name of this.statisticsList_) {
this.displayedElements_[name].textContent =
this.parseFrom_[name](name);
}
}
/** @override */
release() {
this.timer_.stop();
this.timer_ = null;
super.release();
}
};
/**
* @implements {shaka.extern.IUIElement.Factory}
* @final
*/
shaka.ui.AdStatisticsButton.Factory = class {
/** @override */
create(rootElement, controls) {
return new shaka.ui.AdStatisticsButton(rootElement, controls);
}
};
shaka.ui.OverflowMenu.registerElement(
'ad_statistics', new shaka.ui.AdStatisticsButton.Factory());
shaka.ui.ContextMenu.registerElement(
'ad_statistics', new shaka.ui.AdStatisticsButton.Factory());