1 | import { getMatScrollStrategyAlreadyAttachedError } from './scroll-strategy';
|
---|
2 | /**
|
---|
3 | * Strategy that will close the overlay as soon as the user starts scrolling.
|
---|
4 | */
|
---|
5 | export class CloseScrollStrategy {
|
---|
6 | constructor(_scrollDispatcher, _ngZone, _viewportRuler, _config) {
|
---|
7 | this._scrollDispatcher = _scrollDispatcher;
|
---|
8 | this._ngZone = _ngZone;
|
---|
9 | this._viewportRuler = _viewportRuler;
|
---|
10 | this._config = _config;
|
---|
11 | this._scrollSubscription = null;
|
---|
12 | /** Detaches the overlay ref and disables the scroll strategy. */
|
---|
13 | this._detach = () => {
|
---|
14 | this.disable();
|
---|
15 | if (this._overlayRef.hasAttached()) {
|
---|
16 | this._ngZone.run(() => this._overlayRef.detach());
|
---|
17 | }
|
---|
18 | };
|
---|
19 | }
|
---|
20 | /** Attaches this scroll strategy to an overlay. */
|
---|
21 | attach(overlayRef) {
|
---|
22 | if (this._overlayRef && (typeof ngDevMode === 'undefined' || ngDevMode)) {
|
---|
23 | throw getMatScrollStrategyAlreadyAttachedError();
|
---|
24 | }
|
---|
25 | this._overlayRef = overlayRef;
|
---|
26 | }
|
---|
27 | /** Enables the closing of the attached overlay on scroll. */
|
---|
28 | enable() {
|
---|
29 | if (this._scrollSubscription) {
|
---|
30 | return;
|
---|
31 | }
|
---|
32 | const stream = this._scrollDispatcher.scrolled(0);
|
---|
33 | if (this._config && this._config.threshold && this._config.threshold > 1) {
|
---|
34 | this._initialScrollPosition = this._viewportRuler.getViewportScrollPosition().top;
|
---|
35 | this._scrollSubscription = stream.subscribe(() => {
|
---|
36 | const scrollPosition = this._viewportRuler.getViewportScrollPosition().top;
|
---|
37 | if (Math.abs(scrollPosition - this._initialScrollPosition) > this._config.threshold) {
|
---|
38 | this._detach();
|
---|
39 | }
|
---|
40 | else {
|
---|
41 | this._overlayRef.updatePosition();
|
---|
42 | }
|
---|
43 | });
|
---|
44 | }
|
---|
45 | else {
|
---|
46 | this._scrollSubscription = stream.subscribe(this._detach);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | /** Disables the closing the attached overlay on scroll. */
|
---|
50 | disable() {
|
---|
51 | if (this._scrollSubscription) {
|
---|
52 | this._scrollSubscription.unsubscribe();
|
---|
53 | this._scrollSubscription = null;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | detach() {
|
---|
57 | this.disable();
|
---|
58 | this._overlayRef = null;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xvc2Utc2Nyb2xsLXN0cmF0ZWd5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay9vdmVybGF5L3Njcm9sbC9jbG9zZS1zY3JvbGwtc3RyYXRlZ3kudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUUEsT0FBTyxFQUFpQix3Q0FBd0MsRUFBQyxNQUFNLG1CQUFtQixDQUFDO0FBYTNGOztHQUVHO0FBQ0gsTUFBTSxPQUFPLG1CQUFtQjtJQUs5QixZQUNVLGlCQUFtQyxFQUNuQyxPQUFlLEVBQ2YsY0FBNkIsRUFDN0IsT0FBbUM7UUFIbkMsc0JBQWlCLEdBQWpCLGlCQUFpQixDQUFrQjtRQUNuQyxZQUFPLEdBQVAsT0FBTyxDQUFRO1FBQ2YsbUJBQWMsR0FBZCxjQUFjLENBQWU7UUFDN0IsWUFBTyxHQUFQLE9BQU8sQ0FBNEI7UUFSckMsd0JBQW1CLEdBQXNCLElBQUksQ0FBQztRQXlEdEQsaUVBQWlFO1FBQ3pELFlBQU8sR0FBRyxHQUFHLEVBQUU7WUFDckIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFDO1lBRWYsSUFBSSxJQUFJLENBQUMsV0FBVyxDQUFDLFdBQVcsRUFBRSxFQUFFO2dCQUNsQyxJQUFJLENBQUMsT0FBTyxDQUFDLEdBQUcs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