-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
121 lines (96 loc) · 1.86 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
118
119
120
121
/**
* Module dependencies
*/
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var loadAPI = require('./lib/load-api');
var prepareEmbed = require('./lib/prepare-embed');
var sdk;
/**
* Expose `Vimeo`
*/
module.exports = Vimeo;
/**
* Create new `Vimeo` controller
*
* @param {String} id of embedded video
*/
function Vimeo(id) {
sdk = loadAPI();
prepareEmbed(id);
this.attachToEmbed(id);
}
/**
* Mixin events
*/
util.inherits(Vimeo, EventEmitter);
/**
* Play the video.
*
* @api public
*/
Vimeo.prototype.play = function() {
this.player.api('play');
};
/**
* Pause the video.
*
* @api public
*/
Vimeo.prototype.pause = function() {
this.player.api('pause');
};
/**
* Remove all event handlers and free up internal player for
* garbage collection.
*
* @api public
*/
Vimeo.prototype.destroy = function() {
this.unbindEvents();
delete this.player;
};
/**
* Attach a controller to the embedded video
*
* @param {String} id of embedded video
* @api private
*/
Vimeo.prototype.attachToEmbed = function(id) {
var self = this;
sdk(function(err, Froogaloop) {
self.player = window.Froogaloop(id);
self.bindEvents();
});
};
/**
* Bind player events
*
* @api private
*/
Vimeo.prototype.bindEvents = function() {
var self = this;
self.player.addEvent('ready', function() {
self.emit('ready');
self.player.addEvent('play', function() {
self.emit('play');
});
self.player.addEvent('pause', function() {
self.emit('pause');
});
self.player.addEvent('finish', function() {
self.emit('end');
});
});
};
/**
* Unbind all player events
*
* @api private
*/
Vimeo.prototype.unbindEvents = function() {
this.player.removeEvent('ready');
this.player.removeEvent('play');
this.player.removeEvent('pause');
this.player.removeEvent('finish');
};