-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathvideojs-vimeo.js
2420 lines (2055 loc) · 70.5 KB
/
videojs-vimeo.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
/**
* videojs-vimeo
* @version 3.0.1
* @copyright 2017 Benoit Tremblay <[email protected]>
* @license MIT
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.videojsVimeo = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global){
/*! @vimeo/player v2.1.0 | (c) 2017 Vimeo | MIT License | https://github.com/vimeo/player.js */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Vimeo = global.Vimeo || {}, global.Vimeo.Player = factory());
}(this, (function () { 'use strict';
var arrayIndexOfSupport = typeof Array.prototype.indexOf !== 'undefined';
var postMessageSupport = typeof window.postMessage !== 'undefined';
if (!arrayIndexOfSupport || !postMessageSupport) {
throw new Error('Sorry, the Vimeo Player API is not available in this browser.');
}
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var index = createCommonjsModule(function (module, exports) {
(function (exports) {
'use strict';
//shared pointer
var i;
//shortcuts
var defineProperty = Object.defineProperty,
is = function is(a, b) {
return a === b || a !== a && b !== b;
};
//Polyfill global objects
if (typeof WeakMap == 'undefined') {
exports.WeakMap = createCollection({
// WeakMap#delete(key:void*):boolean
'delete': sharedDelete,
// WeakMap#clear():
clear: sharedClear,
// WeakMap#get(key:void*):void*
get: sharedGet,
// WeakMap#has(key:void*):boolean
has: mapHas,
// WeakMap#set(key:void*, value:void*):void
set: sharedSet
}, true);
}
if (typeof Map == 'undefined' || typeof new Map().values !== 'function' || !new Map().values().next) {
exports.Map = createCollection({
// WeakMap#delete(key:void*):boolean
'delete': sharedDelete,
//:was Map#get(key:void*[, d3fault:void*]):void*
// Map#has(key:void*):boolean
has: mapHas,
// Map#get(key:void*):boolean
get: sharedGet,
// Map#set(key:void*, value:void*):void
set: sharedSet,
// Map#keys(void):Iterator
keys: sharedKeys,
// Map#values(void):Iterator
values: sharedValues,
// Map#entries(void):Iterator
entries: mapEntries,
// Map#forEach(callback:Function, context:void*):void ==> callback.call(context, key, value, mapObject) === not in specs`
forEach: sharedForEach,
// Map#clear():
clear: sharedClear
});
}
if (typeof Set == 'undefined' || typeof new Set().values !== 'function' || !new Set().values().next) {
exports.Set = createCollection({
// Set#has(value:void*):boolean
has: setHas,
// Set#add(value:void*):boolean
add: sharedAdd,
// Set#delete(key:void*):boolean
'delete': sharedDelete,
// Set#clear():
clear: sharedClear,
// Set#keys(void):Iterator
keys: sharedValues, // specs actually say "the same function object as the initial value of the values property"
// Set#values(void):Iterator
values: sharedValues,
// Set#entries(void):Iterator
entries: setEntries,
// Set#forEach(callback:Function, context:void*):void ==> callback.call(context, value, index) === not in specs
forEach: sharedForEach
});
}
if (typeof WeakSet == 'undefined') {
exports.WeakSet = createCollection({
// WeakSet#delete(key:void*):boolean
'delete': sharedDelete,
// WeakSet#add(value:void*):boolean
add: sharedAdd,
// WeakSet#clear():
clear: sharedClear,
// WeakSet#has(value:void*):boolean
has: setHas
}, true);
}
/**
* ES6 collection constructor
* @return {Function} a collection class
*/
function createCollection(proto, objectOnly) {
function Collection(a) {
if (!this || this.constructor !== Collection) return new Collection(a);
this._keys = [];
this._values = [];
this._itp = []; // iteration pointers
this.objectOnly = objectOnly;
//parse initial iterable argument passed
if (a) init.call(this, a);
}
//define size for non object-only collections
if (!objectOnly) {
defineProperty(proto, 'size', {
get: sharedSize
});
}
//set prototype
proto.constructor = Collection;
Collection.prototype = proto;
return Collection;
}
/** parse initial iterable argument passed */
function init(a) {
var i;
//init Set argument, like `[1,2,3,{}]`
if (this.add) a.forEach(this.add, this);
//init Map argument like `[[1,2], [{}, 4]]`
else a.forEach(function (a) {
this.set(a[0], a[1]);
}, this);
}
/** delete */
function sharedDelete(key) {
if (this.has(key)) {
this._keys.splice(i, 1);
this._values.splice(i, 1);
// update iteration pointers
this._itp.forEach(function (p) {
if (i < p[0]) p[0]--;
});
}
// Aurora here does it while Canary doesn't
return -1 < i;
}
function sharedGet(key) {
return this.has(key) ? this._values[i] : undefined;
}
function has(list, key) {
if (this.objectOnly && key !== Object(key)) throw new TypeError("Invalid value used as weak collection key");
//NaN or 0 passed
if (key != key || key === 0) for (i = list.length; i-- && !is(list[i], key);) {} else i = list.indexOf(key);
return -1 < i;
}
function setHas(value) {
return has.call(this, this._values, value);
}
function mapHas(value) {
return has.call(this, this._keys, value);
}
/** @chainable */
function sharedSet(key, value) {
this.has(key) ? this._values[i] = value : this._values[this._keys.push(key) - 1] = value;
return this;
}
/** @chainable */
function sharedAdd(value) {
if (!this.has(value)) this._values.push(value);
return this;
}
function sharedClear() {
(this._keys || 0).length = this._values.length = 0;
}
/** keys, values, and iterate related methods */
function sharedKeys() {
return sharedIterator(this._itp, this._keys);
}
function sharedValues() {
return sharedIterator(this._itp, this._values);
}
function mapEntries() {
return sharedIterator(this._itp, this._keys, this._values);
}
function setEntries() {
return sharedIterator(this._itp, this._values, this._values);
}
function sharedIterator(itp, array, array2) {
var p = [0],
done = false;
itp.push(p);
return {
next: function next() {
var v,
k = p[0];
if (!done && k < array.length) {
v = array2 ? [array[k], array2[k]] : array[k];
p[0]++;
} else {
done = true;
itp.splice(itp.indexOf(p), 1);
}
return { done: done, value: v };
}
};
}
function sharedSize() {
return this._values.length;
}
function sharedForEach(callback, context) {
var it = this.entries();
for (;;) {
var r = it.next();
if (r.done) break;
callback.call(context, r.value[1], r.value[0], this);
}
}
})('object' != 'undefined' && typeof commonjsGlobal != 'undefined' ? commonjsGlobal : window);
});
var npo_src = createCommonjsModule(function (module) {
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/*! Native Promise Only
v0.8.1 (c) Kyle Simpson
MIT License: http://getify.mit-license.org
*/
(function UMD(name, context, definition) {
// special form of UMD for polyfilling across evironments
context[name] = context[name] || definition();
if ('object' != "undefined" && module.exports) {
module.exports = context[name];
} else if (typeof undefined == "function" && undefined.amd) {
undefined(function $AMD$() {
return context[name];
});
}
})("Promise", typeof commonjsGlobal != "undefined" ? commonjsGlobal : commonjsGlobal, function DEF() {
/*jshint validthis:true */
"use strict";
var builtInProp,
cycle,
scheduling_queue,
ToString = Object.prototype.toString,
timer = typeof setImmediate != "undefined" ? function timer(fn) {
return setImmediate(fn);
} : setTimeout;
// dammit, IE8.
try {
Object.defineProperty({}, "x", {});
builtInProp = function builtInProp(obj, name, val, config) {
return Object.defineProperty(obj, name, {
value: val,
writable: true,
configurable: config !== false
});
};
} catch (err) {
builtInProp = function builtInProp(obj, name, val) {
obj[name] = val;
return obj;
};
}
// Note: using a queue instead of array for efficiency
scheduling_queue = function Queue() {
var first, last, item;
function Item(fn, self) {
this.fn = fn;
this.self = self;
this.next = void 0;
}
return {
add: function add(fn, self) {
item = new Item(fn, self);
if (last) {
last.next = item;
} else {
first = item;
}
last = item;
item = void 0;
},
drain: function drain() {
var f = first;
first = last = cycle = void 0;
while (f) {
f.fn.call(f.self);
f = f.next;
}
}
};
}();
function schedule(fn, self) {
scheduling_queue.add(fn, self);
if (!cycle) {
cycle = timer(scheduling_queue.drain);
}
}
// promise duck typing
function isThenable(o) {
var _then,
o_type = typeof o === "undefined" ? "undefined" : _typeof(o);
if (o != null && (o_type == "object" || o_type == "function")) {
_then = o.then;
}
return typeof _then == "function" ? _then : false;
}
function notify() {
for (var i = 0; i < this.chain.length; i++) {
notifyIsolated(this, this.state === 1 ? this.chain[i].success : this.chain[i].failure, this.chain[i]);
}
this.chain.length = 0;
}
// NOTE: This is a separate function to isolate
// the `try..catch` so that other code can be
// optimized better
function notifyIsolated(self, cb, chain) {
var ret, _then;
try {
if (cb === false) {
chain.reject(self.msg);
} else {
if (cb === true) {
ret = self.msg;
} else {
ret = cb.call(void 0, self.msg);
}
if (ret === chain.promise) {
chain.reject(TypeError("Promise-chain cycle"));
} else if (_then = isThenable(ret)) {
_then.call(ret, chain.resolve, chain.reject);
} else {
chain.resolve(ret);
}
}
} catch (err) {
chain.reject(err);
}
}
function resolve(msg) {
var _then,
self = this;
// already triggered?
if (self.triggered) {
return;
}
self.triggered = true;
// unwrap
if (self.def) {
self = self.def;
}
try {
if (_then = isThenable(msg)) {
schedule(function () {
var def_wrapper = new MakeDefWrapper(self);
try {
_then.call(msg, function $resolve$() {
resolve.apply(def_wrapper, arguments);
}, function $reject$() {
reject.apply(def_wrapper, arguments);
});
} catch (err) {
reject.call(def_wrapper, err);
}
});
} else {
self.msg = msg;
self.state = 1;
if (self.chain.length > 0) {
schedule(notify, self);
}
}
} catch (err) {
reject.call(new MakeDefWrapper(self), err);
}
}
function reject(msg) {
var self = this;
// already triggered?
if (self.triggered) {
return;
}
self.triggered = true;
// unwrap
if (self.def) {
self = self.def;
}
self.msg = msg;
self.state = 2;
if (self.chain.length > 0) {
schedule(notify, self);
}
}
function iteratePromises(Constructor, arr, resolver, rejecter) {
for (var idx = 0; idx < arr.length; idx++) {
(function IIFE(idx) {
Constructor.resolve(arr[idx]).then(function $resolver$(msg) {
resolver(idx, msg);
}, rejecter);
})(idx);
}
}
function MakeDefWrapper(self) {
this.def = self;
this.triggered = false;
}
function MakeDef(self) {
this.promise = self;
this.state = 0;
this.triggered = false;
this.chain = [];
this.msg = void 0;
}
function Promise(executor) {
if (typeof executor != "function") {
throw TypeError("Not a function");
}
if (this.__NPO__ !== 0) {
throw TypeError("Not a promise");
}
// instance shadowing the inherited "brand"
// to signal an already "initialized" promise
this.__NPO__ = 1;
var def = new MakeDef(this);
this["then"] = function then(success, failure) {
var o = {
success: typeof success == "function" ? success : true,
failure: typeof failure == "function" ? failure : false
};
// Note: `then(..)` itself can be borrowed to be used against
// a different promise constructor for making the chained promise,
// by substituting a different `this` binding.
o.promise = new this.constructor(function extractChain(resolve, reject) {
if (typeof resolve != "function" || typeof reject != "function") {
throw TypeError("Not a function");
}
o.resolve = resolve;
o.reject = reject;
});
def.chain.push(o);
if (def.state !== 0) {
schedule(notify, def);
}
return o.promise;
};
this["catch"] = function $catch$(failure) {
return this.then(void 0, failure);
};
try {
executor.call(void 0, function publicResolve(msg) {
resolve.call(def, msg);
}, function publicReject(msg) {
reject.call(def, msg);
});
} catch (err) {
reject.call(def, err);
}
}
var PromisePrototype = builtInProp({}, "constructor", Promise,
/*configurable=*/false);
// Note: Android 4 cannot use `Object.defineProperty(..)` here
Promise.prototype = PromisePrototype;
// built-in "brand" to signal an "uninitialized" promise
builtInProp(PromisePrototype, "__NPO__", 0,
/*configurable=*/false);
builtInProp(Promise, "resolve", function Promise$resolve(msg) {
var Constructor = this;
// spec mandated checks
// note: best "isPromise" check that's practical for now
if (msg && (typeof msg === "undefined" ? "undefined" : _typeof(msg)) == "object" && msg.__NPO__ === 1) {
return msg;
}
return new Constructor(function executor(resolve, reject) {
if (typeof resolve != "function" || typeof reject != "function") {
throw TypeError("Not a function");
}
resolve(msg);
});
});
builtInProp(Promise, "reject", function Promise$reject(msg) {
return new this(function executor(resolve, reject) {
if (typeof resolve != "function" || typeof reject != "function") {
throw TypeError("Not a function");
}
reject(msg);
});
});
builtInProp(Promise, "all", function Promise$all(arr) {
var Constructor = this;
// spec mandated checks
if (ToString.call(arr) != "[object Array]") {
return Constructor.reject(TypeError("Not an array"));
}
if (arr.length === 0) {
return Constructor.resolve([]);
}
return new Constructor(function executor(resolve, reject) {
if (typeof resolve != "function" || typeof reject != "function") {
throw TypeError("Not a function");
}
var len = arr.length,
msgs = Array(len),
count = 0;
iteratePromises(Constructor, arr, function resolver(idx, msg) {
msgs[idx] = msg;
if (++count === len) {
resolve(msgs);
}
}, reject);
});
});
builtInProp(Promise, "race", function Promise$race(arr) {
var Constructor = this;
// spec mandated checks
if (ToString.call(arr) != "[object Array]") {
return Constructor.reject(TypeError("Not an array"));
}
return new Constructor(function executor(resolve, reject) {
if (typeof resolve != "function" || typeof reject != "function") {
throw TypeError("Not a function");
}
iteratePromises(Constructor, arr, function resolver(idx, msg) {
resolve(msg);
}, reject);
});
});
return Promise;
});
});
/**
* @module lib/callbacks
*/
var callbackMap = new WeakMap();
/**
* Store a callback for a method or event for a player.
*
* @author Brad Dougherty <[email protected]>
* @param {Player} player The player object.
* @param {string} name The method or event name.
* @param {(function(this:Player, *): void|{resolve: function, reject: function})} callback
* The callback to call or an object with resolve and reject functions for a promise.
* @return {void}
*/
function storeCallback(player, name, callback) {
var playerCallbacks = callbackMap.get(player.element) || {};
if (!(name in playerCallbacks)) {
playerCallbacks[name] = [];
}
playerCallbacks[name].push(callback);
callbackMap.set(player.element, playerCallbacks);
}
/**
* Get the callbacks for a player and event or method.
*
* @author Brad Dougherty <[email protected]>
* @param {Player} player The player object.
* @param {string} name The method or event name
* @return {function[]}
*/
function getCallbacks(player, name) {
var playerCallbacks = callbackMap.get(player.element) || {};
return playerCallbacks[name] || [];
}
/**
* Remove a stored callback for a method or event for a player.
*
* @author Brad Dougherty <[email protected]>
* @param {Player} player The player object.
* @param {string} name The method or event name
* @param {function} [callback] The specific callback to remove.
* @return {boolean} Was this the last callback?
*/
function removeCallback(player, name, callback) {
var playerCallbacks = callbackMap.get(player.element) || {};
if (!playerCallbacks[name]) {
return true;
}
// If no callback is passed, remove all callbacks for the event
if (!callback) {
playerCallbacks[name] = [];
callbackMap.set(player.element, playerCallbacks);
return true;
}
var index = playerCallbacks[name].indexOf(callback);
if (index !== -1) {
playerCallbacks[name].splice(index, 1);
}
callbackMap.set(player.element, playerCallbacks);
return playerCallbacks[name] && playerCallbacks[name].length === 0;
}
/**
* Return the first stored callback for a player and event or method.
*
* @param {Player} player The player object.
* @param {string} name The method or event name.
* @return {function} The callback, or false if there were none
*/
function shiftCallbacks(player, name) {
var playerCallbacks = getCallbacks(player, name);
if (playerCallbacks.length < 1) {
return false;
}
var callback = playerCallbacks.shift();
removeCallback(player, name, callback);
return callback;
}
/**
* Move callbacks associated with an element to another element.
*
* @author Brad Dougherty <[email protected]>
* @param {HTMLElement} oldElement The old element.
* @param {HTMLElement} newElement The new element.
* @return {void}
*/
function swapCallbacks(oldElement, newElement) {
var playerCallbacks = callbackMap.get(oldElement);
callbackMap.set(newElement, playerCallbacks);
callbackMap.delete(oldElement);
}
/**
* @module lib/functions
*/
/**
* Get the name of the method for a given getter or setter.
*
* @author Brad Dougherty <[email protected]>
* @param {string} prop The name of the property.
* @param {string} type Either “get” or “set”.
* @return {string}
*/
function getMethodName(prop, type) {
if (prop.indexOf(type.toLowerCase()) === 0) {
return prop;
}
return '' + type.toLowerCase() + prop.substr(0, 1).toUpperCase() + prop.substr(1);
}
/**
* Check to see if the object is a DOM Element.
*
* @author Brad Dougherty <[email protected]>
* @param {*} element The object to check.
* @return {boolean}
*/
function isDomElement(element) {
return element instanceof window.HTMLElement;
}
/**
* Check to see whether the value is a number.
*
* @author Brad Dougherty <[email protected]>
* @see http://dl.dropboxusercontent.com/u/35146/js/tests/isNumber.html
* @param {*} value The value to check.
* @param {boolean} integer Check if the value is an integer.
* @return {boolean}
*/
function isInteger(value) {
// eslint-disable-next-line eqeqeq
return !isNaN(parseFloat(value)) && isFinite(value) && Math.floor(value) == value;
}
/**
* Check to see if the URL is a Vimeo url.
*
* @author Brad Dougherty <[email protected]>
* @param {string} url The url string.
* @return {boolean}
*/
function isVimeoUrl(url) {
return (/^(https?:)?\/\/((player|www).)?vimeo.com(?=$|\/)/.test(url)
);
}
/**
* Get the Vimeo URL from an element.
* The element must have either a data-vimeo-id or data-vimeo-url attribute.
*
* @author Brad Dougherty <[email protected]>
* @param {object} oEmbedParameters The oEmbed parameters.
* @return {string}
*/
function getVimeoUrl() {
var oEmbedParameters = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var id = oEmbedParameters.id;
var url = oEmbedParameters.url;
var idOrUrl = id || url;
if (!idOrUrl) {
throw new Error('An id or url must be passed, either in an options object or as a data-vimeo-id or data-vimeo-url attribute.');
}
if (isInteger(idOrUrl)) {
return 'https://vimeo.com/' + idOrUrl;
}
if (isVimeoUrl(idOrUrl)) {
return idOrUrl.replace('http:', 'https:');
}
if (id) {
throw new TypeError('\u201C' + id + '\u201D is not a valid video id.');
}
throw new TypeError('\u201C' + idOrUrl + '\u201D is not a vimeo.com url.');
}
/**
* @module lib/embed
*/
var oEmbedParameters = ['id', 'url', 'width', 'maxwidth', 'height', 'maxheight', 'portrait', 'title', 'byline', 'color', 'autoplay', 'autopause', 'loop', 'responsive'];
/**
* Get the 'data-vimeo'-prefixed attributes from an element as an object.
*
* @author Brad Dougherty <[email protected]>
* @param {HTMLElement} element The element.
* @param {Object} [defaults={}] The default values to use.
* @return {Object<string, string>}
*/
function getOEmbedParameters(element) {
var defaults = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return oEmbedParameters.reduce(function (params, param) {
var value = element.getAttribute('data-vimeo-' + param);
if (value || value === '') {
params[param] = value === '' ? 1 : value;
}
return params;
}, defaults);
}
/**
* Make an oEmbed call for the specified URL.
*
* @author Brad Dougherty <[email protected]>
* @param {string} videoUrl The vimeo.com url for the video.
* @param {Object} [params] Parameters to pass to oEmbed.
* @return {Promise}
*/
function getOEmbedData(videoUrl) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(function (resolve, reject) {
if (!isVimeoUrl(videoUrl)) {
throw new TypeError('\u201C' + videoUrl + '\u201D is not a vimeo.com url.');
}
var url = 'https://vimeo.com/api/oembed.json?url=' + encodeURIComponent(videoUrl);
for (var param in params) {
if (params.hasOwnProperty(param)) {
url += '&' + param + '=' + encodeURIComponent(params[param]);
}
}
var xhr = 'XDomainRequest' in window ? new XDomainRequest() : new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function () {
if (xhr.status === 404) {
reject(new Error('\u201C' + videoUrl + '\u201D was not found.'));
return;
}
if (xhr.status === 403) {
reject(new Error('\u201C' + videoUrl + '\u201D is not embeddable.'));
return;
}
try {
var json = JSON.parse(xhr.responseText);
resolve(json);
} catch (error) {
reject(error);
}
};
xhr.onerror = function () {
var status = xhr.status ? ' (' + xhr.status + ')' : '';
reject(new Error('There was an error fetching the embed code from Vimeo' + status + '.'));
};
xhr.send();
});
}
/**
* Create an embed from oEmbed data inside an element.
*
* @author Brad Dougherty <[email protected]>
* @param {object} data The oEmbed data.
* @param {HTMLElement} element The element to put the iframe in.
* @return {HTMLIFrameElement} The iframe embed.
*/
function createEmbed(_ref, element) {
var html = _ref.html;
if (!element) {
throw new TypeError('An element must be provided');
}
if (element.getAttribute('data-vimeo-initialized') !== null) {
return element.querySelector('iframe');
}
var div = document.createElement('div');
div.innerHTML = html;
element.appendChild(div.firstChild);
element.setAttribute('data-vimeo-initialized', 'true');
return element.querySelector('iframe');
}
/**
* Initialize all embeds within a specific element
*
* @author Brad Dougherty <[email protected]>
* @param {HTMLElement} [parent=document] The parent element.
* @return {void}
*/
function initializeEmbeds() {
var parent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
var elements = [].slice.call(parent.querySelectorAll('[data-vimeo-id], [data-vimeo-url]'));
var handleError = function handleError(error) {
if ('console' in window && console.error) {
console.error('There was an error creating an embed: ' + error);
}
};
elements.forEach(function (element) {
try {
// Skip any that have data-vimeo-defer
if (element.getAttribute('data-vimeo-defer') !== null) {
return;
}
var params = getOEmbedParameters(element);
var url = getVimeoUrl(params);
getOEmbedData(url, params).then(function (data) {
return createEmbed(data, element);
}).catch(handleError);
} catch (error) {
handleError(error);
}
});
}
/**
* Resize embeds when messaged by the player.
*
* @author Brad Dougherty <[email protected]>
* @param {HTMLElement} [parent=document] The parent element.
* @return {void}
*/
function resizeEmbeds() {
var parent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
var onMessage = function onMessage(event) {
if (!isVimeoUrl(event.origin)) {
return;
}
if (!event.data || event.data.event !== 'spacechange') {
return;
}
var iframes = parent.querySelectorAll('iframe');
for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentWindow !== event.source) {
continue;
}
var space = iframes[i].parentElement;
if (space && space.className.indexOf('vimeo-space') !== -1) {