From 6b6a5ca6bd495db8256fa597effa284b45c6acbb Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 15 Sep 2020 16:20:47 +0200
Subject: [PATCH] Fix PbCloison editor (code lost after rebase)

---
 src/app/calculators/prebarrage/config.json    | 11 ++-
 .../dialog-new-pb-cloison.component.ts        |  2 +-
 .../formulaire/definition/form-pb-cloison.ts  | 42 +++---------
 .../elements/select-field-custom.ts           | 68 ++++++++++++++++++-
 4 files changed, 81 insertions(+), 42 deletions(-)

diff --git a/src/app/calculators/prebarrage/config.json b/src/app/calculators/prebarrage/config.json
index e68895e29..510ae56d1 100644
--- a/src/app/calculators/prebarrage/config.json
+++ b/src/app/calculators/prebarrage/config.json
@@ -161,14 +161,12 @@
                 "fields": [
                     {
                         "id": "select_upstream_basin",
-                        "type": "select_reference",
-                        "reference": "nub",
+                        "type": "select_custom",
                         "source": "upstream_basin"
                     },
                     {
                         "id": "select_downstream_basin",
-                        "type": "select_reference",
-                        "reference": "nub",
+                        "type": "select_custom",
                         "source": "downstream_basin"
                     }
                 ]
@@ -182,9 +180,8 @@
             },
             {
                 "type": "options",
-                "upstreamBasinSelectId": "select_upstream_basin",
-                "downstreamBasinSelectId": "select_downstream_basin",
-                "selectIds": [ "select_structure", "select_loidebit" ]
+                "selectIds": [ "select_structure", "select_loidebit" ],
+                "customSelectIds": [ "select_upstream_basin", "select_downstream_basin" ]
             }
         ]
     },
