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
581 lines (480 loc) · 15.3 KB
/
index.js
File metadata and controls
581 lines (480 loc) · 15.3 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* global document */
// TODO: This file is too too large.
import React, { Component, PropTypes } from 'react';
import styled from 'styled-components';
import * as customPropTypes from 'customPropTypes';
import { connect } from 'react-redux';
import { camelize } from 'humps';
import Loadable from 'react-loadable';
import LocaleFormattedMessage from 'components/LocaleFormattedMessage';
// Helpers
import debug from 'helpers/debug';
import scroller from 'utils/scroller';
// Redux
import * as AudioActions from 'redux/actions/audioplayer';
import ComponentLoader from 'components/ComponentLoader';
import Track from './Track';
import Segments from './Segments';
import ScrollButton from './ScrollButton';
const style = require('./style.scss');
const RepeatDropdown = Loadable({
loader: () =>
import(/* webpackChunkName: "repeatdropdown" */ './RepeatDropdown'),
LoadingComponent: ComponentLoader
});
const Wrapper = styled.div`
width: 100%;
position: absolute;
top: 0;
left: 0;
height: 10%;
`;
const ControlItem = styled.li`
vertical-align: middle;
padding-right: 20px;
color: #939598;
`;
export class Audioplayer extends Component {
state = {
loadingFile: false
};
componentDidMount() {
const { currentFile, currentVerse, audio, verses, load } = this.props; // eslint-disable-line no-shadow, max-len
const nextVerse = verses[this.getNext()];
debug('component:Audioplayer', 'componentDidMount');
if (currentFile) {
return this.handleAddFileListeners(currentFile);
}
load({
chapterId: currentVerse.chapterId,
verseId: currentVerse.id,
verseKey: currentVerse.verseKey,
audio
});
if (nextVerse) {
load({
chapterId: nextVerse.chapterId,
verseId: nextVerse.id,
verseKey: nextVerse.verseKey,
audio
});
}
return false;
}
componentWillReceiveProps(nextProps) {
// Make sure we have a current ayah to mount it to Audio
if (!this.props.currentVerse && !nextProps.currentFile) {
return false;
}
// First load
if (this.props.currentFile !== nextProps.currentFile) {
if (this.props.currentFile) {
this.handleRemoveFileListeners(this.props.currentFile);
}
return this.handleAddFileListeners(nextProps.currentFile);
}
// Change verse
if (this.props.currentVerse.verseKey !== nextProps.currentVerse.verseKey) {
if (this.props.currentFile) {
this.handleRemoveFileListeners(this.props.currentFile);
}
return this.handleAddFileListeners(nextProps.currentFile);
}
if (this.props.audio !== nextProps.audio) {
Object.keys(this.props.files).forEach(key =>
this.props.load({
chapterId: this.props.verses[key].chapterId,
verseId: this.props.verses[key].id,
verseKey: this.props.verses[key].verseKey,
audio: nextProps.audio
})
);
}
return false;
}
componentDidUpdate(previousProps) {
const {
currentFile,
isPlaying,
verses,
audio,
currentVerse,
load
} = this.props;
if (
currentVerse.verseKey !== previousProps.currentVerse.verseKey &&
verses[this.getNext()]
) {
const verse = verses[this.getNext()];
load({
chapterId: verse.chapterId,
verseId: verse.id,
verseKey: verse.verseKey,
audio
});
}
if (!currentFile) return false;
if (isPlaying) {
const playPromise = currentFile.play();
// Catch/silence error when a pause interrupts a play request
// on browsers which return a promise
if (playPromise !== undefined && typeof playPromise.then === 'function') {
playPromise.then(null, () => {});
}
} else {
currentFile.pause();
}
return false;
}
componentWillUnmount() {
const { files, currentFile } = this.props;
debug('component:Audioplayer', 'componentWillUnmount');
if (files[currentFile]) {
return this.handleRemoveFileListeners(files[currentFile]);
}
return false;
}
getPrevious() {
const { currentVerse, verseIds } = this.props;
const index = verseIds.findIndex(id => id === currentVerse.verseKey);
return verseIds[index - 1];
}
getNext() {
const { currentVerse, chapter, onLoadAyahs, verseIds } = this.props;
const ayahNum = currentVerse.verseKey.split(':')[1];
const index = verseIds.findIndex(id => id === currentVerse.verseKey);
if (chapter.versesCount === ayahNum + 1) {
// We are at the end of the chapter!
return false;
}
if (verseIds.length - 3 <= index + 1) {
// Need to load the next set of ayahs!
onLoadAyahs();
}
return verseIds[index + 1];
}
handleAyahChange = (direction = 'next') => {
const { isPlaying, play, pause, currentVerse } = this.props; // eslint-disable-line no-shadow, max-len
const previouslyPlaying = isPlaying;
if (isPlaying) pause();
const nextVerse = this[camelize(`get_${direction}`)]();
if (!nextVerse) return pause();
this.props[direction](currentVerse.verseKey);
this.handleScrollTo(nextVerse);
this.preloadNext();
if (previouslyPlaying) play();
return false;
};
scrollToVerse = (ayahNum = this.props.currentVerse.verseKey) => {
scroller.scrollTo(`verse:${ayahNum}`, -45);
};
handleScrollTo = (ayahNum) => {
const { shouldScroll } = this.props;
if (shouldScroll) {
this.scrollToVerse(ayahNum);
}
};
play = () => {
this.handleScrollTo();
this.props.play();
this.preloadNext();
};
preloadNext() {
const { currentVerse, files } = this.props;
const ayahIds = Object.keys(files);
const index = ayahIds.findIndex(id => id === currentVerse.verseKey) + 1;
for (let id = index; id <= index + 2; id += 1) {
if (ayahIds[id]) {
const verseKey = ayahIds[id];
if (files[verseKey]) {
files[verseKey].setAttribute('preload', 'auto');
}
}
}
}
handleRepeat = (file) => {
const {
repeat,
currentVerse,
setRepeat, // eslint-disable-line no-shadow
setAyah // eslint-disable-line no-shadow
} = this.props;
const [chapter, ayah] = currentVerse.verseKey
.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(`${chapter}:${repeat.from}`);
return this.play();
}
}
return false;
};
handleScrollToggle = (event) => {
event.preventDefault();
const { shouldScroll, currentVerse } = this.props;
if (!shouldScroll) {
// we use the inverse (!) here because we're toggling, so false is true
this.scrollToVerse(currentVerse.verseKey);
}
this.props.toggleScroll();
};
handleAddFileListeners(file) {
// NOTE: if no file, just wait.
if (!file) return false;
const { update } = 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 = 0; // eslint-disable-line
// 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
};
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
return file;
}
handleRemoveFileListeners = (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
return (
<a
tabIndex="-1"
className={`pointer text-center ${style.playingButton} ${style.buttons}`}
onClick={isPlaying ? pause : this.play}
>
<i className={`ss-icon ${isPlaying ? 'ss-pause' : 'ss-play'}`} />
</a>
);
}
renderPreviousButton() {
const { currentVerse, files } = this.props;
if (!files) return false;
const index = Object.keys(files).findIndex(
id => id === currentVerse.verseKey
);
return (
<a
tabIndex="-1"
className={`pointer ${style.buttons} ${!index ? style.disabled : ''}`}
onClick={() => index && this.handleAyahChange('previous')}
>
<i className="ss-icon ss-skipback" />
</a>
);
}
renderNextButton() {
const { chapter, currentVerse } = this.props;
if (!chapter) return false;
const isEnd =
chapter.versesCount === parseInt(currentVerse.verseKey.split(':')[1], 10);
return (
<a
tabIndex="-1"
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,
segments,
isLoading,
currentVerse,
currentFile,
currentTime,
duration,
chapter,
isPlaying,
repeat, // eslint-disable-line no-shadow
shouldScroll, // eslint-disable-line no-shadow
setRepeat // eslint-disable-line no-shadow
} = this.props;
if (isLoading || !currentFile) {
return (
<li className={`${style.container} ${className}`}>
<div>
<LocaleFormattedMessage
id="app.loading"
defaultMessage="Loading..."
/>
</div>
</li>
);
}
return (
<div
className={`${isPlaying &&
style.isPlaying} ${style.container} ${className}`}
>
<Wrapper>
{currentFile && (
<Track
progress={currentTime / duration * 100}
onTrackChange={this.handleTrackChange}
/>
)}
{segments &&
segments[currentVerse.verseKey] && (
<Segments
segments={segments[currentVerse.verseKey]}
currentVerse={currentVerse.verseKey}
currentTime={currentTime}
/>
)}
</Wrapper>
<ul className="list-inline" style={{ margin: 0 }}>
<ControlItem>
<LocaleFormattedMessage
id="player.currentVerse"
defaultMessage="Ayah"
/>
: {currentVerse.verseKey.split(':')[1]}
</ControlItem>
<ControlItem>{this.renderPreviousButton()}</ControlItem>
<ControlItem>{this.renderPlayStopButtons()}</ControlItem>
<ControlItem>{this.renderNextButton()}</ControlItem>
<ControlItem>
<RepeatDropdown
repeat={repeat}
setRepeat={setRepeat}
current={parseInt(currentVerse.verseKey.split(':')[1], 10)}
chapter={chapter}
/>
</ControlItem>
<ControlItem>
<ScrollButton
shouldScroll={shouldScroll}
onScrollToggle={this.handleScrollToggle}
/>
</ControlItem>
</ul>
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
const files = state.audioplayer.files[ownProps.chapter.id];
const verseIds = Object.keys(ownProps.verses);
return {
files,
verseIds,
segments: state.audioplayer.segments[ownProps.chapter.id],
currentFile: files[ownProps.currentVerse.verseKey],
chapterId: ownProps.chapter.id,
isPlaying: state.audioplayer.isPlaying,
isLoading: state.audioplayer.isLoading,
repeat: state.audioplayer.repeat,
shouldScroll: state.audioplayer.shouldScroll,
duration: state.audioplayer.duration,
currentTime: state.audioplayer.currentTime,
audio: state.options.audio
};
};
Audioplayer.propTypes = {
className: PropTypes.string,
chapter: customPropTypes.surahType,
onLoadAyahs: PropTypes.func.isRequired,
segments: customPropTypes.segments,
// NOTE: should be PropTypes.instanceOf(Audio) but not on server.
files: PropTypes.object, // eslint-disable-line
currentVerse: PropTypes.verseType,
isLoading: PropTypes.bool.isRequired,
play: PropTypes.func.isRequired,
pause: PropTypes.func.isRequired,
next: PropTypes.func.isRequired, // eslint-disable-line
previous: PropTypes.func.isRequired, // eslint-disable-line
update: PropTypes.func.isRequired,
repeat: customPropTypes.timeInterval.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,
load: PropTypes.func.isRequired,
// NOTE: should be PropTypes.instanceOf(Audio) but not on server.
currentFile: PropTypes.any, // eslint-disable-line
audio: PropTypes.number.isRequired,
verses: customPropTypes.verses,
verseIds: PropTypes.object // eslint-disable-line
};
export default connect(mapStateToProps, AudioActions)(Audioplayer);