Skip to content

Commit

Permalink
fix(menu): fix swipe to go back and left menu conflict
Browse files Browse the repository at this point in the history
Closes #5575
  • Loading branch information
adamdbradley committed May 20, 2016
1 parent b742e1f commit f18a624
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/components/menu/menu-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ export class MenuController {
return Promise.resolve(false);
}

tempDisable(temporarilyDisable: boolean) {
this._menus.forEach(menu => {
menu.tempDisable(temporarilyDisable);
});
}

/**
* Toggle the menu. If it's closed, it will open, and if opened, it
* will close.
Expand Down
30 changes: 17 additions & 13 deletions src/components/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export class Menu extends Ion {
private _isSwipeEnabled: boolean = true;
private _isPers: boolean = false;
private _init: boolean = false;
private _prevEnabled: boolean;

/**
* @private
Expand Down Expand Up @@ -419,10 +420,10 @@ export class Menu extends Ion {
*/
swipeStart() {
// user started swiping the menu open/close
if (this._isPrevented() || !this._isEnabled || !this._isSwipeEnabled) return;

this._before();
this._getType().setProgressStart(this.isOpen);
if (this._isEnabled && this._isSwipeEnabled && !this._isPrevented()) {
this._before();
this._getType().setProgressStart(this.isOpen);
}
}

/**
Expand Down Expand Up @@ -451,9 +452,6 @@ export class Menu extends Ion {
}
}

/**
* @private
*/
private _before() {
// this places the menu into the correct location before it animates in
// this css class doesn't actually kick off any animations
Expand All @@ -466,9 +464,6 @@ export class Menu extends Ion {
}
}

/**
* @private
*/
private _after(isOpen: boolean) {
// keep opening/closing the menu disabled for a touch more yet
// only add listeners/css if it's enabled and isOpen
Expand All @@ -495,15 +490,24 @@ export class Menu extends Ion {
/**
* @private
*/
tempDisable(temporarilyDisable: boolean) {
if (temporarilyDisable) {
this._prevEnabled = this._isEnabled;
this._getType().setProgessStep(0);
this.enable(false);

} else {
this.enable(this._prevEnabled);
this._after(false);
}
}

private _prevent() {
// used to prevent unwanted opening/closing after swiping open/close
// or swiping open the menu while pressing down on the MenuToggle
this._preventTime = Date.now() + 20;
}

/**
* @private
*/
private _isPrevented() {
return this._preventTime > Date.now();
}
Expand Down
11 changes: 8 additions & 3 deletions src/components/menu/test/basic/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {App, IonicApp, MenuController, Page, NavController, Alert} from '../../../../../src';
import {ViewChild} from '@angular/core';
import {App, IonicApp, MenuController, Page, NavController, Alert, Nav} from '../../../../../src';


@Page({
Expand All @@ -16,6 +17,10 @@ class Page1 {
});
this.nav.present(alert);
}

goToPage2() {
this.nav.push(Page2);
}
}


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

constructor(private app: IonicApp, private menu: MenuController) {
this.rootPage = Page1;
Expand All @@ -54,8 +60,7 @@ class E2EApp {
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
let nav = this.app.getComponent('nav');
nav.setRoot(page.component).then(() => {
this.nav.setRoot(page.component).then(() => {
// wait for the root page to be completely loaded
// then close the menu
this.menu.close();
Expand Down
2 changes: 1 addition & 1 deletion src/components/menu/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@

</ion-menu>

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

<div [hidden]="isChangeDetecting()"></div>
4 changes: 4 additions & 0 deletions src/components/menu/test/basic/page1.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ <h3>Page 1</h3>
<button (click)="presentAlert()">Open alert</button>
</p>

<p>
<button (click)="goToPage2()">Go to Page 2</button>
</p>

<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>

</ion-content>
4 changes: 3 additions & 1 deletion src/components/nav/nav-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IonicApp} from '../app/app';
import {Keyboard} from '../../util/keyboard';
import {NavParams} from './nav-params';
import {pascalCaseToDashCase, isBlank} from '../../util/util';
import {MenuController} from '../menu/menu-controller';
import {NavPortal} from './nav-portal';
import {SwipeBackGesture} from './swipe-back';
import {Transition} from '../../transitions/transition';
Expand Down Expand Up @@ -1555,7 +1556,8 @@ export class NavController extends Ion {
edge: 'left',
threshold: this._sbThreshold
};
this._sbGesture = new SwipeBackGesture(this.getNativeElement(), opts, this);
let menuCtrl = this._app.getAppInjector().get(MenuController);
this._sbGesture = new SwipeBackGesture(this.getNativeElement(), opts, this, menuCtrl);
}

if (this.canSwipeBack()) {
Expand Down
12 changes: 9 additions & 3 deletions src/components/nav/swipe-back.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {MenuController} from '../menu/menu-controller';
import {NavController} from './nav-controller';
import {SlideEdgeGesture} from '../../gestures/slide-edge-gesture';
import {SlideData} from '../../gestures/slide-gesture';
Expand All @@ -9,7 +10,8 @@ export class SwipeBackGesture extends SlideEdgeGesture {
constructor(
element: HTMLElement,
options: any,
private _nav: NavController
private _nav: NavController,
private _menuCtrl: MenuController
) {
super(element, assign({
direction: 'x',
Expand All @@ -33,9 +35,11 @@ export class SwipeBackGesture extends SlideEdgeGesture {
return false;
}

onSlideBeforeStart() {
console.debug('swipeBack, onSlideBeforeStart');
onSlideBeforeStart(slideData: SlideData, ev: any) {
console.debug('swipeBack, onSlideBeforeStart', ev.srcEvent.type);
this._nav.swipeBackStart();

this._menuCtrl.tempDisable(true);
}

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

this._nav.swipeBackEnd(shouldComplete, currentStepValue);

this._menuCtrl.tempDisable(false);
}

}

0 comments on commit f18a624

Please sign in to comment.