Skip to content

Commit

Permalink
fix(gestures): gesture controller is handled by components
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Nov 15, 2016
1 parent 909293a commit ca5bce2
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 251 deletions.
19 changes: 18 additions & 1 deletion src/components/action-sheet/action-sheet-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Form } from '../../util/form';
import { Key } from '../../util/key';
import { NavParams } from '../../navigation/nav-params';
import { ViewController } from '../../navigation/view-controller';

import { BlockerDelegate, GestureController, BLOCK_ALL } from '../../gestures/gesture-controller';
import { assert } from '../../util/util';

/**
* @private
Expand Down Expand Up @@ -53,15 +54,18 @@ export class ActionSheetCmp {
hdrId: string;
id: number;
mode: string;
gestureBlocker: BlockerDelegate;

constructor(
private _viewCtrl: ViewController,
private _config: Config,
private _elementRef: ElementRef,
private _form: Form,
gestureCtrl: GestureController,
params: NavParams,
renderer: Renderer
) {
this.gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
this.d = params.data;
this.mode = _config.get('mode');
renderer.setElementClass(_elementRef.nativeElement, `action-sheet-${this.mode}`, true);
Expand Down Expand Up @@ -110,6 +114,14 @@ export class ActionSheetCmp {
this.d.buttons = buttons;
}

ionViewWillEnter() {
this.gestureBlocker.block();
}

ionViewDidLeave() {
this.gestureBlocker.unblock();
}

ionViewDidEnter() {
this._form.focusOut();

Expand Down Expand Up @@ -166,6 +178,11 @@ export class ActionSheetCmp {
dismiss(role: any): Promise<any> {
return this._viewCtrl.dismiss(null, role);
}

ngOnDestroy() {
assert(this.gestureBlocker.blocked === false, 'gesture blocker must be already unblocked');
this.gestureBlocker.destroy();
}
}

let actionSheetIds = -1;
47 changes: 32 additions & 15 deletions src/components/alert/alert-component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Component, ElementRef, HostListener, Renderer, ViewEncapsulation } from '@angular/core';

import { Config } from '../../config/config';
import { isPresent } from '../../util/util';
import { isPresent, assert } from '../../util/util';
import { Key } from '../../util/key';
import { NavParams } from '../../navigation/nav-params';
import { ViewController } from '../../navigation/view-controller';

import { GestureController, BlockerDelegate, BLOCK_ALL } from '../../gestures/gesture-controller';

/**
* @private
Expand Down Expand Up @@ -86,14 +86,18 @@ export class AlertCmp {
msgId: string;
subHdrId: string;
mode: string;
gestureBlocker: BlockerDelegate;

constructor(
public _viewCtrl: ViewController,
public _elementRef: ElementRef,
public _config: Config,
gestureCtrl: GestureController,
params: NavParams,
renderer: Renderer
) {
// gesture blocker is used to disable gestures dynamically
this.gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
this.d = params.data;
this.mode = _config.get('mode');
renderer.setElementClass(_elementRef.nativeElement, `alert-${this.mode}`, true);
Expand Down Expand Up @@ -172,6 +176,27 @@ export class AlertCmp {
}
}

ionViewWillEnter() {
this.gestureBlocker.block();
}

ionViewDidLeave() {
this.gestureBlocker.unblock();
}

ionViewDidEnter() {
let activeElement: any = document.activeElement;
if (document.activeElement) {
activeElement.blur();
}

let focusableEle = this._elementRef.nativeElement.querySelector('input,button');
if (focusableEle) {
focusableEle.focus();
}
this.enabled = true;
}

@HostListener('body:keyup', ['$event'])
keyUp(ev: KeyboardEvent) {
if (this.enabled && this._viewCtrl.isLast()) {
Expand All @@ -193,19 +218,6 @@ export class AlertCmp {
}
}

ionViewDidEnter() {
let activeElement: any = document.activeElement;
if (document.activeElement) {
activeElement.blur();
}

let focusableEle = this._elementRef.nativeElement.querySelector('input,button');
if (focusableEle) {
focusableEle.focus();
}
this.enabled = true;
}

btnClick(button: any, dismissDelay?: number) {
if (!this.enabled) {
return;
Expand Down Expand Up @@ -293,6 +305,11 @@ export class AlertCmp {
});
return values;
}

ngOnDestroy() {
assert(this.gestureBlocker.blocked === false, 'gesture blocker must be already unblocked');
this.gestureBlocker.destroy();
}
}

let alertIds = -1;
4 changes: 0 additions & 4 deletions src/components/backdrop/backdrop.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ ion-backdrop {
opacity: .01;
transform: translateZ(0);
}

ion-backdrop.hide-backdrop {
display: none;
}
25 changes: 3 additions & 22 deletions src/components/backdrop/backdrop.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { Directive, ElementRef, Input, Renderer } from '@angular/core';

import { GestureController } from '../../gestures/gesture-controller';
import { isTrueProperty } from '../../util/util';

import { Directive, ElementRef, Renderer } from '@angular/core';

/**
* @private
Expand All @@ -16,26 +12,11 @@ import { isTrueProperty } from '../../util/util';
},
})
export class Backdrop {
private _gestureID: number = null;
@Input() disableScroll = true;

constructor(
private _gestureCtrl: GestureController,
private _elementRef: ElementRef,
private _renderer: Renderer) { }

ngOnInit() {
if (isTrueProperty(this.disableScroll)) {
this._gestureID = this._gestureCtrl.newID();
this._gestureCtrl.disableScroll(this._gestureID);
}
}

ngOnDestroy() {
if (this._gestureID) {
this._gestureCtrl.enableScroll(this._gestureID);
}
}
private _renderer: Renderer
) { }

getNativeElement(): HTMLElement {
return this._elementRef.nativeElement;
Expand Down
17 changes: 9 additions & 8 deletions src/components/item/item-sliding-gesture.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ItemSliding } from './item-sliding';
import { List } from '../list/list';

import { GesturePriority } from '../../gestures/gesture-controller';
import { GestureController, GesturePriority } from '../../gestures/gesture-controller';
import { PanGesture } from '../../gestures/drag-gesture';
import { pointerCoord } from '../../util/dom';
import { NativeRafDebouncer } from '../../util/debouncer';

const DRAG_THRESHOLD = 10;
const MAX_ATTACK_ANGLE = 20;

/**
* @private
*/
export class ItemSlidingGesture extends PanGesture {

private preSelectedContainer: ItemSliding = null;
Expand All @@ -17,13 +17,14 @@ export class ItemSlidingGesture extends PanGesture {
private firstCoordX: number;
private firstTimestamp: number;

constructor(public list: List) {
constructor(public list: List, gestureCtrl: GestureController) {
super(list.getNativeElement(), {
maxAngle: MAX_ATTACK_ANGLE,
threshold: DRAG_THRESHOLD,
maxAngle: 20,
threshold: 10,
zone: false,
debouncer: new NativeRafDebouncer(),
gesture: list._gestureCtrl.create('item-sliding', {
gesture: gestureCtrl.createGesture({
name: 'item-sliding',
priority: GesturePriority.SlidingItem,
})
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class List extends Ion {

} else if (!this._slidingGesture) {
console.debug('enableSlidingItems');
this._slidingGesture = new ItemSlidingGesture(this);
this._slidingGesture = new ItemSlidingGesture(this, this._gestureCtrl);
this._slidingGesture.listen();
}
}
Expand Down
29 changes: 21 additions & 8 deletions src/components/loading/loading-component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Component, ElementRef, Renderer, ViewEncapsulation } from '@angular/core';

import { Config } from '../../config/config';
import { isDefined, isUndefined } from '../../util/util';
import { isDefined, isUndefined, assert } from '../../util/util';
import { NavParams } from '../../navigation/nav-params';
import { ViewController } from '../../navigation/view-controller';
import { LoadingOptions } from './loading-options';
import { BlockerDelegate, GestureController, BLOCK_ALL } from '../../gestures/gesture-controller';

/**
* @private
*/
@Component({
selector: 'ion-loading',
template:
'<ion-backdrop [class.hide-backdrop]="!d.showBackdrop"></ion-backdrop>' +
'<ion-backdrop [hidden]="!d.showBackdrop"></ion-backdrop>' +
'<div class="loading-wrapper">' +
'<div *ngIf="showSpinner" class="loading-spinner">' +
'<ion-spinner [name]="d.spinner"></ion-spinner>' +
Expand All @@ -29,14 +30,17 @@ export class LoadingCmp {
id: number;
showSpinner: boolean;
durationTimeout: number;
gestureBlocker: BlockerDelegate;

constructor(
private _viewCtrl: ViewController,
private _config: Config,
private _elementRef: ElementRef,
gestureCtrl: GestureController,
params: NavParams,
renderer: Renderer
) {
this.gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
this.d = params.data;

renderer.setElementClass(_elementRef.nativeElement, `loading-${_config.get('mode')}`, true);
Expand All @@ -62,17 +66,21 @@ export class LoadingCmp {
this.showSpinner = isDefined(this.d.spinner) && this.d.spinner !== 'hide';
}

ionViewWillEnter() {
this.gestureBlocker.block();
}

ionViewDidLeave() {
this.gestureBlocker.unblock();
}

ionViewDidEnter() {
let activeElement: any = document.activeElement;
if (document.activeElement) {
activeElement.blur();
}
activeElement && activeElement.blur();

// If there is a duration, dismiss after that amount of time
if ( this.d && this.d.duration ) {
this.durationTimeout = (<any> setTimeout( () => {
this.dismiss('backdrop');
}, this.d.duration));
this.durationTimeout = setTimeout(() => this.dismiss('backdrop'), this.d.duration);
}

}
Expand All @@ -83,6 +91,11 @@ export class LoadingCmp {
}
return this._viewCtrl.dismiss(null, role);
}

ngOnDestroy() {
assert(this.gestureBlocker.blocked === false, 'gesture blocker must be already unblocked');
this.gestureBlocker.destroy();
}
}

let loadingIds = -1;
16 changes: 5 additions & 11 deletions src/components/menu/menu-gestures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Menu } from './menu';
import { SlideEdgeGesture } from '../../gestures/slide-edge-gesture';
import { SlideData } from '../../gestures/slide-gesture';
import { assign } from '../../util/util';
import { GestureController, GesturePriority } from '../../gestures/gesture-controller';
import { GestureController, GesturePriority, GESTURE_MENU_SWIPE } from '../../gestures/gesture-controller';
import { NativeRafDebouncer } from '../../util/debouncer';

/**
Expand All @@ -14,16 +14,17 @@ export class MenuContentGesture extends SlideEdgeGesture {
public menu: Menu,
contentEle: HTMLElement,
gestureCtrl: GestureController,
options: any = {}) {
options: any = {}
) {
super(contentEle, assign({
direction: 'x',
edge: menu.side,
threshold: 0,
maxEdgeStart: menu.maxEdgeStart || 50,
maxAngle: 40,
zone: false,
debouncer: new NativeRafDebouncer(),
gesture: gestureCtrl.create('menu-swipe', {
gesture: gestureCtrl.createGesture({
name: GESTURE_MENU_SWIPE,
priority: GesturePriority.MenuSwipe,
})
}, options));
Expand Down Expand Up @@ -52,13 +53,6 @@ export class MenuContentGesture extends SlideEdgeGesture {
let z = (this.menu.side === 'right' ? slide.min : slide.max);
let stepValue = (slide.distance / z);

console.debug('menu gesture, onSlide', this.menu.side,
'distance', slide.distance,
'min', slide.min,
'max', slide.max,
'z', z,
'stepValue', stepValue);

this.menu.swipeProgress(stepValue);
}

Expand Down
Loading

0 comments on commit ca5bce2

Please sign in to comment.