diff --git a/src/app/components/dialog-new-pb-cloison/dialog-new-pb-cloison.component.ts b/src/app/components/dialog-new-pb-cloison/dialog-new-pb-cloison.component.ts
index a249ca814..9f0962115 100644
--- a/src/app/components/dialog-new-pb-cloison/dialog-new-pb-cloison.component.ts
+++ b/src/app/components/dialog-new-pb-cloison/dialog-new-pb-cloison.component.ts
@@ -18,7 +18,7 @@ export class DialogNewPbCloisonComponent implements OnInit {
     /** the selected downstream basin */
     public downstreamIndex: number;
 
-    /** list of connectable basins, plus reiver upstream / downstrem */
+    /** list of connectable basins, plus river upstream / downstream */
     protected availableBasins: PbBassin[];
 
     constructor(
diff --git a/src/app/formulaire/definition/form-pb-cloison.ts b/src/app/formulaire/definition/form-pb-cloison.ts
index e8b9a8835..b1599380c 100644
--- a/src/app/formulaire/definition/form-pb-cloison.ts
+++ b/src/app/formulaire/definition/form-pb-cloison.ts
@@ -9,34 +9,6 @@ import { SelectFieldCustom } from "../elements/select-field-custom";
 
 export class FormulairePbCloison extends FormulaireParallelStructure {
 
-    /** id of select configuring upstream basin Nub */
-    private _upstreamBasinSelectId: string;
-
-    /** id of select configuring downstream basin Nub */
-    private _downstreamBasinSelectId: string;
-
-    protected parseOptions(json: {}) {
-        super.parseOptions(json);
-        this._upstreamBasinSelectId = this.getOption(json, "upstreamBasinSelectId");
-        this._downstreamBasinSelectId = this.getOption(json, "downstreamBasinSelectId");
-    }
-
-    protected completeParse(firstNotif: boolean = true) {
-        super.completeParse(firstNotif);
-        if (this._upstreamBasinSelectId) {
-            const sel = this.getFormulaireNodeById(this._upstreamBasinSelectId);
-            if (sel) {
-                sel.addObserver(this);
-            }
-        }
-        if (this._downstreamBasinSelectId) {
-            const sel = this.getFormulaireNodeById(this._downstreamBasinSelectId);
-            if (sel) {
-                sel.addObserver(this);
-            }
-        }
-    }
-
     // interface Observer
 
     public update(sender: IObservable, data: any) {
@@ -72,23 +44,27 @@ export class FormulairePbCloison extends FormulaireParallelStructure {
             const nub = this._currentNub as PbCloison;
             const pb = nub.parent;
             // empty "" data.value.value should return undefined, which is good for amont/aval
-            const newBasin = pb.findChild(data.value.value) as PbBassin;
-            if (sender.id === this._upstreamBasinSelectId) {
-                // remove and recreate wall (easier for pointers consistency)
+            const newBasin = pb.findChild(data.value?.value) as PbBassin;
+            if (sender.id === "select_upstream_basin") {
+                // remove and recreate wall (easier for pointers consistency) but preserve UID
+                const uid = nub.uid;
                 const oldDownstreamBasin = nub.bassinAval;
                 pb.deleteChild(pb.findChildPosition(nub.uid));
                 const newWall = new PbCloison(newBasin, oldDownstreamBasin);
+                newWall.setUid(uid);
                 // copy structures
                 for (const s of nub.structures) {
                     newWall.addChild(s);
                 }
                 pb.addChild(newWall);
                 this.currentNub = newWall;
-            } else if (sender.id === this._downstreamBasinSelectId) {
-                // remove and recreate wall (easier for pointers consistency)
+            } else if (sender.id === "select_downstream_basin") {
+                // remove and recreate wall (easier for pointers consistency) but preserve UID
+                const uid = nub.uid;
                 const oldUpstreamBasin = nub.bassinAmont;
                 pb.deleteChild(pb.findChildPosition(nub.uid));
                 const newWall = new PbCloison(oldUpstreamBasin, newBasin);
+                newWall.setUid(uid);
                 // copy structures
                 for (const s of nub.structures) {
                     newWall.addChild(s);
diff --git a/src/app/formulaire/elements/select-field-custom.ts b/src/app/formulaire/elements/select-field-custom.ts
index a9f28c9c6..1036e25ac 100644
--- a/src/app/formulaire/elements/select-field-custom.ts
+++ b/src/app/formulaire/elements/select-field-custom.ts
@@ -3,7 +3,7 @@ import { ServiceFactory } from "../../services/service-factory";
 import { SelectField } from "./select-field";
 import { decodeHtml, arraysAreEqual } from "../../util";
 
-import { FishSpecies, Session, Solveur, FishPass, CalculatorType, Verificateur, Nub } from "jalhyd";
+import { FishSpecies, Session, Solveur, FishPass, CalculatorType, Verificateur, Nub, PbCloison, PreBarrage } from "jalhyd";
 
 import { sprintf } from "sprintf-js";
 
@@ -50,6 +50,18 @@ export class SelectFieldCustom extends SelectField {
                     }));
                 }
                 break;
+
+            case "upstream_basin": // PbCloisons, bassin amont
+                const ub = (nub as PbCloison).bassinAmont;
+                // console.log("-- load UB", ub, this._entriesBaseId + ub?.uid);
+                this.setValueFromId(this._entriesBaseId + (ub ? ub.uid : "none"));
+                break;
+
+            case "downstream_basin": // PbCloisons, bassin aval
+                const db = (nub as PbCloison).bassinAval;
+                // console.log("-- load DB", db, this._entriesBaseId + db?.uid);
+                this.setValueFromId(this._entriesBaseId + (db ? db.uid : "none"));
+                break;
         }
     }
 
@@ -152,6 +164,60 @@ export class SelectFieldCustom extends SelectField {
                     );
                 }
                 break;
+
+            case "upstream_basin": // PbCloisons, bassin amont
+                const pbWallU = this.parentForm.currentNub as PbCloison;
+                const preBarrageU = pbWallU.parent as PreBarrage;
+                const posDb = pbWallU.bassinAval?.findPositionInParent();
+                // river upstream
+                this.addEntry(
+                    new SelectEntry(
+                        this._entriesBaseId + "none",
+                        undefined,
+                        ServiceFactory.i18nService.localizeText("INFO_LIB_AMONT")
+                    )
+                );
+                // all available basins, depending on current downstream basin
+                for (const b of preBarrageU.bassins) {
+                    const pos = b.findPositionInParent();
+                    if (posDb === undefined || pos < posDb) {
+                        this.addEntry(
+                            new SelectEntry(
+                                this._entriesBaseId + b.uid,
+                                b.uid,
+                                ServiceFactory.i18nService.localizeMessage(b.description)
+                            )
+                        );
+                    }
+                }
+                break;
+
+            case "downstream_basin": // PbCloisons, bassin aval
+                const pbWallD = this.parentForm.currentNub as PbCloison;
+                const preBarrageD = pbWallD.parent as PreBarrage;
+                const posUb = pbWallD.bassinAmont?.findPositionInParent();
+                // all available basins, depending on current upstream basin
+                for (const b of preBarrageD.bassins) {
+                    const pos = b.findPositionInParent();
+                    if (posUb === undefined || pos > posUb) {
+                        this.addEntry(
+                            new SelectEntry(
+                                this._entriesBaseId + b.uid,
+                                b.uid,
+                                ServiceFactory.i18nService.localizeMessage(b.description)
+                            )
+                        );
+                    }
+                }
+                // river downstream
+                this.addEntry(
+                    new SelectEntry(
+                        this._entriesBaseId + "none",
+                        undefined,
+                        ServiceFactory.i18nService.localizeText("INFO_LIB_AVAL")
+                    )
+                );
+                break;
         }
     }
 
-- 
GitLab