Initial Commit - NG UI
[osm/NG-UI.git] / src / directive / GoToTopDirective.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17  */
18
19 /**
20  * @file Directive for go to top button.
21  */
22 import { Directive, HostListener } from '@angular/core';
23 import { SharedService } from 'SharedService';
24
25 /**
26  * Creating Directive for handling go to top button
27  * @Directive appGottoTop selector
28  */
29 @Directive({
30   selector: '[appGottoTop]'
31 })
32 /** Exporting a class @exports GoToTopDirective */
33 export class GoToTopDirective {
34   /** To set scroll top position @private */
35   private topPosToStartShowing: number = 100;
36
37   /** Contains all methods related to shared @private */
38   private sharedService: SharedService;
39
40   constructor(sharedService: SharedService) {
41     this.sharedService = sharedService;
42   }
43   /**
44    * to listen the scroll event in DOM @public
45    */
46   @HostListener('window:scroll') public enableGotoTop(): void {
47     const scrollPosition: number = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop);
48     if (scrollPosition >= this.topPosToStartShowing) {
49       this.sharedService.showGotoTop = true;
50     } else {
51       this.sharedService.showGotoTop = false;
52     }
53   }
54
55 }