-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dial.js
178 lines (169 loc) · 5.91 KB
/
Dial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
import React, { PropTypes, Component } from 'react';
import { View, PanResponder } from 'react-native';
//
// It would be great if we didn't need to use NativeMethodsMixin.measure but
// unfortunately the onLayout functionality does not supploy pageX and pageY
// coordinates and the PanResponder can return x,y coordinates that are relative
// to a child component rather than the component that the pan responder is attached to
//
// We could probably hack out a solution that works with nested views and
// layouts...
export default function CreateDialComponent(NativeMethodsMixin) {
const Dial = React.createClass({
mixins: [NativeMethodsMixin],
getInitialState: function() {
return this.getStateForValue(this.props.value);
},
componentWillReceiveProps(nextProps) {
if(this.props.value !== nextProps.value) {
this.setState(this.getStateForValue(nextProps.value))
}
},
getStateForValue(value) {
return {
angle: value,
previousAngle: value % 360,
active: false
};
},
getConstrainedAngle(angle) {
return Math.max(
Math.min(angle, this.props.maximumValue),
this.props.minimumValue
);
},
componentWillMount: function() {
const endGesture = (evt, gestureState) => {
const constrainedAngle = this.getConstrainedAngle(this.state.angle);
this.setState({
active: false,
angle: constrainedAngle,
previousAngle: constrainedAngle % 360
});
if(this.props.onSlidingComplete) {
this.props.onSlidingComplete(constrainedAngle);
}
};
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderTerminationRequest: (evt, gestureState) => false, // false because of scrollview nesting issue
onShouldBlockNativeResponder: (evt, gestureState) => true,
onPanResponderGrant: () => {
this.updateLayout();
this.setState({ active: true });
if(this.props.onSlidingBegin) {
this.props.onSlidingBegin();
}
},
onPanResponderMove: (evt, gestureState) => {
this.updateLayout(); // this is here because of annoying issues when nested in a scrollview
const point = {
x: evt.nativeEvent.pageX - (this.layout.pageX + this.layout.width / 2),
y: evt.nativeEvent.pageY - (this.layout.pageY + this.layout.height / 2)
};
const currentAngle = getAngleDeg({x: 0, y: -1}, point);
const newAngle = this.state.angle + getAngleDiff(this.state.previousAngle, currentAngle);
const constrainedAngle = this.getConstrainedAngle(newAngle);
this.setState({
angle: newAngle,
previousAngle: currentAngle
});
if(this.props.onValueChange) {
this.props.onValueChange(constrainedAngle);
}
function getAngleDiff(deg1, deg2) {
const diff = deg2 - deg1;
if(diff < -180) {
return diff + 360;
}
if(diff > 180) {
return diff - 360;
}
return diff;
}
function getAngleDeg(v1, v2) {
let deg = Math.atan2(v2.y - v1.y, v2.x - v1.x) * 180 / Math.PI + 90;
return deg < 0 ? deg + 360 : deg;
}
},
onPanResponderRelease: endGesture,
onPanResponderTerminate: endGesture
});
},
componentDidMount() {
requestAnimationFrame(()=>this.updateLayout());
},
updateLayout: function() {
this.measure((x, y, width, height, pageX, pageY) => {
this.layout = {x, y, width, height, pageX, pageY};
console.log(this.layout);
});
},
render: function() {
const {style, trackWidth = 3, handleDiameter = 28} = this.props;
const {active, angle} = this.state;
const constrainedAngle = this.getConstrainedAngle(angle);
const defaultSize = 180;
const size = this.state.size ?
Math.min(this.state.size.width, this.state.size.height) - handleDiameter :
defaultSize - handleDiameter;
const handleColor = '#FFF';
const handleStyle = {
width: handleDiameter,
height: handleDiameter,
borderRadius: handleDiameter/2,
backgroundColor: handleColor,
shadowColor: "black",
shadowOpacity: 0.6,
shadowRadius: 1.5,
shadowOffset: {
height: 1,
width: 0
},
left: size/2 - handleDiameter/2 - trackWidth,
top: -1*handleDiameter/2 - trackWidth/2,
transform: [{
rotate: (360 - constrainedAngle % 360) + 'deg'
}]
};
return (
<View style={style} onLayout={evt=>this.setState({size:evt.nativeEvent.layout})}>
{size ?
<View style={{
margin: handleDiameter / 2,
width: size,
height: size,
borderRadius: size / 2,
borderWidth: trackWidth,
borderColor: active ? '#DDD' : '#CCC',
transform: [{
rotate: constrainedAngle % 360 + 'deg'
}]
}}
>
<View style={handleStyle} {...this._panResponder.panHandlers}/>
</View>
: null }
</View>
);
}
});
Dial.propTypes = {
minimumValue: PropTypes.number,
maximumValue: PropTypes.number,
value: PropTypes.number,
onValueChange: PropTypes.func,
onSlidingComplete: PropTypes.func,
onSlidingBegin: PropTypes.func
};
Dial.defaultProps = {
minimumValue: -360,
maximumValue: Number.MAX_VALUE,
value: 0
};
return Dial;
}