From 52ab94a3174d8a6b490d5b91af21af2595ca827d Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Wed, 18 Sep 2019 11:15:36 +0200
Subject: [PATCH] Fix #291 - Lechapt-Calmon: reset results when material
 changes

---
 .../lechapt-calmon/lechapt-calmon.config.json |  1 +
 .../concrete/form-lechapt-calmon.ts           | 38 +++++++++++++++++++
 src/app/services/formulaire.service.ts        |  5 +++
 3 files changed, 44 insertions(+)
 create mode 100644 src/app/formulaire/definition/concrete/form-lechapt-calmon.ts

diff --git a/src/app/calculators/lechapt-calmon/lechapt-calmon.config.json b/src/app/calculators/lechapt-calmon/lechapt-calmon.config.json
index 35f9c27fd..302bf06a1 100644
--- a/src/app/calculators/lechapt-calmon/lechapt-calmon.config.json
+++ b/src/app/calculators/lechapt-calmon/lechapt-calmon.config.json
@@ -27,6 +27,7 @@
     {
         "type": "options",
         "idCal": "J",
+        "lcMaterialSelectId": "select_material",
         "help": "hyd_en_charge/lechapt-calmon"
     }
 ]
\ No newline at end of file
diff --git a/src/app/formulaire/definition/concrete/form-lechapt-calmon.ts b/src/app/formulaire/definition/concrete/form-lechapt-calmon.ts
new file mode 100644
index 000000000..0bdda676d
--- /dev/null
+++ b/src/app/formulaire/definition/concrete/form-lechapt-calmon.ts
@@ -0,0 +1,38 @@
+import { IObservable } from "jalhyd";
+
+import { FormulaireBase } from "./form-base";
+import { FieldSet } from "../../fieldset";
+
+/**
+ * Formulaire pour Lechapt et Calmon
+ */
+export class FormulaireLechaptCalmon extends FormulaireBase {
+
+    /** id of select configuring material */
+    private _lcMaterialSelectId: string;
+
+    protected parseOptions(json: {}) {
+        super.parseOptions(json);
+        this._lcMaterialSelectId = this.getOption(json, "lcMaterialSelectId");
+    }
+
+    public afterParseFieldset(fs: FieldSet) {
+        if (this._lcMaterialSelectId) {
+            const sel = fs.getFormulaireNodeById(this._lcMaterialSelectId);
+            if (sel) {
+                fs.properties.addObserver(this);
+            }
+        }
+    }
+
+    // interface Observer
+
+    public update(sender: IObservable, data: any) {
+        super.update(sender, data);
+        if (data.action === "propertyChange") {
+            if (data.name === "material") {
+                this.reset();
+            }
+        }
+    }
+}
diff --git a/src/app/services/formulaire.service.ts b/src/app/services/formulaire.service.ts
index 9dc427094..c7ec3a6fa 100644
--- a/src/app/services/formulaire.service.ts
+++ b/src/app/services/formulaire.service.ts
@@ -36,6 +36,7 @@ import { NgParameter } from "../formulaire/ngparam";
 import { FieldsetContainer } from "../formulaire/fieldset-container";
 import { FormulairePab } from "../formulaire/definition/concrete/form-pab";
 import { FormulaireMacrorugoCompound } from "../formulaire/definition/concrete/form-macrorugo-compound";
+import { FormulaireLechaptCalmon } from "../formulaire/definition/concrete/form-lechapt-calmon";
 
 @Injectable()
 export class FormulaireService extends Observable {
@@ -297,6 +298,10 @@ export class FormulaireService extends Observable {
                 f = new FormulaireCourbeRemous();
                 break;
 
+            case CalculatorType.LechaptCalmon:
+                f = new FormulaireLechaptCalmon();
+                break;
+
             case CalculatorType.ParallelStructure:
             case CalculatorType.Dever:
             case CalculatorType.Cloisons:
-- 
GitLab