From c60792dda51cf6a0adcbcd78a478ea5bf436f332 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Mon, 10 Aug 2020 11:46:46 +0200
Subject: [PATCH] =?UTF-8?q?Fix=20#440=20d=C3=A9finition=20de=20la=20valeur?=
 =?UTF-8?q?=20initiale=20d'un=20calcul?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../dialog-edit-param-computed.component.html |  7 ++++--
 .../dialog-edit-param-computed.component.ts   | 24 ++++++++++++++++---
 .../param-computed.component.ts               | 16 +++++++++----
 src/app/formulaire/elements/ngparam.ts        |  8 +++++++
 4 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.html b/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.html
index 8e56fa408..e3a40065d 100644
--- a/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.html
+++ b/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.html
@@ -7,8 +7,11 @@
   </div>
 
   <div mat-dialog-actions [attr.align]="'end'">
-    <button mat-raised-button [mat-dialog-close]="true" cdkFocusInitial>
-      {{ uitextClose }}
+    <button mat-raised-button color="primary" [mat-dialog-close]="true" cdkFocusInitial>
+        {{ uitextCancel }}
+    </button>
+    <button mat-raised-button color="warn" (click)="onValidate()">
+        {{ uitextValidate }}
     </button>
   </div>
 
diff --git a/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.ts b/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.ts
index 9b3725771..d971e1ea6 100644
--- a/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.ts
+++ b/src/app/components/dialog-edit-param-computed/dialog-edit-param-computed.component.ts
@@ -4,6 +4,8 @@ import { I18nService } from "../../services/internationalisation.service";
 import { NgParameter } from "../../formulaire/elements/ngparam";
 import { NgParamInputComponent } from "../ngparam-input/ngparam-input.component";
 
+import { ParamDefinition } from "jalhyd";
+
 @Component({
     selector: "dialog-edit-param-computed",
     templateUrl: "dialog-edit-param-computed.component.html",
@@ -22,11 +24,21 @@ export class DialogEditParamComputedComponent implements OnInit {
       private intlService: I18nService,
       @Inject(MAT_DIALOG_DATA) public data: any
     ) {
-      this.param = data.param;
+      // copy given parameter in a "fake" Ngparameter
+      const nP = data.param as NgParameter;
+      const p = nP.paramDefinition as ParamDefinition;
+      const pDef = new ParamDefinition(undefined, p.symbol, p.domain, p.unit, p.getValue(), p.family, p.visible);
+      this.param = new NgParameter(pDef, undefined);
+      this.param.setLabel(nP.label);
+      this.param.unit = nP.unit;
+    }
+
+    public get uitextCancel(): string {
+        return this.intlService.localizeText("INFO_OPTION_CANCEL");
     }
 
-    public get uitextClose() {
-      return this.intlService.localizeText("INFO_OPTION_CLOSE");
+    public get uitextValidate(): string {
+        return this.intlService.localizeText("INFO_OPTION_VALIDATE");
     }
 
     public get uitextEditParamComputedInitialValue() {
@@ -36,4 +48,10 @@ export class DialogEditParamComputedComponent implements OnInit {
     public ngOnInit() {
       this._ngParamInputComponent.model = this.param;
     }
+
+    public onValidate() {
+      this.dialogRef.close({
+        value: this.param.getValue()
+      });
+    }
 }
diff --git a/src/app/components/param-computed/param-computed.component.ts b/src/app/components/param-computed/param-computed.component.ts
index 43f2548b8..e1f901071 100644
--- a/src/app/components/param-computed/param-computed.component.ts
+++ b/src/app/components/param-computed/param-computed.component.ts
@@ -1,7 +1,7 @@
 import { Component, Input } from "@angular/core";
 import { MatDialog } from "@angular/material/dialog";
 import { NgParameter } from "../../formulaire/elements/ngparam";
-import { ParamCalculability, Structure } from "jalhyd";
+import { ParamCalculability } from "jalhyd";
 import { DialogEditParamComputedComponent } from "../dialog-edit-param-computed/dialog-edit-param-computed.component";
 import { I18nService } from "../../services/internationalisation.service";
 
@@ -47,17 +47,23 @@ export class ParamComputedComponent {
     }
 
     public openDialog() {
-        // modification de la valeur initiale, sans avoir à remettre le mode de
-        // paramètre sur "fixé"
-        this.editInitialValueDialog.open(
+        // modification de la valeur initiale, sans avoir à remettre le mode de paramètre sur "fixé"
+        const dialogRef = this.editInitialValueDialog.open(
             DialogEditParamComputedComponent,
             {
                 data: {
                     param: this.param
                 },
-                autoFocus: false
+                autoFocus: false,
+                disableClose: true
             }
         );
+        dialogRef.afterClosed().subscribe(result => {
+            if (result && result.value !== undefined) {
+                // use setInitValue() and not setValue() to prevent value mode from going back to SINGLE
+                this.param.paramDefinition.setInitValue(result.value);
+            }
+        });
     }
 
     public get uitextEditInitialValue() {
diff --git a/src/app/formulaire/elements/ngparam.ts b/src/app/formulaire/elements/ngparam.ts
index 0a8baafb2..183c4e241 100644
--- a/src/app/formulaire/elements/ngparam.ts
+++ b/src/app/formulaire/elements/ngparam.ts
@@ -287,6 +287,14 @@ export class NgParameter extends InputField implements Observer {
         }
     }
 
+    /**
+     * Trick method for overriding label; used in fake NgParameter such
+     * as the one in DialogEditParamComputedComponent
+     */
+    public setLabel(l: string) {
+        this._label = l;
+    }
+
     /**
      * Sets this parameter as the one to be computed
      */
-- 
GitLab