-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathvimeo.js
More file actions
291 lines (240 loc) · 8.3 KB
/
vimeo.js
File metadata and controls
291 lines (240 loc) · 8.3 KB
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
/* global Vimeo */
import $ from 'jquery';
function addVimeoFunctions(AblePlayer) {
AblePlayer.prototype.initVimeoPlayer = function () {
var thisObj, deferred, promise, containerId, vimeoId, options;
thisObj = this;
deferred = new this.defer();
promise = deferred.promise();
containerId = this.mediaId + '_vimeo';
// add container to which Vimeo player iframe will be appended
this.$mediaContainer.prepend($('<div>').attr('id', containerId));
// if a described version is available && user prefers description
// init player using the described version
vimeoId = (this.vimeoDescId && this.prefDesc) ? this.vimeoDescId : this.vimeoId;
this.activeVimeoId = vimeoId;
// Notes re. Vimeo Embed Options:
// If a video is owned by a user with a paid Plus, PRO, or Business account,
// setting the "controls" option to "false" will hide the default controls, without hiding captions.
// This is a new option from Vimeo; previously used "background:true" to hide the controller,
// but that had unwanted side effects:
// - In addition to hiding the controls, it also hides captions
// - It automatically autoplays (initializing the player with autoplay:false does not override this)
// - It automatically loops (but this can be overridden by initializing the player with loop:false)
// - It automatically sets volume to 0 (not sure if this can be overridden, since no longer using the background option)
if (this.playerWidth) {
if (this.vimeoUrlHasParams) {
// use url param, not id
options = {
url: vimeoId,
width: this.playerWidth,
controls: false
}
} else {
options = {
id: vimeoId,
width: this.playerWidth,
controls: false
}
}
} else {
// initialize without width & set width later
if (this.vimeoUrlHasParams) {
options = {
url: vimeoId,
controls: false
}
} else {
options = {
id: vimeoId,
controls: false
}
}
}
this.vimeoPlayer = new Vimeo.Player(containerId, options);
this.vimeoPlayer.ready().then(function() {
// add tabindex -1 on iframe so vimeo frame cannot be focused on
$('#'+containerId).children('iframe').attr({
'tabindex': '-1',
'aria-hidden': true
});
// get video's intrinsic size and initiate player dimensions
thisObj.vimeoPlayer.getVideoWidth().then(function(width) {
if (width) {
// also get height
thisObj.vimeoPlayer.getVideoHeight().then(function(height) {
if (height) {
thisObj.resizePlayer(width,height);
}
});
}
}).catch(function(error) {
// an error occurred getting height or width
// TODO: Test this to see how gracefully it organically recovers
});
if (!thisObj.hasPlaylist) {
// remove the media element, since Vimeo replaces that with its own element in an iframe
// this is handled differently for playlists. See buildplayer.js > cuePlaylistItem()
thisObj.$media.remove();
// define variables that will impact player setup
// vimeoSupportsPlaybackRateChange
// changing playbackRate is only supported if the video is hosted on a Pro or Business account
// unfortunately there is no direct way to query for that information.
// this.vimeoPlayer.getPlaybackRate() returns a value, regardless of account type
// This is a hack:
// Attempt to change the playbackRate. If it results in an error, assume changing playbackRate is not supported.
// Supported playbackRate values are 0.5 to 2.
thisObj.vimeoPlaybackRate = 1;
thisObj.vimeoPlayer.setPlaybackRate(thisObj.vimeoPlaybackRate).then(function(playbackRate) {
// playback rate was set
thisObj.vimeoSupportsPlaybackRateChange = true;
}).catch(function(error) {
thisObj.vimeoSupportsPlaybackRateChange = false;
});
deferred.resolve();
}
});
return promise;
};
AblePlayer.prototype.getVimeoPaused = function () {
var deferred, promise;
deferred = new this.defer();
promise = deferred.promise();
this.vimeoPlayer.getPaused().then(function (paused) {
// paused is Boolean
deferred.resolve(paused);
});
return promise;
}
AblePlayer.prototype.getVimeoEnded = function () {
var deferred, promise;
deferred = new this.defer();
promise = deferred.promise();
this.vimeoPlayer.getEnded().then(function (ended) {
// ended is Boolean
deferred.resolve(ended);
});
return promise;
}
AblePlayer.prototype.getVimeoState = function () {
var deferred, promise, promises, gettingPausedPromise, gettingEndedPromise;
deferred = new this.defer();
promise = deferred.promise();
promises = [];
gettingPausedPromise = this.vimeoPlayer.getPaused();
gettingEndedPromise = this.vimeoPlayer.getEnded();
promises.push(gettingPausedPromise);
promises.push(gettingEndedPromise);
gettingPausedPromise.then(function (paused) {
deferred.resolve(paused);
});
gettingEndedPromise.then(function (ended) {
deferred.resolve(ended);
});
$.when.apply($, promises).then(function () {
deferred.resolve();
});
return promise;
}
AblePlayer.prototype.getVimeoCaptionTracks = function () {
// get data via Vimeo Player API, and push data to this.captions
// Note: Vimeo doesn't expose the caption cues themselves
// so this.captions will only include metadata about caption tracks; not cues
var deferred = new this.defer();
var promise = deferred.promise();
var thisObj, i, isDefaultTrack;
thisObj = this;
this.vimeoPlayer.getTextTracks().then(function(tracks) {
// each Vimeo track includes the following:
// label (local name of the language)
// language (2-character code)
// kind (captions or subtitles, as declared by video owner)
// mode ('disabled' or 'showing')
if (tracks.length) {
// create a new button for each caption track
for (i=0; i<tracks.length; i++) {
thisObj.hasCaptions = true;
if (thisObj.prefCaptions === 1) {
thisObj.captionsOn = true;
} else {
thisObj.captionsOn = false;
}
// assign the default track based on language of the player
if (tracks[i]['language'] === thisObj.lang) {
isDefaultTrack = true;
} else {
isDefaultTrack = false;
}
thisObj.tracks.push({
'kind': tracks[i]['kind'],
'language': tracks[i]['language'],
'label': tracks[i]['label'],
'def': isDefaultTrack
});
}
thisObj.captions = thisObj.tracks;
thisObj.hasCaptions = true;
// setupPopups again with new captions array, replacing original
thisObj.setupPopups('captions');
deferred.resolve();
} else {
thisObj.hasCaptions = false;
thisObj.usingVimeoCaptions = false;
deferred.resolve();
}
});
return promise;
};
AblePlayer.prototype.getVimeoPosterUrl = function (vimeoId) {
const thisObj = this;
// Vimeo Oembed only returns a 640px width image. Hope at some point there's an alternative.
var url = 'http://vimeo.com/api/oembed.json?url=https://vimeo.com/' + vimeoId, imageUrl = '';
console.log( url );
fetch( url ).then( response => {
return response.json();
})
.then( json => {
imageUrl = json.thumbnail_url;
})
.catch( error => {
if (thisObj.debug) {
console.log( 'Vimeo API query: ' + error );
}
});
return imageUrl;
};
AblePlayer.prototype.getVimeoId = function (url) {
// return a Vimeo ID, extracted from a full Vimeo URL
// Supported URL patterns are anything containing 'vimeo.com'
// and ending with a '/' followed by the ID.
// (Vimeo IDs do not have predicatable lengths)
// Update: If URL contains parameters, return the full url
// This will need to be passed to the Vimeo Player API
// as a url parameter, not as an id parameter
this.vimeoUrlHasParams = false;
let urlObject;
if (typeof url === 'number') {
// this is likely already a vimeo ID
return url;
} else {
urlObject = new URL(url);
}
if ( 'vimeo.com' === urlObject.hostname || 'player.vimeo.com' === urlObject.hostname ) {
// this is a full Vimeo URL
if ( '' !== urlObject.search ) {
// URL contains parameters
this.vimeoUrlHasParams = true;
return url;
} else {
if ( 'player.vimeo.com' === urlObject.hostname ) {
return urlObject.pathname.replace( '/video/', '' );
} else {
return urlObject.pathname.replace( '/', '' );
}
}
} else {
return url;
}
};
}
export default addVimeoFunctions;