Skip to content

Commit ff24152

Browse files
committed
fix(menu): add/remove gesture listeners per enabled menu
1 parent 9888a9c commit ff24152

File tree

6 files changed

+124
-74
lines changed

6 files changed

+124
-74
lines changed

ionic/components/menu/menu-controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class MenuController {
209209
*/
210210
isEnabled(menuId?: string): boolean {
211211
let menu = this.get(menuId);
212-
return menu && menu.isEnabled || false;
212+
return menu && menu.enabled || false;
213213
}
214214

215215
/**
@@ -235,6 +235,14 @@ export class MenuController {
235235
return (this._menus.length ? this._menus[0] : null);
236236
}
237237

238+
239+
/**
240+
* @return {Array<Menu>} Returns an array of all menu instances.
241+
*/
242+
getMenus(): Array<Menu> {
243+
return this._menus;
244+
}
245+
238246
/**
239247
* @private
240248
*/

ionic/components/menu/menu-gestures.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ export class MenuContentGesture extends SlideEdgeGesture {
1717
threshold: 0,
1818
maxEdgeStart: menu.maxEdgeStart || 75
1919
}, options));
20-
21-
this.listen();
2220
}
2321

2422
canStart(ev) {
2523
let menu = this.menu;
2624

27-
if (!menu.isEnabled || !menu.isSwipeEnabled) {
28-
console.debug('menu can not start, isEnabled:', menu.isEnabled, 'isSwipeEnabled:', menu.isSwipeEnabled, 'side:', menu.side);
25+
if (!menu.enabled || !menu.swipeEnabled) {
26+
console.debug('menu can not start, isEnabled:', menu.enabled, 'isSwipeEnabled:', menu.swipeEnabled, 'side:', menu.side);
2927
return false;
3028
}
3129

ionic/components/menu/menu.ts

Lines changed: 90 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {MenuContentGesture, MenuTargetGesture} from './menu-gestures';
88
import {Gesture} from '../../gestures/gesture';
99
import {MenuController} from './menu-controller';
1010
import {MenuType} from './menu-types';
11-
import {isFalseProperty} from '../../util/util';
11+
import {isTrueProperty} from '../../util/util';
1212

1313

1414
/**
@@ -17,12 +17,11 @@ import {isFalseProperty} from '../../util/util';
1717
@Component({
1818
selector: 'ion-menu',
1919
host: {
20-
'role': 'navigation',
21-
'[attr.side]': 'side',
22-
'[attr.type]': 'type',
23-
'[attr.swipeEnabled]': 'swipeEnabled'
20+
'role': 'navigation'
2421
},
25-
template: '<ng-content></ng-content><div tappable disable-activated class="backdrop"></div>',
22+
template:
23+
'<ng-content></ng-content>' +
24+
'<div tappable disable-activated class="backdrop"></div>',
2625
directives: [forwardRef(() => MenuBackdrop)]
2726
})
2827
export class Menu extends Ion {
@@ -32,23 +31,16 @@ export class Menu extends Ion {
3231
private _menuGesture: Gesture;
3332
private _type: MenuType;
3433
private _resizeUnreg: Function;
35-
34+
private _isEnabled: boolean = true;
35+
private _isSwipeEnabled: boolean = true;
36+
private _isListening: boolean = false;
37+
private _init: boolean = false;
3638

3739
/**
3840
* @private
3941
*/
4042
isOpen: boolean = false;
4143

42-
/**
43-
* @private
44-
*/
45-
isEnabled: boolean = true;
46-
47-
/**
48-
* @private
49-
*/
50-
isSwipeEnabled: boolean = true;
51-
5244
/**
5345
* @private
5446
*/
@@ -83,7 +75,28 @@ export class Menu extends Ion {
8375
/**
8476
* @private
8577
*/
86-
@Input() swipeEnabled: any;
78+
@Input()
79+
get enabled(): boolean {
80+
return this._isEnabled;
81+
}
82+
83+
set enabled(val: boolean) {
84+
this._isEnabled = isTrueProperty(val);
85+
this._setListeners();
86+
}
87+
88+
/**
89+
* @private
90+
*/
91+
@Input()
92+
get swipeEnabled(): boolean {
93+
return this._isSwipeEnabled;
94+
}
95+
96+
set swipeEnabled(val: boolean) {
97+
this._isSwipeEnabled = isTrueProperty(val);
98+
this._setListeners();
99+
}
87100

88101
/**
89102
* @private
@@ -112,6 +125,8 @@ export class Menu extends Ion {
112125
*/
113126
ngOnInit() {
114127
let self = this;
128+
self._init = true;
129+
115130
let content = self.content;
116131
self._cntEle = (content instanceof Node) ? content : content && content.getNativeElement && content.getNativeElement();
117132

@@ -132,23 +147,29 @@ export class Menu extends Ion {
132147
}
133148
self._renderer.setElementAttribute(self._elementRef.nativeElement, 'type', self.type);
134149

135-
// add the gesture listeners
136-
self._zone.runOutsideAngular(function() {
137-
self._cntGesture = new MenuContentGesture(self, self.getContentElement());
138-
self._menuGesture = new MenuTargetGesture(self, self.getNativeElement());
139-
140-
self.onContentClick = function(ev: UIEvent) {
141-
if (self.isEnabled) {
142-
ev.preventDefault();
143-
ev.stopPropagation();
144-
self.close();
145-
}
146-
};
147-
});
150+
// add the gestures
151+
self._cntGesture = new MenuContentGesture(self, self.getContentElement());
152+
self._menuGesture = new MenuTargetGesture(self, self.getNativeElement());
148153

149-
if (isFalseProperty(self.swipeEnabled)) {
150-
self.isSwipeEnabled = false;
154+
// register listeners if this menu is enabled
155+
// check if more than one menu is on the same side
156+
let hasEnabledSameSideMenu = self._menuCtrl.getMenus().some(m => {
157+
return m.side === self.side && m.enabled;
158+
});
159+
if (hasEnabledSameSideMenu) {
160+
// auto-disable if another menu on the same side is already enabled
161+
self._isEnabled = false;
151162
}
163+
self._setListeners();
164+
165+
// create a reusable click handler on this instance, but don't assign yet
166+
self.onContentClick = function(ev: UIEvent) {
167+
if (self._isEnabled) {
168+
ev.preventDefault();
169+
ev.stopPropagation();
170+
self.close();
171+
}
172+
};
152173

153174
self._cntEle.classList.add('menu-content');
154175
self._cntEle.classList.add('menu-content-' + self.type);
@@ -157,6 +178,34 @@ export class Menu extends Ion {
157178
self._menuCtrl.register(self);
158179
}
159180

181+
/**
182+
* @private
183+
*/
184+
private _setListeners() {
185+
let self = this;
186+
187+
if (self._init) {
188+
// only listen/unlisten if the menu has initialized
189+
190+
if (self._isEnabled && self._isSwipeEnabled && !self._isListening) {
191+
// should listen, but is not currently listening
192+
console.debug('menu, gesture listen', self.side);
193+
self._zone.runOutsideAngular(function() {
194+
self._cntGesture.listen();
195+
self._menuGesture.listen();
196+
});
197+
self._isListening = true;
198+
199+
} else if (self._isListening && (!self._isEnabled || !self._isSwipeEnabled)) {
200+
// should not listen, but is currently listening
201+
console.debug('menu, gesture unlisten', self.side);
202+
self._cntGesture.unlisten();
203+
self._menuGesture.unlisten();
204+
self._isListening = false;
205+
}
206+
}
207+
}
208+
160209
/**
161210
* @private
162211
*/
@@ -176,7 +225,7 @@ export class Menu extends Ion {
176225
* @param {boolean} shouldOpen If the Menu is open or not.
177226
* @return {Promise} returns a promise once set
178227
*/
179-
setOpen(shouldOpen): Promise<boolean> {
228+
setOpen(shouldOpen: boolean): Promise<boolean> {
180229
// _isPrevented is used to prevent unwanted opening/closing after swiping open/close
181230
// or swiping open the menu while pressing down on the menuToggle button
182231
if ((shouldOpen && this.isOpen) || this._isPrevented()) {
@@ -198,7 +247,7 @@ export class Menu extends Ion {
198247
*/
199248
setProgressStart() {
200249
// user started swiping the menu open/close
201-
if (this._isPrevented() || !this.isEnabled || !this.isSwipeEnabled) return;
250+
if (this._isPrevented() || !this._isEnabled || !this._isSwipeEnabled) return;
202251

203252
this._before();
204253
this._getType().setProgressStart(this.isOpen);
@@ -209,7 +258,7 @@ export class Menu extends Ion {
209258
*/
210259
setProgessStep(stepValue: number) {
211260
// user actively dragging the menu
212-
if (this.isEnabled && this.isSwipeEnabled) {
261+
if (this._isEnabled && this._isSwipeEnabled) {
213262
this._prevent();
214263
this._getType().setProgessStep(stepValue);
215264
this.opening.next(stepValue);
@@ -221,7 +270,7 @@ export class Menu extends Ion {
221270
*/
222271
setProgressEnd(shouldComplete: boolean, currentStepValue: number) {
223272
// user has finished dragging the menu
224-
if (this.isEnabled && this.isSwipeEnabled) {
273+
if (this._isEnabled && this._isSwipeEnabled) {
225274
this._prevent();
226275
this._getType().setProgressEnd(shouldComplete, currentStepValue, (isOpen) => {
227276
console.debug('menu, progress end', this.side);
@@ -236,7 +285,7 @@ export class Menu extends Ion {
236285
private _before() {
237286
// this places the menu into the correct location before it animates in
238287
// this css class doesn't actually kick off any animations
239-
if (this.isEnabled) {
288+
if (this._isEnabled) {
240289
this.getNativeElement().classList.add('show-menu');
241290
this.getBackdropElement().classList.add('show-backdrop');
242291

@@ -252,7 +301,7 @@ export class Menu extends Ion {
252301
// keep opening/closing the menu disabled for a touch more yet
253302
// only add listeners/css if it's enabled and isOpen
254303
// and only remove listeners/css if it's not open
255-
if ((this.isEnabled && isOpen) || !isOpen) {
304+
if ((this._isEnabled && isOpen) || !isOpen) {
256305
this._prevent();
257306

258307
this.isOpen = isOpen;
@@ -318,7 +367,7 @@ export class Menu extends Ion {
318367
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
319368
*/
320369
enable(shouldEnable: boolean): Menu {
321-
this.isEnabled = shouldEnable;
370+
this.enabled = shouldEnable;
322371
if (!shouldEnable && this.isOpen) {
323372
this.close();
324373
}
@@ -331,7 +380,7 @@ export class Menu extends Ion {
331380
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
332381
*/
333382
swipeEnable(shouldEnable: boolean): Menu {
334-
this.isSwipeEnabled = shouldEnable;
383+
this.swipeEnabled = shouldEnable;
335384
return this;
336385
}
337386

ionic/components/menu/test/multiple/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {App, Page, MenuController} from '../../../../../ionic/ionic';
55
templateUrl: 'page1.html'
66
})
77
class Page1 {
8-
constructor(menu: MenuController) {
9-
this.menu = menu;
8+
activeMenu: string;
9+
10+
constructor(private menu: MenuController) {
1011
this.menu1Active();
1112
}
1213
menu1Active() {
@@ -26,6 +27,8 @@ class Page1 {
2627
templateUrl: 'main.html'
2728
})
2829
class E2EApp {
30+
rootPage;
31+
2932
constructor() {
3033
this.rootPage = Page1;
3134
}

ionic/gestures/gesture.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ export class Gesture {
3333
assign(this._options, opts);
3434
}
3535

36-
on(type, cb) {
36+
on(type: string, cb: Function) {
3737
if(type == 'pinch' || type == 'rotate') {
3838
this._hammer.get('pinch').set({enable: true});
3939
}
4040
this._hammer.on(type, cb);
4141
(this._callbacks[type] || (this._callbacks[type] = [])).push(cb);
4242
}
4343

44-
off(type, cb) {
44+
off(type: string, cb: Function) {
4545
this._hammer.off(type, this._callbacks[type] ? cb : null);
4646
}
4747

@@ -50,19 +50,20 @@ export class Gesture {
5050
}
5151

5252
unlisten() {
53+
var type, i;
5354
if (this._hammer) {
54-
for (let type in this._callbacks) {
55-
for (let i = 0; i < this._callbacks[type].length; i++) {
55+
for (type in this._callbacks) {
56+
for (i = 0; i < this._callbacks[type].length; i++) {
5657
this._hammer.off(type, this._callbacks[type]);
5758
}
5859
}
59-
this._hammer.destroy();
60-
this._hammer = null;
6160
this._callbacks = {};
61+
this._hammer.destroy();
6262
}
6363
}
6464

6565
destroy() {
66-
this.unlisten()
66+
this.unlisten();
67+
this._hammer = this.element = this._options = null;
6768
}
6869
}

0 commit comments

Comments
 (0)