Skip to content

Commit

Permalink
feat(gesture-controller): disable/enable scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jul 13, 2016
1 parent d230cb4 commit 72c24bc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/components/alert/test/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class E2EPage {
</ion-header>
<ion-content padding>
Hi, I'm Bob, and I'm a modal.
<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>
`
})
Expand Down
26 changes: 23 additions & 3 deletions src/components/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class App {
*/
clickBlock: ClickBlock;

/**
* @private
*/
appRoot: AppRoot;

viewDidLoad: EventEmitter<any> = new EventEmitter();
viewWillEnter: EventEmitter<any> = new EventEmitter();
viewDidEnter: EventEmitter<any> = new EventEmitter();
Expand Down Expand Up @@ -86,6 +91,17 @@ export class App {
}
}

/**
* @private
*/
setScrollDisabled(disabled: boolean) {
if (!this.appRoot) {
console.error('appRoot is missing, scrolling can not be enabled/disabled');
return;
}
this.appRoot.disableScroll = disabled;
}

/**
* @private
* Boolean if the app is actively enabled or not.
Expand Down Expand Up @@ -282,9 +298,13 @@ export class AppRoot {
@ViewChild('anchor', {read: ViewContainerRef}) private _viewport: ViewContainerRef;

constructor(
private _cmp: UserComponent,
private _cr: ComponentResolver,
private _renderer: Renderer) {}
private _cmp: UserComponent,
private _cr: ComponentResolver,
private _renderer: Renderer,
app: App
) {
app.appRoot = this;
}

ngAfterViewInit() {
// load the user app's root component
Expand Down
34 changes: 7 additions & 27 deletions src/components/backdrop/backdrop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Directive, ElementRef, Input } from '@angular/core';

import { AppRoot } from '../app/app';
import { DisableScroll, GestureController, GestureDelegate } from '../../gestures/gesture-controller';
import { isTrueProperty } from '../../util/util';


Expand All @@ -16,41 +16,21 @@ import { isTrueProperty } from '../../util/util';
},
})
export class Backdrop {
private static nuBackDrops: number = 0;

private static push(appRoot: AppRoot) {
if (this.nuBackDrops === 0) {
appRoot.disableScroll = true;
}
this.nuBackDrops++;
}

private static pop(appRoot: AppRoot) {
if (this.nuBackDrops > 0) {
this.nuBackDrops--;

if (this.nuBackDrops === 0) {
appRoot.disableScroll = false;
}
}
}

private pushed: boolean = false;
private _gestureID: number = null;
@Input() disableScroll = true;

constructor(private _appRoot: AppRoot, private _elementRef: ElementRef) {}
constructor(private _gestureCtrl: GestureController, private _elementRef: ElementRef) {}

ngOnInit() {
if (isTrueProperty(this.disableScroll)) {
Backdrop.push(this._appRoot);
this.pushed = true;
this._gestureID = this._gestureCtrl.newID();
this._gestureCtrl.disableScroll(this._gestureID);
}
}

ngOnDestroy() {
if (this.pushed) {
Backdrop.pop(this._appRoot);
this.pushed = false;
if (this._gestureID) {
this._gestureCtrl.enableScroll(this._gestureID);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function ionicProviders(customProviders?: Array<any>, config?: any): any[
provide(Events, {useValue: events}),
provide(FeatureDetect, {useValue: featureDetect}),
Form,
GestureController,
HTTP_PROVIDERS,
Keyboard,
LoadingController,
Expand All @@ -78,7 +79,6 @@ export function ionicProviders(customProviders?: Array<any>, config?: any): any[
TapClick,
ToastController,
Translate,
GestureController,
];

if (isPresent(customProviders)) {
Expand Down
22 changes: 14 additions & 8 deletions src/gestures/gesture-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import { Injectable } from '@angular/core';

import { forwardRef, Inject, Injectable } from '@angular/core';
import { App } from '../components/app/app';

export const enum GesturePriority {
Expand Down Expand Up @@ -30,12 +29,17 @@ export class GestureController {
private requestedStart: { [eventId: number]: number } = {};
private disabledGestures: { [eventName: string]: Set<number> } = {};
private disabledScroll: Set<number> = new Set<number>();
private appRoot: App;
private capturedID: number = null;

constructor(@Inject(forwardRef(() => App)) private _app: App) { }

create(name: string, opts: GestureOptions = {}): GestureDelegate {
return new GestureDelegate(name, this.newID(), this, opts);
}

newID(): number {
let id = this.id; this.id++;
return new GestureDelegate(name, id, this, opts);
return id;
}

start(gestureName: string, id: number, priority: number): boolean {
Expand Down Expand Up @@ -94,16 +98,18 @@ export class GestureController {
disableScroll(id: number) {
let isEnabled = !this.isScrollDisabled();
this.disabledScroll.add(id);
if (isEnabled && this.isScrollDisabled()) {
// this.appRoot.disableScroll = true;
if (this._app && isEnabled && this.isScrollDisabled()) {
console.debug('GestureController: Disabling scrolling');
this._app.setScrollDisabled(true);
}
}

enableScroll(id: number) {
let isDisabled = this.isScrollDisabled();
this.disabledScroll.delete(id);
if (isDisabled && !this.isScrollDisabled()) {
// this.appRoot.disableScroll = false;
if (this._app && isDisabled && !this.isScrollDisabled()) {
console.debug('GestureController: Enabling scrolling');
this._app.setScrollDisabled(false);
}
}

Expand Down

0 comments on commit 72c24bc

Please sign in to comment.