2016-11-04 13:51:17 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import FontAwesome from 'react-fontawesome'
|
|
|
|
|
|
2018-03-23 16:54:56 +13:00
|
|
|
export default class VolumeControl extends React.Component{
|
2016-11-04 13:51:17 +13:00
|
|
|
|
2017-10-10 13:05:28 +13:00
|
|
|
constructor(props){
|
2017-05-22 20:23:46 +12:00
|
|
|
super(props)
|
2016-11-04 13:51:17 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleClick(e){
|
|
|
|
|
var slider = e.target;
|
2017-10-10 13:05:28 +13:00
|
|
|
if (slider.className != 'slider' ) slider = slider.parentElement;
|
2016-11-04 13:51:17 +13:00
|
|
|
|
2017-06-16 15:40:39 +12:00
|
|
|
var sliderX = e.clientX - slider.getBoundingClientRect().left;
|
2017-06-16 09:37:40 +12:00
|
|
|
var sliderWidth = slider.getBoundingClientRect().width;
|
2017-10-10 13:05:28 +13:00
|
|
|
var percent = Math.round((sliderX / sliderWidth ) * 100);
|
2017-06-16 15:40:39 +12:00
|
|
|
|
|
|
|
|
if (percent > 100){
|
|
|
|
|
percent = 100
|
2017-10-10 13:05:28 +13:00
|
|
|
} else if (percent < 0){
|
2017-06-16 15:40:39 +12:00
|
|
|
percent = 0
|
|
|
|
|
}
|
2016-11-04 13:51:17 +13:00
|
|
|
|
2018-03-23 16:54:56 +13:00
|
|
|
this.props.onVolumeChange(percent);
|
2017-05-23 08:50:53 +12:00
|
|
|
}
|
2016-11-04 13:51:17 +13:00
|
|
|
|
2017-09-05 20:25:13 +12:00
|
|
|
handleWheel(e){
|
|
|
|
|
|
|
|
|
|
// Identify which direction we've scrolled (inverted)
|
|
|
|
|
// This is simplified and doesn't consider momentum as it varies wildly
|
|
|
|
|
// between browsers and devices
|
|
|
|
|
var direction = (e.deltaY > 0 ? -1 : 1)
|
|
|
|
|
var percent = this.props.volume;
|
|
|
|
|
|
|
|
|
|
percent += direction * 5
|
2017-08-29 14:35:55 +02:00
|
|
|
|
|
|
|
|
if (percent > 100){
|
|
|
|
|
percent = 100
|
2017-10-10 13:05:28 +13:00
|
|
|
} else if (percent < 0){
|
2017-08-29 14:35:55 +02:00
|
|
|
percent = 0
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 16:54:56 +13:00
|
|
|
this.props.onVolumeChange(percent);
|
2017-08-29 14:35:55 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 05:47:36 +12:00
|
|
|
renderMuteButton(){
|
|
|
|
|
if (this.props.mute){
|
|
|
|
|
return (
|
2018-03-23 16:54:56 +13:00
|
|
|
<a className="control mute-control has-tooltip" onClick={() => this.props.onMuteChange(false)}>
|
|
|
|
|
<FontAwesome className="red-text" name="volume-off" />
|
2017-06-18 05:47:36 +12:00
|
|
|
<span className="tooltip">Unmute</span>
|
|
|
|
|
</a>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
2018-03-23 16:54:56 +13:00
|
|
|
<a className="control mute-control has-tooltip" onClick={() => this.props.onMuteChange(true)}>
|
2017-06-18 05:47:36 +12:00
|
|
|
<FontAwesome className="muted" name="volume-off" />
|
|
|
|
|
<span className="tooltip">Mute</span>
|
|
|
|
|
</a>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 13:51:17 +13:00
|
|
|
render(){
|
2017-05-23 08:50:53 +12:00
|
|
|
var className = "volume-control"
|
2017-06-18 05:47:36 +12:00
|
|
|
if (this.props.mute){
|
|
|
|
|
className += " muted"
|
|
|
|
|
}
|
2016-11-04 13:51:17 +13:00
|
|
|
return (
|
2017-08-29 14:35:55 +02:00
|
|
|
<span className={className} onWheel={e => this.handleWheel(e)}>
|
2017-06-22 08:59:40 +12:00
|
|
|
{this.renderMuteButton()}
|
|
|
|
|
<div className="slider-wrapper">
|
|
|
|
|
<div className="slider horizontal" onClick={e => this.handleClick(e)}>
|
|
|
|
|
<div className="track">
|
|
|
|
|
<div className="progress" style={{ width: this.props.volume+'%' }}></div>
|
2016-11-04 13:51:17 +13:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2017-06-22 08:59:40 +12:00
|
|
|
</div>
|
2016-11-04 13:51:17 +13:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|