From 02482c8a102f639c57c2ec53c54942d38cfb6a9c Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Thu, 19 Apr 2018 11:15:22 +0200
Subject: [PATCH] =?UTF-8?q?=20#77=20sections=20param=C3=A9tr=C3=A9es=20:?=
 =?UTF-8?q?=20le=20changement=20de=20type=20de=20section=20est=20d=C3=A9te?=
 =?UTF-8?q?ct=C3=A9=20par=20abonnement=20au=20propri=C3=A9t=C3=A9s=20du=20?=
 =?UTF-8?q?fieldset=20contenant=20le=20select,=20plus=20au=20select=20lui?=
 =?UTF-8?q?=20m=C3=AAme?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../section-param/section-param.config.json   |  2 +-
 .../formulaire/definition/form-def-section.ts | 44 ++++++++++++-------
 src/app/formulaire/fieldset.ts                |  9 ++++
 3 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/src/app/calculators/section-param/section-param.config.json b/src/app/calculators/section-param/section-param.config.json
index 9740f86d1..eaf075531 100644
--- a/src/app/calculators/section-param/section-param.config.json
+++ b/src/app/calculators/section-param/section-param.config.json
@@ -289,6 +289,6 @@
     },
     {
         "type": "options",
-        "sectionSelectId": "select_section"
+        "sectionSourceId": "fs_section"
     }
 ]
\ No newline at end of file
diff --git a/src/app/formulaire/definition/form-def-section.ts b/src/app/formulaire/definition/form-def-section.ts
index bfc65ea32..ac9050d13 100644
--- a/src/app/formulaire/definition/form-def-section.ts
+++ b/src/app/formulaire/definition/form-def-section.ts
@@ -8,12 +8,13 @@ import { Field } from "../field";
 import { IObservable, Observer } from "../../services/observer";
 import { NgParameter, ParamRadioConfig } from "../ngparam";
 import { FormulaireDefinition } from "./form-definition";
+import { FieldSet } from "../fieldset";
 
 export class FormDefSection implements Observer {
     /**
-     * id du SelectField configurant le type de section
+     * id de l'élément configurant le type de section
      */
-    private _sectionSelectFieldId: string;
+    private _sectionSourceId: string;
 
     /**
      * Type de noeud de calcul actuel de la section (si la calculette est basée sur une section et qu'un menu permet 
@@ -27,8 +28,8 @@ export class FormDefSection implements Observer {
         this._formBase = base;
     }
 
-    private get hasSectionNodeTypeSelect(): boolean {
-        return this._sectionSelectFieldId != undefined;
+    private get hasSectionNodeTypeSource(): boolean {
+        return this._sectionSourceId != undefined;
     }
 
     public get sectionNodeType(): ComputeNodeType {
@@ -36,7 +37,7 @@ export class FormDefSection implements Observer {
     }
 
     private updateSectionNodeType() {
-        const nt = this.getNodeTypeFromSelectField();
+        const nt = this.getNodeTypeFromSourceElement();
         if (this._sectionNodeType !== nt) {
             this._sectionNodeType = nt;
             this._formBase.reset();
@@ -53,26 +54,37 @@ export class FormDefSection implements Observer {
         return { symbol, label };
     }
 
-    private getNodeTypeFromSelectField(): ComputeNodeType {
-        return this._formBase.getSelectedValue(this._sectionSelectFieldId);
+    /**
+     * @return l'élément déterminant le type de section
+     */
+    private getSectionSourceElement(): FieldSet {
+        const fs = this._formBase.getFormulaireNodeById(this._sectionSourceId);
+        if (fs === undefined || !(fs instanceof FieldSet))
+            throw new Error(`le champ ${this._sectionSourceId} n'est pas du type FieldSet`);
+        return fs;
+    }
+
+    /**
+     * @return le type de section déterminé par l'élément source
+     */
+    private getNodeTypeFromSourceElement(): ComputeNodeType {
+        const fs: FieldSet = this.getSectionSourceElement();
+        return fs.getPropValue("nodeType");
     }
 
     public initParse() {
     }
 
     public parseOptions(json: {}) {
-        // id du SelectField configurant le type de section
-        this._sectionSelectFieldId = this._formBase.getOption(json, "sectionSelectId");
+        // id de l'élément configurant le type de section
+        this._sectionSourceId = this._formBase.getOption(json, "sectionSourceId");
     }
 
     public completeParse() {
         // si le formulaire a un menu pour le type de section, on s'abonne à la valeur du champ correspondant
-        if (this.hasSectionNodeTypeSelect) {
-            const f: Field = this._formBase.getFieldById(this._sectionSelectFieldId);
-            if (f == undefined || !(f instanceof SelectField))
-                throw new Error(`le champ ${this._sectionSelectFieldId} n'est pas du type SelectField`);
-            const sectTypeField: SelectField = f as SelectField;
-            sectTypeField.addObserver(this);
+        if (this.hasSectionNodeTypeSource) {
+            const se = this.getSectionSourceElement();
+            se.addObserver(this);
             this.updateSectionNodeType()
         }
     }
@@ -80,7 +92,7 @@ export class FormDefSection implements Observer {
     // interface Observer 
 
     update(sender: any, data: any): void {
-        if (sender instanceof SelectField) {
+        if (data.action == "propertyChange") {
             this.updateSectionNodeType();
         }
     }
diff --git a/src/app/formulaire/fieldset.ts b/src/app/formulaire/fieldset.ts
index 4a4eb4ddb..3b098f78a 100644
--- a/src/app/formulaire/fieldset.ts
+++ b/src/app/formulaire/fieldset.ts
@@ -106,10 +106,19 @@ export class FieldSet extends FormulaireElement implements Observer {
         return this._props.getPropValue(key);
     }
 
+    private notifyPropChange(prop: string, val: any) {
+        this.notifyObservers({
+            "action": "propertyChange",
+            "name": prop,
+            "value": val
+        }, this)
+    }
+
     public setPropValue(key: string, val: any): boolean {
         const changed = this._props.getPropValue(key) !== val;
         if (changed) {
             this._props.setPropValue(key, val);
+            this.notifyPropChange(key, val);
 
             // si prop=type d'ouvrage, on prend une loi de débit compatible avec (spécifique aux ouvrages //) comme valeur par défaut
             if (key === "structureType") {
-- 
GitLab