forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsideMenuController.js
306 lines (260 loc) · 8.74 KB
/
sideMenuController.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
(function(ionic) {
'use strict';
/**
* The SideMenuController is a controller with a left and/or right menu that
* can be slid out and toggled. Seen on many an app.
*
* The right or left menu can be disabled or not used at all, if desired.
*/
ionic.controllers.SideMenuController = ionic.controllers.ViewController.inherit({
initialize: function(options) {
var self = this;
this.left = options.left;
this.right = options.right;
this.content = options.content;
this.dragThresholdX = options.dragThresholdX || 10;
this._rightShowing = false;
this._leftShowing = false;
this._isDragging = false;
if(this.content) {
this.content.onDrag = function(e) {
self._handleDrag(e);
};
this.content.onEndDrag =function(e) {
self._endDrag(e);
};
}
},
/**
* Set the content view controller if not passed in the constructor options.
*
* @param {object} content
*/
setContent: function(content) {
var self = this;
this.content = content;
this.content.onDrag = function(e) {
self._handleDrag(e);
};
this.content.endDrag = function(e) {
self._endDrag(e);
};
},
isOpenLeft: function() {
return this.getOpenAmount() > 0;
},
isOpenRight: function() {
return this.getOpenAmount() < 0;
},
/**
* Toggle the left menu to open 100%
*/
toggleLeft: function(shouldOpen) {
var openAmount = this.getOpenAmount();
if (arguments.length === 0) {
shouldOpen = openAmount <= 0;
}
this.content.enableAnimation();
if(!shouldOpen) {
this.openPercentage(0);
} else {
this.openPercentage(100);
}
},
/**
* Toggle the right menu to open 100%
*/
toggleRight: function(shouldOpen) {
var openAmount = this.getOpenAmount();
if (arguments.length === 0) {
shouldOpen = openAmount >= 0;
}
this.content.enableAnimation();
if(!shouldOpen) {
this.openPercentage(0);
} else {
this.openPercentage(-100);
}
},
/**
* Close all menus.
*/
close: function() {
this.openPercentage(0);
},
/**
* @return {float} The amount the side menu is open, either positive or negative for left (positive), or right (negative)
*/
getOpenAmount: function() {
return this.content && this.content.getTranslateX() || 0;
},
/**
* @return {float} The ratio of open amount over menu width. For example, a
* menu of width 100 open 50 pixels would be open 50% or a ratio of 0.5. Value is negative
* for right menu.
*/
getOpenRatio: function() {
var amount = this.getOpenAmount();
if(amount >= 0) {
return amount / this.left.width;
}
return amount / this.right.width;
},
isOpen: function() {
return this.getOpenRatio() == 1;
},
/**
* @return {float} The percentage of open amount over menu width. For example, a
* menu of width 100 open 50 pixels would be open 50%. Value is negative
* for right menu.
*/
getOpenPercentage: function() {
return this.getOpenRatio() * 100;
},
/**
* Open the menu with a given percentage amount.
* @param {float} percentage The percentage (positive or negative for left/right) to open the menu.
*/
openPercentage: function(percentage) {
var p = percentage / 100;
if(this.left && percentage >= 0) {
this.openAmount(this.left.width * p);
} else if(this.right && percentage < 0) {
var maxRight = this.right.width;
this.openAmount(this.right.width * p);
}
},
/**
* Open the menu the given pixel amount.
* @param {float} amount the pixel amount to open the menu. Positive value for left menu,
* negative value for right menu (only one menu will be visible at a time).
*/
openAmount: function(amount) {
var maxLeft = this.left && this.left.width || 0;
var maxRight = this.right && this.right.width || 0;
// Check if we can move to that side, depending if the left/right panel is enabled
if(!(this.left && this.left.isEnabled) && amount > 0) {
this.content.setTranslateX(0);
return;
}
if(!(this.right && this.right.isEnabled) && amount < 0) {
this.content.setTranslateX(0);
return;
}
if(this._leftShowing && amount > maxLeft) {
this.content.setTranslateX(maxLeft);
return;
}
if(this._rightShowing && amount < -maxRight) {
this.content.setTranslateX(-maxRight);
return;
}
this.content.setTranslateX(amount);
if(amount >= 0) {
this._leftShowing = true;
this._rightShowing = false;
if(amount > 0) {
// Push the z-index of the right menu down
this.right && this.right.pushDown && this.right.pushDown();
// Bring the z-index of the left menu up
this.left && this.left.bringUp && this.left.bringUp();
}
} else {
this._rightShowing = true;
this._leftShowing = false;
// Bring the z-index of the right menu up
this.right && this.right.bringUp && this.right.bringUp();
// Push the z-index of the left menu down
this.left && this.left.pushDown && this.left.pushDown();
}
},
/**
* Given an event object, find the final resting position of this side
* menu. For example, if the user "throws" the content to the right and
* releases the touch, the left menu should snap open (animated, of course).
*
* @param {Event} e the gesture event to use for snapping
*/
snapToRest: function(e) {
// We want to animate at the end of this
this.content.enableAnimation();
this._isDragging = false;
// Check how much the panel is open after the drag, and
// what the drag velocity is
var ratio = this.getOpenRatio();
if(ratio === 0) {
// Just to be safe
this.openPercentage(0);
return;
}
var velocityThreshold = 0.3;
var velocityX = e.gesture.velocityX;
var direction = e.gesture.direction;
// Less than half, going left
//if(ratio > 0 && ratio < 0.5 && direction == 'left' && velocityX < velocityThreshold) {
//this.openPercentage(0);
//}
// Going right, less than half, too slow (snap back)
if(ratio > 0 && ratio < 0.5 && direction == 'right' && velocityX < velocityThreshold) {
this.openPercentage(0);
}
// Going left, more than half, too slow (snap back)
else if(ratio > 0.5 && direction == 'left' && velocityX < velocityThreshold) {
this.openPercentage(100);
}
// Going left, less than half, too slow (snap back)
else if(ratio < 0 && ratio > -0.5 && direction == 'left' && velocityX < velocityThreshold) {
this.openPercentage(0);
}
// Going right, more than half, too slow (snap back)
else if(ratio < 0.5 && direction == 'right' && velocityX < velocityThreshold) {
this.openPercentage(-100);
}
// Going right, more than half, or quickly (snap open)
else if(direction == 'right' && ratio >= 0 && (ratio >= 0.5 || velocityX > velocityThreshold)) {
this.openPercentage(100);
}
// Going left, more than half, or quickly (span open)
else if(direction == 'left' && ratio <= 0 && (ratio <= -0.5 || velocityX > velocityThreshold)) {
this.openPercentage(-100);
}
// Snap back for safety
else {
this.openPercentage(0);
}
},
// End a drag with the given event
_endDrag: function(e) {
if(this._isDragging) {
this.snapToRest(e);
}
this._startX = null;
this._lastX = null;
this._offsetX = null;
},
// Handle a drag event
_handleDrag: function(e) {
// If we don't have start coords, grab and store them
if(!this._startX) {
this._startX = e.gesture.touches[0].pageX;
this._lastX = this._startX;
} else {
// Grab the current tap coords
this._lastX = e.gesture.touches[0].pageX;
}
// Calculate difference from the tap points
if(!this._isDragging && Math.abs(this._lastX - this._startX) > this.dragThresholdX) {
// if the difference is greater than threshold, start dragging using the current
// point as the starting point
this._startX = this._lastX;
this._isDragging = true;
// Initialize dragging
this.content.disableAnimation();
this._offsetX = this.getOpenAmount();
}
if(this._isDragging) {
this.openAmount(this._offsetX + (this._lastX - this._startX));
}
}
});
})(ionic);