-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
PlaybackControls.tsx
309 lines (268 loc) · 7.67 KB
/
PlaybackControls.tsx
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
import React, { Component } from 'react'
import cx from 'classnames'
import styles from './PlaybackControls.css'
import { PlaybackState, IMediaPlayerState, RepeatMode } from 'lobby/reducers/mediaPlayer'
import { connect } from 'react-redux'
import {
server_requestPlayPause,
server_requestNextMedia,
server_requestSeek,
server_requestRepeatMedia
} from 'lobby/actions/mediaPlayer'
import { hasPlaybackPermissions } from 'lobby/reducers/mediaPlayer.helpers'
import { parseCuePoints, copyMediaLink, openMediaInBrowser } from 'media/utils'
import { setVolume, setMute } from 'actions/settings'
import { IAppState } from 'reducers'
// Must include Slider first for CSS precedence order
import { Slider } from 'components/media/Slider'
Slider.name // don't strip unused import
import { VolumeSlider } from 'components/media/VolumeSlider'
import { Timeline } from 'components/media/Timeline'
import { MoreButton } from 'components/media/MoreButton'
import { IconButton } from 'components/common/button'
import { t } from 'locale'
import { IReactReduxProps } from 'types/redux-thunk'
import { setPopupPlayer } from 'actions/ui'
import { PopupWindow } from 'components/Popup'
const Button: React.SFC<{
className?: string
icon: string
title?: string
/** Highlight button as turned on */
enabled?: boolean
/** Disable button interaction */
disabled?: boolean
onClick?: React.MouseEventHandler<HTMLButtonElement>
}> = props => {
return (
<IconButton
icon={props.icon}
disabled={props.disabled}
className={cx(props.className, styles.button, {
[styles.buttonEnabled]: props.enabled
})}
title={props.title}
onClick={props.onClick}
>
{props.children}
</IconButton>
)
}
const ButtonListItem: React.SFC<{
icon: string
title?: string
onClick?: React.MouseEventHandler<HTMLButtonElement>
}> = props => {
return (
<IconButton
icon={props.icon}
className={styles.buttonListItem}
title={props.title}
onClick={props.onClick}
>
{props.children}
</IconButton>
)
}
interface IProps {
className?: string
reload?: React.MouseEventHandler<HTMLButtonElement>
openBrowser: (url?: string) => void
showInfo: Function
showInteract: Function
toggleChat: Function
openSettings: Function
}
interface IConnectedProps extends IMediaPlayerState {
mute: boolean
volume: number
/** Has permission to change playback state */
dj: boolean
queueLocked: boolean
}
const mapStateToProps = (state: IAppState): IConnectedProps => {
return {
...state.mediaPlayer,
mute: state.settings.mute,
volume: state.settings.volume,
dj: hasPlaybackPermissions(state),
queueLocked: state.mediaPlayer.queueLocked
}
}
type PrivateProps = IProps & IConnectedProps & IReactReduxProps
class _PlaybackControls extends Component<PrivateProps> {
private getCuePoints() {
const { current: media } = this.props
if (media) {
let cuePoints = parseCuePoints(media)
return cuePoints
}
}
render(): JSX.Element | null {
const {
current: media,
playback,
startTime,
pauseTime,
dj,
queueLocked,
repeatMode
} = this.props
const playbackIcon = playback === PlaybackState.Playing ? 'pause' : 'play'
const isIdle = playback === PlaybackState.Idle
const isPaused = playback === PlaybackState.Paused
const duration = (media && media.duration) || 0
const isTimed = duration > 0
const isAddAllowed = dj || !queueLocked
const addMediaBtn = (
<Button
className={styles.addMediaButton}
icon="plus"
onClick={e => {
this.props.openBrowser()
}}
>
{t('addMedia')}
</Button>
)
const permTitle = t('requiresDJPermissions')
const playPauseBtn = (
<Button
key="playpause"
icon={playbackIcon}
title={dj ? undefined : permTitle}
disabled={isIdle}
onClick={this.playPause}
/>
)
const nextBtn = (
<Button
key="next"
icon="skip-forward"
title={dj ? t('next') : permTitle}
disabled={isIdle}
onClick={this.next}
/>
)
const repeatBtn = (
<Button
icon={repeatMode === RepeatMode.One ? 'repeat-one' : 'repeat'}
title={dj ? t('repeat') : permTitle}
enabled={repeatMode !== RepeatMode.Off}
disabled={isIdle}
onClick={this.repeat}
/>
)
const timeline =
isIdle || !isTimed ? (
<span className={styles.spacer} />
) : (
<Timeline
className={styles.spacer}
time={(isPaused ? pauseTime : startTime! + this.props.serverClockSkew) || 0}
paused={isPaused}
duration={media && media.duration}
onSeek={this.seek}
cuePoints={this.getCuePoints()}
/>
)
const volumeSlider = (
<VolumeSlider
mute={this.props.mute}
volume={this.props.volume}
onChange={this.setVolume}
onMute={this.toggleMute}
/>
)
const infoBtn = media && media.description && (
<Button icon="info" title={t('info')} onClick={() => this.props.showInfo()} />
)
const settingsBtn = (
<Button icon="settings" title={t('settings')} onClick={() => this.props.openSettings()} />
)
return (
<div className={cx(this.props.className, styles.container)}>
{isIdle && isAddAllowed ? addMediaBtn : [playPauseBtn, nextBtn]}
{repeatBtn}
{timeline}
{volumeSlider}
{infoBtn}
{settingsBtn}
{this.renderMenu()}
</div>
)
}
private renderMenu() {
const { current: media } = this.props
const mediaButtons = media && (
<>
<hr className={styles.menuDivider} />
{FEATURE_POPUP_PLAYER ? (
<ButtonListItem
icon="external-link"
onClick={() => {
PopupWindow.focus()
this.props.dispatch(setPopupPlayer(true))
}}
>
Open in popup
</ButtonListItem>
) : null}
<ButtonListItem icon="external-link" onClick={this.openLink}>
{t('openInBrowser')}
</ButtonListItem>
<ButtonListItem icon="clipboard" onClick={this.copyLink}>
{t('copyLink')}
</ButtonListItem>
<ButtonListItem icon="rotate-cw" onClick={this.props.reload}>
{t('reload')}
</ButtonListItem>
</>
)
return (
<MoreButton buttonClassName={styles.button}>
<ButtonListItem icon="message-circle" onClick={() => this.props.toggleChat()}>
{t('chat')}
</ButtonListItem>
<ButtonListItem icon="mouse-pointer" onClick={() => this.props.showInteract()}>
{t('interact')}
</ButtonListItem>
{mediaButtons}
</MoreButton>
)
}
private playPause = () => {
this.props.dispatch!(server_requestPlayPause())
}
private next = () => {
this.props.dispatch!(server_requestNextMedia())
}
private repeat = () => {
this.props.dispatch!(server_requestRepeatMedia())
}
private seek = (time: number) => {
this.props.dispatch!(server_requestSeek(time))
}
private setVolume = (volume: number) => {
this.props.dispatch!(setVolume(volume))
}
private toggleMute = () => {
const mute = !this.props.mute
this.props.dispatch!(setMute(mute))
}
private openLink = () => {
const { current: media } = this.props
if (media) {
openMediaInBrowser(media)
}
}
private copyLink = () => {
const { current: media } = this.props
if (media) {
copyMediaLink(media)
}
}
}
export const PlaybackControls = connect(mapStateToProps)(_PlaybackControls) as React.ComponentClass<
IProps
>