2022-02-15 12:32:27 +01:00
|
|
|
import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core';
|
2020-05-05 18:17:12 +02:00
|
|
|
|
2022-02-03 12:26:04 +01:00
|
|
|
import { LanguageEnum } from '../../../../core/enums/language.enum';
|
2020-05-05 18:17:12 +02:00
|
|
|
import { LanguageService } from '../../../../core/services/language.service';
|
2021-05-03 11:32:03 +02:00
|
|
|
import { StorageService } from '../../../../core/services/storage.service';
|
2022-02-08 13:44:17 +01:00
|
|
|
import { DOCUMENT } from '@angular/common';
|
2022-02-14 10:37:38 +01:00
|
|
|
import { PollService } from '../../../../core/services/poll.service';
|
2020-05-05 18:17:12 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-language-selector',
|
|
|
|
templateUrl: './language-selector.component.html',
|
|
|
|
styleUrls: ['./language-selector.component.scss'],
|
|
|
|
})
|
2020-11-05 14:39:01 +01:00
|
|
|
export class LanguageSelectorComponent implements OnInit {
|
2022-02-11 11:43:50 +01:00
|
|
|
@Input() idSuffix: string = '';
|
2022-02-08 13:44:17 +01:00
|
|
|
public currentLang: string;
|
2022-02-03 11:16:28 +01:00
|
|
|
public availableLanguages: any = ['FR', 'EN', 'ES'];
|
2021-12-13 16:18:24 +01:00
|
|
|
language: string;
|
2022-02-08 13:44:17 +01:00
|
|
|
language_to_apply: string;
|
2022-02-15 14:15:40 +01:00
|
|
|
display_lang_dialog: boolean = false;
|
2020-05-05 18:17:12 +02:00
|
|
|
|
2022-02-08 13:44:17 +01:00
|
|
|
constructor(
|
|
|
|
private languageService: LanguageService,
|
|
|
|
private storageService: StorageService,
|
2022-02-15 14:06:25 +01:00
|
|
|
public pollService: PollService,
|
2022-02-15 12:32:27 +01:00
|
|
|
private cd: ChangeDetectorRef,
|
2022-02-08 13:44:17 +01:00
|
|
|
@Inject(DOCUMENT) private document: any
|
|
|
|
) {}
|
2020-05-05 18:17:12 +02:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.availableLanguages = this.languageService.getAvailableLanguages();
|
2022-02-08 18:06:47 +01:00
|
|
|
|
2022-02-03 11:16:28 +01:00
|
|
|
if (this.storageService.language && this.availableLanguages.indexOf(this.storageService.language) !== -1) {
|
|
|
|
this.languageService.setLanguage(this.storageService.language);
|
2022-02-08 18:06:47 +01:00
|
|
|
this.currentLang = this.storageService.language;
|
2022-02-03 11:16:28 +01:00
|
|
|
}
|
2022-02-08 18:06:47 +01:00
|
|
|
this.currentLang = this.languageService.getLangage();
|
|
|
|
this.language_to_apply = '' + this.currentLang;
|
2020-05-05 18:17:12 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 13:44:17 +01:00
|
|
|
setLang(newlang: string = 'fr'): void {
|
|
|
|
this.currentLang = newlang;
|
|
|
|
this.languageService.setLanguage(newlang);
|
2022-02-03 11:16:28 +01:00
|
|
|
this.storageService.language = this.currentLang;
|
2022-02-14 10:37:38 +01:00
|
|
|
this.pollService.updateTitle();
|
2020-05-05 18:17:12 +02:00
|
|
|
}
|
2021-05-03 11:32:03 +02:00
|
|
|
|
2022-02-08 13:44:17 +01:00
|
|
|
focusOnCancelButton() {
|
|
|
|
this.display_lang_dialog = false;
|
|
|
|
let buttonClose = this.document.querySelector('#display_cancel_popup_button');
|
|
|
|
if (buttonClose) {
|
|
|
|
buttonClose.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 11:32:03 +02:00
|
|
|
nextLang(): void {
|
2021-05-18 11:46:35 +02:00
|
|
|
const index = this.availableLanguages.indexOf(this.currentLang);
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
// on passe au language suivant si il existe, sinon on revient au numéro 0
|
|
|
|
|
|
|
|
if (this.availableLanguages[index + 1]) {
|
|
|
|
this.currentLang = this.availableLanguages[index + 1];
|
|
|
|
} else {
|
|
|
|
this.currentLang = this.availableLanguages[0];
|
|
|
|
}
|
|
|
|
this.setLang();
|
|
|
|
}
|
2021-05-03 11:32:03 +02:00
|
|
|
}
|
2022-02-08 13:44:17 +01:00
|
|
|
|
|
|
|
applyLangAndClosePopup() {
|
|
|
|
this.setLang(this.language_to_apply);
|
|
|
|
this.display_lang_dialog = false;
|
|
|
|
let elem = this.document.querySelector('#lang_button_popup');
|
|
|
|
if (elem) {
|
|
|
|
elem.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openDialogLang() {
|
|
|
|
this.display_lang_dialog = true;
|
2022-02-15 12:32:27 +01:00
|
|
|
this.cd.detectChanges();
|
|
|
|
let elem = this.document.querySelector('#close_dialog');
|
2022-02-08 13:44:17 +01:00
|
|
|
if (elem) {
|
|
|
|
elem.focus();
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 18:17:12 +02:00
|
|
|
}
|