From dffc35d91ab14f5aa0f7212ee5be2fa191d68e18 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 30 Jun 2020 12:14:32 +0200
Subject: [PATCH] Patch lifecycle issue when switching from a PreBarrage to
 another

---
 .../generic-calculator/calculator.component.html      | 10 +++++++---
 src/app/components/pb-schema/pb-schema.component.ts   | 11 +++++++----
 src/app/formulaire/definition/form-definition.ts      |  6 ------
 3 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/app/components/generic-calculator/calculator.component.html b/src/app/components/generic-calculator/calculator.component.html
index ff30b8e81..906fc6a98 100644
--- a/src/app/components/generic-calculator/calculator.component.html
+++ b/src/app/components/generic-calculator/calculator.component.html
@@ -81,9 +81,13 @@
                             fxFlex.lt-md="1 0 500px"
                             fxFlex.lt-sm="1 0 300px">
 
-                            <pb-schema [pbSchema]="formElements[0]" (radio)=onRadioClick($event)
-                                (validChange)=onElementValid() (nodeSelected)="onPBNodeSelected($event)">
-                            </pb-schema>
+                            <!-- without this loop, injecting formElements[0] in <pb-schema> makes it not 
+                                trigger lifecycle hooks when switching from a PreBarrage module to another… -->
+                            <ng-template ngFor let-fe [ngForOf]="formElements">
+                                <pb-schema *ngIf="isPbSchema(fe)" [pbSchema]="fe" (radio)=onRadioClick($event)
+                                    (validChange)=onElementValid() (nodeSelected)="onPBNodeSelected($event)">
+                                </pb-schema>
+                            </ng-template>
 
                             <div fxHide.sm fxFlex.gt-sm="0 0 16px"></div>
                         </div>
diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index 3ac6e598c..615af66cd 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -2,12 +2,12 @@ import { Component, Input, Output, EventEmitter, OnInit, AfterViewInit, ViewChil
 import { MatDialog } from "@angular/material/dialog";
 
 import {
-    PreBarrage, PbBassin, PbBassinParams, PbCloison, CreateStructure, LoiDebit, Structure, Observer, IObservable
+    PreBarrage, PbBassin, PbBassinParams, PbCloison, Observer, IObservable
  } from "jalhyd";
 
 import * as mermaid from "mermaid";
 
-import { HotkeysService, Hotkey } from 'angular2-hotkeys';
+import { HotkeysService, Hotkey } from "angular2-hotkeys";
 
 import { I18nService } from "../../services/internationalisation.service";
 import { PbSchema } from "../../formulaire/elements/pb-schema";
@@ -115,10 +115,12 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     }
 
     public ngAfterViewInit(): void {
-        this.refreshEventListeners();
-        this.updateValidity();
         // subscribe to "refresh" event passed indirectly by FormulairePbCloison (change upstream/downstream basin)
         this.pbSchema.addObserver(this);
+        // @WARNING clodo trick to prevent blank diagram when switching from a PreBarrage to another
+        setTimeout(() => {
+            this.refresh();
+        }, 10);
     }
 
     /** Add click listener on every node and link in the graph */
@@ -360,6 +362,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
     public onCopyClick() {
         const wall = this._selectedItem as PbCloison;
         const wallCopy = new PbCloison(wall.bassinAmont, wall.bassinAval);
+        wallCopy.loadObjectRepresentation(wall.objectRepresentation());
         this.model.addChild(wallCopy);
         this.unselect();
         this.refresh();
diff --git a/src/app/formulaire/definition/form-definition.ts b/src/app/formulaire/definition/form-definition.ts
index 5eb6ae295..32b063f9e 100644
--- a/src/app/formulaire/definition/form-definition.ts
+++ b/src/app/formulaire/definition/form-definition.ts
@@ -446,12 +446,6 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
         return new TopFormulaireElementIterator(this);
     }
 
-    /**
-     * Appelé par CalculatorComponent lorsque le Formulaire est chargé dans la vue,
-     * c'est à dire lorsqu'on affiche un module de calcul à l'écran
-     */
-    public onCalculatorInit() {}
-
     //  interface Observer
 
     public update(sender: any, data: any) {
-- 
GitLab