forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.js
931 lines (840 loc) · 30.9 KB
/
custom.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shakaDemo.Custom');
goog.require('ShakaDemoAssetInfo');
goog.require('shakaDemo.AssetCard');
goog.require('shakaDemo.Input');
goog.require('shakaDemo.InputContainer');
goog.require('shakaDemo.MessageIds');
goog.require('shakaDemo.TextInput');
/** @type {?shakaDemo.Custom} */
let shakaDemoCustom;
/**
* Shaka Player demo, custom asset page layout.
*/
shakaDemo.Custom = class {
/**
* Register the page configuration.
*/
static init() {
const elements = shakaDemoMain.addNavButton('custom');
shakaDemoCustom = new shakaDemo.Custom(elements.container);
}
/** @param {!Element} container */
constructor(container) {
/** @private {!HTMLDialogElement} */
this.dialog_ =
/** @type {!HTMLDialogElement} */(document.createElement('dialog'));
this.dialog_.classList.add('mdl-dialog');
container.appendChild(this.dialog_);
if (!this.dialog_.showModal) {
dialogPolyfill.registerDialog(this.dialog_);
}
/** @private {!Set.<!ShakaDemoAssetInfo>} */
this.assets_ = this.loadAssetInfos_();
/** @private {!HTMLInputElement} */
this.manifestField_;
/** @private {!Array.<!shakaDemo.AssetCard>} */
this.assetCards_ = [];
this.savedList_ = document.createElement('div');
container.appendChild(this.savedList_);
// Add the "new" button, which shows the dialog.
const addButtonContainer = document.createElement('div');
addButtonContainer.classList.add('add-button-container');
container.appendChild(addButtonContainer);
// Style it as an MDL Floating Action Button (FAB).
const buttonStyle = shakaDemo.Custom.ButtonStyle_.FAB;
const addButton = this.makeButton_('add', buttonStyle, () => {
this.showAssetDialog_(ShakaDemoAssetInfo.makeBlankAsset());
});
addButtonContainer.appendChild(addButton);
document.addEventListener('shaka-main-selected-asset-changed', () => {
this.updateSelected_();
});
document.addEventListener('shaka-main-offline-progress', () => {
this.updateOfflineProgress_();
});
document.addEventListener('shaka-main-locale-changed', () => {
this.remakeSavedList_();
});
document.addEventListener('shaka-main-page-changed', () => {
if (!this.savedList_.childNodes.length &&
!container.classList.contains('hidden')) {
// Now that the page is showing, create the contents that we deferred
// until now.
this.remakeSavedList_();
}
});
}
/** @return {!Array.<!ShakaDemoAssetInfo>} */
assets() {
return Array.from(this.assets_);
}
/**
* Updates progress bars on asset cards.
* @private
*/
updateOfflineProgress_() {
for (const card of this.assetCards_) {
card.updateProgress();
}
}
/**
* A utility to simplify the creation of fields on the dialog.
* @param {!shakaDemo.InputContainer} container
* @param {string} name
* @param {function(!HTMLInputElement, !Element)} setup
* @param {function(!Element, !shakaDemo.Input)} onChange
* @param {boolean=} isTextArea
* @private
*/
makeField_(container, name, setup, onChange, isTextArea) {
container.addRow(/* labelString= */ null, /* tooltipString= */ null);
const input =
new shakaDemo.TextInput(container, name, onChange, isTextArea);
input.extra().textContent = name;
setup(input.input(), input.container());
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @return {!Element} div
* @private
*/
makeAssetDialogContentsHeaders_(assetInProgress, inputsToCheck) {
const headersDiv = document.createElement('div');
// Because this field can theoretically contain an unlimited number of
// values, it has to take up an entire section by itself.
const makeEmptyRow = () => {
makePreFilledRow(/* headerName= */ null, /* headerValue= */ null);
};
/**
* @type {!Array.<{
* headerName: ?string,
* div: !Element,
* }>}
*/
const collisionCheckEntries = [];
/** @type {function(?string, ?string)} */
const makePreFilledRow = (headerName, headerValue) => {
const div = document.createElement('div');
headersDiv.appendChild(div);
const containerStyle = shakaDemo.InputContainer.Style.VERTICAL;
const headerText = shakaDemo.MessageIds.LICENSE_HEADER_TITLE;
const container = new shakaDemo.InputContainer(
div, headerText, containerStyle,
/* docLink= */ null);
const collisionCheckEntry = {
headerName,
div,
};
collisionCheckEntries.push(collisionCheckEntry);
// Don't add a new row for a row that was pre-filled.
let firstTime = !headerName;
const onChange = (newHeaderName, newHeaderValue) => {
if (headerName) {
// In case the header named changed, remove the old header.
assetInProgress.licenseRequestHeaders.delete(headerName);
}
// Set the new values.
headerName = newHeaderName;
collisionCheckEntry.headerName = newHeaderName;
headerValue = newHeaderValue;
if (!headerName || !headerValue) {
if (!firstTime) {
// The user has set a field that used to be filled to empty.
// This signals that they probably want to remove this header.
headersDiv.removeChild(div);
}
return;
}
if (firstTime) {
firstTime = false;
// You have filled out this row for the first time; add a new row, in
// case the user wants to add more headers.
makeEmptyRow();
// Update the componentHandler, to account for the new MDL elements.
componentHandler.upgradeDom();
}
assetInProgress.addLicenseRequestHeader(headerName, headerValue);
// Eliminate any OTHER header with the same name. Assume this newly
// added/modified one is the "correct" one.
for (const entry of collisionCheckEntries) {
if (entry == collisionCheckEntry) {
// You can't "collide" with yourself.
continue;
}
if (headerName != entry.headerName) {
// It's not a collision.
continue;
}
// Remove the entry for the old field from the array.
const idx = collisionCheckEntries.indexOf(entry);
collisionCheckEntries.splice(idx, 1);
// Remove the div for the old field from the overall headers div.
headersDiv.removeChild(entry.div);
break;
}
};
const nameSetup = (input, container) => {
if (headerName) {
input.value = headerName;
}
};
const nameOnChange = (input) => {
onChange(input.value, headerValue);
};
const licenseHeaderName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.LICENSE_HEADER_NAME);
this.makeField_(container, licenseHeaderName, nameSetup, nameOnChange);
const valueSetup = (input, container) => {
if (headerValue) {
input.value = headerValue;
}
};
const valueOnChange = (input) => {
onChange(headerName, input.value);
};
const licenseHeaderValue = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.LICENSE_HEADER_VALUE);
this.makeField_(container, licenseHeaderValue, valueSetup, valueOnChange);
};
if (assetInProgress.licenseRequestHeaders.size == 0) {
// It starts out with a single empty row, but each time you start filling
// out one for the first time it adds a new one. Empty rows are ignored in
// the actual data.
makeEmptyRow();
} else {
// Make a row for each header.
for (const headerName of assetInProgress.licenseRequestHeaders.keys()) {
makePreFilledRow(
headerName, assetInProgress.licenseRequestHeaders.get(headerName));
}
// ...and also an empty one at the end.
makeEmptyRow();
}
return headersDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @return {!Element} div
* @private
*/
makeAssetDialogContentsHLS_(assetInProgress, inputsToCheck) {
const mediaPlaylistDiv = document.createElement('div');
const containerStyle = shakaDemo.InputContainer.Style.FLEX;
const container = new shakaDemo.InputContainer(
mediaPlaylistDiv, /* headerText= */ null, containerStyle,
/* docLink= */ null);
container.getClassList().add('wide-input');
container.setDefaultRowClass('wide-input');
const fullMimeTypeSetup = (input, container) => {
if (assetInProgress.mediaPlaylistFullMimeType) {
input.value = assetInProgress.mediaPlaylistFullMimeType;
}
const defaultConfig = shaka.util.PlayerConfiguration.createDefault();
input.placeholder = defaultConfig.manifest.hls.mediaPlaylistFullMimeType;
};
const fullMimeTypeOnChange = (input) => {
assetInProgress.setMediaPlaylistFullMimeType(input.value);
};
const fullMimeTypeName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.HLS_FULL_MIME_TYPE);
this.makeField_(
container, fullMimeTypeName, fullMimeTypeSetup, fullMimeTypeOnChange);
return mediaPlaylistDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @return {!Element} div
* @private
*/
makeAssetDialogContentsAds_(assetInProgress, inputsToCheck) {
const adsDiv = document.createElement('div');
const containerStyle = shakaDemo.InputContainer.Style.VERTICAL;
const container = new shakaDemo.InputContainer(
adsDiv, /* headerText= */ null, containerStyle,
/* docLink= */ null);
// Make the ad tag URL field.
const adTagSetup = (input, container) => {
if (assetInProgress.adTagUri) {
input.value = assetInProgress.adTagUri;
}
};
const adTagOnChange = (input) => {
assetInProgress.adTagUri = input.value;
};
const adTagURLName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.AD_TAG_URL);
this.makeField_(
container, adTagURLName, adTagSetup, adTagOnChange);
// Make the content source id field.
const contentSrcIdSetup = (input, container) => {
if (assetInProgress.imaContentSrcId) {
input.value = assetInProgress.imaContentSrcId;
}
this.manifestField_.required =
this.checkManifestRequired_(assetInProgress);
};
const contentSrcIdOnChange = (input) => {
assetInProgress.imaContentSrcId = input.value;
this.manifestField_.required =
this.checkManifestRequired_(assetInProgress);
};
const contentSrcIdName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.IMA_CONTENT_SRC_ID);
this.makeField_(
container, contentSrcIdName, contentSrcIdSetup, contentSrcIdOnChange);
// Make the video id field.
const videoIdSetup = (input, container) => {
if (assetInProgress.imaVideoId) {
input.value = assetInProgress.imaVideoId;
}
this.manifestField_.required =
this.checkManifestRequired_(assetInProgress);
};
const videoIdOnChange = (input) => {
assetInProgress.imaVideoId = input.value;
this.manifestField_.required =
this.checkManifestRequired_(assetInProgress);
};
const videoIdName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.IMA_VIDEO_ID);
this.makeField_(
container, videoIdName, videoIdSetup, videoIdOnChange);
// Make the asset key field.
const assetKeySetup = (input, container) => {
if (assetInProgress.imaAssetKey) {
input.value = assetInProgress.imaAssetKey;
}
this.manifestField_.required =
this.checkManifestRequired_(assetInProgress);
};
const assetKeyChange = (input) => {
assetInProgress.imaAssetKey = input.value;
this.manifestField_.required =
this.checkManifestRequired_(assetInProgress);
};
const assetKeyName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.IMA_ASSET_KEY);
this.makeField_(
container, assetKeyName, assetKeySetup, assetKeyChange);
return adsDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @return {!Element} div
* @private
*/
makeAssetDialogContentsDrm_(assetInProgress, inputsToCheck) {
const drmDiv = document.createElement('div');
const containerStyle = shakaDemo.InputContainer.Style.VERTICAL;
const container = new shakaDemo.InputContainer(
drmDiv, /* headerText= */ null, containerStyle,
/* docLink= */ null);
// The license server and drm system fields need to know each others
// contents, and react to each others changes, to work.
// To simplify things, this method picks out the process of setting license
// server URLs; it can be called within both fields.
let licenseServerUrlInput;
let customDrmSystemInput;
const setLicenseServerURLs = () => {
const licenseServerURL = licenseServerUrlInput.value;
const customDRMSystem = customDrmSystemInput.value;
if (licenseServerURL) {
// Make a license server entry for every common DRM plugin.
assetInProgress.licenseServers.clear();
for (const drmSystem of shakaDemo.Main.commonDrmSystems) {
assetInProgress.licenseServers.set(drmSystem, licenseServerURL);
}
if (customDRMSystem) {
// Make a custom entry too.
assetInProgress.licenseServers.set(customDRMSystem, licenseServerURL);
}
} else {
assetInProgress.licenseServers.clear();
}
};
// Make the license server URL field.
const licenseSetup = (input, container) => {
licenseServerUrlInput = input;
const drmSystems = assetInProgress.licenseServers.keys();
// Custom assets have only a single license server URL, no matter how
// many key systems they have. Thus, it's safe to say that the license
// server URL associated with the first key system is the asset's
// over-all license server URL.
const drmSystem = drmSystems.next();
if (drmSystem && drmSystem.value) {
input.value = assetInProgress.licenseServers.get(drmSystem.value);
}
};
const licenseOnChange = (input) => {
setLicenseServerURLs();
};
const licenseServerURLName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.LICENSE_SERVER_URL);
this.makeField_(
container, licenseServerURLName, licenseSetup, licenseOnChange);
// Make the license certificate URL field.
const certSetup = (input, container) => {
if (assetInProgress.certificateUri) {
input.value = assetInProgress.certificateUri;
}
};
const certOnChange = (input) => {
assetInProgress.certificateUri = input.value;
};
const licenseCertificateURLName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.LICENSE_CERTIFICATE_URL);
this.makeField_(
container, licenseCertificateURLName, certSetup, certOnChange);
// Make the drm system field.
const drmSetup = (input, container) => {
customDrmSystemInput = input;
const drmSystems = assetInProgress.licenseServers.keys();
for (const drmSystem of drmSystems) {
if (!shakaDemo.Main.commonDrmSystems.includes(drmSystem)) {
input.value = drmSystem;
break;
}
}
};
const drmOnChange = (input) => {
setLicenseServerURLs();
};
const DRMSystemName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.DRM_SYSTEM);
this.makeField_(
container, DRMSystemName, drmSetup, drmOnChange);
return drmDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @return {!Element} div
* @private
*/
makeAssetDialogContentsExtra_(assetInProgress, inputsToCheck) {
const extraConfigDiv = document.createElement('div');
const containerStyle = shakaDemo.InputContainer.Style.FLEX;
const container = new shakaDemo.InputContainer(
extraConfigDiv, /* headerText= */ null, containerStyle,
/* docLink= */ null);
container.getClassList().add('wide-input');
container.setDefaultRowClass('wide-input');
const extraSetup = (input, container) => {
input.setAttribute('rows', 10);
if (assetInProgress.extraConfig) {
// Pretty-print the extra config.
input.value = JSON.stringify(
assetInProgress.extraConfig,
/* replacer= */ null, /* spacing= */ 2);
}
inputsToCheck.push(input);
// Make an error that shows up if you did not provide valid JSON.
const error = document.createElement('span');
error.classList.add('mdl-textfield__error');
error.textContent = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.INVALID_JSON_CONFIG_ERROR);
container.appendChild(error);
};
const extraOnChange = (inputElement, inputWrapper) => {
try {
if (!inputElement.value) {
assetInProgress.extraConfig = null;
} else {
const config = /** @type {!Object} */(JSON.parse(inputElement.value));
assetInProgress.extraConfig = config;
}
inputWrapper.setValid(true);
} catch (exception) {
inputWrapper.setValid(false);
}
};
const extraConfigLabel = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.EXTRA_SHAKA_PLAYER_CONFIG);
this.makeField_(
container, extraConfigLabel, extraSetup, extraOnChange,
/* isTextArea= */ true);
return extraConfigDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @param {!Element} iconDiv
* @return {!Element} div
* @private
*/
makeAssetDialogContentsMain_(assetInProgress, inputsToCheck, iconDiv) {
const mainDiv = document.createElement('div');
const containerStyle = shakaDemo.InputContainer.Style.VERTICAL;
const container = new shakaDemo.InputContainer(
mainDiv, /* headerText= */ null, containerStyle,
/* docLink= */ null);
// Make the manifest URL field.
const manifestSetup = (input, container) => {
input.value = assetInProgress.manifestUri;
inputsToCheck.push(input);
// Make an error that shows up if you did not provide an URL.
const error = document.createElement('span');
error.classList.add('mdl-textfield__error');
error.textContent = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.MANIFEST_URL_ERROR);
container.appendChild(error);
// Add a regex that will detect empty strings.
input.required = this.checkManifestRequired_(assetInProgress);
input.pattern = '^(?!([\r\n\t\f\v ]+)$).*$';
this.manifestField_ = input;
};
const manifestOnChange = (input) => {
assetInProgress.manifestUri = input.value;
};
const manifestURLName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.MANIFEST_URL);
this.makeField_(
container, manifestURLName, manifestSetup, manifestOnChange);
// Make the name field.
const nameSetup = (input, container) => {
input.value = assetInProgress.name;
inputsToCheck.push(input);
// Make an error that shows up if you have an empty/duplicate name.
const error = document.createElement('span');
error.classList.add('mdl-textfield__error');
error.textContent = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.NAME_ERROR);
container.appendChild(error);
// Make a regex that will detect duplicates.
input.required = true;
input.pattern = '^(?!( *';
for (const asset of this.assets_) {
if (asset == assetInProgress) {
// If editing an existing asset, it's okay if the name doesn't change.
continue;
}
const escape = (input) => {
return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
input.pattern += '|' + escape(asset.name);
}
input.pattern += ')$).*$';
};
const nameOnChange = (input) => {
assetInProgress.name = input.value;
};
const nameName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.NAME);
this.makeField_(
container, nameName, nameSetup, nameOnChange);
// Make the icon field.
const iconSetup = (input, container) => {
if (assetInProgress.iconUri) {
input.value = assetInProgress.iconUri;
const img =
/** @type {!HTMLImageElement} */(document.createElement('img'));
img.src = input.value;
img.alt = ''; // Not necessary to understand the page
iconDiv.appendChild(img);
}
};
const iconOnChange = (input) => {
shaka.util.Dom.removeAllChildren(iconDiv);
assetInProgress.iconUri = input.value;
if (input.value) {
const img =
/** @type {!HTMLImageElement} */(document.createElement('img'));
img.src = input.value;
img.alt = ''; // Not necessary to understand the page
iconDiv.appendChild(img);
}
};
const iconURLName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.ICON_URL);
this.makeField_(
container, iconURLName, iconSetup, iconOnChange);
// Make the MIME type field.
const mimeTypeSetup = (input, container) => {
if (assetInProgress.mimeType) {
input.value = assetInProgress.mimeType;
}
};
const mimeTypeOnChange = (input) => {
assetInProgress.mimeType = input.value || null;
};
const mimeTypeName = shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.MIME_TYPE);
this.makeField_(
container, mimeTypeName, mimeTypeSetup, mimeTypeOnChange);
return mainDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @private
*/
checkManifestRequired_(assetInProgress) {
// The manifest field is required unless we're getting the manifest
// from the Google Ad Manager using IMA ids.
const isDaiAdManifest = (assetInProgress.imaContentSrcId &&
assetInProgress.imaVideoId) || assetInProgress.imaAssetKey != null;
return !isDaiAdManifest;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @param {!Array.<!HTMLInputElement>} inputsToCheck
* @return {!Element} div
* @private
*/
makeAssetDialogContentsFinish_(assetInProgress, inputsToCheck) {
const finishDiv = document.createElement('tr');
const buttonStyle = shakaDemo.Custom.ButtonStyle_.RAISED;
const saveString =
shakaDemoMain.getLocalizedString(shakaDemo.MessageIds.SAVE_BUTTON);
finishDiv.appendChild(this.makeButton_(saveString, buttonStyle, () => {
for (const input of inputsToCheck) {
if (!input.validity.valid) {
return;
}
}
shakaDemoMain.setupOfflineSupport(assetInProgress);
this.assets_.add(assetInProgress);
this.saveAssetInfos_(this.assets_);
this.remakeSavedList_();
this.dialog_.close();
}));
const cancelString =
shakaDemoMain.getLocalizedString(shakaDemo.MessageIds.CANCEL_BUTTON);
finishDiv.appendChild(this.makeButton_(cancelString, buttonStyle, () => {
this.dialog_.close();
}));
return finishDiv;
}
/**
* @param {!ShakaDemoAssetInfo} assetInProgress
* @private
*/
showAssetDialog_(assetInProgress) {
// Remove buttons for any previous assets.
shaka.util.Dom.removeAllChildren(this.dialog_);
// An array of inputs which have validity checks which we care about.
/** @type {!Array.<!HTMLInputElement>} */
const inputsToCheck = [];
// Make the contents divs.
const iconDiv = document.createElement('div');
const mainDiv = this.makeAssetDialogContentsMain_(
assetInProgress, inputsToCheck, iconDiv);
const drmDiv = this.makeAssetDialogContentsDrm_(
assetInProgress, inputsToCheck);
const headersDiv = this.makeAssetDialogContentsHeaders_(
assetInProgress, inputsToCheck);
const adsDiv = this.makeAssetDialogContentsAds_(
assetInProgress, inputsToCheck);
const hlsDiv = this.makeAssetDialogContentsHLS_(
assetInProgress, inputsToCheck);
const extraConfigDiv = this.makeAssetDialogContentsExtra_(
assetInProgress, inputsToCheck);
const finishDiv = this.makeAssetDialogContentsFinish_(
assetInProgress, inputsToCheck);
// Make the buttons that control which tab is visible.
const tabDiv = document.createElement('tr');
const tabsToHide = [];
const buttonsToSwitch = [];
const addTabButton = (messageId, tabToShow, startOn) => {
const buttonStyle = shakaDemo.Custom.ButtonStyle_.PLAIN;
const name = shakaDemoMain.getLocalizedString(messageId);
const button = this.makeButton_(name, buttonStyle, () => {
for (const tab of tabsToHide) {
tab.classList.add('hidden');
}
tabToShow.classList.remove('hidden');
for (const button of buttonsToSwitch) {
button.classList.remove('mdl-button--accent');
}
button.classList.add('mdl-button--accent');
});
tabDiv.appendChild(button);
tabsToHide.push(tabToShow);
buttonsToSwitch.push(button);
if (startOn) {
button.classList.add('mdl-button--accent');
} else {
tabToShow.classList.add('hidden');
}
};
addTabButton(
shakaDemo.MessageIds.MAIN_TAB, mainDiv, /* startOn= */ true);
addTabButton(
shakaDemo.MessageIds.DRM_TAB, drmDiv, /* startOn= */ false);
addTabButton(
shakaDemo.MessageIds.HEADERS_TAB, headersDiv, /* startOn= */ false);
addTabButton(
shakaDemo.MessageIds.ADS_TAB, adsDiv, /* startOn= */ false);
addTabButton(
shakaDemo.MessageIds.HLS_TAB, hlsDiv, /* startOn= */ false);
addTabButton(
shakaDemo.MessageIds.EXTRA_TAB, extraConfigDiv, /* startOn= */ false);
// Append the divs in the desired order.
this.dialog_.appendChild(tabDiv);
this.dialog_.appendChild(mainDiv);
this.dialog_.appendChild(drmDiv);
this.dialog_.appendChild(headersDiv);
this.dialog_.appendChild(adsDiv);
this.dialog_.appendChild(hlsDiv);
this.dialog_.appendChild(extraConfigDiv);
this.dialog_.appendChild(finishDiv);
this.dialog_.appendChild(iconDiv);
// Update the componentHandler, to account for the new MDL elements.
componentHandler.upgradeDom();
// Show the dialog last, so that it knows where to place it.
this.dialog_.showModal();
}
/**
* @return {!Set.<!ShakaDemoAssetInfo>}
* @private
*/
loadAssetInfos_() {
const savedString = window.localStorage.getItem(shakaDemo.Custom.saveId_);
if (savedString) {
const assets =
/** @type {!Array.<!ShakaDemoAssetInfo>} */(JSON.parse(savedString));
return new Set(assets.map((json) => {
const asset = ShakaDemoAssetInfo.fromJSON(json);
shakaDemoMain.setupOfflineSupport(asset);
return asset;
}));
}
return new Set();
}
/**
* @param {!Set.<!ShakaDemoAssetInfo>} assetInfos
* @private
*/
saveAssetInfos_(assetInfos) {
const saveId = shakaDemo.Custom.saveId_;
const assets = Array.from(assetInfos);
window.localStorage.setItem(saveId, JSON.stringify(assets));
}
/**
* @param {string} name
* @param {shakaDemo.Custom.ButtonStyle_} buttonStyle
* What style should this button be in?
* @param {function()} callback
* @return {!Element}
* @private
*/
makeButton_(name, buttonStyle, callback) {
const button = document.createElement('button');
switch (buttonStyle) {
case shakaDemo.Custom.ButtonStyle_.FAB: {
button.classList.add('mdl-button--fab');
button.classList.add('mdl-button--colored');
const icon = document.createElement('i');
icon.classList.add('material-icons-round');
icon.textContent = name;
button.appendChild(icon);
} break;
case shakaDemo.Custom.ButtonStyle_.RAISED:
button.textContent = name;
button.classList.add('mdl-button--raised');
break;
case shakaDemo.Custom.ButtonStyle_.PLAIN:
button.textContent = name;
break;
}
button.addEventListener('click', callback);
button.classList.add('mdl-button');
button.classList.add('mdl-js-button');
button.classList.add('mdl-js-ripple-effect');
return button;
}
/**
* @param {!ShakaDemoAssetInfo} asset
* @return {!shakaDemo.AssetCard}
* @private
*/
createAssetCardFor_(asset) {
const savedList = this.savedList_;
const isFeatured = false;
return new shakaDemo.AssetCard(savedList, asset, isFeatured, (c) => {
c.addButton(shakaDemo.MessageIds.PLAY, () => {
shakaDemoMain.loadAsset(asset);
this.updateSelected_();
});
c.addButton(shakaDemo.MessageIds.EDIT_CUSTOM, async () => {
if (asset.unstoreCallback) {
await asset.unstoreCallback();
}
this.showAssetDialog_(asset);
});
c.addButton(shakaDemo.MessageIds.DELETE_CUSTOM, async () => {
this.assets_.delete(asset);
if (asset.unstoreCallback) {
await asset.unstoreCallback();
}
this.saveAssetInfos_(this.assets_);
this.remakeSavedList_();
}, shakaDemo.MessageIds.DELETE_CUSTOM);
c.addStoreButton();
});
}
/**
* Updates which asset card is selected.
* @private
*/
updateSelected_() {
for (const card of this.assetCards_) {
card.selectByAsset(shakaDemoMain.selectedAsset);
}
}
/** @private */
remakeSavedList_() {
shaka.util.Dom.removeAllChildren(this.savedList_);
if (this.assets_.size == 0) {
// Add in a message telling you what to do.
const makeMessage = (textClass, text) => {
const textElement = document.createElement('h2');
textElement.classList.add('mdl-typography--' + textClass);
textElement.textContent = text;
this.savedList_.appendChild(textElement);
};
makeMessage('title',
shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.CUSTOM_INTRO_ONE));
makeMessage('body-2',
shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.CUSTOM_INTRO_TWO));
makeMessage('body-1',
shakaDemoMain.getLocalizedString(
shakaDemo.MessageIds.CUSTOM_INTRO_THREE));
} else {
// Make asset cards for the assets.
this.assetCards_ = Array.from(this.assets_).map((asset) => {
return this.createAssetCardFor_(asset);
});
this.updateSelected_();
}
}
};
/**
* @enum {number}
* @private
*/
shakaDemo.Custom.ButtonStyle_ = {
RAISED: 0,
FAB: 1,
PLAIN: 2,
};
/**
* The name of the field in window.localStorage that is used to store a user's
* custom assets.
* @const {string}
*/
shakaDemo.Custom.saveId_ = 'shakaPlayerDemoSavedAssets';
document.addEventListener('shaka-main-loaded', shakaDemo.Custom.init);
document.addEventListener('shaka-main-cleanup', () => {
shakaDemoCustom = null;
});