Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[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   // eslint-disable-next-line @typescript-eslint/no-magic-numbers
36   private topPosToStartShowing: number = 100;
37
38   /** Contains all methods related to shared @private */
39   private sharedService: SharedService;
40
41   constructor(sharedService: SharedService) {
42     this.sharedService = sharedService;
43   }
44   /**
45    * to listen the scroll event in DOM @public
46    */
47   @HostListener('window:scroll') public enableGotoTop(): void {
48     // eslint-disable-next-line deprecation/deprecation
49     const scrollPosition: number = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop);
50     if (scrollPosition >= this.topPosToStartShowing) {
51       this.sharedService.showGotoTop = true;
52     } else {
53       this.sharedService.showGotoTop = false;
54     }
55   }
56 }