Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / user-settings / UserSettingsComponent.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  * @file User Settings Modal Component.
20  */
21
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { SharedService } from 'SharedService';
27
28 /**
29  * Creating component
30  * @Component takes UserSettingsComponent.html as template url
31  */
32 @Component({
33     templateUrl: './UserSettingsComponent.html',
34     styleUrls: ['./UserSettingsComponent.scss']
35 })
36 /** Exporting a class @exports UserSettingsComponent */
37 export class UserSettingsComponent implements OnInit {
38     /** Invoke service injectors @public */
39     public injector: Injector;
40
41     /** Supported language list for the dropdown */
42     public languageList: {}[];
43
44     /** FormGroup instance added to the form @ html @public */
45     public usersettingsForm: FormGroup;
46
47     /** Instance for active modal service @public */
48     public activeModal: NgbActiveModal;
49
50     /** Form submission Add */
51     public submitted: boolean = false;
52
53     /** FormBuilder instance added to the formBuilder @private */
54     private formBuilder: FormBuilder;
55
56     /** Instance for translate service @private */
57     private translateService: TranslateService;
58
59     /** Contains all methods related to shared @private */
60     private sharedService: SharedService;
61
62     constructor(injector: Injector) {
63         this.injector = injector;
64         this.formBuilder = this.injector.get(FormBuilder);
65         this.activeModal = this.injector.get(NgbActiveModal);
66         this.translateService = this.injector.get(TranslateService);
67         this.sharedService = this.injector.get(SharedService);
68     }
69
70     /**
71      * Lifecyle Hooks the trigger before component is instantiate
72      */
73     public ngOnInit(): void {
74         this.initializeSettings();
75     }
76
77     /** Initialize user's settings */
78     public initializeSettings(): void {
79         this.languageList = this.sharedService.languageCodeList();
80         /** Initializing Form Action */
81         this.usersettingsForm = this.formBuilder.group({
82             selectedLanguage: [null, [Validators.required]]
83         });
84         const setLanguage: string = localStorage.getItem('languageCode');
85         if (setLanguage !== null && this.validateLanguageList(setLanguage)) {
86             this.usersettingsForm.get('selectedLanguage').setValue(setLanguage);
87         } else {
88             this.usersettingsForm.get('selectedLanguage').setValue('en');
89         }
90     }
91
92     /** convenience getter for easy access to form fields */
93     get f(): FormGroup['controls'] { return this.usersettingsForm.controls; }
94
95     /** On modal submit UserSettingsSubmit will called @public */
96     public usersettingsSubmit(): void {
97         this.submitted = true;
98         if (!this.usersettingsForm.invalid) {
99             const selectedLanguage: string = this.usersettingsForm.value.selectedLanguage;
100             localStorage.setItem('languageCode', this.usersettingsForm.value.selectedLanguage);
101             this.translateService.use(selectedLanguage);
102             location.reload();
103         }
104     }
105     /** Validate language code in the language list @private */
106     private validateLanguageList(setLanguage: string): boolean {
107         return this.languageList.some((item: { code: string }) => item.code === setLanguage);
108     }
109 }