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 | */
|
---|
8 | import { _getEventTarget } from '@angular/cdk/platform';
|
---|
9 | import { getMutableClientRect, adjustClientRect } from './client-rect';
|
---|
10 | /** Keeps track of the scroll position and dimensions of the parents of an element. */
|
---|
11 | export class ParentPositionTracker {
|
---|
12 | constructor(_document, _viewportRuler) {
|
---|
13 | this._document = _document;
|
---|
14 | this._viewportRuler = _viewportRuler;
|
---|
15 | /** Cached positions of the scrollable parent elements. */
|
---|
16 | this.positions = new Map();
|
---|
17 | }
|
---|
18 | /** Clears the cached positions. */
|
---|
19 | clear() {
|
---|
20 | this.positions.clear();
|
---|
21 | }
|
---|
22 | /** Caches the positions. Should be called at the beginning of a drag sequence. */
|
---|
23 | cache(elements) {
|
---|
24 | this.clear();
|
---|
25 | this.positions.set(this._document, {
|
---|
26 | scrollPosition: this._viewportRuler.getViewportScrollPosition(),
|
---|
27 | });
|
---|
28 | elements.forEach(element => {
|
---|
29 | this.positions.set(element, {
|
---|
30 | scrollPosition: { top: element.scrollTop, left: element.scrollLeft },
|
---|
31 | clientRect: getMutableClientRect(element)
|
---|
32 | });
|
---|
33 | });
|
---|
34 | }
|
---|
35 | /** Handles scrolling while a drag is taking place. */
|
---|
36 | handleScroll(event) {
|
---|
37 | const target = _getEventTarget(event);
|
---|
38 | const cachedPosition = this.positions.get(target);
|
---|
39 | if (!cachedPosition) {
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 | // Used when figuring out whether an element is inside the scroll parent. If the scrolled
|
---|
43 | // parent is the `document`, we use the `documentElement`, because IE doesn't support
|
---|
44 | // `contains` on the `document`.
|
---|
45 | const scrolledParentNode = target === this._document ? target.documentElement : target;
|
---|
46 | const scrollPosition = cachedPosition.scrollPosition;
|
---|
47 | let newTop;
|
---|
48 | let newLeft;
|
---|
49 | if (target === this._document) {
|
---|
50 | const viewportScrollPosition = this._viewportRuler.getViewportScrollPosition();
|
---|
51 | newTop = viewportScrollPosition.top;
|
---|
52 | newLeft = viewportScrollPosition.left;
|
---|
53 | }
|
---|
54 | else {
|
---|
55 | newTop = target.scrollTop;
|
---|
56 | newLeft = target.scrollLeft;
|
---|
57 | }
|
---|
58 | const topDifference = scrollPosition.top - newTop;
|
---|
59 | const leftDifference = scrollPosition.left - newLeft;
|
---|
60 | // Go through and update the cached positions of the scroll
|
---|
61 | // parents that are inside the element that was scrolled.
|
---|
62 | this.positions.forEach((position, node) => {
|
---|
63 | if (position.clientRect && target !== node && scrolledParentNode.contains(node)) {
|
---|
64 | adjustClientRect(position.clientRect, topDifference, leftDifference);
|
---|
65 | }
|
---|
66 | });
|
---|
67 | scrollPosition.top = newTop;
|
---|
68 | scrollPosition.left = newLeft;
|
---|
69 | return { top: topDifference, left: leftDifference };
|
---|
70 | }
|
---|
71 | }
|
---|
72 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFyZW50LXBvc2l0aW9uLXRyYWNrZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL2RyYWctZHJvcC9wYXJlbnQtcG9zaXRpb24tdHJhY2tlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFHSCxPQUFPLEVBQUMsZUFBZSxFQUFDLE1BQU0sdUJBQXVCLENBQUM7QUFDdEQsT0FBTyxFQUFDLG9CQUFvQixFQUFFLGdCQUFnQixFQUFDLE1BQU0sZUFBZSxDQUFDO0FBUXJFLHNGQUFzRjtBQUN0RixNQUFNLE9BQU8scUJBQXFCO0lBT2hDLFlBQW9CLFNBQW1CLEVBQVUsY0FBNkI7UUFBMUQsY0FBUyxHQUFULFNBQVMsQ0FBVTtRQUFVLG1CQUFjLEdBQWQsY0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