From ea4ed26b9c3ebf81ca444934ad5a5cb21c5c2b5c Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@irstea.fr>
Date: Wed, 30 Sep 2020 17:32:49 +0200
Subject: [PATCH 1/3] refactor: update scheme on modification of prebarrage
 structure

- !! console.debug everywhere

Refs #469
---
 .../calculator.component.ts                   |  2 ++
 .../pb-schema/pb-schema.component.ts          | 28 +++++++------------
 .../formulaire/definition/form-definition.ts  | 17 ++++++-----
 .../formulaire/definition/form-prebarrage.ts  |  3 ++
 src/app/formulaire/elements/pb-schema.ts      |  1 +
 src/app/services/formulaire.service.ts        |  1 +
 6 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index dc3d0900a..9671f8878 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -541,6 +541,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      * réception d'un événement de changement de valeur d'un input
      */
     public onInputChange(event: any) {
+        console.debug(`CalculatorComponent.onInputChange(${event?.symbol})`);
         this._formulaire.resetResults([], (event ? event.symbol : undefined));
     }
 
@@ -584,6 +585,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
 
     /** réception d'un événement de clic sur un nœud du schéma de PréBarrage */
     public onPBNodeSelected(event: any) {
+        console.debug(`GenericCalculatorComponent.onPBNodeSelected(${event?.node?.uid})`);
         // show proper form (actually subform elements) or proper results,
         // depending on what was clicked
         (this._formulaire as FormulairePrebarrage).nodeSelected(event.node);
diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index 026f5e3ee..0ba88a7ba 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -298,6 +298,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     }
 
     private selectNode(item: any) {
+        console.debug(`PbSchemaComponent.selectNode(${item?.id})`);
         // highlight clicked element
         this.clearHighlightedItems();
         item.classList.add("node-highlighted");
@@ -449,9 +450,8 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
                 this.model.addChild(new PbCloison(undefined, this._selectedItem.bassinAval));
             }
         }
-        this.unselect();
-        this.refresh();
         this.clearResults();
+        this.refreshWithSelection();
         this.calculatorComponent.showPBInputData = true;
     }
 
@@ -476,10 +476,8 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
         const wallCopy = new PbCloison(wall.bassinAmont, wall.bassinAval);
         wallCopy.loadObjectRepresentation(wall.objectRepresentation());
         this.model.addChild(wallCopy);
-        this.unselect();
-        this.refresh();
-        this.selectNodeOnSchema(wallCopy);
         this.clearResults();
+        this.refreshWithSelection(wallCopy.uid);
         this.calculatorComponent.showPBInputData = true;
     }
 
@@ -491,10 +489,8 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     public onAddBasinClick() {
         const newBasin = new PbBassin(new PbBassinParams(20, 99));
         this.model.addChild(newBasin);
-        this.unselect();
-        this.refresh();
-        this.selectNodeOnSchema(newBasin);
         this.clearResults();
+        this.refreshWithSelection(newBasin.uid);
         this.calculatorComponent.showPBInputData = true;
     }
 
@@ -526,10 +522,8 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
                     result.down === 0 ? undefined : this.model.bassins[result.down - 1]
                 );
                 this.model.addChild(wall);
-                this.unselect();
-                this.refresh();
-                this.selectNodeOnSchema(wall);
                 this.clearResults();
+                this.refreshWithSelection(wall.uid);
                 this.calculatorComponent.showPBInputData = true;
             }
         });
@@ -552,10 +546,8 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
             this.model.moveBasin(this._selectedItem.uid, this.model.findBasinPosition(this._selectedItem.uid) - 1);
         }
         const basin = this._selectedItem;
-        this.unselect();
-        this.refresh();
-        this.selectNodeOnSchema(basin);
         this.clearResults();
+        this.refreshWithSelection(this._selectedItem.uid);
         this.calculatorComponent.showPBInputData = true;
     }
 
@@ -576,10 +568,8 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
             this.model.moveBasin(this._selectedItem.uid, this.model.findBasinPosition(this._selectedItem.uid) + 1);
         }
         const basin = this._selectedItem;
-        this.unselect();
-        this.refresh();
-        this.selectNodeOnSchema(basin);
         this.clearResults();
+        this.refreshWithSelection(this._selectedItem.uid);
         this.calculatorComponent.showPBInputData = true;
     }
 
