Files
Iris/src/js/components/Dragger.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-11-10 16:17:10 +13:00
2019-09-05 15:36:11 +12:00
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as uiActions from '../services/ui/actions';
import * as mopidyActions from '../services/mopidy/actions';
import * as spotifyActions from '../services/spotify/actions';
class Dragger extends React.Component {
constructor(props) {
super(props);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
this.state = {
active: false,
position_x: 0,
position_y: 0,
};
}
componentDidMount() {
window.addEventListener('mousemove', this.handleMouseMove, false);
window.addEventListener('mouseup', this.handleMouseUp, false);
}
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMouseMove, false);
window.removeEventListener('mouseup', this.handleMouseUp, false);
}
handleMouseMove(e) {
if (!this.props.dragger) return null;
const threshold = 10;
if (
e.clientX > this.props.dragger.start_x + threshold
|| e.clientX < this.props.dragger.start_x - threshold
|| e.clientY > this.props.dragger.start_y + threshold
|| e.clientY < this.props.dragger.start_y - threshold) {
this.setState({
position_x: e.clientX,
position_y: e.clientY,
});
const dropzones = document.getElementsByClassName('dropzone');
for (let i = 0; i < dropzones.length; i++) {
dropzones[i].classList.remove('hover');
}
if (e.target.classList.contains('dropzone') && !e.target.classList.contains('hover')) {
e.target.className += ' hover';
}
// if not already, activate
if (!this.props.dragger.active) this.props.uiActions.dragActive();
}
}
handleMouseUp(e) {
if (!this.props.dragger) return null;
this.props.uiActions.dragEnd(e);
}
render() {
if (!this.props.dragger || !this.props.dragger.active) return null;
const style = {
left: this.state.position_x,
top: this.state.position_y,
};
return (
<div className="dragger" style={style}>
Dragging
{' '}
{ this.props.dragger.victims.length }
{' '}
things
</div>
);
}
2016-11-10 16:17:10 +13:00
}
2019-09-05 15:36:11 +12:00
const mapStateToProps = (state, ownProps) => ({
dragger: state.ui.dragger,
});
2016-11-10 16:17:10 +13:00
2019-09-05 15:36:11 +12:00
const mapDispatchToProps = (dispatch) => ({
uiActions: bindActionCreators(uiActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
});
2016-11-10 16:17:10 +13:00
2019-09-05 15:36:11 +12:00
export default connect(mapStateToProps, mapDispatchToProps)(Dragger);