b15d5dea6f0134951dc1f0648da9b4a12246fa41
[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  * Config options for the RepositionScrollStrategy.
20  */
21 export interface RepositionScrollStrategyConfig { scrollThrottle?: number; }
22
23 /**
24  * Strategy that will update the element position as the user is scrolling.
25  */
26 export class RepositionScrollStrategy implements ScrollStrategy {
27   private _scrollSubscription: Subscription|null = null;
28   private _overlayRef: OverlayRef;
29
30   constructor(
31                 private _scrollDispatcher: ScrollDispatcher,
32                 private _config?: RepositionScrollStrategyConfig) {}
33
34   attach(overlayRef: OverlayRef) {
35         if (this._overlayRef) {
36                 throw getMdScrollStrategyAlreadyAttachedError();
37         }
38
39         this._overlayRef = overlayRef;
40   }
41
42   enable() {
43         if (!this._scrollSubscription) {
44                 const throttle = this._config ? this._config.scrollThrottle : 0;
45
46                 this._scrollSubscription =
47                         this._scrollDispatcher.scrolled(throttle, () => {
48                         this._overlayRef.updatePosition();
49                         });
50         }
51   }
52
53   disable() {
54         if (this._scrollSubscription) {
55                 this._scrollSubscription.unsubscribe();
56                 this._scrollSubscription = null;
57         }
58   }
59 }