From 6e6f899fcf4c0f98b047d7115849f20115bfde61 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Tue, 19 Dec 2017 15:43:52 +0100 Subject: [PATCH] ajout du bouton de fermeture d'une calculette closes #21 --- src/app/app.component.ts | 56 +++++++++++++++++-- .../generic/calculator.component.html | 5 +- .../generic/calculator.component.ts | 4 ++ .../services/formulaire/formulaire.service.ts | 12 ++++ 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index abc8c0c27..c658ad70c 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -113,10 +113,62 @@ export class AppComponent implements OnInit, OnDestroy, Observer { case "invalidid": this.toList(); break; + + + case "close": + const uid: number = data["formid"]; + this.closeCalculator(uid); + break; } } } + private closeCalculator(formId: number) { + // recherche de la calculette correspondante à formId + + const closedIndex = this._calculators.reduce((resultIndex, calc, currIndex) => { + if (resultIndex == -1 && calc["uid"] == formId) + resultIndex = currIndex; + return resultIndex; + }, -1); + + /* + * 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 + + if (newId == -1) + this.toList(); + else + this.toCalc(newId); + } + + private toList() { + this.router.navigate(['/list']); + } + + private toCalc(id: number) { + this.router.navigate(['/calculator', id]); + } + // sidenav private openNav() { @@ -132,9 +184,5 @@ export class AppComponent implements OnInit, OnDestroy, Observer { this.toList(); } - private toList() { - this.router.navigate(['/list']); - } - // sidenav } diff --git a/src/app/calculators/generic/calculator.component.html b/src/app/calculators/generic/calculator.component.html index 0c875daeb..7ad429e2a 100644 --- a/src/app/calculators/generic/calculator.component.html +++ b/src/app/calculators/generic/calculator.component.html @@ -1,7 +1,10 @@ <div *ngIf="hasData()" class="row"> - <div class="col"> + <div class="col-10"> <h1>{{uitextTitre}}</h1> </div> + <div class="col-2"> + <button type="button" class="btn btn-primary float-right black" (click)="onCloseForm()">×</button> + </div> </div> <field-set *ngFor="let fs of fieldSets" [style.display]="getFieldsetStyleDisplay(fs.id)" [fieldSet]=fs (onRadio)=onRadioClick($event) (onSelectChange)=onSelectChanged($event)></field-set> diff --git a/src/app/calculators/generic/calculator.component.ts b/src/app/calculators/generic/calculator.component.ts index 5aa78c18d..d0ac5c571 100644 --- a/src/app/calculators/generic/calculator.component.ts +++ b/src/app/calculators/generic/calculator.component.ts @@ -128,6 +128,10 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, OnDestroy, O this._formulaire.resetRadiosAndResults(symbol, option); } + private onCloseForm() { + this.formulaireService.requestCloseForm(this._formulaire.uid); + } + /** * met à jour l'interface */ diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts index d83991a8e..e6c93a407 100644 --- a/src/app/services/formulaire/formulaire.service.ts +++ b/src/app/services/formulaire/formulaire.service.ts @@ -179,4 +179,16 @@ export class FormulaireService extends Observable { throw "FormulaireService.getConfigPathPrefix() : valeur de CalculatorType " + ct + " non implémentée" } } + + public requestCloseForm(uid: number) { + const form = this.getFormulaireFromId(uid); + if (form != undefined) { + this._formulaires = this._formulaires.filter(f => f.uid !== uid); + + this.notifyObservers({ + "action": "close", + "formid": uid + }); + } + } } -- GitLab