From fa7fd1b614e3f429f4c13abebb06cfd22727d1f5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Tue, 14 Feb 2023 12:45:40 +0100
Subject: [PATCH] feat: solver: set Ytarget to undefined if "empty fields"
 option is set

refs #601
---
 .../calculator.component.ts                   |  1 +
 .../formulaire/definition/form-definition.ts  | 13 +++++++++++++
 src/app/formulaire/definition/form-solveur.ts | 19 ++++++++++++++++++-
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index 49e81d946..48997ffc9 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -412,6 +412,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      */
     protected afterFirstViewChecked() {
         this.updateUIValidity();
+        this.formulaire.setFirstDisplayCompleted();
     }
 
     public onCloseForm() {
diff --git a/src/app/formulaire/definition/form-definition.ts b/src/app/formulaire/definition/form-definition.ts
index 9acb17e78..740e50d8f 100644
--- a/src/app/formulaire/definition/form-definition.ts
+++ b/src/app/formulaire/definition/form-definition.ts
@@ -62,6 +62,11 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
     */
     protected _calcResults: CalculatorResults[];
 
+    /**
+     * Flag indiquant que le composant calculette est proche de la fin du 1er affichage (1er appel Ă  AfterViewChecked)
+     */
+    private _firstDisplay: boolean = true;
+
     constructor(parent?: FormulaireNode) {
         super(parent);
         SessionSettings.instance.addObserver(this);
@@ -81,6 +86,14 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
         return this._calculateDisabled;
     }
 
+    public get isFirstDisplay(): boolean {
+        return this._firstDisplay;
+    }
+
+    public setFirstDisplayCompleted() {
+        this._firstDisplay = false;
+    }
+
     public getPropValue(key: string): any {
         if (this._currentNub === undefined)
             return this.defaultProperties[key];
diff --git a/src/app/formulaire/definition/form-solveur.ts b/src/app/formulaire/definition/form-solveur.ts
index 0e3e8db93..cf2807a7f 100644
--- a/src/app/formulaire/definition/form-solveur.ts
+++ b/src/app/formulaire/definition/form-solveur.ts
@@ -1,9 +1,10 @@
-import { IObservable, ParamDefinition, Nub } from "jalhyd";
+import { IObservable, ParamDefinition, Nub, Props, Solveur } from "jalhyd";
 
 import { NgParameter } from "../elements/ngparam";
 import { FormulaireFixedVar } from "./form-fixedvar";
 import { SelectField } from "../elements/select/select-field";
 import { FieldSet } from "../elements/fieldset";
+import { ServiceFactory } from "app/services/service-factory";
 
 /**
  * Formulaire pour les Solveurs
@@ -34,6 +35,16 @@ export class FormulaireSolveur extends FormulaireFixedVar {
         }
     }
 
+    public initNub(props?: Props) {
+        super.initNub(props);
+
+        // subscribe to Ytarget to be able to reset it during form construction (with empty fields option set)
+        // cf. nghyd#601
+
+        const n: Solveur = this._currentNub as Solveur;
+        n.prms.Ytarget.addObserver(this);
+    }
+
     // interface Observer
 
     public update(sender: IObservable, data: any) {
@@ -84,6 +95,12 @@ export class FormulaireSolveur extends FormulaireFixedVar {
                 // refresh parameters selector
                 this.refreshParameterEntries();
             }
+        } else if (sender instanceof ParamDefinition) {
+            if (data.action === "paramdefinitionAfterValue" && sender.symbol === "Ytarget") {
+                if (this.isFirstDisplay && ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit) {
+                    sender.setValue(undefined);
+                }
+            }
         }
     }
 
-- 
GitLab