From 4e39f6945f2c302271d32a6a054ff0b6d343c90b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Thu, 28 Apr 2022 15:01:35 +0200
Subject: [PATCH] refactor: move empty fields management to jalhyd

refs #516
---
 jalhyd_branch                                 |  2 +-
 .../base-param-input.component.ts             |  2 +-
 .../calculator-list.component.ts              |  4 ---
 .../pb-schema/pb-schema.component.ts          | 25 ---------------
 .../formulaire/definition/form-prebarrage.ts  | 15 ---------
 src/app/formulaire/definition/form-section.ts |  5 ---
 src/app/formulaire/elements/fieldset.ts       | 18 ++---------
 .../formulaire/elements/formulaire-node.ts    | 31 -------------------
 src/app/services/app-setup.service.ts         | 12 ++++++-
 9 files changed, 15 insertions(+), 99 deletions(-)

diff --git a/jalhyd_branch b/jalhyd_branch
index 626e97d71..f6d80c358 100644
--- a/jalhyd_branch
+++ b/jalhyd_branch
@@ -1 +1 @@
-devel
\ No newline at end of file
+306-gerer-un-flag-modified-dans-paramdefinition
\ No newline at end of file
diff --git a/src/app/components/base-param-input/base-param-input.component.ts b/src/app/components/base-param-input/base-param-input.component.ts
index 25573571b..883fa0772 100644
--- a/src/app/components/base-param-input/base-param-input.component.ts
+++ b/src/app/components/base-param-input/base-param-input.component.ts
@@ -15,7 +15,7 @@ export class NgBaseParam extends Observable {
 
     constructor(symb: string, d: ParamDomain | ParamDomainValue, val: number, unit?: string) {
         super();
-        this._param = new ParamDefinition(null, symb, d, unit, val);
+        this._param = new ParamDefinition(null, symb, d, unit, val, undefined, undefined, false);
     }
 
     public get param() {
diff --git a/src/app/components/calculator-list/calculator-list.component.ts b/src/app/components/calculator-list/calculator-list.component.ts
index fcc96d90f..cd37dad30 100644
--- a/src/app/components/calculator-list/calculator-list.component.ts
+++ b/src/app/components/calculator-list/calculator-list.component.ts
@@ -181,10 +181,6 @@ export class CalculatorListComponent implements OnInit {
                 }
             }
         }
-
-        if (this.appSetupService.enableEmptyFieldsOnFormInit) {
-            f.emptyFields();
-        }
     }
 
     public get nbOpenCalculators() {
diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index e1e74c9e6..1862a8dcf 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -492,35 +492,10 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
         return this.i18nService.localizeText("INFO_FIELDSET_COPY");
     }
 
-    /**
-     * Set value of all single parameters to undefined, except for the given parameter ids
-     */
-    private emptyFields(basin: PbBassin, except: string[] = FormulaireNode.NeverEmptyFields) {
-        // save current calculated param, as setting value on a CALC param will
-        // change its mode and choose another calculated param by default
-        let calcP: ParamDefinition;
-        for (const p of basin.parameterIterator) {
-            if (
-                [ParamValueMode.SINGLE, ParamValueMode.CALCUL].includes(p.valueMode)
-                && !except.includes(p.symbol)
-            ) {
-                if (p.valueMode === ParamValueMode.CALCUL) {
-                    calcP = p;
-                }
-                p.setValue(undefined);
-            }
-        }
-        // restore original calculated param
-        if (calcP !== undefined) {
-            calcP.setCalculated();
-        }
-    }
-
     /** Adds a new lone basin */
     public onAddBasinClick() {
         const newBasin = new PbBassin(new PbBassinParams(20, 99));
         this.model.addChild(newBasin);
-        this.emptyFields(newBasin);
         this.clearResults();
         this.refreshWithSelection(newBasin.uid);
         this.calculatorComponent.showPBInputData = true;
diff --git a/src/app/formulaire/definition/form-prebarrage.ts b/src/app/formulaire/definition/form-prebarrage.ts
index ebe3ea8ff..811007d2c 100644
--- a/src/app/formulaire/definition/form-prebarrage.ts
+++ b/src/app/formulaire/definition/form-prebarrage.ts
@@ -322,21 +322,6 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
         }
     }
 
-    /**
-     * Set value of all single parameters to undefined, except for the given parameter ids
-     */
-    public emptyFields(except: string[] = FormulaireNode.NeverEmptyFields) {
-        // save current calculated param, as setting value on a CALC param will
-        // change its mode and choose another calculated param by default
-        const paramCalculated = this.currentNub.calculatedParam;
-        for (const p of this.currentNub.parameterIterator) {
-            if (! except.includes(p.symbol)) {
-                p.setValue(undefined);
-            }
-        }
-        this.currentNub.calculatedParam = paramCalculated;
-    }
-
     /**
      *  Check validity of all model parameters
      *  @param withChildren check parameters of child nub as well
diff --git a/src/app/formulaire/definition/form-section.ts b/src/app/formulaire/definition/form-section.ts
index fd45ceb50..c408e572a 100644
--- a/src/app/formulaire/definition/form-section.ts
+++ b/src/app/formulaire/definition/form-section.ts
@@ -20,11 +20,6 @@ export class FormulaireSection extends FormulaireFixedVar {
             }
             // show / hide dependent fields
             this.refreshFieldsets();
-            // empty fields ? only those belonging to the specific section type
-            if (ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit) {
-                // "LargeurBerge" is hackily used as LargeurFond in Rectangular and Trapez sections, omit it here
-                this.emptyFields([ "Ks", "Q", "If", "YB", "iPrec", "Y" ]);
-            }
             this.reset();
         }
     }
diff --git a/src/app/formulaire/elements/fieldset.ts b/src/app/formulaire/elements/fieldset.ts
index e5a9ca15a..4081f8241 100644
--- a/src/app/formulaire/elements/fieldset.ts
+++ b/src/app/formulaire/elements/fieldset.ts
@@ -73,23 +73,13 @@ export class FieldSet extends FormulaireElement implements Observer {
      * @param backup list of NgParameter object representation
      * @see backupParameters
      */
