51189dc1aebb08aab902ac943acdb3c4fc0b37de
[sdc/sdc-workflow-designer.git] /
1 /**
2  * @license
3  * Copyright Google Inc. 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 /* tslint:disable:array-type member-access variable-name typedef
9  only-arrow-functions directive-class-suffix component-class-suffix
10  component-selector*/
11 import {Subscription} from 'rxjs/Subscription';
12
13 import {OverlayRef} from '../overlay-ref';
14
15 import {ScrollDispatcher} from './scroll-dispatcher';
16 import {getMdScrollStrategyAlreadyAttachedError, ScrollStrategy} from './scroll-strategy';
17
18
19 /**
20  * Strategy that will close the overlay as soon as the user starts scrolling.
21  */
22 export class CloseScrollStrategy implements ScrollStrategy {
23   private _scrollSubscription: Subscription|null = null;
24   private _overlayRef: OverlayRef;
25
26   constructor(private _scrollDispatcher: ScrollDispatcher) {}
27
28   attach(overlayRef: OverlayRef) {
29         if (this._overlayRef) {
30                 throw getMdScrollStrategyAlreadyAttachedError();
31         }
32
33         this._overlayRef = overlayRef;
34   }
35
36   enable() {
37         if (!this._scrollSubscription) {
38                 this._scrollSubscription = this._scrollDispatcher.scrolled(0, () => {
39                 if (this._overlayRef.hasAttached()) {
40                         this._overlayRef.detach();
41                 }
42
43                 this.disable();
44                 });
45         }
46   }
47
48   disable() {
49         if (this._scrollSubscription) {
50                 this._scrollSubscription.unsubscribe();
51                 this._scrollSubscription = null;
52         }
53   }
54 }