Skip to content

Commit 72c24bc

Browse files
committed
feat(gesture-controller): disable/enable scrolling
1 parent d230cb4 commit 72c24bc

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ class E2EPage {
304304
</ion-header>
305305
<ion-content padding>
306306
Hi, I'm Bob, and I'm a modal.
307+
<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>
307308
</ion-content>
308309
`
309310
})

src/components/app/app.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export class App {
3131
*/
3232
clickBlock: ClickBlock;
3333

34+
/**
35+
* @private
36+
*/
37+
appRoot: AppRoot;
38+
3439
viewDidLoad: EventEmitter<any> = new EventEmitter();
3540
viewWillEnter: EventEmitter<any> = new EventEmitter();
3641
viewDidEnter: EventEmitter<any> = new EventEmitter();
@@ -86,6 +91,17 @@ export class App {
8691
}
8792
}
8893

94+
/**
95+
* @private
96+
*/
97+
setScrollDisabled(disabled: boolean) {
98+
if (!this.appRoot) {
99+
console.error('appRoot is missing, scrolling can not be enabled/disabled');
100+
return;
101+
}
102+
this.appRoot.disableScroll = disabled;
103+
}
104+
89105
/**
90106
* @private
91107
* Boolean if the app is actively enabled or not.
@@ -282,9 +298,13 @@ export class AppRoot {
282298
@ViewChild('anchor', {read: ViewContainerRef}) private _viewport: ViewContainerRef;
283299

284300
constructor(
285-
private _cmp: UserComponent,
286-
private _cr: ComponentResolver,
287-
private _renderer: Renderer) {}
301+
private _cmp: UserComponent,
302+
private _cr: ComponentResolver,
303+
private _renderer: Renderer,
304+
app: App
305+
) {
306+
app.appRoot = this;
307+
}
288308

289309
ngAfterViewInit() {
290310
// load the user app's root component

src/components/backdrop/backdrop.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Directive, ElementRef, Input } from '@angular/core';
22

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

66

@@ -16,41 +16,21 @@ import { isTrueProperty } from '../../util/util';
1616
},
1717
})
1818
export class Backdrop {
19-
private static nuBackDrops: number = 0;
20-
21-
private static push(appRoot: AppRoot) {
22-
if (this.nuBackDrops === 0) {
23-
appRoot.disableScroll = true;
24-
}
25-
this.nuBackDrops++;
26-
}
27-
28-
private static pop(appRoot: AppRoot) {
29-
if (this.nuBackDrops > 0) {
30-
this.nuBackDrops--;
31-
32-
if (this.nuBackDrops === 0) {
33-
appRoot.disableScroll = false;
34-
}
35-
}
36-
}
37-
38-
private pushed: boolean = false;
19+
private _gestureID: number = null;
3920
@Input() disableScroll = true;
4021

41-
constructor(private _appRoot: AppRoot, private _elementRef: ElementRef) {}
22+
constructor(private _gestureCtrl: GestureController, private _elementRef: ElementRef) {}
4223

4324
ngOnInit() {
4425
if (isTrueProperty(this.disableScroll)) {
45-
Backdrop.push(this._appRoot);
46-
this.pushed = true;
26+
this._gestureID = this._gestureCtrl.newID();
27+
this._gestureCtrl.disableScroll(this._gestureID);
4728
}
4829
}
4930

5031
ngOnDestroy() {
51-
if (this.pushed) {
52-
Backdrop.pop(this._appRoot);
53-
this.pushed = false;
32+
if (this._gestureID) {
33+
this._gestureCtrl.enableScroll(this._gestureID);
5434
}
5535
}
5636

src/config/providers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function ionicProviders(customProviders?: Array<any>, config?: any): any[
6565
provide(Events, {useValue: events}),
6666
provide(FeatureDetect, {useValue: featureDetect}),
6767
Form,
68+
GestureController,
6869
HTTP_PROVIDERS,
6970
Keyboard,
7071
LoadingController,
@@ -78,7 +79,6 @@ export function ionicProviders(customProviders?: Array<any>, config?: any): any[
7879
TapClick,
7980
ToastController,
8081
Translate,
81-
GestureController,
8282
];
8383

8484
if (isPresent(customProviders)) {

src/gestures/gesture-controller.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

2-
import { Injectable } from '@angular/core';
3-
2+
import { forwardRef, Inject, Injectable } from '@angular/core';
43
import { App } from '../components/app/app';
54

65
export const enum GesturePriority {
@@ -30,12 +29,17 @@ export class GestureController {
3029
private requestedStart: { [eventId: number]: number } = {};
3130
private disabledGestures: { [eventName: string]: Set<number> } = {};
3231
private disabledScroll: Set<number> = new Set<number>();
33-
private appRoot: App;
3432
private capturedID: number = null;
3533

34+
constructor(@Inject(forwardRef(() => App)) private _app: App) { }
35+
3636
create(name: string, opts: GestureOptions = {}): GestureDelegate {
37+
return new GestureDelegate(name, this.newID(), this, opts);
38+
}
39+
40+
newID(): number {
3741
let id = this.id; this.id++;
38-
return new GestureDelegate(name, id, this, opts);
42+
return id;
3943
}
4044

4145
start(gestureName: string, id: number, priority: number): boolean {
@@ -94,16 +98,18 @@ export class GestureController {
9498
disableScroll(id: number) {
9599
let isEnabled = !this.isScrollDisabled();
96100
this.disabledScroll.add(id);
97-
if (isEnabled && this.isScrollDisabled()) {
98-
// this.appRoot.disableScroll = true;
101+
if (this._app && isEnabled && this.isScrollDisabled()) {
102+
console.debug('GestureController: Disabling scrolling');
103+
this._app.setScrollDisabled(true);
99104
}
100105
}
101106

102107
enableScroll(id: number) {
103108
let isDisabled = this.isScrollDisabled();
104109
this.disabledScroll.delete(id);
105-
if (isDisabled && !this.isScrollDisabled()) {
106-
// this.appRoot.disableScroll = false;
110+
if (this._app && isDisabled && !this.isScrollDisabled()) {
111+
console.debug('GestureController: Enabling scrolling');
112+
this._app.setScrollDisabled(false);
107113
}
108114
}
109115

0 commit comments

Comments
 (0)