This repository was archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathindex.js
More file actions
471 lines (380 loc) · 12.7 KB
/
index.js
File metadata and controls
471 lines (380 loc) · 12.7 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { camelize } from 'humps';
// Redux
import * as AudioActions from '../../redux/actions/audioplayer';
// Components
import Track from './Track';
import Segments from './Segments';
import ScrollButton from './ScrollButton';
import RepeatDropdown from './RepeatDropdown';
// Helpers
import debug from '../../helpers/debug';
import scroller from '../../utils/scroller';
const style = require('./style.scss');
export class Audioplayer extends Component {
static propTypes = {
className: PropTypes.string,
surah: PropTypes.object.isRequired,
onLoadAyahs: PropTypes.func.isRequired,
segments: PropTypes.object,
files: PropTypes.object,
currentAyah: PropTypes.string,
buildOnClient: PropTypes.func.isRequired,
isLoadedOnClient: PropTypes.bool.isRequired,
isSupported: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired,
play: PropTypes.func.isRequired,
pause: PropTypes.func.isRequired,
next: PropTypes.func.isRequired,
previous: PropTypes.func.isRequired,
update: PropTypes.func.isRequired,
repeat: PropTypes.object.isRequired,
shouldScroll: PropTypes.bool.isRequired,
setRepeat: PropTypes.func.isRequired,
setAyah: PropTypes.func.isRequired,
toggleScroll: PropTypes.func.isRequired,
isPlaying: PropTypes.bool,
currentTime: PropTypes.number,
duration: PropTypes.number,
currentFile: PropTypes.object
};
static defaultProps = {
className: 'col-md-3'
};
componentDidMount() {
const { isLoadedOnClient, buildOnClient, surah, currentFile } = this.props; // eslint-disable-line no-shadow, max-len
debug('component:Audioplayer', 'componentDidMount');
if (!isLoadedOnClient && __CLIENT__) {
debug('component:Audioplayer', 'componentDidMount on client');
return buildOnClient(surah.id);
}
return this.handleAddFileListeners(currentFile);
}
componentWillReceiveProps(nextProps) {
// When you go directly to the surah page, /2, the files are not loaded yet
if (this.props.isLoadedOnClient !== nextProps.isLoadedOnClient) {
return this.handleAddFileListeners(nextProps.currentFile);
}
if (this.props.currentAyah !== nextProps.currentAyah) {
this.handleAddFileListeners(nextProps.currentFile);
if (this.props.currentFile) {
this.handleRemoveFileListeneres(this.props.currentFile);
}
return false;
}
return false;
}
componentWillUnmount() {
const { files, currentFile } = this.props;
debug('component:Audioplayer', 'componentWillUnmount');
if (files[currentFile]) {
return this.handleRemoveFileListeneres(files[currentFile]);
}
return false;
}
getPrevious() {
const { currentAyah, files } = this.props;
const ayahIds = Object.keys(files);
const index = ayahIds.findIndex(id => id === currentAyah);
return ayahIds[index - 1];
}
getNext() {
const { currentAyah, surah, files, onLoadAyahs } = this.props;
const ayahIds = Object.keys(files);
const ayahNum = currentAyah.split(':')[1];
const index = ayahIds.findIndex(id => id === currentAyah);
if (surah.ayat === ayahNum + 1) {
// We are at the end of the surah!
return false;
}
if (ayahIds.length - 3 <= index + 1) {
// Need to load the next set of ayahs!
onLoadAyahs();
}
return ayahIds[index + 1];
}
handleAyahChange = (direction = 'next') => {
const { isPlaying, play, pause, currentAyah } = this.props; // eslint-disable-line no-shadow, max-len
const previouslyPlaying = isPlaying;
if (isPlaying) pause();
if (!this[camelize(`get_${direction}`)]()) return pause();
this.props[direction](currentAyah);
this.handleScrollTo(currentAyah);
this.preloadNext();
if (previouslyPlaying) play();
return false;
}
handleScrollTo = (ayahNum = this.props.currentAyah) => {
const { shouldScroll } = this.props;
if (shouldScroll) {
scroller.scrollTo(`ayah:${ayahNum}`, -150);
}
}
play = () => {
this.handleScrollTo();
this.props.play();
this.preloadNext();
}
preloadNext() {
const { currentAyah, files } = this.props;
const ayahIds = Object.keys(files);
const index = ayahIds.findIndex(id => id === currentAyah) + 1;
for (let id = index; id <= index + 2; id++) {
if (ayahIds[id]) {
const ayahKey = ayahIds[id];
if (files[ayahKey]) {
files[ayahKey].setAttribute('preload', 'auto');
}
}
}
}
handleRepeat = (file) => {
const {
repeat,
currentAyah,
setRepeat, // eslint-disable-line no-shadow
setAyah // eslint-disable-line no-shadow
} = this.props;
const [surah, ayah] = currentAyah.split(':').map(val => parseInt(val, 10));
file.pause();
if (repeat.from > ayah && repeat.to < ayah) {
// user selected a range where current ayah is outside
return this.handleAyahChange();
}
if (repeat.from === repeat.to) {
// user selected single ayah repeat
if (ayah !== repeat.from) return this.handleAyahChange();
if (repeat.times === 1) {
// end of times
setRepeat({});
return this.handleAyahChange();
}
setRepeat({ ...repeat, times: repeat.times - 1 });
file.currentTime = 0; // eslint-disable-line no-param-reassign
return file.play();
}
if (repeat.from !== repeat.to) {
// user selected a range
if (ayah < repeat.to) {
// still in range
return this.handleAyahChange();
}
if (ayah === repeat.to) {
// end of range
if (repeat.times === 1) {
// end of times
setRepeat({});
return this.handleAyahChange();
}
setRepeat({ ...repeat, times: repeat.times - 1 });
setAyah(`${surah}:${repeat.from}`);
return this.play();
}
}
return false;
}
handleScrollToggle = (event) => {
event.preventDefault();
const { shouldScroll, currentAyah } = this.props;
if (!shouldScroll) { // we use the inverse (!) here because we're toggling, so false is true
const elem = document.getElementsByName(`ayah:${currentAyah}`)[0];
if (elem && elem.getBoundingClientRect().top < 0) { // if the ayah is above our scroll offset
scroller.scrollTo(`ayah:${currentAyah}`, -150);
} else {
scroller.scrollTo(`ayah:${currentAyah}`, -80);
}
}
this.props.toggleScroll();
}
handleAddFileListeners(file) {
const { update, currentTime } = this.props; // eslint-disable-line no-shadow
debug('component:Audioplayer', `Attaching listeners to ${file.src}`);
// Preload file
file.setAttribute('preload', 'auto');
const onLoadeddata = () => {
// Default current time to zero. This will change
file.currentTime = ( // eslint-disable-line no-param-reassign
file.currentTime ||
currentTime ||
0
);
return update({
duration: file.duration,
isLoading: false
});
};
const onTimeupdate = () => update({
currentTime: file.currentTime,
duration: file.duration
});
const onEnded = () => {
const { repeat } = this.props;
if (repeat.from) {
return this.handleRepeat(file);
}
if (file.readyState >= 3 && file.paused) {
file.pause();
}
return this.handleAyahChange();
};
const onPlay = () => {
file.ontimeupdate = onTimeupdate; // eslint-disable-line no-param-reassign
};
const onPause = () => {
file.ontimeupdate = null; // eslint-disable-line no-param-reassign
};
const onProgress = () => {
};
file.onloadeddata = onLoadeddata; // eslint-disable-line no-param-reassign
file.onpause = onPause; // eslint-disable-line no-param-reassign
file.onplay = onPlay; // eslint-disable-line no-param-reassign
file.onended = onEnded; // eslint-disable-line no-param-reassign
file.onprogress = onProgress; // eslint-disable-line no-param-reassign
return file;
}
handleRemoveFileListeneres(file) {
file.pause();
file.currentTime = 0; // eslint-disable-line no-param-reassign
file.onloadeddata = null; // eslint-disable-line no-param-reassign
file.ontimeupdate = null; // eslint-disable-line no-param-reassign
file.onplay = null; // eslint-disable-line no-param-reassign
file.onPause = null; // eslint-disable-line no-param-reassign
file.onended = null; // eslint-disable-line no-param-reassign
file.onprogress = null; // eslint-disable-line no-param-reassign
}
handleTrackChange = (fraction) => {
const { currentFile, update } = this.props; // eslint-disable-line no-shadow
update({
currentTime: fraction * currentFile.duration
});
currentFile.currentTime = fraction * currentFile.duration;
}
renderPlayStopButtons() {
const { isPlaying, pause } = this.props; // eslint-disable-line no-shadow
let icon = <i className="ss-icon ss-play" />;
if (isPlaying) {
icon = <i className="ss-icon ss-pause" />;
}
return (
<a className={`pointer ${style.buttons}`} onClick={isPlaying ? pause : this.play}>
{icon}
</a>
);
}
renderPreviousButton() {
const { currentAyah, files } = this.props;
const index = Object.keys(files).findIndex(id => id === currentAyah);
return (
<a
className={`pointer ${style.buttons} ${!index ? style.disabled : ''}`}
onClick={() => index && this.handleAyahChange('previous')}
>
<i className="ss-icon ss-skipback" />
</a>
);
}
renderNextButton() {
const { surah, currentAyah } = this.props;
const isEnd = surah.ayat === parseInt(currentAyah.split(':')[1], 10);
return (
<a
className={`pointer ${style.buttons} ${isEnd ? style.disabled : ''}`}
onClick={() => !isEnd && this.handleAyahChange()}
>
<i className="ss-icon ss-skipforward" />
</a>
);
}
render() {
debug('component:Audioplayer', 'render');
const {
className,
currentFile,
segments,
isLoading,
currentAyah,
currentTime,
isSupported,
duration,
surah,
isLoadedOnClient,
repeat, // eslint-disable-line no-shadow
shouldScroll, // eslint-disable-line no-shadow
setRepeat // eslint-disable-line no-shadow
} = this.props;
if (!isSupported) {
return (
<li className={`${style.container} ${className}`}>
Your browser does not support this audio.
</li>
);
}
if (isLoading) {
return (
<li className={`${style.container} ${className}`}>
<div>Loading...</div>
</li>
);
}
return (
<div className={`${style.padding_left} ${style.container} ${className}`}>
<ul className={style.list}>
<li className={style.verse}>
Playing: {currentAyah.split(':')[1]}
</li>
<li>
{this.renderPreviousButton()}
</li>
<li>
{this.renderPlayStopButtons()}
</li>
<li>
{this.renderNextButton()}
</li>
<li>
<RepeatDropdown
repeat={repeat}
setRepeat={setRepeat}
current={parseInt(currentAyah.split(':')[1], 10)}
surah={surah}
/>
</li>
<li>
<ScrollButton shouldScroll={shouldScroll} onScrollToggle={this.handleScrollToggle} />
</li>
</ul>
<div className={style.wrapper}>
{isLoadedOnClient ?
<Track
progress={currentTime / duration * 100}
onTrackChange={this.handleTrackChange}
/> : null}
{isLoadedOnClient && segments[currentAyah] && typeof segments[currentAyah] !== 'string' ?
<Segments
audio={currentFile}
segments={segments[currentAyah]}
currentAyah={currentAyah}
currentTime={currentTime}
/> : null}
</div>
</div>
);
}
}
const mapStateToProps = (state, ownProps) => ({
files: state.audioplayer.files[ownProps.surah.id],
segments: state.audioplayer.segments[ownProps.surah.id],
currentFile: state.audioplayer.currentFile,
currentAyah: state.audioplayer.currentAyah,
surahId: state.audioplayer.surahId,
isSupported: state.audioplayer.isSupported,
isPlaying: state.audioplayer.isPlaying,
isLoadedOnClient: state.audioplayer.isLoadedOnClient,
isLoading: state.audioplayer.isLoading,
repeat: state.audioplayer.repeat,
shouldScroll: state.audioplayer.shouldScroll,
duration: state.audioplayer.duration,
currentTime: state.audioplayer.currentTime,
});
export default connect(mapStateToProps, AudioActions)(Audioplayer);