forked from u-wave/react-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
149 lines (134 loc) · 3.71 KB
/
app.js
File metadata and controls
149 lines (134 loc) · 3.71 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
/* eslint-env browser */
import React from 'react';
import ReactDOM from 'react-dom';
import YouTube from '..';
const videos = [
{ id: 'ZuuVjuLNvFY', name: 'JUNNY - kontra (Feat. Lil Gimch, Keeflow)' },
{ id: 'PYE7jXNjFWw', name: 'T W L V - Follow' },
{ id: 'ld8ugY47cps', name: 'SLCHLD - I can\'t love you anymore' },
{ id: null, name: '<none>' },
];
const qualities = ['auto', '240', '380', '480', '720', '1080', '1440', '2160'];
const hashVideoRx = /^#!\/video\/(\d)$/;
const hash = typeof window.location !== 'undefined'
? window.location.hash : ''; // eslint-disable-line no-undef
const defaultVideo = hashVideoRx.test(hash)
? parseInt(hash.replace(hashVideoRx, '$1'), 10)
: 0;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
videoIndex: defaultVideo,
suggestedQuality: 'auto',
volume: 1,
paused: false,
};
this.handlePause = this.handlePause.bind(this);
this.handlePlayerPause = this.handlePlayerPause.bind(this);
this.handlePlayerPlay = this.handlePlayerPlay.bind(this);
this.handleVolume = this.handleVolume.bind(this);
this.handleQuality = this.handleQuality.bind(this);
}
selectVideo(index) {
this.setState({ videoIndex: index });
}
handlePause(event) {
this.setState({
paused: event.target.checked,
});
}
handlePlayerPause() {
this.setState({ paused: true });
}
handlePlayerPlay() {
this.setState({ paused: false });
}
handleVolume(event) {
this.setState({
volume: parseFloat(event.target.value),
});
}
handleQuality(event) {
this.setState({
suggestedQuality: qualities[event.target.selectedIndex],
});
}
render() {
const {
videoIndex, volume, paused, suggestedQuality,
} = this.state;
const video = videos[videoIndex];
return (
<div className="row">
<div className="col s4">
<h5>
Video
</h5>
<div className="collection">
{videos.map((choice, index) => (
<a
key={choice.id}
href={`#!/video/${index}`}
className={`collection-item ${video === choice ? 'active' : ''}`}
onClick={() => this.selectVideo(index)}
>
{choice.name}
</a>
))}
</div>
<h5>
Paused
</h5>
<p>
<label htmlFor="paused">
<input
type="checkbox"
id="paused"
checked={paused}
onChange={this.handlePause}
/>
<span>Paused</span>
</label>
</p>
<h5>
Volume
</h5>
<input
type="range"
value={volume}
min={0}
max={1}
step={0.01}
onChange={this.handleVolume}
/>
<h5>
Quality
</h5>
<select className="browser-default" onChange={this.handleQuality}>
{qualities.map((quality) => (
<option key={quality} value={quality}>
{quality}
</option>
))}
</select>
</div>
<div className="col s8 center-align">
<YouTube
video={video.id}
width={640}
height={480}
autoplay
controls={false}
suggestedQuality={suggestedQuality}
volume={volume}
paused={paused}
onPause={this.handlePlayerPause}
onPlaying={this.handlePlayerPlay}
/>
</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('example'));