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