diff --git a/src/app/app.component.ts b/src/app/app.component.ts index abc8c0c2777887644e661319afaf6fa62da27a72..c658ad70c49e504837494113adcbe715e7510344 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 0c875daebf755d3f251c0e2227d4da51561c40bd..7ad429e2a78794bbd8479bb7ce87b4185dbd360e 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 5aa78c18da3dcfa8b5b56025481e12fc4fb646e6..d0ac5c5712fa5840cf56705ef22eeae490399921 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 d83991a8e3d024d6717488ec290d6a440b83da44..e6c93a407fac7e06af2c2a3368a903023442f5fd 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 + }); + } + } }