From ae9ef6a0f60b74c82ed00cb18fcd29f1a3f7af40 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Thu, 14 Jun 2018 16:37:41 +0200
Subject: [PATCH] =?UTF-8?q?=20#48=20ParamLinkComponent=20:=20remplissage?=
 =?UTF-8?q?=20de=20la=20liste=20des=20param=C3=A8tres=20liables=20en=20fon?=
 =?UTF-8?q?ction=20des=20calculettes=20ouvertes?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../param-link/param-link.component.html      |  7 +-
 .../param-link/param-link.component.ts        | 73 +++++++++++++++++--
 src/app/formulaire/ngparam.ts                 | 14 +++-
 .../services/formulaire/formulaire.service.ts | 22 ++++++
 4 files changed, 104 insertions(+), 12 deletions(-)

diff --git a/src/app/components/param-link/param-link.component.html b/src/app/components/param-link/param-link.component.html
index 994ddba8b..1fc116676 100644
--- a/src/app/components/param-link/param-link.component.html
+++ b/src/app/components/param-link/param-link.component.html
@@ -1,10 +1,13 @@
 <div class="row">
-    <div class="btn-group col-12 col-sm-3" dropdown (click)="onSelectLinkableParam($event)">
+    <div class="btn-group col-6 col-sm-3" dropdown (click)="onSelectLinkableParam($event)">
         <button dropdownToggle class="btn btn-primary dropdown-toggle waves-light my-1" type="button" mdbRippleRadius>
             {{currentLinkedParamLabel}}
         </button>
         <div class="dropdown-menu">
-            <a class="dropdown-item" *ngFor="let e of _linkableParams" [value]=e.value>{{e.label}}</a>
+            <a class="dropdown-item" *ngFor="let e of _linkableParams" [value]=e>{{selectItemLabel(e)}}</a>
         </div>
     </div>
+    <div class="col-6 text-danger">
+        {{_message}}
+    </div>
 </div>
\ No newline at end of file
diff --git a/src/app/components/param-link/param-link.component.ts b/src/app/components/param-link/param-link.component.ts
index 92e1aa333..4ccaf65e1 100644
--- a/src/app/components/param-link/param-link.component.ts
+++ b/src/app/components/param-link/param-link.component.ts
@@ -1,12 +1,14 @@
-import { Component, Input, Output, EventEmitter } from "@angular/core";
+import { Component, Input, Output, EventEmitter, OnChanges } from "@angular/core";
 
 import { NgParameter } from "../../formulaire/ngparam";
+import { ServiceFactory } from "../../services/service-factory";
+import { ParamValueMode } from "jalhyd";
 
 @Component({
     selector: "param-link",
     templateUrl: "./param-link.component.html"
 })
