blob: 05710147c60b873578206609fede7cd8060836a8 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 */
22import { Component, HostListener, Injector } from '@angular/core';
23import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
24import { DEFAULT_INTERRUPTSOURCES, Idle } from '@ng-idle/core';
25import { AuthenticationService } from 'AuthenticationService';
26import { DeviceCheckService } from 'DeviceCheckService';
SANDHYA.JSc84f1122024-06-04 21:50:03 +053027import { SharedService, isNullOrUndefined } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053028
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 */
39export class AppComponent {
SANDHYA.JS52474e72024-06-10 21:47:25 +053040 /** Handle idle time out @public */
41 public idleTime: number;
kumaran.m3b4814a2020-05-01 19:48:54 +053042 /** 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 {
SANDHYA.JS52474e72024-06-10 21:47:25 +053074 if (!isNullOrUndefined((sessionStorage.getItem('timeout')))) {
75 this.idleTime = Number(sessionStorage.getItem('timeout'));
76 } else {
77 this.idleTime = 1200;
78 }
kumaran.m3b4814a2020-05-01 19:48:54 +053079 const idleTimeOutWarning: number = 5;
80 // sets an idle timeout in seconds.
SANDHYA.JS52474e72024-06-10 21:47:25 +053081 this.idle.setIdle(this.idleTime);
kumaran.m3b4814a2020-05-01 19:48:54 +053082 //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();
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +053094 if (sessionStorage.getItem('id_token') !== null) {
kumaran.m3b4814a2020-05-01 19:48:54 +053095 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}