forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_card.js
270 lines (246 loc) · 8.77 KB
/
asset_card.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
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Creates and contains an MDL card that presents info about the given asset.
* @final
*/
class AssetCard {
/**
* @param {!Element} parentDiv
* @param {!ShakaDemoAssetInfo} asset
* @param {boolean} isFeatured True if this card should use the "featured"
* style, which use the asset's short name and have descriptions.
* @param {function(!AssetCard)} remakeButtonsFn
*/
constructor(parentDiv, asset, isFeatured, remakeButtonsFn) {
/** @private {!Element} */
this.card_ = document.createElement('div');
/** @private {!ShakaDemoAssetInfo} */
this.asset_ = asset;
/** @private {!Element} */
this.actions_ = document.createElement('div');
/** @private {!Element} */
this.featureIconsContainer_ = document.createElement('div');
/** @private {!Element} */
this.progressBar_ = document.createElement('progress');
/** @private {function(!AssetCard)} */
this.remakeButtonsFn_ = remakeButtonsFn;
// Lay out the card.
this.card_.classList.add('mdl-card-wide');
this.card_.classList.add('mdl-card');
this.card_.classList.add('mdl-shadow--2dp');
this.card_.classList.add('asset-card');
const titleDiv = document.createElement('div');
titleDiv.classList.add('mdl-card__title');
this.card_.appendChild(titleDiv);
const titleText = document.createElement('h2');
titleText.classList.add('mdl-card__title-text');
titleText.textContent = asset.shortName || asset.name;
titleDiv.appendChild(titleText);
if (asset.iconUri) {
const img = document.createElement('img');
img.src = asset.iconUri;
img.alt = ''; // Not necessary to understand the page
this.card_.appendChild(img);
}
if (asset.description && isFeatured) {
const supportingText = document.createElement('div');
supportingText.classList.add('mdl-card__supporting-text');
supportingText.textContent = asset.description;
this.card_.appendChild(supportingText);
}
this.card_.appendChild(this.featureIconsContainer_);
this.addFeatureIcons_(asset);
this.actions_.classList.add('mdl-card__actions');
this.actions_.classList.add('mdl-card--border');
this.card_.appendChild(this.actions_);
this.remakeButtons();
const progressContainer = document.createElement('div');
this.progressBar_.classList.add('hidden');
this.progressBar_.setAttribute('max', 1);
this.progressBar_.setAttribute('value', asset.storedProgress);
progressContainer.appendChild(this.progressBar_);
this.card_.appendChild(progressContainer);
parentDiv.appendChild(this.card_);
}
/**
* @param {string} icon
* @param {string} title
* @private
*/
addFeatureIcon_(icon, title) {
const iconDiv = document.createElement('div');
iconDiv.classList.add('feature-icon');
iconDiv.setAttribute('icon', icon);
this.featureIconsContainer_.appendChild(iconDiv);
ShakaDemoTooltips.make(this.featureIconsContainer_, iconDiv, title);
}
/**
* @param {!ShakaDemoAssetInfo} asset
* @private
*/
addFeatureIcons_(asset) {
const Feature = shakaAssets.Feature;
const KeySystem = shakaAssets.KeySystem;
const icons = new Map()
.set(Feature.SUBTITLES, 'subtitles')
.set(Feature.CAPTIONS, 'closed_caption')
.set(Feature.LIVE, 'live')
.set(Feature.TRICK_MODE, 'trick_mode')
.set(Feature.HIGH_DEFINITION, 'high_definition')
.set(Feature.ULTRA_HIGH_DEFINITION, 'ultra_high_definition')
.set(Feature.SURROUND, 'surround_sound')
.set(Feature.MULTIPLE_LANGUAGES, 'multiple_languages')
.set(Feature.AUDIO_ONLY, 'audio_only');
for (const feature of asset.features) {
const icon = icons.get(feature);
if (icon) {
this.addFeatureIcon_(icon, feature);
}
}
for (const drm of asset.drm) {
switch (drm) {
case KeySystem.WIDEVINE:
this.addFeatureIcon_('widevine', 'Widevine DRM');
break;
case KeySystem.CLEAR_KEY:
this.addFeatureIcon_('clear_key', 'Clear Key DRM');
break;
case KeySystem.PLAYREADY:
this.addFeatureIcon_('playready', 'PlayReady DRM');
break;
}
}
}
/**
* Modify an asset to make it clear that it is unsupported.
* @param {?string} unsupportedReason
*/
markAsUnsupported(unsupportedReason) {
this.card_.classList.add('asset-card-unsupported');
this.makeUnsupportedButton_('Not Available', unsupportedReason);
}
/**
* Make a button that represents the lack of a working button.
* @param {string} buttonName
* @param {?string} unsupportedReason
* @private
*/
makeUnsupportedButton_(buttonName, unsupportedReason) {
const button = this.addButton(buttonName, () => {});
button.setAttribute('disabled', '');
// Place the tooltip into the parent container of the card, so that the
// tooltip won't be clipped by other asset cards.
// Also, tooltips don't work on disabled buttons (on some platforms), so
// the button itself has to be "uprooted" and placed in a synthetic div
// specifically to attach the tooltip to.
const tooltipContainer = this.card_.parentElement;
if (tooltipContainer && unsupportedReason) {
const attachPoint = document.createElement('div');
if (button.parentElement) {
button.parentElement.removeChild(button);
}
attachPoint.classList.add('tooltip-attach-point');
attachPoint.appendChild(button);
this.actions_.appendChild(attachPoint);
ShakaDemoTooltips.make(tooltipContainer, attachPoint, unsupportedReason);
}
}
/**
* Select this card if the card's asset matches |asset|.
* Used to simplify the implementation of "shaka-main-selected-asset-changed"
* handlers.
* @param {ShakaDemoAssetInfo} asset
*/
selectByAsset(asset) {
this.card_.classList.remove('selected');
if (this.asset_ == asset) {
this.card_.classList.add('selected');
}
}
/** Remake the buttons of the card. */
remakeButtons() {
shaka.ui.Utils.removeAllChildren(this.actions_);
this.remakeButtonsFn_(this);
}
/**
* Adds a button to the bottom of the card that controls storage behavior.
* This is a separate function because it involves a significant amount of
* custom behavior, such as the download bar.
*/
addStoreButton() {
const unsupportedReason = shakaDemoMain.getAssetUnsupportedReason(
this.asset_, /* needOffline= */ true);
if (unsupportedReason || !this.asset_.storeCallback) {
// This can't be stored.
this.makeUnsupportedButton_('Download', unsupportedReason);
return;
}
if (this.asset_.isStored()) {
const deleteButton = this.addButton('Delete', async () => {
deleteButton.disabled = true;
await this.asset_.unstoreCallback();
this.remakeButtons();
});
} else {
const downloadButton = this.addButton('Download', async () => {
downloadButton.disabled = true;
await this.asset_.storeCallback();
this.remakeButtons();
});
}
}
/**
* Updates the progress bar on the card.
*/
updateProgress() {
if (this.asset_.storedProgress < 1) {
this.progressBar_.classList.remove('hidden');
for (const button of this.actions_.childNodes) {
button.disabled = true;
}
} else {
this.progressBar_.classList.add('hidden');
for (const button of this.actions_.childNodes) {
button.disabled = false;
}
}
this.progressBar_.setAttribute('value', this.asset_.storedProgress);
}
/**
* Adds a button to the bottom of the card that will call |onClick| when
* clicked. For example, a play or delete button.
* @param {string} name
* @param {function()} onclick
* @return {!Element}
*/
addButton(name, onclick) {
const button = document.createElement('button');
button.classList.add('mdl-button');
button.classList.add('mdl-button--colored');
button.classList.add('mdl-js-button');
button.classList.add('mdl-js-ripple-effect');
button.textContent = name;
button.addEventListener('click', () => {
if (!button.hasAttribute('disabled')) {
onclick();
}
});
this.actions_.appendChild(button);
return button;
}
}