forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
341 lines (308 loc) · 10.6 KB
/
search.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
/** @license
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shakaDemo.Search');
/** @type {?shakaDemo.Search} */
let shakaDemoSearch;
/**
* Shaka Player demo, feature discovery page layout.
*/
shakaDemo.Search = class {
/**
* Register the page configuration.
*/
static init() {
const container = shakaDemoMain.addNavButton('search');
shakaDemoSearch = new shakaDemo.Search(container);
}
/** @param {!Element} container */
constructor(container) {
/** @private {!Array.<!shakaAssets.Feature>} */
this.desiredFeatures_ = [];
/** @private {?shakaAssets.Source} */
this.desiredSource_;
/** @private {?shakaAssets.KeySystem} */
this.desiredDRM_;
/** @private {!Element} */
this.resultsDiv_ = document.createElement('div');
this.remakeSearchDiv_(container);
/** @private {!Array.<!shakaDemo.AssetCard>} */
this.assetCards_ = [];
document.addEventListener('shaka-main-selected-asset-changed', () => {
this.updateSelected_();
});
document.addEventListener('shaka-main-offline-progress', () => {
this.updateOfflineProgress_();
});
document.addEventListener('shaka-main-locale-changed', () => {
this.remakeSearchDiv_(container);
this.remakeResultsDiv_();
// Update the componentHandler, to account for any new MDL elements added.
componentHandler.upgradeDom();
});
document.addEventListener('shaka-main-page-changed', () => {
if (!this.resultsDiv_.childNodes.length &&
!container.classList.contains('hidden')) {
// Now that the page is showing, create the contents that we deferred
// until now.
this.remakeResultsDiv_();
}
});
}
/**
* @param {!ShakaDemoAssetInfo} asset
* @return {!shakaDemo.AssetCard}
* @private
*/
createAssetCardFor_(asset) {
const resultsDiv = this.resultsDiv_;
const isFeatured = false;
return new shakaDemo.AssetCard(resultsDiv, asset, isFeatured, (c) => {
const unsupportedReason = shakaDemoMain.getAssetUnsupportedReason(
asset, /* needOffline= */ false);
if (unsupportedReason) {
c.markAsUnsupported(unsupportedReason);
} else {
c.addButton(shakaDemo.MessageIds.PLAY, () => {
shakaDemoMain.loadAsset(asset);
this.updateSelected_();
});
c.addStoreButton();
}
});
}
/**
* Updates progress bars on asset cards.
* @private
*/
updateOfflineProgress_() {
for (const card of this.assetCards_) {
card.updateProgress();
}
}
/**
* Updates which asset card is selected.
* @private
*/
updateSelected_() {
for (const card of this.assetCards_) {
card.selectByAsset(shakaDemoMain.selectedAsset);
}
}
/** @private */
remakeResultsDiv_() {
shaka.util.Dom.removeAllChildren(this.resultsDiv_);
const assets = this.searchResults_();
this.assetCards_ = assets.map((asset) => this.createAssetCardFor_(asset));
this.updateSelected_();
}
/**
* @param {!shakaDemo.Search.SearchTerm} term
* @param {shakaDemo.Search.TermType} type
* @param {!Array.<!shakaDemo.Search.SearchTerm>} others
* @private
*/
addDesiredTerm_(term, type, others) {
switch (type) {
case shakaDemo.Search.TermType.DRM:
this.desiredDRM_ = /** @type {shakaAssets.KeySystem} */ (term);
break;
case shakaDemo.Search.TermType.SOURCE:
this.desiredSource_ = /** @type {shakaAssets.Source} */ (term);
break;
case shakaDemo.Search.TermType.FEATURE:
// Only this term should be in the desired features.
for (const term of others) {
const index = this.desiredFeatures_.indexOf(
/** @type {shakaAssets.Feature} */ (term));
if (index != -1) {
this.desiredFeatures_.splice(index, 1);
}
}
this.desiredFeatures_.push(/** @type {shakaAssets.Feature} */ (term));
break;
}
}
/**
* @param {!shakaDemo.Search.SearchTerm} term
* @param {shakaDemo.Search.TermType} type
* @private
*/
removeDesiredTerm_(term, type) {
let index;
switch (type) {
case shakaDemo.Search.TermType.DRM:
this.desiredDRM_ = null;
break;
case shakaDemo.Search.TermType.SOURCE:
this.desiredSource_ = null;
break;
case shakaDemo.Search.TermType.FEATURE:
index = this.desiredFeatures_.indexOf(
/** @type {shakaAssets.Feature} */ (term));
if (index != -1) {
this.desiredFeatures_.splice(index, 1);
}
break;
}
}
/**
* Creates an input for a single search term.
* @param {!shakaDemo.InputContainer} searchContainer
* @param {!shakaDemo.Search.SearchTerm} choice
* The term this represents.
* @param {shakaDemo.Search.TermType} type
* The type of term that this term is.
* @param {?shakaDemo.MessageIds} tooltip
* @private
*/
makeBooleanInput_(searchContainer, choice, type, tooltip) {
// Give the container a significant amount of right padding, to make
// it clearer which toggle corresponds to which label.
searchContainer.addRow(choice, tooltip, 'significant-right-padding');
const onChange = (input) => {
if (input.checked) {
this.addDesiredTerm_(choice, type, [choice]);
} else {
this.removeDesiredTerm_(choice, type);
}
this.remakeResultsDiv_();
// Update the componentHandler, to account for any new MDL elements
// added. Notably, tooltips.
componentHandler.upgradeDom();
};
// eslint-disable-next-line no-new
new shakaDemo.BoolInput(searchContainer, choice, onChange);
}
/**
* Creates an input for a group of related but mutually-exclusive search
* terms.
* @param {!shakaDemo.InputContainer} searchContainer
* @param {!shakaDemo.MessageIds} name
* @param {!Array.<!shakaDemo.Search.SearchTerm>} choices
* An array of the terms in this term group.
* @param {shakaDemo.Search.TermType} type
* The type of term that this term group contains. All of the
* terms in the "choices" array must be of this type.
* @private
*/
makeSelectInput_(searchContainer, name, choices, type) {
searchContainer.addRow(null, null);
const nullOption = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.UNDEFINED);
const valuesObject = {};
for (const term of choices) {
valuesObject[term] = shakaDemoMain.getLocalizedString(term);
}
valuesObject[nullOption] = nullOption;
let lastValue = nullOption;
const onChange = (input) => {
if (input.value != nullOption) {
this.addDesiredTerm_(input.value, type, choices);
} else {
this.removeDesiredTerm_(lastValue, type);
}
lastValue = input.value;
this.remakeResultsDiv_();
// Update the componentHandler, to account for any new MDL elements added.
// Notably, tooltips.
componentHandler.upgradeDom();
};
const input = new shakaDemo.SelectInput(
searchContainer, name, onChange, valuesObject);
input.input().value = nullOption;
}
/**
* @param {!Element} container
* @private
*/
remakeSearchDiv_(container) {
shaka.util.Dom.removeAllChildren(container);
const Feature = shakaAssets.Feature;
const FEATURE = shakaDemo.Search.TermType.FEATURE;
const DRM = shakaDemo.Search.TermType.DRM;
const SOURCE = shakaDemo.Search.TermType.SOURCE;
// Core term inputs.
const coreContainer = new shakaDemo.InputContainer(
container, /* headerText= */ null, shakaDemo.InputContainer.Style.FLEX,
/* docLink= */ null);
this.makeSelectInput_(coreContainer,
shakaDemo.MessageIds.MANIFEST_SEARCH,
[Feature.DASH, Feature.HLS], FEATURE);
this.makeSelectInput_(coreContainer,
shakaDemo.MessageIds.CONTAINER_SEARCH,
[Feature.MP4, Feature.MP2TS, Feature.WEBM], FEATURE);
this.makeSelectInput_(coreContainer,
shakaDemo.MessageIds.DRM_SEARCH,
Object.values(shakaAssets.KeySystem), DRM);
this.makeSelectInput_(coreContainer,
shakaDemo.MessageIds.SOURCE_SEARCH,
Object.values(shakaAssets.Source).filter((term) => {
return term != shakaAssets.Source.CUSTOM;
}), SOURCE);
// Special terms.
const containerStyle = shakaDemo.InputContainer.Style.FLEX;
const specialContainer = new shakaDemo.InputContainer(
container, /* headerText= */ null, containerStyle,
/* docLink= */ null);
this.makeBooleanInput_(specialContainer, Feature.LIVE, FEATURE,
shakaDemo.MessageIds.LIVE_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.HIGH_DEFINITION, FEATURE,
shakaDemo.MessageIds.HIGH_DEFINITION_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.XLINK, FEATURE,
shakaDemo.MessageIds.XLINK_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.SUBTITLES, FEATURE,
shakaDemo.MessageIds.SUBTITLES_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.TRICK_MODE, FEATURE,
shakaDemo.MessageIds.TRICK_MODE_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.SURROUND, FEATURE,
shakaDemo.MessageIds.SURROUND_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.OFFLINE, FEATURE,
shakaDemo.MessageIds.OFFLINE_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.STORED, FEATURE,
shakaDemo.MessageIds.STORED_SEARCH);
this.makeBooleanInput_(specialContainer, Feature.AUDIO_ONLY, FEATURE,
shakaDemo.MessageIds.AUDIO_ONLY_SEARCH);
container.appendChild(this.resultsDiv_);
}
/**
* @return {!Array.<!ShakaDemoAssetInfo>}
* @private
*/
searchResults_() {
return shakaAssets.testAssets.filter((asset) => {
if (asset.disabled) {
return false;
}
if (this.desiredDRM_ && !asset.drm.includes(this.desiredDRM_)) {
return false;
}
if (this.desiredSource_ && asset.source != this.desiredSource_) {
return false;
}
for (const feature of this.desiredFeatures_) {
if (feature == shakaAssets.Feature.STORED) {
if (!asset.isStored()) {
return false;
}
} else if (!asset.features.includes(feature)) {
return false;
}
}
return true;
});
}
};
/** @typedef {shakaAssets.Feature|shakaAssets.Source} */
shakaDemo.Search.SearchTerm;
/** @enum {string} */
shakaDemo.Search.TermType = {
FEATURE: 'Feature',
DRM: 'DRM',
SOURCE: 'Source',
};
document.addEventListener('shaka-main-loaded', shakaDemo.Search.init);
document.addEventListener('shaka-main-cleanup', () => {
shakaDemoSearch = null;
});