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