-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathDefaultPlayer.js
162 lines (156 loc) · 5.78 KB
/
DefaultPlayer.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
import React from 'react';
import PropTypes from 'prop-types';
import videoConnect from './../video/video';
import copy from './copy';
import {
setVolume,
showTrack,
toggleTracks,
toggleMute,
togglePause,
setCurrentTime,
toggleFullscreen,
getPercentagePlayed,
getPercentageBuffered
} from './../video/api';
import styles from './DefaultPlayer.css';
import Time from './Time/Time';
import Seek from './Seek/Seek';
import Volume from './Volume/Volume';
import Captions from './Captions/Captions';
import PlayPause from './PlayPause/PlayPause';
import Fullscreen from './Fullscreen/Fullscreen';
import Overlay from './Overlay/Overlay';
const DefaultPlayer = ({
copy,
video,
style,
controls,
children,
className,
onSeekChange,
onVolumeChange,
onVolumeClick,
onCaptionsClick,
onPlayPauseClick,
onFullscreenClick,
onCaptionsItemClick,
...restProps
}) => {
return (
<div className={[
styles.component,
className
].join(' ')}
style={style}>
<video
className={styles.video}
{...restProps}>
{ children }
</video>
<Overlay
onClick={onPlayPauseClick}
{...video} />
{ controls && controls.length && !video.error
? <div className={styles.controls}>
{ controls.map((control, i) => {
switch (control) {
case 'Seek':
return <Seek
key={i}
ariaLabel={copy.seek}
className={styles.seek}
onChange={onSeekChange}
{...video} />;
case 'PlayPause':
return <PlayPause
key={i}
ariaLabelPlay={copy.play}
ariaLabelPause={copy.pause}
onClick={onPlayPauseClick}
{...video} />;
case 'Fullscreen':
return <Fullscreen
key={i}
ariaLabel={copy.fullscreen}
onClick={onFullscreenClick}
{...video} />;
case 'Time':
return <Time
key={i}
{...video} />;
case 'Volume':
return <Volume
key={i}
onClick={onVolumeClick}
onChange={onVolumeChange}
ariaLabelMute={copy.mute}
ariaLabelUnmute={copy.unmute}
ariaLabelVolume={copy.volume}
{...video} />;
case 'Captions':
return video.textTracks && video.textTracks.length
? <Captions
key={i}
onClick={onCaptionsClick}
ariaLabel={copy.captions}
onItemClick={onCaptionsItemClick}
{...video}/>
: null;
default:
return null;
}
}) }
</div>
: null }
</div>
);
};
const controls = ['PlayPause', 'Seek', 'Time', 'Volume', 'Fullscreen', 'Captions'];
DefaultPlayer.defaultProps = {
copy,
controls,
video: {}
};
DefaultPlayer.propTypes = {
copy: PropTypes.object.isRequired,
controls: PropTypes.arrayOf(PropTypes.oneOf(controls)),
video: PropTypes.object.isRequired
};
const connectedPlayer = videoConnect(
DefaultPlayer,
({ networkState, readyState, error, ...restState }) => ({
video: {
readyState,
networkState,
error: error || networkState === 3,
// TODO: This is not pretty. Doing device detection to remove
// spinner on iOS devices for a quick and dirty win. We should see if
// we can use the same readyState check safely across all browsers.
loading: readyState < (/iPad|iPhone|iPod/.test(navigator.userAgent) ? 1 : 4),
percentagePlayed: getPercentagePlayed(restState),
percentageBuffered: getPercentageBuffered(restState),
...restState
}
}),
(videoEl, state) => ({
onFullscreenClick: () => toggleFullscreen(videoEl.parentElement),
onVolumeClick: () => toggleMute(videoEl, state),
onCaptionsClick: () => toggleTracks(state),
onPlayPauseClick: () => togglePause(videoEl, state),
onCaptionsItemClick: (track) => showTrack(state, track),
onVolumeChange: (e) => setVolume(videoEl, state, e.target.value),
onSeekChange: (e) => setCurrentTime(videoEl, state, e.target.value * state.duration / 100)
})
);
export {
connectedPlayer as default,
DefaultPlayer,
Time,
Seek,
Volume,
Captions,
PlayPause,
Fullscreen,
Overlay
};