summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/paletx/core/overlay/scroll/scroll-dispatcher.ts
blob: 2c145af555464f556918c2fd21472e4ca80f49e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
/* tslint:disable:array-type member-access variable-name typedef
 only-arrow-functions directive-class-suffix component-class-suffix
 component-selector*/
import {Platform} from '@angular/cdk';
import {auditTime} from 'rxjs/operator/auditTime';
import {ElementRef, Injectable, NgZone, Optional, SkipSelf} from '@angular/core';
import {fromEvent} from 'rxjs/observable/fromEvent';
import {merge} from 'rxjs/observable/merge';
import {Subject} from 'rxjs/Subject';
import {Subscription} from 'rxjs/Subscription';

import {Scrollable} from './scrollable';


/** Time in ms to throttle the scrolling events by default. */
export const DEFAULT_SCROLL_TIME = 20;

/**
 * Service contained all registered Scrollable references and emits an event
 * when any one of the Scrollable references emit a scrolled event.
 */
@Injectable()
export class ScrollDispatcher {
  /** Subject for notifying that a registered scrollable reference element has
   * been scrolled. */
  _scrolled: Subject<void> = new Subject<void>();

  /** Keeps track of the global `scroll` and `resize` subscriptions. */
  _globalSubscription: Subscription|null = null;

  /** Keeps track of the amount of subscriptions to `scrolled`. Used for
   * cleaning up afterwards. */
  private _scrolledCount = 0;

  /**
   * Map of all the scrollable references that are registered with the service
   * and their scroll event subscriptions.
   */
  scrollableReferences: Map<Scrollable, Subscription> = new Map();

  constructor(private _ngZone: NgZone, private _platform: Platform) {}

  /**
   * Registers a Scrollable with the service and listens for its scrolled
   * events. When the scrollable is scrolled, the service emits the event in its
   * scrolled observable.
   * @param scrollable Scrollable instance to be registered.
   */
  register(scrollable: Scrollable): void {
	const scrollSubscription =
		scrollable.elementScrolled().subscribe(() => this._notify());

	this.scrollableReferences.set(scrollable, scrollSubscription);
  }

  /**
   * Deregisters a Scrollable reference and unsubscribes from its scroll event
   * observable.
   * @param scrollable Scrollable instance to be deregistered.
   */
  deregister(scrollable: Scrollable): void {
	const scrollableReference = this.scrollableReferences.get(scrollable);

	if (scrollableReference) {
		scrollableReference.unsubscribe();
		this.scrollableReferences.delete(scrollable);
	}
  }

  /**
   * Subscribes to an observable that emits an event whenever any of the
   * registered Scrollable references (or window, document, or body) fire a
   * scrolled event. Can provide a time in ms to override the default "throttle"
   * time.
   */
  scrolled(auditTimeInMs: number = DEFAULT_SCROLL_TIME, callback: () => any):
		Subscription {
    // Scroll events can only happen on the browser, so do nothing if we're not
    // on the browser.
	if (!this._platform.isBrowser) {
		return Subscription.EMPTY;
	}

    // In the case of a 0ms delay, use an observable without auditTime
    // since it does add a perceptible delay in processing overhead.
	const observable = auditTimeInMs > 0 ?
		auditTime.call(this._scrolled.asObservable(), auditTimeInMs) :
		this._scrolled.asObservable();

	this._scrolledCount++;

	if (!this._globalSubscription) {
		this._globalSubscription = this._ngZone.runOutsideAngular(() => {
		return merge(
					fromEvent(window.document, 'scroll'),
					fromEvent(window, 'resize'))
			.subscribe(() => this._notify());
		});
	}

    // Note that we need to do the subscribing from here, in order to be able to
    // remove the global event listeners once there are no more subscriptions.
	const subscription = observable.subscribe(callback);

	subscription.add(() => {
		this._scrolledCount--;

		if (this._globalSubscription && !this.scrollableReferences.size &&
			!this._scrolledCount) {
		this._globalSubscription.unsubscribe();
		this._globalSubscription = null;
		}
	});

	return subscription;
  }

  /** Returns all registered Scrollables that contain the provided element. */
  getScrollContainers(elementRef: ElementRef): Scrollable[] {
	const scrollingContainers: Scrollable[] = [];

	this.scrollableReferences.forEach(
		(_subscription: Subscription, scrollable: Scrollable) => {
			if (this.scrollableContainsElement(scrollable, elementRef)) {
			scrollingContainers.push(scrollable);
			}
		});

	return scrollingContainers;
  }

  /** Returns true if the element is contained within the provided Scrollable.
   */
  scrollableContainsElement(scrollable: Scrollable, elementRef: ElementRef):
		boolean {
	let element = elementRef.nativeElement;
	const scrollableElement = scrollable.getElementRef().nativeElement;

    // Traverse through the element parents until we reach null, checking if any
    // of the elements are the scrollable's element.
	do {
		if (element === scrollableElement) {
		return true;
		}
	} while (element = element.parentElement);

	return false;
  }

  /** Sends a notification that a scroll event has been fired. */
  _notify() {
	this._scrolled.next();
  }
}

export function SCROLL_DISPATCHER_PROVIDER_FACTORY(
	parentDispatcher: ScrollDispatcher, ngZone: NgZone, platform: Platform) {
  return parentDispatcher || new ScrollDispatcher(ngZone, platform);
}

export const SCROLL_DISPATCHER_PROVIDER = {
  // If there is already a ScrollDispatcher available, use that. Otherwise,
  // provide a new one.
  provide: ScrollDispatcher,
  deps: [[new Optional(), new SkipSelf(), ScrollDispatcher], NgZone, Platform],
  useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY
};