Initial Commit - NG UI
[osm/NG-UI.git] / src / app / AppComponent.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 App Components
21  */
22 import { Component, HostListener, Injector } from '@angular/core';
23 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
24 import { DEFAULT_INTERRUPTSOURCES, Idle } from '@ng-idle/core';
25 import { AuthenticationService } from 'AuthenticationService';
26 import { DeviceCheckService } from 'DeviceCheckService';
27 import { SharedService } from 'SharedService';
28 import { isNullOrUndefined } from 'util';
29
30 /**
31  * Creating component
32  * @Component takes AppComponent.html as template url
33  */
34 @Component({
35     selector: 'app-root',
36     templateUrl: './AppComponent.html',
37     styleUrls: ['./AppComponent.scss']
38 })
39 /** Exporting a class @exports AppComponent */
40 export class AppComponent {
41     /** To inject services @public */
42     public injector: Injector;
43     /** Instance for modal service @public */
44     public modalService: NgbModal;
45     /** Device Check service @private */
46     private deviceCheckService: DeviceCheckService;
47     /** Utilizes auth service for any auth operations @private */
48     private authService: AuthenticationService;
49     /** Handle idle time out service @private */
50     private idle: Idle;
51     /** Contains all methods related to shared @private */
52     private sharedService: SharedService;
53
54     constructor(injector: Injector) {
55         this.injector = injector;
56         this.idle = this.injector.get(Idle);
57         this.authService = this.injector.get(AuthenticationService);
58         this.modalService = this.injector.get(NgbModal);
59         this.deviceCheckService = this.injector.get(DeviceCheckService);
60         this.handleIdle();
61         this.sharedService = this.injector.get(SharedService);
62     }
63
64     /**
65      * Lifecyle Hooks the trigger before component is instantiate
66      */
67     public ngOnInit(): void {
68         this.sharedService.fetchOSMVersion();
69     }
70
71     /** To handle handleIdle @public */
72     public handleIdle(): void {
73         const idleTime: number = 1200;
74         const idleTimeOutWarning: number = 5;
75         //  sets an idle timeout in seconds.
76         this.idle.setIdle(idleTime);
77         //sets a timeout period in seconds. after idleTime seconds of inactivity, the user will be considered timed out.
78         this.idle.setTimeout(idleTimeOutWarning);
79         // sets the default interrupts, in this case, things like clicks, scrolls, touches to the document
80         this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
81         this.idle.watch(true);
82         this.idleTimeOut();
83     }
84
85     /** Method to capture idle time out event @public */
86     public idleTimeOut(): void {
87         this.idle.onTimeout.subscribe(() => {
88             this.idle.stop();
89             if (localStorage.getItem('id_token') !== null) {
90                 this.authService.logout();
91             }
92         });
93     }
94
95     /** Handling Window's Storage Hostlistener @public */
96     @HostListener('window:storage', ['$event'])
97     public handleLocalStorageEvent(evt: StorageEvent): void {
98         // On Token Change
99         if (evt.key === 'token_state' && !isNullOrUndefined(evt.key)) {
100             if (evt.oldValue !== evt.newValue) {
101                 window.location.reload();
102             }
103         }
104         // On Langauges Change
105         if (evt.key === 'languageCode' && !isNullOrUndefined(evt.key)) {
106             if (evt.oldValue !== evt.newValue) {
107                 window.location.reload();
108             }
109         }
110     }
111
112     /** Handling Window's POP State Hostlistener @public */
113     @HostListener('window:popstate', ['$event'])
114     public handleOnPOPState(evt: PopStateEvent): void {
115         this.modalService.dismissAll();
116     }
117
118     /** Handling Window's orientationchange Hostlistener @public */
119     @HostListener('window:resize', ['$event'])
120     public onResize(event: Event): void {
121         this.deviceCheckService.checkDeviceType();
122     }
123 }