@@ -659,6 +649,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     }
 
     private unselect() {
+        console.debug(`PbSchemaComponent.unselect()`);
         this._selectedItem = undefined;
         this.clearHighlightedItems();
         this.nodeSelected.emit({}); // nothing selected
@@ -667,7 +658,6 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     /** clear all PB form results whenever the basins / walls layout is changed */
     private clearResults() {
         this.pbSchema.form.reset();
-        this.refreshWithSelection();
     }
 
     /**
@@ -675,6 +665,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
      * nub uid, else keeps previous selection
      */
     private refreshWithSelection(uid ?: string) {
+        console.debug(`PbSchemaComponent.refreshWithSelection(${uid})`);
         // remember previously selected node
         const selectedNodeUID = this._selectedItem?.uid;
         this.unselect();
@@ -691,6 +682,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     // interface Observer
 
     public update(sender: IObservable, data: any) {
+        console.debug(`PbSchemaComponent.update:`, data);
         if (sender instanceof PbSchema) {
             if (data.action === "refresh") {
                 this.refreshWithSelection(data.value);
diff --git a/src/app/formulaire/definition/form-definition.ts b/src/app/formulaire/definition/form-definition.ts
index 54231bc1a..f98124d81 100644
--- a/src/app/formulaire/definition/form-definition.ts
+++ b/src/app/formulaire/definition/form-definition.ts
@@ -419,13 +419,16 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
      *      links will be considered as dependencies @see jalhyd#98
      */
     public resetResults(visited: string[] = [], symbol?: string, forceResetAllDependencies: boolean = false) {
-        visited.push(this.currentNub.uid);
-        // reset GUI results
-        this.resetFormResults();
-        // reset model results
-        this.currentNub.resetResult();
-        // reset the result panels of all forms depending on this one
-        ServiceFactory.formulaireService.resetAllDependingFormsResults(this, visited, symbol, forceResetAllDependencies);
+        console.debug(`FormulaireDefinition.resetResults(${visited})`);
+        if (this.currentNub.result !== undefined) {
+            visited.push(this.currentNub.uid);
+            // reset GUI results
+            this.resetFormResults();
+            // reset model results
+            this.currentNub.resetResult();
+            // reset the result panels of all forms depending on this one
+            ServiceFactory.formulaireService.resetAllDependingFormsResults(this, visited, symbol, forceResetAllDependencies);
+        }
     }
 
     protected abstract compute();
diff --git a/src/app/formulaire/definition/form-prebarrage.ts b/src/app/formulaire/definition/form-prebarrage.ts
index 73b4df1c5..92dddd70c 100644
--- a/src/app/formulaire/definition/form-prebarrage.ts
+++ b/src/app/formulaire/definition/form-prebarrage.ts
@@ -125,6 +125,7 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
      * envoie un événement "nodeSelected" (on a cliqué sur un nœud)
      */
     public nodeSelected(node: PbBassin | PbCloison) {
+        console.debug(`FormulairePrebarrage.NodeSelected(${node?.uid})`);
         // did something change, or we just clicked again on the node that was already selected ?
         if (this._selectedItem !== node) {
             // store for results formatting
@@ -199,6 +200,7 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
     // interface Observer
 
     public update(sender: IObservable, data: any) {
+        console.debug("FormulairePrebarrage.update:", data);
         super.update(sender, data);
         if (sender instanceof FormulairePbCloison) {
             if (data.action === "updateBasin") {
@@ -300,6 +302,7 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
     }
 
     public resetResults() {
+        console.debug("FormulairePrebarrage.resetResults()");
         const hasToRedraw = (this._currentNub.result !== undefined);
         super.resetResults();
         // reset all children nubs
diff --git a/src/app/formulaire/elements/pb-schema.ts b/src/app/formulaire/elements/pb-schema.ts
index f9c5afae5..75565d6a5 100644
--- a/src/app/formulaire/elements/pb-schema.ts
+++ b/src/app/formulaire/elements/pb-schema.ts
@@ -38,6 +38,7 @@ export class PbSchema extends FormulaireElement {
 
     /** Asks PbSchemaComponent to redraw the schema */
     public refresh(nodeUidToSelect?: string) {
+        console.debug(`PbSchema.refresh(${nodeUidToSelect})`);
         this.notifyObservers({
             action: "refresh",
             value: nodeUidToSelect
diff --git a/src/app/services/formulaire.service.ts b/src/app/services/formulaire.service.ts
index 9a1abbe1a..9e000fc0c 100644
--- a/src/app/services/formulaire.service.ts
+++ b/src/app/services/formulaire.service.ts
@@ -815,6 +815,7 @@ export class FormulaireService extends Observable {
                 if (form) {
                     const hadResults = form.hasResults;
                     // form might not have a result, but still have another form depending on it !
+                    console.debug(`FormulaireService.resetAllDependingFormsResults(form->${dn.uid})`);
                     form.resetResults(visited);
                     if (hadResults) {
                         if (notify) {
-- 
GitLab


From ac8514473f4211d5b7785af74af8b8bd5d0d4740 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@irstea.fr>
Date: Wed, 3 Nov 2021 15:49:32 +0100
Subject: [PATCH 2/3] fix(PreBarrage): loss of focus on input with existing
 result

- remove unselection of the child on schema refresh
- add unselection on remove child action to focus on the parent when a child is removed

Refs #469
---
 src/app/components/pb-schema/pb-schema.component.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index 0ba88a7ba..eab19cd1f 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -451,6 +451,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
             }
         }
         this.clearResults();
+        this.unselect();
         this.refreshWithSelection();
         this.calculatorComponent.showPBInputData = true;
     }
@@ -668,7 +669,6 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
         console.debug(`PbSchemaComponent.refreshWithSelection(${uid})`);
         // remember previously selected node
         const selectedNodeUID = this._selectedItem?.uid;
-        this.unselect();
         this.refresh();
         // select a specific node on the schema
         if (uid !== undefined) {
-- 
GitLab


From 098872202d6155196ca24aeda30513b1b603da03 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@irstea.fr>
Date: Wed, 3 Nov 2021 15:53:45 +0100
Subject: [PATCH 3/3] refactor: remove console.debug

Refs #469
---
 .../components/generic-calculator/calculator.component.ts | 4 ++--
 src/app/components/pb-schema/pb-schema.component.ts       | 8 ++++----
 src/app/formulaire/definition/form-definition.ts          | 2 +-
 src/app/formulaire/definition/form-prebarrage.ts          | 6 +++---
 src/app/formulaire/elements/pb-schema.ts                  | 2 +-
 src/app/services/formulaire.service.ts                    | 2 +-
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index 9671f8878..718ff08f5 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -541,7 +541,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      * réception d'un événement de changement de valeur d'un input
      */
     public onInputChange(event: any) {
-        console.debug(`CalculatorComponent.onInputChange(${event?.symbol})`);
+        // console.debug(`CalculatorComponent.onInputChange(${event?.symbol})`);
         this._formulaire.resetResults([], (event ? event.symbol : undefined));
     }
 
@@ -585,7 +585,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
 
     /** réception d'un événement de clic sur un nœud du schéma de PréBarrage */
     public onPBNodeSelected(event: any) {
-        console.debug(`GenericCalculatorComponent.onPBNodeSelected(${event?.node?.uid})`);
+        // console.debug(`GenericCalculatorComponent.onPBNodeSelected(${event?.node?.uid})`);
         // show proper form (actually subform elements) or proper results,
         // depending on what was clicked
         (this._formulaire as FormulairePrebarrage).nodeSelected(event.node);
diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index eab19cd1f..cdbe3fad0 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -298,7 +298,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     }
 
     private selectNode(item: any) {
-        console.debug(`PbSchemaComponent.selectNode(${item?.id})`);
+        // console.debug(`PbSchemaComponent.selectNode(${item?.id})`);
         // highlight clicked element
         this.clearHighlightedItems();
         item.classList.add("node-highlighted");
@@ -650,7 +650,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     }
 
     private unselect() {
-        console.debug(`PbSchemaComponent.unselect()`);
+        // console.debug(`PbSchemaComponent.unselect()`);
         this._selectedItem = undefined;
         this.clearHighlightedItems();
         this.nodeSelected.emit({}); // nothing selected
@@ -666,7 +666,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
      * nub uid, else keeps previous selection
      */
     private refreshWithSelection(uid ?: string) {
-        console.debug(`PbSchemaComponent.refreshWithSelection(${uid})`);
+        // console.debug(`PbSchemaComponent.refreshWithSelection(${uid})`);
         // remember previously selected node
         const selectedNodeUID = this._selectedItem?.uid;
         this.refresh();
@@ -682,7 +682,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     // interface Observer
 
     public update(sender: IObservable, data: any) {
-        console.debug(`PbSchemaComponent.update:`, data);
+        // console.debug(`PbSchemaComponent.update:`, data);
         if (sender instanceof PbSchema) {
             if (data.action === "refresh") {
                 this.refreshWithSelection(data.value);
diff --git a/src/app/formulaire/definition/form-definition.ts b/src/app/formulaire/definition/form-definition.ts
index f98124d81..4e1811f5c 100644
--- a/src/app/formulaire/definition/form-definition.ts
+++ b/src/app/formulaire/definition/form-definition.ts
@@ -419,7 +419,7 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
      *      links will be considered as dependencies @see jalhyd#98
      */
     public resetResults(visited: string[] = [], symbol?: string, forceResetAllDependencies: boolean = false) {
-        console.debug(`FormulaireDefinition.resetResults(${visited})`);
+        // console.debug(`FormulaireDefinition.resetResults(${visited})`);
         if (this.currentNub.result !== undefined) {
             visited.push(this.currentNub.uid);
             // reset GUI results
diff --git a/src/app/formulaire/definition/form-prebarrage.ts b/src/app/formulaire/definition/form-prebarrage.ts
index 92dddd70c..4400467c8 100644
--- a/src/app/formulaire/definition/form-prebarrage.ts
+++ b/src/app/formulaire/definition/form-prebarrage.ts
@@ -125,7 +125,7 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
      * envoie un événement "nodeSelected" (on a cliqué sur un nœud)
      */
     public nodeSelected(node: PbBassin | PbCloison) {
-        console.debug(`FormulairePrebarrage.NodeSelected(${node?.uid})`);
+        // console.debug(`FormulairePrebarrage.NodeSelected(${node?.uid})`);
         // did something change, or we just clicked again on the node that was already selected ?
         if (this._selectedItem !== node) {
             // store for results formatting
@@ -200,7 +200,7 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
     // interface Observer
 
     public update(sender: IObservable, data: any) {
-        console.debug("FormulairePrebarrage.update:", data);
+        // console.debug("FormulairePrebarrage.update:", data);
         super.update(sender, data);
         if (sender instanceof FormulairePbCloison) {
             if (data.action === "updateBasin") {
@@ -302,7 +302,7 @@ export class FormulairePrebarrage extends FormulaireFixedVar {
     }
 
     public resetResults() {
-        console.debug("FormulairePrebarrage.resetResults()");
+        // console.debug("FormulairePrebarrage.resetResults()");
         const hasToRedraw = (this._currentNub.result !== undefined);
         super.resetResults();
         // reset all children nubs
diff --git a/src/app/formulaire/elements/pb-schema.ts b/src/app/formulaire/elements/pb-schema.ts
index 75565d6a5..7b0df1a73 100644
--- a/src/app/formulaire/elements/pb-schema.ts
+++ b/src/app/formulaire/elements/pb-schema.ts
@@ -38,7 +38,7 @@ export class PbSchema extends FormulaireElement {
 
     /** Asks PbSchemaComponent to redraw the schema */
     public refresh(nodeUidToSelect?: string) {
-        console.debug(`PbSchema.refresh(${nodeUidToSelect})`);
+        // console.debug(`PbSchema.refresh(${nodeUidToSelect})`);
         this.notifyObservers({
             action: "refresh",
             value: nodeUidToSelect
diff --git a/src/app/services/formulaire.service.ts b/src/app/services/formulaire.service.ts
index 9e000fc0c..868f2666b 100644
--- a/src/app/services/formulaire.service.ts
+++ b/src/app/services/formulaire.service.ts
@@ -815,7 +815,7 @@ export class FormulaireService extends Observable {
                 if (form) {
                     const hadResults = form.hasResults;
                     // form might not have a result, but still have another form depending on it !
-                    console.debug(`FormulaireService.resetAllDependingFormsResults(form->${dn.uid})`);
+                    // console.debug(`FormulaireService.resetAllDependingFormsResults(form->${dn.uid})`);
                     form.resetResults(visited);
                     if (hadResults) {
                         if (notify) {
-- 
GitLab