-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
117 lines (93 loc) · 2.83 KB
/
index.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
var extend = require('extend');
var newElement = require('new-element');
var sdk = require('require-sdk')('https://www.youtube.com/iframe_api', 'YT');
var loadTrigger = sdk.trigger();
window.onYouTubeIframeAPIReady = function () {
loadTrigger();
delete window.onYouTubeIframeAPIReady;
};
module.exports = play;
function play (input, options, callback) {
var api;
if (arguments.length == 2 && typeof options == 'function') {
callback = options;
options = {};
}
var elementId = options.elementId || defaultElementId();
sdk(function (error, youtube) {
var videoId = pickID(input),
playerVars = {},
playlist;
api = youtube;
// Assemble the playerVars object using the passed options (except top-level items).
extend(playerVars, options, options.playerVars);
delete playerVars.width;
delete playerVars.height;
delete playerVars.playerVars;
// If we don't have a video ID, check to see if `input` is a playlist.
if (!videoId) {
playlist = /(?:\?|&)list=([^&]+)/.exec(input);
if (playlist) {
playerVars.listType = playerVars.listType || 'playlist';
playerVars.list = playlist[1];
}
}
// Automatically cast any boolean values as integers.
for (var i in playerVars) {
if ('boolean' === typeof playerVars[i]) {
playerVars[i] = playerVars[i] ? 1 : 0;
}
}
new api.Player(
elementId,
{
height: options.height,
width: options.width,
playerVars: playerVars,
videoId: videoId,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
});
function onPlayerReady (event) {
callback && callback(undefined, event.target);
}
function onPlayerStateChange (event) {
if (event.data == api.PlayerState.PLAYING && options.onPlay) {
options.onPlay(event.target);
}
if (event.data == api.PlayerState.ENDED && options.onEnd) {
options.onEnd(event.target);
}
if (event.data == api.PlayerState.PAUSED && options.onPause) {
options.onPause(event.target);
}
}
}
/**
* Parse the video ID out of a string.
*
* @param {string} input - The input string, which could be a URL or a video ID.
* @returns {string|null} Either the parsed video ID or NULL.
*/
function pickID (input) {
var videoId;
// Return early if there's no ".", as it's clearly not a URL.
if (!/\./.test(input)) {
return input;
}
videoId = /(?:\?|&)v=([^&]+)/.exec(input);
return videoId ? videoId[1] : null;
}
function defaultElementId () {
var id = 'youtube-video';
var defaultEl = document.getElementById(id);
if (defaultEl) {
defaultEl.parentNode.removeChild(defaultEl);
}
defaultEl = newElement('<div id="{id}"></div>', { id: id });
document.documentElement.appendChild(defaultEl);
return id;
}