forked from omulet/react-native-radial-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
316 lines (279 loc) · 10.6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
'use strict';
import React, {Component} from 'react';
import {
StyleSheet,
View,
Animated,
PanResponder,
Image,
TouchableOpacity,
Easing
} from 'react-native';
import TimerMixin from 'react-timer-mixin';
function generateRadialPositions(count, radius, spread_angle, start_angle, xOffset, yOffset) {
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 + xOffset,
y: -Math.sin(start + rad * i) * radius + yOffset,
};
});
};
var RadialMenu = React.createClass({
mixins: [TimerMixin],
isOpen: false,
getDefaultProps: function () {
return {
itemRadius: 30,
menuRadius: 100,
spreadAngle: 360,
startAngle: 0
};
},
getInitialState: function () {
// TODO 48? Child width
var xOffset = this.props.width - 48 - 15
var yOffset = 15
var children = this.childrenToArray(this.props);
// Define radial position of each child once expanded
var initial_spots = generateRadialPositions(
children.length - 1,
this.props.menuRadius,
this.props.spreadAngle,
this.props.startAngle,
xOffset,
yOffset
);
// Add root child (1st)
initial_spots.unshift({x: xOffset, y: yOffset});
// Define animated values for each child
var item_anims = initial_spots.map((_, i) => {
return new Animated.ValueXY({x: xOffset, y: yOffset});
})
// Define animated values for each child
var item_anims_opacity = initial_spots.map((_, i) => {
return new Animated.Value(0);
})
return {
item_spots: initial_spots,
item_anims,
item_anims_opacity,
children,
xOffset,
yOffset,
// selectedItem: null,
// itemPanResponder: null,
};
},
componentWillMount: function () {
// this.setState({itemPanResponder: this.createPanResponder()});
},
componentWillReceiveProps(nextProps) {
this.setState({children: this.childrenToArray(nextProps)});
},
// React.Children.toArray is still not exposed on RN 0.20.0-rc1
childrenToArray: function (props) {
let children = [];
React.Children.forEach(props.children, (child) => {
children.push(child)
});
return children;
},
onRootPressed: function(e) {
e.stopPropagation();
e.preventDefault();
this.toggleViewState()
},
toggleViewState() {
if (!this.isOpen) {
this.props.onOpen && this.props.onOpen();
// Expand items
Animated.stagger(40,
this.state.item_spots.map((spot, idx) =>
Animated.parallel([
Animated.spring(this.state.item_anims[idx], {
toValue: spot,
friction: 6,
tension: 80
}),
Animated.timing(this.state.item_anims_opacity[idx], {
toValue: 1,
})
])
)
).start();
// If open after X seconds, close automatically
// TODO cancel timeout if user closes before hand
// this.setTimeout(() => {
// if (this.isOpen) {
// this.toggleViewState()
// }
// }, 5000);
} else {
this.props.onClose && this.props.onClose();
Animated.stagger(40,
this.state.item_spots.map((spot, idx) =>
Animated.parallel([
Animated.spring(this.state.item_anims[idx], {
toValue: {x: this.state.xOffset, y: this.state.yOffset},
tension: 60,
friction: 10
}),
Animated.timing(this.state.item_anims_opacity[idx], {
toValue: 0,
})
])
)
).start();
}
this.isOpen = !this.isOpen
},
// itemPanListener: function (e, gestureState) {
// var newSelected = null;
// if (!this.isOpen) {
// 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.isOpen &&
// 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,
//
// // The gesture has started. gestureState.{x,y}0 will be set to zero now
// onPanResponderGrant: () => {
// this.props.onOpen && this.props.onOpen();
// this.isOpen = 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 initial position before we start tracking them
// setTimeout(() => {
// this.isOpen = false
// }, 500);
// },
//
// // The accumulated gesture distance since becoming responder is gestureState.d{x,y}
// onPanResponderMove: Animated.event(
// [
// // ignore the native event
// null,
// // extract dx and dy from gestureState
// {dx: this.state.item_anims[0].x, dy: this.state.item_anims[0].y}
// //{dx: 0, dy: 0}
// ],
// {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() {
const containerStyle = {
position: 'relative',
flex: 1,
width: this.props.width,
height: this.props.height,
backgroundColor: 'transparent',
// borderWidth: 1,
// borderColor: 'purple'
}
return (
<View style={[containerStyle, 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;
if (j == 0) {
return (
<Animated.View
key={i}
style={{
opacity: 1,
transform: this.state.item_anims[j].getTranslateTransform(),
position: 'absolute',
}}>
<TouchableOpacity onPress={this.onRootPressed}>
{this.isOpen
? this.props.collapseIcon
: this.props.children[j]
}
</TouchableOpacity>
</Animated.View>
);
} else {
// {...handlers}
return (
<Animated.View
key={i}
style={{
opacity: this.state.item_anims_opacity[j],
transform: this.state.item_anims[j].getTranslateTransform(),
position: 'absolute',
}}>
{this.state.children[j]}
</Animated.View>
);
}
})}
</View>
);
}
})
module.exports = RadialMenu;