Skip to content

Commit f18a624

Browse files
committed
fix(menu): fix swipe to go back and left menu conflict
Closes #5575
1 parent b742e1f commit f18a624

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

src/components/menu/menu-controller.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ export class MenuController {
156156
return Promise.resolve(false);
157157
}
158158

159+
tempDisable(temporarilyDisable: boolean) {
160+
this._menus.forEach(menu => {
161+
menu.tempDisable(temporarilyDisable);
162+
});
163+
}
164+
159165
/**
160166
* Toggle the menu. If it's closed, it will open, and if opened, it
161167
* will close.

src/components/menu/menu.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export class Menu extends Ion {
198198
private _isSwipeEnabled: boolean = true;
199199
private _isPers: boolean = false;
200200
private _init: boolean = false;
201+
private _prevEnabled: boolean;
201202

202203
/**
203204
* @private
@@ -419,10 +420,10 @@ export class Menu extends Ion {
419420
*/
420421
swipeStart() {
421422
// user started swiping the menu open/close
422-
if (this._isPrevented() || !this._isEnabled || !this._isSwipeEnabled) return;
423-
424-
this._before();
425-
this._getType().setProgressStart(this.isOpen);
423+
if (this._isEnabled && this._isSwipeEnabled && !this._isPrevented()) {
424+
this._before();
425+
this._getType().setProgressStart(this.isOpen);
426+
}
426427
}
427428

428429
/**
@@ -451,9 +452,6 @@ export class Menu extends Ion {
451452
}
452453
}
453454

454-
/**
455-
* @private
456-
*/
457455
private _before() {
458456
// this places the menu into the correct location before it animates in
459457
// this css class doesn't actually kick off any animations
@@ -466,9 +464,6 @@ export class Menu extends Ion {
466464
}
467465
}
468466

469-
/**
470-
* @private
471-
*/
472467
private _after(isOpen: boolean) {
473468
// keep opening/closing the menu disabled for a touch more yet
474469
// only add listeners/css if it's enabled and isOpen
@@ -495,15 +490,24 @@ export class Menu extends Ion {
495490
/**
496491
* @private
497492
*/
493+
tempDisable(temporarilyDisable: boolean) {
494+
if (temporarilyDisable) {
495+
this._prevEnabled = this._isEnabled;
496+
this._getType().setProgessStep(0);
497+
this.enable(false);
498+
499+
} else {
500+
this.enable(this._prevEnabled);
501+
this._after(false);
502+
}
503+
}
504+
498505
private _prevent() {
499506
// used to prevent unwanted opening/closing after swiping open/close
500507
// or swiping open the menu while pressing down on the MenuToggle
501508
this._preventTime = Date.now() + 20;
502509
}
503510

504-
/**
505-
* @private
506-
*/
507511
private _isPrevented() {
508512
return this._preventTime > Date.now();
509513
}

src/components/menu/test/basic/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {App, IonicApp, MenuController, Page, NavController, Alert} from '../../../../../src';
1+
import {ViewChild} from '@angular/core';
2+
import {App, IonicApp, MenuController, Page, NavController, Alert, Nav} from '../../../../../src';
23

34

45
@Page({
@@ -16,6 +17,10 @@ class Page1 {
1617
});
1718
this.nav.present(alert);
1819
}
20+
21+
goToPage2() {
22+
this.nav.push(Page2);
23+
}
1924
}
2025

2126

@@ -40,6 +45,7 @@ class E2EApp {
4045
rootPage;
4146
changeDetectionCount: number = 0;
4247
pages: Array<{title: string, component: any}>;
48+
@ViewChild(Nav) nav: Nav;
4349

4450
constructor(private app: IonicApp, private menu: MenuController) {
4551
this.rootPage = Page1;
@@ -54,8 +60,7 @@ class E2EApp {
5460
openPage(page) {
5561
// Reset the content nav to have just this page
5662
// we wouldn't want the back button to show in this scenario
57-
let nav = this.app.getComponent('nav');
58-
nav.setRoot(page.component).then(() => {
63+
this.nav.setRoot(page.component).then(() => {
5964
// wait for the root page to be completely loaded
6065
// then close the menu
6166
this.menu.close();

src/components/menu/test/basic/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,6 @@
138138

139139
</ion-menu>
140140

141-
<ion-nav id="nav" [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
141+
<ion-nav id="nav" [root]="rootPage" #content></ion-nav>
142142

143143
<div [hidden]="isChangeDetecting()"></div>

src/components/menu/test/basic/page1.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ <h3>Page 1</h3>
5151
<button (click)="presentAlert()">Open alert</button>
5252
</p>
5353

54+
<p>
55+
<button (click)="goToPage2()">Go to Page 2</button>
56+
</p>
57+
5458
<f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f>
5559

5660
</ion-content>

src/components/nav/nav-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {IonicApp} from '../app/app';
66
import {Keyboard} from '../../util/keyboard';
77
import {NavParams} from './nav-params';
88
import {pascalCaseToDashCase, isBlank} from '../../util/util';
9+
import {MenuController} from '../menu/menu-controller';
910
import {NavPortal} from './nav-portal';
1011
import {SwipeBackGesture} from './swipe-back';
1112
import {Transition} from '../../transitions/transition';
@@ -1555,7 +1556,8 @@ export class NavController extends Ion {
15551556
edge: 'left',
15561557
threshold: this._sbThreshold
15571558
};
1558-
this._sbGesture = new SwipeBackGesture(this.getNativeElement(), opts, this);
1559+
let menuCtrl = this._app.getAppInjector().get(MenuController);
1560+
this._sbGesture = new SwipeBackGesture(this.getNativeElement(), opts, this, menuCtrl);
15591561
}
15601562

15611563
if (this.canSwipeBack()) {

src/components/nav/swipe-back.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {MenuController} from '../menu/menu-controller';
12
import {NavController} from './nav-controller';
23
import {SlideEdgeGesture} from '../../gestures/slide-edge-gesture';
34
import {SlideData} from '../../gestures/slide-gesture';
@@ -9,7 +10,8 @@ export class SwipeBackGesture extends SlideEdgeGesture {
910
constructor(
1011
element: HTMLElement,
1112
options: any,
12-
private _nav: NavController
13+
private _nav: NavController,
14+
private _menuCtrl: MenuController
1315
) {
1416
super(element, assign({
1517
direction: 'x',
@@ -33,9 +35,11 @@ export class SwipeBackGesture extends SlideEdgeGesture {
3335
return false;
3436
}
3537

36-
onSlideBeforeStart() {
37-
console.debug('swipeBack, onSlideBeforeStart');
38+
onSlideBeforeStart(slideData: SlideData, ev: any) {
39+
console.debug('swipeBack, onSlideBeforeStart', ev.srcEvent.type);
3840
this._nav.swipeBackStart();
41+
42+
this._menuCtrl.tempDisable(true);
3943
}
4044

4145
onSlide(slide: SlideData) {
@@ -52,6 +56,8 @@ export class SwipeBackGesture extends SlideEdgeGesture {
5256
console.debug('swipeBack, onSlideEnd, shouldComplete', shouldComplete, 'currentStepValue', currentStepValue);
5357

5458
this._nav.swipeBackEnd(shouldComplete, currentStepValue);
59+
60+
this._menuCtrl.tempDisable(false);
5561
}
5662

5763
}

0 commit comments

Comments
 (0)