Newer
Older
import { Component, ApplicationRef, OnInit, OnDestroy } from '@angular/core';
//import { MdDialog } from '@angular/material';
francois.grand
committed
import { environment } from '../environments/environment';
francois.grand
committed
import { InternationalisationService, Language, LanguageCode } from './services/internationalisation/internationalisation.service';
francois.grand
committed
import { Observer } from './services/observer';
import { ErrorService } from './services/error/error.service';
// import { AlertDialog } from './components/alert-dialog/alert-dialog.component';
import { FormulaireService } from './services/formulaire/formulaire.service';
import { FormulaireDefinition } from './formulaire/formulaire-definition';
francois.grand
committed
selector: 'nghyd-app',
export class AppComponent implements OnInit, OnDestroy, Observer {
private _displayErrorDialog: boolean = false;
/**
* id du formulaire courant
* on utilise pas directement FormulaireService.currentFormId pour éviter l'erreur ExpressionChangedAfterItHasBeenCheckedError
*/
private _currentFormId: number;
// constructor(private intlService: InternationalisationService, private appRef: ApplicationRef, private dialog: MdDialog, private errorService: ErrorService) { }
constructor(private intlService: InternationalisationService,
private appRef: ApplicationRef,
private errorService: ErrorService,
private router: Router,
private formulaireService: FormulaireService
) {
}
francois.grand
committed
francois.grand
committed
// process.on('unhandledRejection', (reason, p) => {
// console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
// // Stack Trace
// console.log(reason.stack);
// });
francois.grand
committed
ngOnInit() {
francois.grand
committed
this.intlService.addObserver(this);
this.intlService.setLocale("fr");
this.formulaireService.addObserver(this);
francois.grand
committed
this.subscribeErrorService();
}
ngOnDestroy() {
this.unsubscribeErrorService();
this.formulaireService.removeObserver(this);
}
private get uitextSidenavNewCalc() {
return this.intlService.localizeText("INFO_MENU_NOUVELLE_CALC");
}
private get uitextSidenavParams() {
return this.intlService.localizeText("INFO_SETUP_TITLE");
}
francois.grand
committed
/**
* abonnement au service d'erreurs
*/
private subscribeErrorService() {
this.errorService.addObserver(this);
francois.grand
committed
}
francois.grand
committed
private unsubscribeErrorService() {
this.errorService.removeObserver(this);
}
francois.grand
committed
private updateLocale() {
francois.grand
committed
let tag = this.intlService.currentLanguage.tag;
document['locale'] = tag;
// location.reload(true);
// this.cdRef.markForCheck();
// this.cdRef.detectChanges();
this.appRef.tick();
}
francois.grand
committed
// interface Observer
update(sender: any, data: any): void {
if (sender instanceof ErrorService) {
// on ouvre un dialogue avec le message d'erreur reçu
// if (this._displayErrorDialog) {
// let dialogRef = this.dialog.open(AlertDialog);
// let ad: AlertDialog = dialogRef.componentInstance;
// ad.text = String(data);
// }
// else
console.log(data);
}
else if (sender instanceof FormulaireService) {
switch (data["action"]) {
const f: FormulaireDefinition = data["form"];
this._calculators.push(
{
"title": f.calculatorName,
"uid": String(f.uid)
}
);
// abonnement en tant qu'observateur du nouveau formulaire
f.addObserver(this);
break;
francois.grand
committed
francois.grand
committed
this.toList();
break;
case "currentFormChanged":
/*
utilisation de setTimeout() pour éviter le message console ExpressionChangedAfterItHasBeenCheckedError
relatif au getter getHighlightClass() (qui change de valeur quand le formulaire courant change)
*/
setTimeout(() => {
this._currentFormId = data["formId"];
}, 1);
break;
const form: FormulaireDefinition = data["form"];
this.closeCalculator(form);
francois.grand
committed
else if (sender instanceof InternationalisationService) {
this.updateLocale();
}
else if (sender instanceof FormulaireDefinition) {
switch (data["action"]) {
case "nameChanged":
this.updateCalculatorTitle(sender, data["name"]);
break;
}
}
francois.grand
committed
}
private getCalculatorIndexFromId(formId: number) {
const index = this._calculators.reduce((resultIndex, calc, currIndex) => {
if (resultIndex == -1 && calc["uid"] == formId)
resultIndex = currIndex;
return resultIndex;
}, -1);
return index;
}
private updateCalculatorTitle(f: FormulaireDefinition, title: string) {
const formIndex = this.getCalculatorIndexFromId(f.uid);
this._calculators[formIndex]["title"] = title;
}
private closeCalculator(form: FormulaireDefinition) {
const formId: number = form.uid;
// désabonnement en tant qu'observateur
form.removeObserver(this);
// recherche de la calculette correspondante à formId
const closedIndex = this.getCalculatorIndexFromId(formId);
/*
* détermination de la nouvelle calculette à afficher :
* - celle après celle supprimée
* - ou celle avant celle supprimée si on supprime la dernière
*/
let newId: number = -1;
const l = this._calculators.length;
if (l > 1) {
if (closedIndex == l - 1)
newId = +this._calculators[closedIndex - 1]["uid"];
else
newId = +this._calculators[closedIndex + 1]["uid"];
}
// suppression
this._calculators = this._calculators.filter(calc => {
return formId != +calc["uid"]
});
// MAJ affichage
this._currentFormId = -1;
}
else
this.toCalc(newId);
}
private toList() {
this.router.navigate(['/list']);
}
private toCalc(id: number) {
this.router.navigate(['/calculator', id]);
}
private getHighlightClass(uid: number) {
return uid == this._currentFormId ? "blue darken-2" : "";
francois.grand
committed
// flag d'affichage des repères des colonnes Bootstrap : uniquement en mode dev
// cf. src/environments/*, ng build --env=<mode> (par ex : ng build --env=prod)
private get ruler(): boolean {
francois.grand
committed
// return !environment.production;
return false;
francois.grand
committed
}
// sidenav
private openNav() {
document.getElementById("mySidenav").style.width = "300px";
}
private closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
private newCalc() {
this.closeNav();
francois.grand
committed
this.toList();
}
private params() {
this.closeNav();
this.router.navigate(['/setup']);
}