-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathindex.js
376 lines (338 loc) · 8.98 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
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
import React from 'react';
import PropTypes from 'prop-types';
import Player from '@vimeo/player';
import eventNames from './eventNames';
class Vimeo extends React.Component {
constructor(props) {
super(props);
this.refContainer = this.refContainer.bind(this);
}
componentDidMount() {
this.createPlayer();
}
componentDidUpdate(prevProps) {
// eslint-disable-next-line react/destructuring-assignment
const changes = Object.keys(this.props).filter((name) => this.props[name] !== prevProps[name]);
this.updateProps(changes);
}
componentWillUnmount() {
this.player.destroy();
}
/**
* @private
*/
getInitialOptions() {
/* eslint-disable react/destructuring-assignment */
return {
id: this.props.video,
width: this.props.width,
height: this.props.height,
autopause: this.props.autopause,
autoplay: this.props.autoplay,
byline: this.props.showByline,
color: this.props.color,
controls: this.props.controls,
loop: this.props.loop,
portrait: this.props.showPortrait,
title: this.props.showTitle,
muted: this.props.muted,
background: this.props.background,
responsive: this.props.responsive,
dnt: this.props.dnt,
};
/* eslint-enable react/destructuring-assignment */
}
/**
* @private
*/
updateProps(propNames) {
const { player } = this;
propNames.forEach((name) => {
// eslint-disable-next-line react/destructuring-assignment
const value = this.props[name];
switch (name) {
case 'autopause':
player.setAutopause(value);
break;
case 'color':
player.setColor(value);
break;
case 'loop':
player.setLoop(value);
break;
case 'volume':
player.setVolume(value);
break;
case 'paused':
player.getPaused().then((paused) => {
if (value && !paused) {
return player.pause();
}
if (!value && paused) {
return player.play();
}
return null;
});
break;
case 'width':
case 'height':
player.element[name] = value;
break;
case 'video':
if (value) {
const { start } = this.props;
const loaded = player.loadVideo(value);
// Set the start time only when loading a new video.
// It seems like this has to be done after the video has loaded, else it just starts at
// the beginning!
if (typeof start === 'number') {
loaded.then(() => {
player.setCurrentTime(start);
});
}
} else {
player.unload();
}
break;
default:
// Nothing
}
});
}
/**
* @private
*/
createPlayer() {
const { start, volume } = this.props;
this.player = new Player(this.container, this.getInitialOptions());
Object.keys(eventNames).forEach((dmName) => {
const reactName = eventNames[dmName];
this.player.on(dmName, (event) => {
// eslint-disable-next-line react/destructuring-assignment
const handler = this.props[reactName];
if (handler) {
handler(event);
}
});
});
const { onError, onReady } = this.props;
this.player.ready().then(() => {
if (onReady) {
onReady(this.player);
}
}, (err) => {
if (onError) {
onError(err);
} else {
throw err;
}
});
if (typeof start === 'number') {
this.player.setCurrentTime(start);
}
if (typeof volume === 'number') {
this.updateProps(['volume']);
}
}
/**
* @private
*/
refContainer(container) {
this.container = container;
}
render() {
const { id, className, style } = this.props;
return (
<div
id={id}
className={className}
style={style}
ref={this.refContainer}
/>
);
}
}
if (process.env.NODE_ENV !== 'production') {
Vimeo.propTypes = {
/**
* A Vimeo video ID or URL.
*/
video: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
/**
* DOM ID for the player element.
*/
id: PropTypes.string,
/**
* CSS className for the player element.
*/
className: PropTypes.string,
/**
* Inline style for container element.
*/
style: PropTypes.object, // eslint-disable-line react/forbid-prop-types
/**
* Width of the player element.
*/
width: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
/**
* Height of the player element.
*/
height: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
/**
* Pause the video.
*/
paused: PropTypes.bool, // eslint-disable-line react/no-unused-prop-types
/**
* The playback volume as a number between 0 and 1.
*/
volume: PropTypes.number,
/**
* The time in seconds at which to start playing the video.
*/
start: PropTypes.number,
// Player parameters
/**
* Pause this video automatically when another one plays.
*/
autopause: PropTypes.bool,
/**
* Automatically start playback of the video. Note that this won’t work on
* some devices.
*/
autoplay: PropTypes.bool,
/**
* Show the byline on the video.
*/
showByline: PropTypes.bool,
/**
* Specify the color of the video controls. Colors may be overridden by the
* embed settings of the video. _(Ex: "ef2f9f")_
*/
color: PropTypes.string,
/**
* Blocks the player from tracking any session data, including all cookies and analytics.
*/
dnt: PropTypes.bool,
// Player controls
/**
* Hide all elements in the player (play bar, sharing buttons, etc).
*/
controls: PropTypes.bool,
/**
* Play the video again when it reaches the end.
*/
loop: PropTypes.bool,
/**
* Show the portrait on the video.
*/
showPortrait: PropTypes.bool,
/**
* Show the title on the video.
*/
showTitle: PropTypes.bool,
/**
* Starts in a muted state to help with autoplay
*/
muted: PropTypes.bool,
/**
* Starts in a background state with no controls to help with autoplay
*/
background: PropTypes.bool,
/**
* Enable responsive mode and resize according to parent element (experimental)
*/
responsive: PropTypes.bool,
// Events
/* eslint-disable react/no-unused-prop-types */
/**
* Sent when the Vimeo player API has loaded.
* Receives the Vimeo player object in the first parameter.
*/
onReady: PropTypes.func,
/**
* Sent when the player triggers an error.
*/
onError: PropTypes.func,
/**
* Triggered when the video plays.
*/
onPlay: PropTypes.func,
/**
* Triggered when the video pauses.
*/
onPause: PropTypes.func,
/**
* Triggered any time the video playback reaches the end.
* Note: when `loop` is turned on, the ended event will not fire.
*/
onEnd: PropTypes.func,
/**
* Triggered as the `currentTime` of the video updates. It generally fires
* every 250ms, but it may vary depending on the browser.
*/
onTimeUpdate: PropTypes.func,
/**
* Triggered as the video is loaded. Reports back the amount of the video
* that has been buffered.
*/
onProgress: PropTypes.func,
/**
* Triggered when the player seeks to a specific time. An `onTimeUpdate`
* event will also be fired at the same time.
*/
onSeeked: PropTypes.func,
/**
* Triggered when the active text track (captions/subtitles) changes. The
* values will be `null` if text tracks are turned off.
*/
onTextTrackChange: PropTypes.func,
/**
* Triggered when the active cue for the current text track changes. It also
* fires when the active text track changes. There may be multiple cues
* active.
*/
onCueChange: PropTypes.func,
/**
* Triggered when the current time hits a registered cue point.
*/
onCuePoint: PropTypes.func,
/**
* Triggered when the volume in the player changes. Some devices do not
* support setting the volume of the video independently from the system
* volume, so this event will never fire on those devices.
*/
onVolumeChange: PropTypes.func,
/**
* Triggered when the playback rate changes.
*/
onPlaybackRateChange: PropTypes.func,
/**
* Triggered when a new video is loaded in the player.
*/
onLoaded: PropTypes.func,
/* eslint-enable react/no-unused-prop-types */
};
}
Vimeo.defaultProps = {
autopause: true,
autoplay: false,
showByline: true,
controls: true,
loop: false,
showPortrait: true,
showTitle: true,
muted: false,
background: false,
responsive: false,
dnt: false,
};
export default Vimeo;