react-toolbox/components/time_picker/clock/hand.jsx

112 lines
2.6 KiB
React
Raw Normal View History

import React from 'react';
2015-10-11 22:17:12 +03:00
import style from './style';
import events from '../../utils/events';
import prefixer from '../../utils/prefixer';
import utils from '../../utils/utils';
2015-09-09 23:34:56 +03:00
2015-10-22 02:31:17 +03:00
class Hand extends React.Component {
static propTypes = {
2015-09-06 20:29:51 +03:00
className: React.PropTypes.string,
2015-09-08 12:03:01 +03:00
angle: React.PropTypes.number,
onMove: React.PropTypes.func,
onMoved: React.PropTypes.func
};
2015-09-06 20:29:51 +03:00
static defaultProps = {
className: '',
angle: 0,
length: 0,
origin: {}
};
2015-09-06 20:29:51 +03:00
state = {
knobWidth: 0
};
2015-09-06 20:29:51 +03:00
componentDidMount () {
2015-10-11 22:17:12 +03:00
this.setState({knobWidth: this.refs.knob.offsetWidth});
}
2015-09-06 20:29:51 +03:00
getMouseEventMap () {
2015-09-06 20:29:51 +03:00
return {
2015-10-22 02:31:17 +03:00
mousemove: this.handleMouseMove,
mouseup: this.handleMouseUp
2015-09-06 20:29:51 +03:00
};
}
2015-09-06 20:29:51 +03:00
getTouchEventMap () {
2015-09-06 20:29:51 +03:00
return {
2015-10-22 02:31:17 +03:00
touchmove: this.handleTouchMove,
touchend: this.handleTouchEnd
2015-09-06 20:29:51 +03:00
};
}
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleMouseMove = (event) => {
this.move(events.getMousePosition(event));
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleTouchMove = (event) => {
this.move(events.getTouchPosition(event));
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleMouseUp = () => {
this.end(this.getMouseEventMap());
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleTouchEnd = () => {
this.end(this.getTouchEventMap());
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
mouseStart (event) {
events.addEventsToDocument(this.getMouseEventMap());
this.move(events.getMousePosition(event));
}
2015-09-06 20:29:51 +03:00
touchStart (event) {
events.addEventsToDocument(this.getTouchEventMap());
this.move(events.getTouchPosition(event));
events.pauseEvent(event);
}
2015-09-06 20:29:51 +03:00
getPositionRadius (position) {
const x = this.props.origin.x - position.x;
const y = this.props.origin.y - position.y;
2015-09-06 20:29:51 +03:00
return Math.sqrt(x * x + y * y);
}
2015-09-06 20:29:51 +03:00
trimAngleToValue (angle) {
2015-09-06 20:29:51 +03:00
return this.props.step * Math.round(angle / this.props.step);
}
2015-09-06 20:29:51 +03:00
positionToAngle (position) {
return utils.angle360FromPositions(this.props.origin.x, this.props.origin.y, position.x, position.y);
}
2015-09-06 20:29:51 +03:00
end (evts) {
2015-09-08 12:03:01 +03:00
if (this.props.onMoved) this.props.onMoved();
events.removeEventsFromDocument(evts);
}
2015-09-06 20:29:51 +03:00
move (position) {
const degrees = this.trimAngleToValue(this.positionToAngle(position));
const radius = this.getPositionRadius(position);
2015-09-08 12:03:01 +03:00
if (this.props.onMove) this.props.onMove(degrees === 360 ? 0 : degrees, radius);
}
2015-09-06 20:29:51 +03:00
render () {
2015-10-11 22:17:12 +03:00
const className = `${style.hand} ${this.props.className}`;
const handStyle = prefixer({
2015-09-06 20:29:51 +03:00
height: this.props.length - this.state.knobWidth / 2,
2015-09-08 12:03:01 +03:00
transform: `rotate(${this.props.angle}deg)`
2015-09-06 20:29:51 +03:00
});
return (
2015-10-11 22:17:12 +03:00
<div className={className} style={handStyle}>
<div ref='knob' className={style.knob}></div>
2015-09-06 20:29:51 +03:00
</div>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Hand;