-export class ParamLinkComponent {
+export class ParamLinkComponent implements OnChanges {
     @Input("param")
     private _param: NgParameter;
 
@@ -14,9 +16,21 @@ export class ParamLinkComponent {
     private onValid: EventEmitter<boolean>;
 
     /**
-     * liste des paramètres liables
+     * indice actuel du paramètre sélectionné dans la lsite
      */
-    private _linkableParams: any[] = [{ value: "aaa", label: "Aaa aa" }, { value: "bbb", label: "Bbbb bb" }];
+    private _currentIndex = -1;
+
+    /**
+     * message affiché à côté du select des paramètres
+     */
+    private _message: string;
+
+    /**
+     * liste des paramètres liables sous la forme
+     * {"param":<paramètre liable>, "nub":<Nub d'origine du paramètre>, "formTitle":<nom de la calculette liée au nub>}
+     */
+    private _linkableParams: any[];
+
     constructor() {
         this.onValid = new EventEmitter();
     }
@@ -34,14 +48,59 @@ export class ParamLinkComponent {
     private onSelectLinkableParam(event: any) {
         const next = event.target.value;
 
-        switch (next) {
-        }
+        let i = 0;
+        for (const e of this._linkableParams)
+            if (this._linkableParams[i].param.uid == next.param.uid) {
+                this.linkTo(i);
+                break;
+            }
+            else
+                i++;
     }
 
     /**
      * valeur courante affichée dans le select des paramètres liables
      */
     private get currentLinkedParamLabel(): string {
-        return "azeaz";
+        if (this._linkableParams !== undefined) {
+            if (this._currentIndex === -1 || this._currentIndex >= this._linkableParams.length)
+                return undefined;
+
+            return this.selectItemLabel(this._linkableParams[this._currentIndex]);
+        }
+
+        return undefined;
+    }
+
+    /**
+     * attribut "label" d'une entrée du select des paramètres
+     */
+    private selectItemLabel(i: any) {
+        const s = i.param.symbol;
+        const c = i.formTitle;
+        return `${s} (${c})`;
+    }
+
+    /**
+     * lie le paramètre géré à un des paramètres liables de la liste
+     * @param index indice dans la liste
+     */
+    private linkTo(index: number) {
+        if (this._currentIndex !== index) {
+            this._currentIndex = index;
+            const lp = this._linkableParams[index];
+
+            this._param.linkToParameter(lp.nub, lp.param);
+        }
+    }
+
+    public ngOnChanges() {
+        this._linkableParams = ServiceFactory.instance.formulaireService.getLinkableParameters(this._param);
+        if (this._linkableParams.length > 0) {
+            this.linkTo(0);
+            this._message = undefined;
+        }
+        else
+            this._message = "Aucun paramètre compatible trouvé";
     }
 }
diff --git a/src/app/formulaire/ngparam.ts b/src/app/formulaire/ngparam.ts
index 859900023..b48b72503 100644
--- a/src/app/formulaire/ngparam.ts
+++ b/src/app/formulaire/ngparam.ts
@@ -1,4 +1,4 @@
-import { ParamDefinition, Pair, ParamDomain, ParamValues, ParamValueMode, ParamValueIterator } from "jalhyd";
+import { ParamDefinition, Pair, ParamDomain, ParamValues, ParamValueMode, ParamValueIterator, Nub } from "jalhyd";
 
 import { InputField } from "./input-field";
 import { Dependency } from "./dependency/dependency";
@@ -75,7 +75,7 @@ export class NgParameter extends InputField {
     }
 
     public get radioState() {
-        switch (this._paramDef.paramValues.valueMode) {
+        switch (this._paramDef.valueMode) {
             case ParamValueMode.SINGLE:
                 return ParamRadioConfig.FIX;
 
@@ -116,6 +116,14 @@ export class NgParameter extends InputField {
         this.notifyValueModified(sender);
     }
 
+    /**
+     * crée le lien avec un paramètre
+     */
+    public linkToParameter(n: Nub, p: ParamDefinition) {
+        this.valueMode = ParamValueMode.LINK;
+        this.paramDefinition.defineReference(n, p.symbol);
+    }
+
     get isDefined(): boolean {
         return this._paramDef.isDefined;
     }
@@ -137,7 +145,7 @@ export class NgParameter extends InputField {
         // undefined si on clique en dehors du select après l'avoir ouvert (cad sans avoir fait de sélection)
         // et au même niveau, cad à côté du bouton et non à côté du menu déroulant
         if (m != undefined)
-            this._paramValues.valueMode = m;
+            this._paramDef.valueMode = m;
     }
 
     public checkMin(min: number): boolean {
diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts
index 5984c3f9d..29a370edc 100644
--- a/src/app/services/formulaire/formulaire.service.ts
+++ b/src/app/services/formulaire/formulaire.service.ts
@@ -24,6 +24,7 @@ import { FormulaireRegimeUniforme } from "../../formulaire/definition/concrete/f
 import { FormulairePasseBassinDimensions } from "../../formulaire/definition/concrete/form-passe-bassin-dim";
 import { FormulairePasseBassinPuissance } from "../../formulaire/definition/concrete/form-passe-bassin-puissance";
 import { FormulaireParallelStructure } from "../../formulaire/definition/concrete/form-parallel-structures";
+import { NgParameter } from "../../formulaire/ngparam";
 
 @Injectable()
 export class FormulaireService extends Observable {
@@ -468,4 +469,25 @@ export class FormulaireService extends Observable {
                     throw new Error(`session file : invalid key '${ks}' in session object`);
             }
     }
+
+    /**
+     * @returns liste des paramètres liables à un paramètre sous la forme d'un tableau d'objets
+     * {"param":<paramètre lié>, "nub":<Nub d'origine du paramètre lié>, "formTitle":<nom de la calculette liée au nub>}
+     * @param symbol symbole (Q, J, ...) du paramètre qui sert de clé de recherche des paramètres liables
+     */
+    public getLinkableParameters(p: NgParameter): any[] {
+        let res: any[] = [];
+
+        if (p !== undefined)
+            for (const f of this._formulaires) {
+                const sn = f.currentSessionNub;
+                const ps = sn.getLinkableParameters(p.paramDefinition);
+                for (const np of ps) {
+                    np["formTitle"] = f.calculatorName;
+                    res.push(np);
+                }
+            }
+
+        return res;
+    }
 }
-- 
GitLab