Files
Iris/src/js/components/Fields/VolumeControl.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-11-04 13:51:17 +13:00
import React, { PropTypes } from 'react'
2018-05-16 21:08:45 +12:00
import Icon from '../Icon'
2016-11-04 13:51:17 +13:00
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){
2018-05-02 16:14:53 +12:00
var old_percent = this.props.volume;
2016-11-04 13:51:17 +13:00
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;
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-05-02 16:14:53 +12:00
this.props.onVolumeChange(percent, old_percent);
}
2016-11-04 13:51:17 +13:00
handleWheel(e){
if (this.props.scrollWheel){
2018-05-02 16:14:53 +12:00
var old_percent = this.props.volume;
// 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
if (percent > 100){
percent = 100
} else if (percent < 0){
percent = 0
}
2018-05-02 16:14:53 +12:00
this.props.onVolumeChange(percent, old_percent);
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)}>
2018-05-16 21:08:45 +12:00
<Icon 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)}>
2018-05-16 21:08:45 +12:00
<Icon className="muted" name="volume_mute" />
2017-06-18 05:47:36 +12:00
<span className="tooltip">Mute</span>
</a>
)
}
}
2016-11-04 13:51:17 +13:00
render(){
2018-05-02 16:14:53 +12:00
var className = "volume-control";
2017-06-18 05:47:36 +12:00
if (this.props.mute){
2018-05-02 16:14:53 +12:00
className += " muted";
2017-06-18 05:47:36 +12:00
}
2018-05-02 16:14:53 +12:00
if (this.props.className){
className += " "+this.props.className;
}
2016-11-04 13:51:17 +13:00
return (
<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>
);
}
}