forked from omulet/react-native-radial-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (168 loc) · 5.1 KB
/
index.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
179
180
181
182
183
184
185
186
'use strict';
import React from 'react';
import {
Animated,
PanResponder,
StyleSheet,
View,
} from 'react-native';
function generateRadialPositions(count, radius, spread_angle, start_angle) {
var span = spread_angle < 360 ? 1 : 0;
var start = start_angle * Math.PI / 180;
var rad = spread_angle * Math.PI * 2 / 360 / (count - span);
return [...Array(count)].map((_, i) => {
return {
x: -Math.cos(start + rad * i) * radius,
y: -Math.sin(start + rad * i) * radius,
};
});
};
var RadialMenu = React.createClass({
RMOpening: false,
getDefaultProps: function() {
return {
itemRadius: 30,
menuRadius: 100,
spreadAngle: 360,
startAngle: 0
};
},
getInitialState: function() {
var children = this.childrenToArray();
var initial_spots = generateRadialPositions(
children.length - 1,
this.props.menuRadius,
this.props.spreadAngle,
this.props.startAngle
);
initial_spots.unshift({x: 0, y: 0});
return {
item_spots: initial_spots,
item_anims: initial_spots.map((_, i) => {
return new Animated.ValueXY();
}),
selectedItem: null,
itemPanResponder: null,
children: children,
};
},
componentWillMount: function() {
this.setState({ itemPanResponder: this.createPanResponder() });
},
// React.Children.toArray is still not exposed on RN 0.20.0-rc1
childrenToArray: function() {
let children = [];
React.Children.forEach(this.props.children, (child) => {
children.push(child)
});
return children;
},
itemPanListener: function(e, gestureState) {
var newSelected = null;
if (!this.RMOpening) {
newSelected = this.computeNewSelected(gestureState);
if (this.state.selectedItem !== newSelected) {
if (this.state.selectedItem !== null) {
var restSpot = this.state.item_spots[this.state.selectedItem];
Animated.spring(this.state.item_anims[this.state.selectedItem], {
toValue: restSpot,
}).start();
}
if (newSelected !== null && newSelected !== 0) {
Animated.spring(this.state.item_anims[newSelected], {
toValue: this.state.item_anims[0],
}).start();
}
this.state.selectedItem = newSelected;
}
}
},
releaseItem: function() {
this.props.onClose && this.props.onClose();
this.state.selectedItem && !this.RMOpening &&
this.state.children[this.state.selectedItem].props.onSelect &&
this.state.children[this.state.selectedItem].props.onSelect();
this.state.selectedItem = null;
this.state.item_anims.forEach((item, i) => {
Animated.spring(item, {
toValue: {x: 0, y: 0},
tension: 60,
friction: 10
}).start();
});
},
createPanResponder: function() {
return PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: () => {
this.props.onOpen && this.props.onOpen();
this.RMOpening = true;
Animated.stagger(40,
this.state.item_spots.map((spot, idx) =>
Animated.spring(this.state.item_anims[idx], {
toValue: spot,
friction: 6,
tension: 80
})
)
).start();
// Make sure all items gets to innitial position
// before we start tracking them
setTimeout(() => {this.RMOpening = false}, 500);
},
onPanResponderMove: Animated.event(
[ null, {dx: this.state.item_anims[0].x, dy: this.state.item_anims[0].y} ],
{listener: this.itemPanListener}
),
onPanResponderRelease: this.releaseItem,
onPanResponderTerminate: this.releaseItem,
});
},
computeNewSelected: function(
gestureState: Object,
): ?number {
var {dx, dy} = gestureState;
var minDist = Infinity;
var newSelected = null;
var pointRadius = Math.sqrt(dx * dx + dy * dy);
if (Math.abs(this.props.menuRadius - pointRadius) < this.props.menuRadius / 2) {
this.state.item_spots.forEach((spot, idx) => {
var delta = {x: spot.x - dx, y: spot.y - dy};
var dist = delta.x * delta.x + delta.y * delta.y;
if (dist < minDist) {
minDist = dist;
newSelected = idx;
}
});
}
return newSelected;
},
render: function() {
return (
<View style={[
{ position: 'relative',
backgroundColor: 'transparent',
width: this.props.itemRadius * 2,
height: this.props.itemRadius * 2,
},
this.props.style]}>
{this.state.item_anims.map((_, i) => {
var j = this.state.item_anims.length - i - 1;
var handlers = j > 0 ? {} : this.state.itemPanResponder.panHandlers;
return (
<Animated.View
{...handlers}
key={i}
style={{
transform: this.state.item_anims[j].getTranslateTransform(),
position: 'absolute',
}} >
{this.state.children[j]}
</Animated.View>
);
})}
</View>
);
}
});
module.exports = RadialMenu;