From 229c178e0cf548fdb1ffd35b68f358cd6897c0f5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Tue, 29 Mar 2022 14:18:46 +0200
Subject: [PATCH] fix: fields of min/max/list values dialog filled with default
 values

when:
- "create calculators with empty fields" is set,
- a single value parameter is given a value,
- parameter mode changed to variable,
fields opening edition dialog are incorrectly filled.

refs #480
---
 .../dialog-edit-param-values.component.ts     | 20 ++++---
 .../formulaire/elements/formulaire-node.ts    |  2 +-
 src/app/formulaire/elements/ngparam.ts        | 55 ++++++++++++++++++-
 3 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts b/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts
index 6a0a68d59..f5200cfd0 100644
--- a/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts
+++ b/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts
@@ -11,6 +11,7 @@ import { sprintf } from "sprintf-js";
 import { ParamValueMode, ExtensionStrategy } from "jalhyd";
 
 import { fv } from "../../util";
+import { ServiceFactory } from "app/services/service-factory";
 
 @Component({
     selector: "dialog-edit-param-values",
@@ -395,28 +396,29 @@ export class DialogEditParamValuesComponent implements OnInit {
     }
 
     private initVariableValues() {
+        const canFill: boolean = !ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit;
         // init min / max / step
         if (this.isMinMax) {
             const pVal = this.param.getValue();
-            if (this.param.minValue === undefined) {
-                this.param.setMinValue(this, pVal !== undefined ? this.param.getValue() / 2 : undefined);
+            if (this.param.minValue === undefined && canFill) {
+                this.param.resetMinValue(this, pVal !== undefined ? this.param.getValue() / 2 : undefined);
             }
-            if (this.param.maxValue === undefined) {
-                this.param.setMaxValue(this, pVal !== undefined ? this.param.getValue() * 2 : undefined);
+            if (this.param.maxValue === undefined && canFill) {
+                this.param.resetMaxValue(this, pVal !== undefined ? this.param.getValue() * 2 : undefined);
             }
             let step = this.param.stepValue;
-            if (step === undefined) {
+            if (step === undefined && canFill) {
                 step = pVal !== undefined ? ((this.param.maxValue - this.param.minValue) / 20) : undefined;
+                this.param.resetStepValue(this, step);
             }
-            this.param.setStepValue(this, step);
         }
         // init values list
         if (this.isListe) {
-            if (this.param.valueList === undefined) {
+            if (this.param.valueList === undefined && canFill) {
                 if (this.param.isDefined) {
-                    this.param.setValueList(this, [ this.param.getValue() ]);
+                    this.param.resetValueList(this, [this.param.getValue()]);
                 } else {
-                    this.param.setValueList(this, []);
+                    this.param.resetValueList(this, []);
                 }
                 // set form control initial value
                 this.valuesListForm.controls.valuesList.setValue(this.valuesList);
diff --git a/src/app/formulaire/elements/formulaire-node.ts b/src/app/formulaire/elements/formulaire-node.ts
index dca5bbe5d..b0e214f5e 100644
--- a/src/app/formulaire/elements/formulaire-node.ts
+++ b/src/app/formulaire/elements/formulaire-node.ts
@@ -161,7 +161,7 @@ export abstract class FormulaireNode implements IObservable {
                     if (p.valueMode === ParamValueMode.CALCUL) {
                         calcP = p;
                     }
-                    p.setValue(this, undefined);
+                    p.resetValue(this, undefined);
                 }
             }
         }
diff --git a/src/app/formulaire/elements/ngparam.ts b/src/app/formulaire/elements/ngparam.ts
index c79dc844f..d2e4b0bd4 100644
--- a/src/app/formulaire/elements/ngparam.ts
+++ b/src/app/formulaire/elements/ngparam.ts
@@ -394,6 +394,18 @@ export class NgParameter extends InputField implements Observer {
         );
     }
 
+    /**
+     * fixe la valeur du paramètre en tant que valeur par défaut
+     */
+    public resetValue(sender: any, val: number) {
+        const changed = (this._paramDef.getValue() !== val);
+        this._isValueModified = false;
+        if (changed) {
+            this._paramDef.setValue(val, sender);
+            this.notifyValueModified(sender);
+        }
+    }
+
     /**
      * fixe la valeur du paramètre.
      * une notification préalable est envoyée pour laisser l'occasion aux objets liés de préciser le contexte
@@ -402,8 +414,20 @@ export class NgParameter extends InputField implements Observer {
      * @param val
      */
     public setValue(sender: any, val: number) {
-        this._paramDef.setValue(val, sender);
-        this.notifyValueModified(sender);
+        const changed = (this._paramDef.getValue() !== val);
+        if (changed) {
+            this._paramDef.setValue(val, sender);
+            this.notifyValueModified(sender);
+        }
+    }
+
+    public resetMinValue(sender: any, v: number) {
+        const changed = (this._paramDef.min !== v);
+        this._isValueModified = false;
+        if (changed) {
+            this._paramDef.min = v;
+            this.notifyMinValueModified(sender);
+        }
     }
 
     public setMinValue(sender: any, v: number) {
@@ -415,6 +439,15 @@ export class NgParameter extends InputField implements Observer {
         }
     }
 
+    public resetMaxValue(sender: any, v: number) {
+        const changed = (this._paramDef.max !== v);
+        this._isValueModified = false;
+        if (changed) {
+            this._paramDef.max = v;
+            this.notifyMaxValueModified(sender);
+        }
+    }
+
     public setMaxValue(sender: any, v: number) {
         const changed = (this._paramDef.max !== v);
         if (changed) {
@@ -424,6 +457,15 @@ export class NgParameter extends InputField implements Observer {
         }
     }
 
+    public resetStepValue(sender: any, v: number) {
+        const changed = (this._paramDef.step !== v);
+        this._isValueModified = false;
+        if (changed) {
+            this._paramDef.step = v;
+            this.notifyStepValueModified(sender);
+        }
+    }
+
     public setStepValue(sender: any, v: number) {
         const changed = (this._paramDef.step !== v);
         if (changed) {
@@ -433,6 +475,15 @@ export class NgParameter extends InputField implements Observer {
         }
     }
 
+    public resetValueList(sender: any, l: number[]) {
+        const changed = (JSON.stringify(this._paramDef.valueList) !== JSON.stringify(l));
+        this._isValueModified = false;
+        if (changed) {
+            this._paramDef.valueList = l;
+            this.notifyListValueModified(sender);
+        }
+    }
+
     public setValueList(sender: any, l: number[]) {
         const changed = (JSON.stringify(this._paramDef.valueList) !== JSON.stringify(l));
         if (changed) {
-- 
GitLab