Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Audioplayer repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
mmahalwy committed Jul 10, 2016
1 parent 4395405 commit d6f0cd8
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 49 deletions.
213 changes: 180 additions & 33 deletions src/components/Audioplayer/RepeatButton/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,186 @@
import React, { PropTypes } from 'react';
import React, { Component, PropTypes } from 'react';
import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger';
import Tooltip from 'react-bootstrap/lib/Tooltip';
import Popover from 'react-bootstrap/lib/Popover';
import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';

import SwitchToggle from 'components/SwitchToggle';

const style = require('../style.scss');

const RepeatButton = ({ shouldRepeat, onRepeatToggle }) => {
const tooltip = (
<Tooltip id="repeat-button-tooltip">
Repeats the current ayah on end...
</Tooltip>
);

return (
<div className="text-center">
<input type="checkbox" id="repeat" className={style.checkbox} />
<OverlayTrigger
overlay={tooltip}
placement="right"
trigger={['hover', 'focus']}
export default class RepeatButton extends Component {
static propTypes = {
surah: PropTypes.object.isRequired,
repeat: PropTypes.shape({
from: PropTypes.number.isRequired,
to: PropTypes.number.isRequired,
times: PropTypes.number
}).isRequired,
setRepeat: PropTypes.func.isRequired,
current: PropTypes.number.isRequired
};

state = {
nav: 1
};

handleToggle = () => {
const { repeat, setRepeat, current } = this.props;

if (repeat.from) {
return setRepeat({});
}

return setRepeat({
from: current,
to: current
});
}

handleNavChange = (nav) => {
const { setRepeat, current } = this.props;

this.setState({ nav });

if (nav === 1) {
// Should set single ayah
return setRepeat({
from: current,
to: current
});
}

return setRepeat({
from: current,
to: current + 3
});
}

renderRangeAyahs() {
const { surah, repeat, setRepeat } = this.props;
const array = Array(surah.ayat).join().split(',');

return (
<Col md={12} style={{paddingTop: 15}}>
<ul className="list-inline">
<li>
<select
className="form-control"
value={repeat.from}
onChange={(event) => setRepeat({
...repeat,
from: parseInt(event.target.value, 10),
to: parseInt(event.target.value, 10)
})}
>
{
array.map((ayah, index) => (
<option key={index} value={index + 1}>
{index + 1}
</option>
))
}
</select>
</li>
<li> - </li>
<li>
<select
className="form-control"
value={repeat.to}
onChange={(event) => setRepeat({ ...repeat, to: parseInt(event.target.value, 10)})}
>
{
array.map((ayah, index) => (
<option key={index} value={repeat.from ? index + 1 + repeat.from : index + 1}>
{repeat.from ? index + 1 + repeat.from : index + 1}
</option>
))
}
</select>
</li>
</ul>
</Col>
);
}

renderSingleAyah() {
const { current } = this.props;

return (
<Col md={12}>
<h5>
Repeating Ayah: {current}
</h5>
</Col>
);
}

renderOptions() {
const { repeat } = this.props;

return (
<Row className={!repeat.from && style.disabled}>
<Col md={12}>
<Nav
bsStyle="pills"
activeKey={this.state.nav}
onSelect={this.handleNavChange}
>
<NavItem eventKey={1} title="Single Ayah" className={style.pill}>
Single Ayah
</NavItem>
<NavItem eventKey={2} title="Range" className={style.pill}>
Range
</NavItem>
</Nav>
</Col>
</Row>
);
}

render() {
const { repeat } = this.props;

const popover = (
<Popover
id="FontSizeDropdown"
className={style.popover}
title={
<Row>
<Col md={12} className="text-center">
Toggle repeat{' '}
<SwitchToggle
checked={!!repeat.from}
onToggle={this.handleToggle}
id="repeat-toggle"
flat
/>
</Col>
</Row>
}
>
<label
htmlFor="repeat"
className={`pointer ${style.buttons} ${shouldRepeat ? style.repeat : ''}`}
onClick={onRepeatToggle}
{this.renderOptions()}
<Row>
{this.state.nav === 1 ? this.renderSingleAyah() : this.renderRangeAyahs()}
</Row>
</Popover>
);

return (
<div className="text-center">
<OverlayTrigger
overlay={popover}
placement="bottom"
trigger="click"
rootClose
>
<i className="ss-icon ss-repeat" />
</label>
</OverlayTrigger>
</div>
);
};

RepeatButton.propTypes = {
shouldRepeat: PropTypes.bool.isRequired,
onRepeatToggle: PropTypes.func.isRequired
};

export default RepeatButton;
<i
className={`pointer ss-icon ss-repeat ${style.buttons} ${repeat.from && style.repeat}`}
/>
</OverlayTrigger>
</div>
);
}
}
26 changes: 16 additions & 10 deletions src/components/Audioplayer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
stop,
next,
previous,
toggleRepeat,
setRepeat,
toggleScroll,
buildOnClient,
update
Expand Down Expand Up @@ -37,7 +37,7 @@ const style = require('./style.scss');
isPlaying: state.audioplayer.isPlaying,
isLoadedOnClient: state.audioplayer.isLoadedOnClient,
isLoading: state.audioplayer.isLoading,
shouldRepeat: state.audioplayer.shouldRepeat,
repeat: state.audioplayer.repeat,
shouldScroll: state.audioplayer.shouldScroll,
duration: state.audioplayer.duration,
currentTime: state.audioplayer.currentTime,
Expand All @@ -47,7 +47,7 @@ const style = require('./style.scss');
stop,
next,
previous,
toggleRepeat,
setRepeat,
toggleScroll,
buildOnClient,
update
Expand All @@ -70,9 +70,9 @@ export default class Audioplayer extends Component {
next: PropTypes.func.isRequired,
previous: PropTypes.func.isRequired,
update: PropTypes.func.isRequired,
shouldRepeat: PropTypes.bool.isRequired,
repeat: PropTypes.object.isRequired,
shouldScroll: PropTypes.bool.isRequired,
toggleRepeat: PropTypes.func.isRequired,
setRepeat: PropTypes.func.isRequired,
toggleScroll: PropTypes.func.isRequired,
isPlaying: PropTypes.bool,
currentTime: PropTypes.number,
Expand Down Expand Up @@ -249,9 +249,9 @@ export default class Audioplayer extends Component {
});

