-
Notifications
You must be signed in to change notification settings - Fork 262
/
player.js
executable file
·1356 lines (1237 loc) · 40.3 KB
/
player.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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import './lib/compatibility-check';
import 'weakmap-polyfill';
import Promise from 'native-promise-only';
import { storeCallback, getCallbacks, removeCallback, swapCallbacks } from './lib/callbacks';
import { getMethodName, isDomElement, isVimeoUrl, getVimeoUrl, isNode } from './lib/functions';
import {
getOEmbedParameters,
getOEmbedData,
createEmbed,
initializeEmbeds,
resizeEmbeds,
initAppendVideoMetadata,
checkUrlTimeParam
} from './lib/embed';
import { parseMessageData, postMessage, processData } from './lib/postmessage';
import { initializeScreenfull } from './lib/screenfull.js';
import { TimingSrcConnector } from './lib/timing-src-connector';
const playerMap = new WeakMap();
const readyMap = new WeakMap();
let screenfull = {};
class Player {
/**
* Create a Player.
*
* @param {(HTMLIFrameElement|HTMLElement|string|jQuery)} element A reference to the Vimeo
* player iframe, and id, or a jQuery object.
* @param {object} [options] oEmbed parameters to use when creating an embed in the element.
* @return {Player}
*/
constructor(element, options = {}) {
/* global jQuery */
if (window.jQuery && element instanceof jQuery) {
if (element.length > 1 && window.console && console.warn) {
console.warn('A jQuery object with multiple elements was passed, using the first element.');
}
element = element[0];
}
// Find an element by ID
if (typeof document !== 'undefined' && typeof element === 'string') {
element = document.getElementById(element);
}
// Not an element!
if (!isDomElement(element)) {
throw new TypeError('You must pass either a valid element or a valid id.');
}
// Already initialized an embed in this div, so grab the iframe
if (element.nodeName !== 'IFRAME') {
const iframe = element.querySelector('iframe');
if (iframe) {
element = iframe;
}
}
// iframe url is not a Vimeo url
if (element.nodeName === 'IFRAME' && !isVimeoUrl(element.getAttribute('src') || '')) {
throw new Error('The player element passed isn’t a Vimeo embed.');
}
// If there is already a player object in the map, return that
if (playerMap.has(element)) {
return playerMap.get(element);
}
this._window = element.ownerDocument.defaultView;
this.element = element;
this.origin = '*';
const readyPromise = new Promise((resolve, reject) => {
this._onMessage = (event) => {
if (!isVimeoUrl(event.origin) || this.element.contentWindow !== event.source) {
return;
}
if (this.origin === '*') {
this.origin = event.origin;
}
const data = parseMessageData(event.data);
const isError = data && data.event === 'error';
const isReadyError = isError && data.data && data.data.method === 'ready';
if (isReadyError) {
const error = new Error(data.data.message);
error.name = data.data.name;
reject(error);
return;
}
const isReadyEvent = data && data.event === 'ready';
const isPingResponse = data && data.method === 'ping';
if (isReadyEvent || isPingResponse) {
this.element.setAttribute('data-ready', 'true');
resolve();
return;
}
processData(this, data);
};
this._window.addEventListener('message', this._onMessage);
if (this.element.nodeName !== 'IFRAME') {
const params = getOEmbedParameters(element, options);
const url = getVimeoUrl(params);
getOEmbedData(url, params, element).then((data) => {
const iframe = createEmbed(data, element);
// Overwrite element with the new iframe,
// but store reference to the original element
this.element = iframe;
this._originalElement = element;
swapCallbacks(element, iframe);
playerMap.set(this.element, this);
return data;
}).catch(reject);
}
});
// Store a copy of this Player in the map
readyMap.set(this, readyPromise);
playerMap.set(this.element, this);
// Send a ping to the iframe so the ready promise will be resolved if
// the player is already ready.
if (this.element.nodeName === 'IFRAME') {
postMessage(this, 'ping');
}
if (screenfull.isEnabled) {
const exitFullscreen = () => screenfull.exit();
this.fullscreenchangeHandler = () => {
if (screenfull.isFullscreen) {
storeCallback(this, 'event:exitFullscreen', exitFullscreen);
}
else {
removeCallback(this, 'event:exitFullscreen', exitFullscreen);
}
// eslint-disable-next-line
this.ready().then(() => {
postMessage(this, 'fullscreenchange', screenfull.isFullscreen);
});
};
screenfull.on('fullscreenchange', this.fullscreenchangeHandler);
}
return this;
}
/**
* Get a promise for a method.
*
* @param {string} name The API method to call.
* @param {Object} [args={}] Arguments to send via postMessage.
* @return {Promise}
*/
callMethod(name, args = {}) {
return new Promise((resolve, reject) => {
// We are storing the resolve/reject handlers to call later, so we
// can’t return here.
// eslint-disable-next-line promise/always-return
return this.ready().then(() => {
storeCallback(this, name, {
resolve,
reject
});
postMessage(this, name, args);
}).catch(reject);
});
}
/**
* Get a promise for the value of a player property.
*
* @param {string} name The property name
* @return {Promise}
*/
get(name) {
return new Promise((resolve, reject) => {
name = getMethodName(name, 'get');
// We are storing the resolve/reject handlers to call later, so we
// can’t return here.
// eslint-disable-next-line promise/always-return
return this.ready().then(() => {
storeCallback(this, name, {
resolve,
reject
});
postMessage(this, name);
}).catch(reject);
});
}
/**
* Get a promise for setting the value of a player property.
*
* @param {string} name The API method to call.
* @param {mixed} value The value to set.
* @return {Promise}
*/
set(name, value) {
return new Promise((resolve, reject) => {
name = getMethodName(name, 'set');
if (value === undefined || value === null) {
throw new TypeError('There must be a value to set.');
}
// We are storing the resolve/reject handlers to call later, so we
// can’t return here.
// eslint-disable-next-line promise/always-return
return this.ready().then(() => {
storeCallback(this, name, {
resolve,
reject
});
postMessage(this, name, value);
}).catch(reject);
});
}
/**
* Add an event listener for the specified event. Will call the
* callback with a single parameter, `data`, that contains the data for
* that event.
*
* @param {string} eventName The name of the event.
* @param {function(*)} callback The function to call when the event fires.
* @return {void}
*/
on(eventName, callback) {
if (!eventName) {
throw new TypeError('You must pass an event name.');
}
if (!callback) {
throw new TypeError('You must pass a callback function.');
}
if (typeof callback !== 'function') {
throw new TypeError('The callback must be a function.');
}
const callbacks = getCallbacks(this, `event:${eventName}`);
if (callbacks.length === 0) {
this.callMethod('addEventListener', eventName).catch(() => {
// Ignore the error. There will be an error event fired that
// will trigger the error callback if they are listening.
});
}
storeCallback(this, `event:${eventName}`, callback);
}
/**
* Remove an event listener for the specified event. Will remove all
* listeners for that event if a `callback` isn’t passed, or only that
* specific callback if it is passed.
*
* @param {string} eventName The name of the event.
* @param {function} [callback] The specific callback to remove.
* @return {void}
*/
off(eventName, callback) {
if (!eventName) {
throw new TypeError('You must pass an event name.');
}
if (callback && typeof callback !== 'function') {
throw new TypeError('The callback must be a function.');
}
const lastCallback = removeCallback(this, `event:${eventName}`, callback);
// If there are no callbacks left, remove the listener
if (lastCallback) {
this.callMethod('removeEventListener', eventName).catch((e) => {
// Ignore the error. There will be an error event fired that
// will trigger the error callback if they are listening.
});
}
}
/**
* A promise to load a new video.
*
* @promise LoadVideoPromise
* @fulfill {number} The video with this id or url successfully loaded.
* @reject {TypeError} The id was not a number.
*/
/**
* Load a new video into this embed. The promise will be resolved if
* the video is successfully loaded, or it will be rejected if it could
* not be loaded.
*
* @param {number|string|object} options The id of the video, the url of the video, or an object with embed options.
* @return {LoadVideoPromise}
*/
loadVideo(options) {
return this.callMethod('loadVideo', options);
}
/**
* A promise to perform an action when the Player is ready.
*
* @todo document errors
* @promise LoadVideoPromise
* @fulfill {void}
*/
/**
* Trigger a function when the player iframe has initialized. You do not
* need to wait for `ready` to trigger to begin adding event listeners
* or calling other methods.
*
* @return {ReadyPromise}
*/
ready() {
const readyPromise = readyMap.get(this) || new Promise((resolve, reject) => {
reject(new Error('Unknown player. Probably unloaded.'));
});
return Promise.resolve(readyPromise);
}
/**
* A promise to add a cue point to the player.
*
* @promise AddCuePointPromise
* @fulfill {string} The id of the cue point to use for removeCuePoint.
* @reject {RangeError} the time was less than 0 or greater than the
* video’s duration.
* @reject {UnsupportedError} Cue points are not supported with the current
* player or browser.
*/
/**
* Add a cue point to the player.
*
* @param {number} time The time for the cue point.
* @param {object} [data] Arbitrary data to be returned with the cue point.
* @return {AddCuePointPromise}
*/
addCuePoint(time, data = {}) {
return this.callMethod('addCuePoint', { time, data });
}
/**
* A promise to remove a cue point from the player.
*
* @promise AddCuePointPromise
* @fulfill {string} The id of the cue point that was removed.
* @reject {InvalidCuePoint} The cue point with the specified id was not
* found.
* @reject {UnsupportedError} Cue points are not supported with the current
* player or browser.
*/
/**
* Remove a cue point from the video.
*
* @param {string} id The id of the cue point to remove.
* @return {RemoveCuePointPromise}
*/
removeCuePoint(id) {
return this.callMethod('removeCuePoint', id);
}
/**
* A representation of a text track on a video.
*
* @typedef {Object} VimeoTextTrack
* @property {string} language The ISO language code.
* @property {string} kind The kind of track it is (captions or subtitles).
* @property {string} label The human‐readable label for the track.
*/
/**
* A promise to enable a text track.
*
* @promise EnableTextTrackPromise
* @fulfill {VimeoTextTrack} The text track that was enabled.
* @reject {InvalidTrackLanguageError} No track was available with the
* specified language.
* @reject {InvalidTrackError} No track was available with the specified
* language and kind.
*/
/**
* Enable the text track with the specified language, and optionally the
* specified kind (captions or subtitles).
*
* When set via the API, the track language will not change the viewer’s
* stored preference.
*
* @param {string} language The two‐letter language code.
* @param {string} [kind] The kind of track to enable (captions or subtitles).
* @return {EnableTextTrackPromise}
*/
enableTextTrack(language, kind) {
if (!language) {
throw new TypeError('You must pass a language.');
}
return this.callMethod('enableTextTrack', {
language,
kind
});
}
/**
* A promise to disable the active text track.
*
* @promise DisableTextTrackPromise
* @fulfill {void} The track was disabled.
*/
/**
* Disable the currently-active text track.
*
* @return {DisableTextTrackPromise}
*/
disableTextTrack() {
return this.callMethod('disableTextTrack');
}
/**
* A promise to pause the video.
*
* @promise PausePromise
* @fulfill {void} The video was paused.
*/
/**
* Pause the video if it’s playing.
*
* @return {PausePromise}
*/
pause() {
return this.callMethod('pause');
}
/**
* A promise to play the video.
*
* @promise PlayPromise
* @fulfill {void} The video was played.
*/
/**
* Play the video if it’s paused. **Note:** on iOS and some other
* mobile devices, you cannot programmatically trigger play. Once the
* viewer has tapped on the play button in the player, however, you
* will be able to use this function.
*
* @return {PlayPromise}
*/
play() {
return this.callMethod('play');
}
/**
* Request that the player enters fullscreen.
* @return {Promise}
*/
requestFullscreen() {
if (screenfull.isEnabled) {
return screenfull.request(this.element);
}
return this.callMethod('requestFullscreen');
}
/**
* Request that the player exits fullscreen.
* @return {Promise}
*/
exitFullscreen() {
if (screenfull.isEnabled) {
return screenfull.exit();
}
return this.callMethod('exitFullscreen');
}
/**
* Returns true if the player is currently fullscreen.
* @return {Promise}
*/
getFullscreen() {
if (screenfull.isEnabled) {
return Promise.resolve(screenfull.isFullscreen);
}
return this.get('fullscreen');
}
/**
* Request that the player enters picture-in-picture.
* @return {Promise}
*/
requestPictureInPicture() {
return this.callMethod('requestPictureInPicture');
}
/**
* Request that the player exits picture-in-picture.
* @return {Promise}
*/
exitPictureInPicture() {
return this.callMethod('exitPictureInPicture');
}
/**
* Returns true if the player is currently picture-in-picture.
* @return {Promise}
*/
getPictureInPicture() {
return this.get('pictureInPicture');
}
/**
* A promise to prompt the viewer to initiate remote playback.
*
* @promise RemotePlaybackPromptPromise
* @fulfill {void}
* @reject {NotFoundError} No remote playback device is available.
*/
/**
* Request to prompt the user to initiate remote playback.
*
* @return {RemotePlaybackPromptPromise}
*/
remotePlaybackPrompt() {
return this.callMethod('remotePlaybackPrompt');
}
/**
* A promise to unload the video.
*
* @promise UnloadPromise
* @fulfill {void} The video was unloaded.
*/
/**
* Return the player to its initial state.
*
* @return {UnloadPromise}
*/
unload() {
return this.callMethod('unload');
}
/**
* Cleanup the player and remove it from the DOM
*
* It won't be usable and a new one should be constructed
* in order to do any operations.
*
* @return {Promise}
*/
destroy() {
return new Promise((resolve) => {
readyMap.delete(this);
playerMap.delete(this.element);
if (this._originalElement) {
playerMap.delete(this._originalElement);
this._originalElement.removeAttribute('data-vimeo-initialized');
}
if (this.element && this.element.nodeName === 'IFRAME' && this.element.parentNode) {
// If we've added an additional wrapper div, remove that from the DOM.
// If not, just remove the iframe element.
if (this.element.parentNode.parentNode && this._originalElement && this._originalElement !== this.element.parentNode) {
this.element.parentNode.parentNode.removeChild(this.element.parentNode);
}
else {
this.element.parentNode.removeChild(this.element);
}
}
// If the clip is private there is a case where the element stays the
// div element. Destroy should reset the div and remove the iframe child.
if (this.element && this.element.nodeName === 'DIV' && this.element.parentNode) {
this.element.removeAttribute('data-vimeo-initialized');
const iframe = this.element.querySelector('iframe');
if (iframe && iframe.parentNode) {
// If we've added an additional wrapper div, remove that from the DOM.
// If not, just remove the iframe element.
if (iframe.parentNode.parentNode && this._originalElement && this._originalElement !== iframe.parentNode) {
iframe.parentNode.parentNode.removeChild(iframe.parentNode);
}
else {
iframe.parentNode.removeChild(iframe);
}
}
}
this._window.removeEventListener('message', this._onMessage);
if (screenfull.isEnabled) {
screenfull.off('fullscreenchange', this.fullscreenchangeHandler);
}
resolve();
});
}
/**
* A promise to get the autopause behavior of the video.
*
* @promise GetAutopausePromise
* @fulfill {boolean} Whether autopause is turned on or off.
* @reject {UnsupportedError} Autopause is not supported with the current
* player or browser.
*/
/**
* Get the autopause behavior for this player.
*
* @return {GetAutopausePromise}
*/
getAutopause() {
return this.get('autopause');
}
/**
* A promise to set the autopause behavior of the video.
*
* @promise SetAutopausePromise
* @fulfill {boolean} Whether autopause is turned on or off.
* @reject {UnsupportedError} Autopause is not supported with the current
* player or browser.
*/
/**
* Enable or disable the autopause behavior of this player.
*
* By default, when another video is played in the same browser, this
* player will automatically pause. Unless you have a specific reason
* for doing so, we recommend that you leave autopause set to the
* default (`true`).
*
* @param {boolean} autopause
* @return {SetAutopausePromise}
*/
setAutopause(autopause) {
return this.set('autopause', autopause);
}
/**
* A promise to get the buffered property of the video.
*
* @promise GetBufferedPromise
* @fulfill {Array} Buffered Timeranges converted to an Array.
*/
/**
* Get the buffered property of the video.
*
* @return {GetBufferedPromise}
*/
getBuffered() {
return this.get('buffered');
}
/**
* @typedef {Object} CameraProperties
* @prop {number} props.yaw - Number between 0 and 360.
* @prop {number} props.pitch - Number between -90 and 90.
* @prop {number} props.roll - Number between -180 and 180.
* @prop {number} props.fov - The field of view in degrees.
*/
/**
* A promise to get the camera properties of the player.
*
* @promise GetCameraPromise
* @fulfill {CameraProperties} The camera properties.
*/
/**
* For 360° videos get the camera properties for this player.
*
* @return {GetCameraPromise}
*/
getCameraProps() {
return this.get('cameraProps');
}
/**
* A promise to set the camera properties of the player.
*
* @promise SetCameraPromise
* @fulfill {Object} The camera was successfully set.
* @reject {RangeError} The range was out of bounds.
*/
/**
* For 360° videos set the camera properties for this player.
*
* @param {CameraProperties} camera The camera properties
* @return {SetCameraPromise}
*/
setCameraProps(camera) {
return this.set('cameraProps', camera);
}
/**
* A representation of a chapter.
*
* @typedef {Object} VimeoChapter
* @property {number} startTime The start time of the chapter.
* @property {object} title The title of the chapter.
* @property {number} index The place in the order of Chapters. Starts at 1.
*/
/**
* A promise to get chapters for the video.
*
* @promise GetChaptersPromise
* @fulfill {VimeoChapter[]} The chapters for the video.
*/
/**
* Get an array of all the chapters for the video.
*
* @return {GetChaptersPromise}
*/
getChapters() {
return this.get('chapters');
}
/**
* A promise to get the currently active chapter.
*
* @promise GetCurrentChaptersPromise
* @fulfill {VimeoChapter|undefined} The current chapter for the video.
*/
/**
* Get the currently active chapter for the video.
*
* @return {GetCurrentChaptersPromise}
*/
getCurrentChapter() {
return this.get('currentChapter');
}
/**
* A promise to get the accent color of the player.
*
* @promise GetColorPromise
* @fulfill {string} The hex color of the player.
*/
/**
* Get the accent color for this player. Note this is deprecated in place of `getColorTwo`.
*
* @return {GetColorPromise}
*/
getColor() {
return this.get('color');
}
/**
* A promise to get all colors for the player in an array.
*
* @promise GetColorsPromise
* @fulfill {string[]} The hex colors of the player.
*/
/**
* Get all the colors for this player in an array: [colorOne, colorTwo, colorThree, colorFour]
*
* @return {GetColorPromise}
*/
getColors() {
return Promise.all([
this.get('colorOne'),
this.get('colorTwo'),
this.get('colorThree'),
this.get('colorFour')
]);
}
/**
* A promise to set the accent color of the player.
*
* @promise SetColorPromise
* @fulfill {string} The color was successfully set.
* @reject {TypeError} The string was not a valid hex or rgb color.
* @reject {ContrastError} The color was set, but the contrast is
* outside of the acceptable range.
* @reject {EmbedSettingsError} The owner of the player has chosen to
* use a specific color.
*/
/**
* Set the accent color of this player to a hex or rgb string. Setting the
* color may fail if the owner of the video has set their embed
* preferences to force a specific color.
* Note this is deprecated in place of `setColorTwo`.
*
* @param {string} color The hex or rgb color string to set.
* @return {SetColorPromise}
*/
setColor(color) {
return this.set('color', color);
}
/**
* A promise to set all colors for the player.
*
* @promise SetColorsPromise
* @fulfill {string[]} The colors were successfully set.
* @reject {TypeError} The string was not a valid hex or rgb color.
* @reject {ContrastError} The color was set, but the contrast is
* outside of the acceptable range.
* @reject {EmbedSettingsError} The owner of the player has chosen to
* use a specific color.
*/
/**
* Set the colors of this player to a hex or rgb string. Setting the
* color may fail if the owner of the video has set their embed
* preferences to force a specific color.
* The colors should be passed in as an array: [colorOne, colorTwo, colorThree, colorFour].
* If a color should not be set, the index in the array can be left as null.
*
* @param {string[]} colors Array of the hex or rgb color strings to set.
* @return {SetColorsPromise}
*/
setColors(colors) {
if (!Array.isArray(colors)) {
return new Promise((resolve, reject) => reject(new TypeError('Argument must be an array.')));
}
const nullPromise = new Promise((resolve) => resolve(null));
const colorPromises = [
colors[0] ? this.set('colorOne', colors[0]) : nullPromise,
colors[1] ? this.set('colorTwo', colors[1]) : nullPromise,
colors[2] ? this.set('colorThree', colors[2]) : nullPromise,
colors[3] ? this.set('colorFour', colors[3]) : nullPromise
];
return Promise.all(colorPromises);
}
/**
* A representation of a cue point.
*
* @typedef {Object} VimeoCuePoint
* @property {number} time The time of the cue point.
* @property {object} data The data passed when adding the cue point.
* @property {string} id The unique id for use with removeCuePoint.
*/
/**
* A promise to get the cue points of a video.
*
* @promise GetCuePointsPromise
* @fulfill {VimeoCuePoint[]} The cue points added to the video.
* @reject {UnsupportedError} Cue points are not supported with the current
* player or browser.
*/
/**
* Get an array of the cue points added to the video.
*
* @return {GetCuePointsPromise}
*/
getCuePoints() {
return this.get('cuePoints');
}
/**
* A promise to get the current time of the video.
*
* @promise GetCurrentTimePromise
* @fulfill {number} The current time in seconds.
*/
/**
* Get the current playback position in seconds.
*
* @return {GetCurrentTimePromise}
*/
getCurrentTime() {
return this.get('currentTime');
}
/**
* A promise to set the current time of the video.
*
* @promise SetCurrentTimePromise
* @fulfill {number} The actual current time that was set.
* @reject {RangeError} the time was less than 0 or greater than the
* video’s duration.
*/
/**
* Set the current playback position in seconds. If the player was
* paused, it will remain paused. Likewise, if the player was playing,
* it will resume playing once the video has buffered.
*
* You can provide an accurate time and the player will attempt to seek
* to as close to that time as possible. The exact time will be the
* fulfilled value of the promise.
*
* @param {number} currentTime
* @return {SetCurrentTimePromise}
*/
setCurrentTime(currentTime) {
return this.set('currentTime', currentTime);
}
/**
* A promise to get the duration of the video.
*
* @promise GetDurationPromise
* @fulfill {number} The duration in seconds.
*/
/**
* Get the duration of the video in seconds. It will be rounded to the
* nearest second before playback begins, and to the nearest thousandth
* of a second after playback begins.
*
* @return {GetDurationPromise}
*/
getDuration() {
return this.get('duration');
}
/**
* A promise to get the ended state of the video.
*
* @promise GetEndedPromise
* @fulfill {boolean} Whether or not the video has ended.
*/
/**
* Get the ended state of the video. The video has ended if
* `currentTime === duration`.
*
* @return {GetEndedPromise}
*/
getEnded() {
return this.get('ended');
}
/**
* A promise to get the loop state of the player.
*
* @promise GetLoopPromise
* @fulfill {boolean} Whether or not the player is set to loop.
*/
/**
* Get the loop state of the player.
*
* @return {GetLoopPromise}
*/
getLoop() {
return this.get('loop');
}
/**
* A promise to set the loop state of the player.
*
* @promise SetLoopPromise
* @fulfill {boolean} The loop state that was set.
*/
/**
* Set the loop state of the player. When set to `true`, the player
* will start over immediately once playback ends.
*
* @param {boolean} loop
* @return {SetLoopPromise}
*/
setLoop(loop) {
return this.set('loop', loop);
}
/**
* A promise to set the muted state of the player.
*
* @promise SetMutedPromise
* @fulfill {boolean} The muted state that was set.
*/
/**
* Set the muted state of the player. When set to `true`, the player
* volume will be muted.
*
* @param {boolean} muted
* @return {SetMutedPromise}
*/
setMuted(muted) {
return this.set('muted', muted);
}
/**
* A promise to get the muted state of the player.
*
* @promise GetMutedPromise
* @fulfill {boolean} Whether or not the player is muted.
*/
/**
* Get the muted state of the player.
*
* @return {GetMutedPromise}
*/
getMuted() {
return this.get('muted');
}