Files
Iris/src/js/components/ProgressSlider.js

68 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-11-03 18:22:26 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import * as mopidyActions from '../services/mopidy/actions'
class ProgressSlider extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2016-11-03 18:22:26 +13:00
super(props);
}
handleClick(e){
var slider = e.target;
2017-10-10 13:05:28 +13:00
if (slider.className != 'slider' ) slider = slider.parentElement;
2016-11-03 18:22:26 +13:00
var sliderX = e.clientX - slider.getBoundingClientRect().left;
var sliderWidth = slider.getBoundingClientRect().width;
2017-10-10 13:05:28 +13:00
var percent = (sliderX / sliderWidth ).toFixed(2);
2016-11-03 18:22:26 +13:00
2017-10-10 13:05:28 +13:00
if (this.props.connected && this.props.current_track){
var destination_time = this.props.current_track.duration * percent
2017-10-10 13:05:28 +13:00
this.props.mopidyActions.seek(destination_time )
2016-11-03 18:22:26 +13:00
this.setState({ animating: false })
}
}
render(){
var percent = 0
2017-10-10 13:05:28 +13:00
if (this.props.connected && this.props.current_track){
percent = this.props.time_position / this.props.current_track.duration
2016-11-03 18:22:26 +13:00
percent = percent * 100;
2017-10-10 13:05:28 +13:00
if (percent > 100 ) percent = 100
2016-11-03 18:22:26 +13:00
}
return (
2017-09-13 08:05:27 +12:00
<div className={"progress slider horizontal "+this.props.play_state} onClick={ (e) => this.handleClick(e) } >
2016-11-04 13:51:17 +13:00
<div className="track">
<div className="progress" style={{ width: (percent)+'%' }}></div>
</div>
2016-11-03 18:22:26 +13:00
</div>
);
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return {
current_track: (state.core.current_track !== undefined && state.core.tracks !== undefined && state.core.tracks[state.core.current_track.uri] !== undefined ? state.core.tracks[state.core.current_track.uri] : null),
connected: state.mopidy.connected,
time_position: state.mopidy.time_position,
play_state: state.mopidy.play_state
}
2016-11-03 18:22:26 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
mopidyActions: bindActionCreators(mopidyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(ProgressSlider)