UNPKG

8.54 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { defer, fromEvent, Subject } from 'rxjs';
9import { mapTo, mergeAll, mergeMap, startWith, takeUntil } from 'rxjs/operators';
10/**
11 * PointerFocusTracker keeps track of the currently active item under mouse focus. It also has
12 * observables which emit when the users mouse enters and leaves a tracked element.
13 */
14export class PointerFocusTracker {
15 constructor(
16 /** The list of items being tracked. */
17 _items) {
18 this._items = _items;
19 /** Emits when an element is moused into. */
20 this.entered = this._getItemPointerEntries();
21 /** Emits when an element is moused out. */
22 this.exited = this._getItemPointerExits();
23 /** Emits when this is destroyed. */
24 this._destroyed = new Subject();
25 this.entered.subscribe(element => (this.activeElement = element));
26 this.exited.subscribe(() => {
27 this.previousElement = this.activeElement;
28 this.activeElement = undefined;
29 });
30 }
31 /** Stop the managers listeners. */
32 destroy() {
33 this._destroyed.next();
34 this._destroyed.complete();
35 }
36 /**
37 * Gets a stream of pointer (mouse) entries into the given items.
38 * This should typically run outside the Angular zone.
39 */
40 _getItemPointerEntries() {
41 return defer(() => this._items.changes.pipe(startWith(this._items), mergeMap((list) => list.map(element => fromEvent(element._elementRef.nativeElement, 'mouseenter').pipe(mapTo(element), takeUntil(this._items.changes)))), mergeAll()));
42 }
43 /**
44 * Gets a stream of pointer (mouse) exits out of the given items.
45 * This should typically run outside the Angular zone.
46 */
47 _getItemPointerExits() {
48 return defer(() => this._items.changes.pipe(startWith(this._items), mergeMap((list) => list.map(element => fromEvent(element._elementRef.nativeElement, 'mouseout').pipe(mapTo(element), takeUntil(this._items.changes)))), mergeAll()));
49 }
50}
51//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9pbnRlci1mb2N1cy10cmFja2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4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xcbiAgICAgICAgICAgICksXG4gICAgICAgICAgKSxcbiAgICAgICAgKSxcbiAgICAgICAgbWVyZ2VBbGwoKSxcbiAgICAgICksXG4gICAgKTtcbiAgfVxuXG4gIC8qKlxuICAgKiBHZXRzIGEgc3RyZWFtIG9mIHBvaW50ZXIgKG1vdXNlKSBleGl0cyBvdXQgb2YgdGhlIGdpdmVuIGl0ZW1zLlxuICAgKiBUaGlzIHNob3VsZCB0eXBpY2FsbHkgcnVuIG91dHNpZGUgdGhlIEFuZ3VsYXIgem9uZS5cbiAgICovXG4gIHByaXZhdGUgX2dldEl0ZW1Qb2ludGVyRXhpdHMoKSB7XG4gICAgcmV0dXJuIGRlZmVyKCgpID0+XG4gICAgICB0aGlzLl9pdGVtcy5jaGFuZ2VzLnBpcGUoXG4gICAgICAgIHN0YXJ0V2l0aCh0aGlzLl9pdGVtcyksXG4gICAgICAgIG1lcmdlTWFwKChsaXN0OiBRdWVyeUxpc3Q8VD4pID0+XG4gICAgICAgICAgbGlzdC5tYXAoZWxlbWVudCA9PlxuICAgICAgICAgICAgZnJvbUV2ZW50KGVsZW1lbnQuX2VsZW1lbnRSZWYubmF0aXZlRWxlbWVudCwgJ21vdXNlb3V0JykucGlwZShcbiAgICAgICAgICAgICAgbWFwVG8oZWxlbWVudCksXG4gICAgICAgICAgICAgIHRha2VVbnRpbCh0aGlzLl9pdGVtcy5jaGFuZ2VzKSxcbiAgICAgICAgICAgICksXG4gICAgICAgICAgKSxcbiAgICAgICAgKSxcbiAgICAgICAgbWVyZ2VBbGwoKSxcbiAgICAgICksXG4gICAgKTtcbiAgfVxufVxuIl19
\No newline at end of file