-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathember-youtube.js
444 lines (400 loc) · 10 KB
/
ember-youtube.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
/* global YT, window */
import Component from '@ember/component';
import RSVP from 'rsvp'
import { computed, getProperties, setProperties, observer } from '@ember/object';
import { debug } from '@ember/debug';
import { run } from '@ember/runloop';
import { task } from 'ember-concurrency';
export default Component.extend({
classNames: ['EmberYoutube'],
ytid: null,
width: 560,
height: 315,
// These options are used to load a video.
startSeconds: undefined,
endSeconds: undefined,
suggestedQuality: undefined,
lazyload: false,
showControls: false,
showDebug: false,
showProgress: false,
showExtras: computed.or('showControls', 'showProgress', 'showDebug'),
player: null,
playerState: 'loading',
/* Hooks */
playerCreated() {
/* Callback to be passed. */
},
playerStateChanged() {
/* Callback to be passed. */
},
error() {
/* Callback to be passed. */
},
/* State hooks */
ready() {
/* Callback to be passed. */
},
ended() {
/* Callback to be passed. */
},
playing() {
/* Callback to be passed. */
},
paused() {
/* Callback to be passed. */
},
buffering() {
/* Callback to be passed. */
},
queued() {
/* Callback to be passed. */
},
init() {
this._super();
setProperties(this, {
// YouTube's embedded player can take a number of optional parameters.
// https://developers.google.com/youtube/player_parameters#Parameters
// https://developers.google.com/youtube/youtube_player_demo
playerVars: Object.assign({}, this.playerVars),
// from YT.PlayerState
stateNames: {
'-1': 'ready', // READY
0: 'ended', // YT.Player.ENDED
1: 'playing', // YT.PlayerState.PLAYING
2: 'paused', // YT.PlayerState.PAUSED
3: 'buffering', // YT.PlayerState.BUFFERING
5: 'queued' // YT.PlayerState.CUED
}
});
this._register();
},
// Expose the component to the outside world.
_register() {
const delegate = this.get('delegate');
const delegateAs = this.get('delegate-as');
run.schedule('afterRender', () => {
if (!delegate) {
return;
}
delegate.set(delegateAs || 'emberYouTube', this);
});
},
didInsertElement() {
this._super(...arguments);
this.addProgressBarClickHandler();
if (!this.get('lazyload') && this.get('ytid')) {
// If "lazyload" is not enabled and we have an ID, we can start immediately.
// Otherwise the `loadVideo` observer will take care of things.
this.get('loadAndCreatePlayer').perform();
}
},
willDestroyElement() {
this.get('loadAndCreatePlayer').cancelAll();
// clear the timer
this.stopTimer();
// remove progress bar click handler
this.removeProgressBarClickHandler();
// destroy video player
const player = this.get('player');
if (player) {
player.destroy();
this.set('player', null);
}
// clear up if "delegated"
const delegate = this.get('delegate');
const delegateAs = this.get('delegate-as');
if (delegate) {
delegate.set(delegateAs || 'emberYouTube', null);
}
},
loadAndCreatePlayer: task(function * () {
try {
yield this.loadYouTubeApi();
let player = yield this.createPlayer();
this.setProperties({
player,
playerState: 'ready'
});
this.playerCreated(player);
this.loadVideo();
} catch(err) {
if (this.get('showDebug')) {
debug(err);
}
throw err
}
}).drop(),
// A promise that is resolved when window.onYouTubeIframeAPIReady is called.
// The promise is resolved with a reference to window.YT object.
loadYouTubeApi() {
return new RSVP.Promise((resolve) => {
let previous;
previous = window.onYouTubeIframeAPIReady;
// The API will call this function when page has finished downloading
// the JavaScript for the player API.
window.onYouTubeIframeAPIReady = () => {
if (previous) {
previous();
}
resolve(window.YT);
};
if (window.YT && window.YT.loaded) {
// If already loaded, make sure not to load the script again.
resolve(window.YT);
} else {
let ytScript = document.createElement("script");
ytScript.src = "https://www.youtube.com/iframe_api";
document.head.appendChild(ytScript);
}
});
},
// A promise that is immediately resolved with a YouTube player object.
createPlayer() {
const playerVars = this.get('playerVars');
const width = this.get('width');
const height = this.get('height');
const container = this.element.querySelector('.EmberYoutube-player');
let player;
return new RSVP.Promise((resolve, reject) => {
if (!container) {
reject(`Couldn't find the container element to create a YouTube player`);
}
player = new YT.Player(container, {
width,
height,
playerVars,
events: {
onReady() {
resolve(player);
},
onStateChange: this.onPlayerStateChange.bind(this),
onError: this.onPlayerError.bind(this)
}
});
});
},
// Gets called by the YouTube player.
onPlayerStateChange(event) {
// Set a readable state name
let state = this.get('stateNames.' + event.data.toString());
this.set('playerState', state);
if (this.get('showDebug')) {
debug(state);
}
// send actions outside
this[state](event);
this.playerStateChanged(event);
// send actions inside
this.send(state);
},
// Gets called by the YouTube player.
onPlayerError(event) {
let errorCode = event.data;
this.set('playerState', 'error');
// Send the event to the controller
this.error(errorCode);
if (this.get('showDebug')) {
debug('error' + errorCode);
}
// switch(errorCode) {
// case 2:
// debug('Invalid parameter');
// break;
// case 100:
// debug('Not found/private');
// this.send('playNext');
// break;
// case 101:
// case 150:
// debug('Embed not allowed');
// this.send('playNext');
// break;
// default:
// break;
// }
},
// Returns a boolean that indicates playback status by looking at the player state.
isPlaying: computed('playerState', {
get() {
const player = this.get('player');
if (!player) {
return false;
}
return player.getPlayerState() === 1;
}
}),
// Load (and plays) a video every time ytid changes.
ytidDidChange: observer('ytid', function () {
const player = this.get('player');
const ytid = this.get('ytid');
if (!ytid) {
return;
}
if (!player) {
this.get('loadAndCreatePlayer').perform();
return;
}
this.loadVideo();
}),
loadVideo() {
const player = this.get('player');
const ytid = this.get('ytid');
// Set parameters for the video to be played.
let options = getProperties(this, ['startSeconds', 'endSeconds', 'suggestedQuality']);
options.videoId = ytid;
// Either load or cue depending on `autoplay`.
if (this.playerVars.autoplay) {
player.loadVideoById(options);
} else {
player.cueVideoById(options);
}
},
updateTime() {
const player = this.get('player');
if (player && player.getDuration && player.getCurrentTime) {
this.set('currentTime', player.getCurrentTime());
this.set('duration', player.getDuration());
}
},
startTimer() {
// stop any previously started timer we forgot to clear
this.stopTimer();
// set initial time by getting the computed properties
this.updateTime();
// and also once every second so the progressbar is up to date
let timer = window.setInterval(() => {
this.updateTime();
}, 1000);
// save the timer so we can stop it later
this.set('timer', timer);
},
stopTimer() {
window.clearInterval(this.get('timer'));
},
// A wrapper around the YouTube method to get current time.
currentTime: computed({
get() {
let player = this.get('player');
let value = player ? player.getCurrentTime() : 0;
return value;
},
set(key, value) {
return value;
}
}),
// A wrapper around the YouTube method to get the duration.
duration: computed({
get() {
let player = this.get('player');
let value = player ? player.getDuration() : 0;
return value;
},
set(key, value) {
return value;
}
}),
// A wrapper around the YouTube method to get and set volume.
volume: computed({
get() {
let player = this.get('player');
let value = player ? player.getVolume() : 0;
return value;
},
set(name, vol) {
let player = this.get('player');
// Clamp between 0 and 100
if (vol > 100) {
vol = 100;
} else if (vol < 0) {
vol = 0;
}
if (player) {
player.setVolume(vol);
}
return vol;
}
}),
// OK, this is stupid but couldn't access the "event" inside
// an ember action so here's a manual click handler instead.
addProgressBarClickHandler() {
this.element.addEventListener(
"click",
this.progressBarClick.bind(this),
false
);
},
progressBarClick(event) {
let self = this;
let element = event.srcElement;
if (element.tagName.toLowerCase() !== "progress") return;
// get the x position of the click inside our progress el
let x = event.pageX - element.getBoundingClientRect().x;
// convert it to a value relative to the duration (max)
let clickedValue = (x * element.max) / element.offsetWidth;
// 250 = 0.25 seconds into player
self.set("currentTime", clickedValue);
self.send("seekTo", clickedValue);
},
removeProgressBarClickHandler() {
this.element.removeEventListener(
"click",
this.progressBarClick.bind(this),
false
);
},
actions: {
play() {
if (this.get('player')) {
this.get('player').playVideo();
}
},
pause() {
if (this.get('player')) {
this.get('player').pauseVideo();
}
},
togglePlay() {
if (this.get('player') && this.get('isPlaying')) {
this.send('pause');
} else {
this.send('play');
}
},
mute() {
if (this.get('player')) {
this.get('player').mute();
this.set('isMuted', true);
}
},
unMute() {
if (this.get('player')) {
this.get('player').unMute();
this.set('isMuted', false);
}
},
toggleVolume() {
if (this.get('player').isMuted()) {
this.send('unMute');
} else {
this.send('mute');
}
},
seekTo(seconds) {
if (this.get('player')) {
this.get('player').seekTo(seconds);
}
},
// YouTube events.
ready() {},
ended() {},
playing() {
this.startTimer();
},
paused() {
this.stopTimer();
},
buffering() {},
queued() {}
}
});