From 438653f23f58e52800605b9a2b45f30c2d2926ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Mon, 1 Aug 2022 17:33:34 +0200
Subject: [PATCH] fix: pre-dams: select upstream basin on calculator
 initialisation

refs #500 #560
---
 .../pb-schema/pb-schema.component.ts          | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index 347e451a6..20b44a7c4 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -124,6 +124,12 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
                 this.selectNodeOnSchema(this._selectedItem);
             }, 20); // timeout has to be greater than the 10ms of ngAfterViewInit()
         }
+        else {
+            // select upstream basin since it's form is already displayed (and not undisplayable...)
+            setTimeout(() => {
+                this.selectUpstreamBasin();
+            }, 20); // timeout has to be greater than the 10ms of ngAfterViewInit()
+        }
     }
 
     private render() {
@@ -421,6 +427,21 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
         });
     }
 
+    /**
+     * select upstream basin on schema
+     */
+    private selectUpstreamBasin() {
+        let done = false; // optimisation : simulate break in forEach
+        this.nativeElement.querySelectorAll("g.node").forEach(item => {
+            if (!done) {
+                if (MermaidUtil.isMermaidId("amont", item.id)) {
+                    this.selectNode(item);
+                    done = true;
+                }
+            }
+        });
+    }
+
     // at this time @Input data is supposed to be already populated
     public ngOnInit() {
         this.model = this.pbSchema.pb;
-- 
GitLab