const onEnded = () => {
const { shouldRepeat } = this.props;
const { repeat } = this.props;

if (shouldRepeat) {
if (repeat) {
file.pause();
file.currentTime = 0; // eslint-disable-line no-param-reassign
return file.play();
Expand Down Expand Up @@ -361,10 +361,11 @@ export default class Audioplayer extends Component {
currentTime,
isSupported,
duration,
surah,
isLoadedOnClient,
shouldRepeat, // eslint-disable-line no-shadow
repeat, // eslint-disable-line no-shadow
shouldScroll, // eslint-disable-line no-shadow
toggleRepeat // eslint-disable-line no-shadow
setRepeat // eslint-disable-line no-shadow
} = this.props;

if (!isSupported) {
Expand Down Expand Up @@ -399,7 +400,12 @@ export default class Audioplayer extends Component {
{this.renderNextButton()}
</li>
<li>
<RepeatButton shouldRepeat={shouldRepeat} onRepeatToggle={toggleRepeat} />
<RepeatButton
repeat={repeat}
setRepeat={setRepeat}
current={parseInt(currentAyah.split(':')[1], 10)}
surah={surah}
/>
</li>
<li>
<ScrollButton shouldScroll={shouldScroll} onScrollToggle={this.handleScrollToggle} />
Expand Down
23 changes: 23 additions & 0 deletions src/components/Audioplayer/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,32 @@
:local .disabled{
opacity: 0.5;
cursor: not-allowed !important;
pointer-events: none;
}

.verse{
padding: 0px;
color: $olive;
}

:local .popover{
:global(.popover-title){
font-family: $font-montserrat;
text-transform: uppercase;
color: $cream;
padding-top: 15px;
padding-bottom: 15px;
font-size: 0.75em;
}

:global(.popover-content){
:global(a){
font-size: 0.8em;
}
}
.pill{
:global(a){
padding: 10px 15px;
}
}
}
16 changes: 10 additions & 6 deletions src/redux/modules/audioplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const START = '@@quran/audioplayer/START';
const STOP = '@@quran/audioplayer/STOP';
export const NEXT = '@@quran/audioplayer/NEXT';
const PREVIOUS = '@@quran/audioplayer/PREVIOUS';
const TOGGLE_REPEAT = '@@quran/audioplayer/TOGGLE_REPEAT';
const SET_REPEAT = '@@quran/audioplayer/SET_REPEAT';
const TOGGLE_SCROLL = '@@quran/audioplayer/TOGGLE_SCROLL';
const BUILD_ON_CLIENT = '@@quran/audioplayer/BUILD_ON_CLIENT';
const UPDATE = '@@quran/audioplayer/UPDATE';
Expand All @@ -31,7 +31,10 @@ const initialState = {
currentTime: 0,
isSupported: true,
isPlaying: false,
shouldRepeat: false,
repeat: {
from: undefined,
to: undefined
},
shouldScroll: false,
isLoadedOnClient: false,
isLoading: true
Expand Down Expand Up @@ -182,10 +185,10 @@ export default function reducer(state = initialState, action = {}) {
currentTime: 0
};
}
case TOGGLE_REPEAT:
case SET_REPEAT:
return {
...state,
shouldRepeat: !state.shouldRepeat
repeat: action.repeat
};
case TOGGLE_SCROLL:
return {
Expand Down Expand Up @@ -296,9 +299,10 @@ export function previous(currentAyah) {
};
}

export function toggleRepeat() {
export function setRepeat(repeat) {
return {
type: TOGGLE_REPEAT
type: SET_REPEAT,
repeat
};
}

Expand Down

0 comments on commit d6f0cd8

Please sign in to comment.