From 170e5f96ddbe85c450afbc1b66c86861333d74de Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@irstea.fr>
Date: Tue, 25 Jun 2019 11:13:59 +0200
Subject: [PATCH] jalhyd/#33 Delete references to PabCloisons. #93 Working
 generation of PAB from cloison (new version).

---
 .../calculator-list.component.ts              |  1 -
 .../calculator.component.ts                   | 20 ++----------
 .../generic-input/generic-input.component.ts  |  4 +--
 src/app/formulaire/fieldset.ts                | 30 ------------------
 .../select-field-model-cloison-aval.ts        | 31 -------------------
 .../formulaire/select-field-model-cloisons.ts | 31 -------------------
 .../services/formulaire/formulaire.service.ts |  3 +-
 7 files changed, 7 insertions(+), 113 deletions(-)
 delete mode 100644 src/app/formulaire/select-field-model-cloison-aval.ts
 delete mode 100644 src/app/formulaire/select-field-model-cloisons.ts

diff --git a/src/app/components/calculator-list/calculator-list.component.ts b/src/app/components/calculator-list/calculator-list.component.ts
index f05e933f0..0b2acf60d 100644
--- a/src/app/components/calculator-list/calculator-list.component.ts
+++ b/src/app/components/calculator-list/calculator-list.component.ts
@@ -79,7 +79,6 @@ export class CalculatorListComponent implements OnInit {
                     if ( // those sub-Nub types cannot be built outside a parent
                         t !== CalculatorType.Structure
                         && t !== CalculatorType.Section
-                        && t !== CalculatorType.PabCloisons
                     ) {
                         unusedTheme.calculators.push({
                             type: t,
diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index 0392ec276..e662fc437 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -2,7 +2,7 @@ import { Component, OnInit, DoCheck, OnDestroy, ViewChild, ViewChildren,
          QueryList, AfterViewChecked, ElementRef } from "@angular/core";
 import { ActivatedRoute, Router } from "@angular/router";
 
-import { Observer, Session, Cloisons, Pab, PabCloisons, ParamValueMode, CalculatorType } from "jalhyd";
+import { Observer, Session, Cloisons, Pab, ParamValueMode, CalculatorType } from "jalhyd";
 
 import { FormulaireService } from "../../services/formulaire/formulaire.service";
 import { I18nService } from "../../services/internationalisation/internationalisation.service";
@@ -21,7 +21,6 @@ import { ServiceFactory } from "../../services/service-factory";
 import { MatDialog } from "@angular/material";
 import { DialogConfirmCloseCalcComponent } from "../dialog-confirm-close-calc/dialog-confirm-close-calc.component";
 import { DialogGeneratePABComponent } from "../dialog-generate-pab/dialog-generate-pab.component";
-import { SelectFieldModel } from "../../formulaire/select-field-model";
 import { PabTable } from "../../formulaire/pab-table";
 
 @Component({
@@ -484,21 +483,8 @@ export class GenericCalculatorComponent extends BaseComponent implements OnInit,
                 params.Z1.singleValue = result.coteAmont;
                 params.Z2.singleValue = result.coteAval;
                 // création des bassins
-                for (let i = 0; i < result.nbBassins; i++) {
-                    const pabCloisons = new PabCloisons(this._formulaire.currentNub as Cloisons);
-                    pab.addChild(pabCloisons); // @TODO should be replace afterwards
-                    for (const e of f.allFormElements) {
-                        if (e instanceof FieldsetContainer) {
-                            const newFieldset = e.addFromTemplate(0, undefined, pabCloisons);
-                            // set selected value by ID; nub should be set by "select value changed" event listener
-                            const modeleSelect = (newFieldset.getFormulaireNodeById("select_modele_cloisons") as SelectFieldModel);
-                            modeleSelect.updateEntries();
-                            // ID of the Cloisons nub used by pabCloisons as a model
-                            modeleSelect.setValueFromId(this._formulaire.currentNub.uid);
-                            break;
-                        }
-                    }
-                }
+                pab.deleteChild(0);
+                pab.addCloisonsFromModel(this._formulaire.currentNub as Cloisons, result.nbBassins);
                 // go to new PAB
                 this.router.navigate(["/calculator", f.uid]);
               });
diff --git a/src/app/components/generic-input/generic-input.component.ts b/src/app/components/generic-input/generic-input.component.ts
index 4b23185d6..19232853b 100644
--- a/src/app/components/generic-input/generic-input.component.ts
+++ b/src/app/components/generic-input/generic-input.component.ts
@@ -1,7 +1,7 @@
 import { Input, Output, EventEmitter, ChangeDetectorRef, OnChanges, ViewChild } from "@angular/core";
 import { NgModel } from "@angular/forms";
 import { BaseComponent } from "../base/base.component";
-import { isNumeric, Structure, PabCloisons } from "jalhyd";
+import { isNumeric, Structure, Pab } from "jalhyd";
 import { FormulaireDefinition } from "../../formulaire/definition/form-definition";
 import { NgParameter } from "../../formulaire/ngparam";
 import { I18nService } from "../../services/internationalisation/internationalisation.service";
@@ -46,7 +46,7 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
                 // if inside a nested Structure, prefix with Structure position
                 // to disambiguate
                 const nub = param.paramDefinition.parentNub;
-                if (nub && (nub instanceof Structure || nub instanceof PabCloisons)) {
+                if (nub && (nub instanceof Structure || nub.parent instanceof Pab)) {
                     id = nub.findPositionInParent() + "_" + id;
                 }
             }
diff --git a/src/app/formulaire/fieldset.ts b/src/app/formulaire/fieldset.ts
index a1eb7b57d..10d1a13ad 100644
--- a/src/app/formulaire/fieldset.ts
+++ b/src/app/formulaire/fieldset.ts
@@ -8,8 +8,6 @@ import { NgParameter, ParamRadioConfig } from "./ngparam";
 import { FormulaireDefinition } from "./definition/form-definition";
 import { StringMap } from "../stringmap";
 import { FormulaireNode } from "./formulaire-node";
-import { SelectFieldModelCloisons } from "./select-field-model-cloisons";
-import { SelectFieldModelCloisonAval } from "./select-field-model-cloison-aval";
 import { FieldsetContainer } from "./fieldset-container";
 
 export class FieldSet extends FormulaireElement implements Observer {
@@ -87,24 +85,6 @@ export class FieldSet extends FormulaireElement implements Observer {
         return res;
     }
 
-    // non-generic version of parse_select for SelectFieldModel because
-    // downcasting is not possible with @Input() apparently
-    private parse_select_cloisons(json: {}): SelectField {
-        const res: SelectFieldModel = new SelectFieldModelCloisons(this, CalculatorType.Cloisons);
-        res.parseConfig(json);
-        res.addObserver(this);
-        return res;
-    }
-
-    // non-generic version of parse_select for SelectFieldCloisonAval because
-    // downcasting is not possible with @Input() apparently
-    private parse_select_cloison_aval(json: {}): SelectField {
-        const res: SelectFieldModel = new SelectFieldModelCloisonAval(this, CalculatorType.ParallelStructure);
-        res.parseConfig(json);
-        res.addObserver(this);
-        return res;
-    }
-
     public get properties(): Props {
         return this.nub.properties;
     }
@@ -182,16 +162,6 @@ export class FieldSet extends FormulaireElement implements Observer {
                     this.addField(param);
                     break;
 
-                case "select_cloisons":
-                    param = this.parse_select_cloisons(field);
-                    this.addField(param);
-                    break;
-
-                case "select_cloison_aval":
-                    param = this.parse_select_cloison_aval(field);
-                    this.addField(param);
-                    break;
-
             }
         }
     }
diff --git a/src/app/formulaire/select-field-model-cloison-aval.ts b/src/app/formulaire/select-field-model-cloison-aval.ts
deleted file mode 100644
index b178d9af3..000000000
--- a/src/app/formulaire/select-field-model-cloison-aval.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import { Session } from "jalhyd";
-import { SelectEntry } from "./select-entry";
-import { FieldSet } from "./fieldset";
-import { SelectFieldModel } from "./select-field-model";
-
-/**
- * A select field that populates itself with references to
- * available ParallelStructure modules (used by PAB)
- */
-export class SelectFieldModelCloisonAval extends SelectFieldModel {
-
-    protected initSelectedValue() {
-        if (this.parent instanceof FieldSet) {
-            const mc = this.parent.nub.properties.getPropValue("modeleCloisonAval");
-            if (mc) {
-                this._selectedEntry = new SelectEntry(mc, {});
-            } // else if current model is undefined, do not select it so that default ParallelStructures will be chosen (if available)
-        }
-    }
-
-    /**
-     * Populates entries with available ParallelStructures
-     */
-    protected populate() {
-        const psNubs = Session.getInstance().getParallelStructureNubs();
-        for (const cl of psNubs) {
-            const e = new SelectEntry(cl.uid, cl);
-            this.addEntry(e);
-        }
-    }
-}
diff --git a/src/app/formulaire/select-field-model-cloisons.ts b/src/app/formulaire/select-field-model-cloisons.ts
deleted file mode 100644
index 8a0241a4d..000000000
--- a/src/app/formulaire/select-field-model-cloisons.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import { Session } from "jalhyd";
-import { SelectEntry } from "./select-entry";
-import { FieldSet } from "./fieldset";
-import { SelectFieldModel } from "./select-field-model";
-
-/**
- * A select field that populates itself with references to
- * available Cloisons modules (used by PAB)
- */
-export class SelectFieldModelCloisons extends SelectFieldModel {
-
-    protected initSelectedValue() {
-        if (this.parent instanceof FieldSet) {
-            const mc = this.parent.nub.properties.getPropValue("modeleCloisons");
-            if (mc) {
-                this._selectedEntry = new SelectEntry(mc, {});
-            } // else if current model is undefined, do not select it so that default Cloisons will be chosen (if available)
-        }
-    }
-
-    /**
-     * Populates entries with available Cloisons
-     */
-    protected populate() {
-        const cloisonsNubs = Session.getInstance().getCloisonsNubs();
-        for (const cl of cloisonsNubs) {
-            const e = new SelectEntry(cl.uid, cl);
-            this.addEntry(e);
-        }
-    }
-}
diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts
index 76308dbd5..4e8fe3393 100644
--- a/src/app/services/formulaire/formulaire.service.ts
+++ b/src/app/services/formulaire/formulaire.service.ts
@@ -620,9 +620,10 @@ export class FormulaireService extends Observable {
                     }
                     // list Cloisons / downWall models dependencies for each PAB Nub
                     if (e.props.calcType === CalculatorType.Pab) {
+                        // TODO Check if can be removed
                         // Cloisons models
                         e.children.forEach((c) => {
-                            if (c.props.calcType === CalculatorType.PabCloisons) { // who knows ?
+                            if (c.props.calcType === CalculatorType.Cloisons) { // who knows ?
                                 if (c.props.modeleCloisons && ! nubInfo.requires.includes(c.props.modeleCloisons)) {
                                     nubInfo.requires.push(c.props.modeleCloisons);
                                 }
-- 
GitLab