-    public restoreParameters(backup: any[], except: string[] = FormulaireNode.NeverEmptyFields) {
+    public restoreParameters(backup: any[]) {
         // for (const p of this.allFormElements) {
         for (const p of this._kids) {
             if (p instanceof NgParameter) {
                 for (const bp of backup) {
                     if (p.symbol === bp.prmDef.symbol) {
-                        // if source parameter has been user modified, copy value
-                        if (bp.modified) {
-                            p.loadObjectRepresentation(bp);
-                        }
-                        else {
-                            // can parameter be emptied ?
-                            if (ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit && !except.includes(bp.prmDef.symbol)) {
-                                p.resetValue(this, undefined);
-                            }
-                            // else let parameter to default value
-                        }
+                        p.loadObjectRepresentation(bp);
                         break;
                     }
                 }
@@ -421,10 +411,6 @@ export class FieldSet extends FormulaireElement implements Observer {
                         }
                     }
 
-                    if (ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit) {
-                        const f = this.parentForm;
-                        f.emptyFields();
-                    }
                     break; // switch (data.action)
             }
         }
diff --git a/src/app/formulaire/elements/formulaire-node.ts b/src/app/formulaire/elements/formulaire-node.ts
index 0ab8a88df..b57bec770 100644
--- a/src/app/formulaire/elements/formulaire-node.ts
+++ b/src/app/formulaire/elements/formulaire-node.ts
@@ -9,11 +9,6 @@ import { NgParameter } from "./ngparam";
  */
 export abstract class FormulaireNode implements IObservable {
 
-    /**
-     * fields in fieldset that must not be empty due to enableEmptyFieldsOnFormInit option
-     */
-    public static readonly NeverEmptyFields = ["Cd0", "CdWS", "CdGR", "CdGRS", "CdCunge", "CdWR", "CdO", "CdT"];
-
     /** aide en ligne */
     protected _helpLink: string;
 
@@ -145,32 +140,6 @@ export abstract class FormulaireNode implements IObservable {
         return new DeepFormulaireElementIterator(this);
     }
 
-    /**
-     * Set value of all single parameters to undefined, except for the given parameter ids
-     */
-    public emptyFields(except: string[] = FormulaireNode.NeverEmptyFields) {
-        // save current calculated param, as setting value on a CALC param will
-        // change its mode and choose another calculated param by default
-        let calcP: NgParameter;
-        for (const p of this.allFormElements) {
-            if (p instanceof NgParameter) {
-                if (
-                    [ParamValueMode.SINGLE, ParamValueMode.CALCUL].includes(p.valueMode)
-                    && !except.includes(p.id) && !p.isValueModified
-                ) {
-                    if (p.valueMode === ParamValueMode.CALCUL) {
-                        calcP = p;
-                    }
-                    p.resetValue(this, undefined);
-                }
-            }
-        }
-        // restore original calculated param
-        if (calcP !== undefined) {
-            calcP.setCalculated();
-        }
-    }
-
     /**
      * notifie un événement aux observateurs
      */
diff --git a/src/app/services/app-setup.service.ts b/src/app/services/app-setup.service.ts
index d1f01b2af..193b32aaf 100644
--- a/src/app/services/app-setup.service.ts
+++ b/src/app/services/app-setup.service.ts
@@ -24,7 +24,6 @@ export class ApplicationSetupService extends Observable {
     private _maxIterations = 100; // tied to model
     public enableNotifications = true;
     public enableHotkeys = false;
-    public enableEmptyFieldsOnFormInit = true;
 
     public set computePrecision(p: number) {
         this._computePrecision = p;
@@ -46,6 +45,14 @@ export class ApplicationSetupService extends Observable {
         return this._maxIterations;
     }
 
+    public get enableEmptyFieldsOnFormInit() {
+        return !SessionSettings.useDefaultParamValue;
+    }
+
+    public set enableEmptyFieldsOnFormInit(b: boolean) {
+        SessionSettings.useDefaultParamValue = !b;
+    }
+
     /**
      * just stores the current language preference, does not transmit it to I18nService, that is
      * not available here.
@@ -71,6 +78,9 @@ export class ApplicationSetupService extends Observable {
         // related to @HostListener("window:beforeunload") in AppComponent
         this.warnBeforeTabClose = true;
 
+        // by default, create empty fields for new calculators
+        this.enableEmptyFieldsOnFormInit = true;
+
         // load JSON config
         this.readValuesFromConfig().then((data) => {
             const configLanguage = this.language;
-- 
GitLab