isSwipeBackEnabled

This commit is contained in:
Adam Bradley
2015-07-17 14:43:30 -05:00
parent a1c5936b17
commit 57734f87f6
4 changed files with 39 additions and 20 deletions

View File

@@ -9,7 +9,7 @@
</ion-content>
</ion-aside>
<ion-nav #content [register]="content" register-id="myNav"></ion-nav>
<ion-nav #content [register]="content" register-id="myNav" swipe-back-enabled="false"></ion-nav>
<style>
my-modal {

View File

@@ -1,4 +1,4 @@
import {Component, Directive, View, ElementRef, Parent, Optional, forwardRef, onInit, Injector} from 'angular2/angular2';
import {Directive, View, ElementRef, Parent, Optional, forwardRef, Injector} from 'angular2/angular2';
import {IonicComponent} from '../../config/annotations';
import {ViewController} from '../view/view-controller';
@@ -9,7 +9,9 @@ import {ViewController} from '../view/view-controller';
properties: [
'root'
],
lifecycle: [onInit]
defaultProperties: {
'swipeBackEnabled': true
}
})
@View({
template: '<template pane-anchor></template>',
@@ -25,10 +27,14 @@ export class Nav extends ViewController {
super(parentViewCtrl, injector, elementRef);
}
onInit() {
onIonInit() {
if (this.root) {
this.push(this.root);
}
// default the swipe back to be enabled
let isSwipeBackEnabled = (this.swipeBackEnabled || '').toString() !== 'false';
this.isSwipeBackEnabled( isSwipeBackEnabled );
}
}

View File

@@ -17,7 +17,8 @@ export class SwipeHandle {
@Parent() @Inject(forwardRef(() => Pane)) pane: Pane,
elementRef: ElementRef
) {
if (!viewCtrl || !pane) return;
if (!viewCtrl || !viewCtrl.isSwipeBackEnabled() || !pane) return;
const self = this;
@@ -31,7 +32,7 @@ export class SwipeHandle {
self.onDragHorizontal(ev);
}
gesture.on('panend', ev => { self.onDragEnd(ev); });
gesture.on('panend', gestureEv => { self.onDragEnd(gestureEv.gesture); });
gesture.on('panleft', dragHorizontal);
gesture.on('panright', dragHorizontal);
@@ -39,12 +40,12 @@ export class SwipeHandle {
self.width = null;
}
onDragEnd(ev) {
ev.preventDefault();
ev.stopPropagation();
onDragEnd(gesture) {
gesture.srcEvent.preventDefault();
gesture.srcEvent.stopPropagation();
// TODO: POLISH THESE NUMBERS WITH GOOD MATHIFICATION
let progress = (ev.gesture.center.x - this.startX) / this.width;
let progress = (gesture.center.x - this.startX) / this.width;
let completeSwipeBack = (progress > 0.5);
let playbackRate = 4;
@@ -74,23 +75,25 @@ export class SwipeHandle {
this.startX = null;
}
onDragHorizontal(ev) {
onDragHorizontal(gestureEv) {
let gesture = gestureEv.gesture;
if (this.startX === null) {
// starting drag
ev.preventDefault();
ev.stopPropagation();
gesture.srcEvent.preventDefault();
gesture.srcEvent.stopPropagation();
this.startX = ev.gesture.center.x;
this.startX = gesture.center.x;
this.width = this.pane.width() - this.startX;
this.viewCtrl.swipeBackStart();
}
this.viewCtrl.swipeBackProgress( (ev.gesture.center.x - this.startX) / this.width );
this.viewCtrl.swipeBackProgress( (gesture.center.x - this.startX) / this.width );
}
get showHandle() {
return (this.viewCtrl ? this.viewCtrl.swipeBackEnabled() : false);
return (this.viewCtrl ? this.viewCtrl.canSwipeBack() : false);
}
onDestroy() {

View File

@@ -38,6 +38,7 @@ export class ViewController extends Ion {
this.sbTransition = null;
this.sbActive = false;
this.sbEnabled = true;
this.id = ++ctrlIds;
this._ids = -1;
@@ -364,10 +365,19 @@ export class ViewController extends Ion {
}
}
swipeBackEnabled() {
let activeItem = this.getActive();
if (activeItem) {
return activeItem.enableBack();
isSwipeBackEnabled(val) {
if (arguments.length) {
this.sbEnabled = !!val;
}
return this.sbEnabled;
}
canSwipeBack() {
if (this.sbEnabled) {
let activeItem = this.getActive();
if (activeItem) {
return activeItem.enableBack();
}
}
return false;
}