From fadb25dbb0a6fb08a249093f071bc524df9d63a4 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Fri, 15 Jun 2018 11:07:14 +0200
Subject: [PATCH] =?UTF-8?q?=20#48=20ParamLinkComponent=20:=20MAJ=20de=20la?=
 =?UTF-8?q?=20liste=20des=20param=C3=A8tres=20liables=20apr=C3=A8s=20cr?=
 =?UTF-8?q?=C3=A9ation=20d'une=20calculette?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../param-link/param-link.component.ts        | 39 ++++++++++++++++---
 1 file changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/app/components/param-link/param-link.component.ts b/src/app/components/param-link/param-link.component.ts
index 4ccaf65e1..0ce835b5d 100644
--- a/src/app/components/param-link/param-link.component.ts
+++ b/src/app/components/param-link/param-link.component.ts
@@ -2,13 +2,14 @@ import { Component, Input, Output, EventEmitter, OnChanges } from "@angular/core
 
 import { NgParameter } from "../../formulaire/ngparam";
 import { ServiceFactory } from "../../services/service-factory";
-import { ParamValueMode } from "jalhyd";
+import { ParamValueMode, Observer } from "jalhyd";
+import { FormulaireService } from "../../services/formulaire/formulaire.service";
 
 @Component({
     selector: "param-link",
     templateUrl: "./param-link.component.html"
 })
-export class ParamLinkComponent implements OnChanges {
+export class ParamLinkComponent implements OnChanges, Observer {
     @Input("param")
     private _param: NgParameter;
 
@@ -31,8 +32,12 @@ export class ParamLinkComponent implements OnChanges {
      */
     private _linkableParams: any[];
 
+    private _formService: FormulaireService;
+
     constructor() {
         this.onValid = new EventEmitter();
+        this._formService = ServiceFactory.instance.formulaireService;
+        this._formService.addObserver(this);
     }
 
     /**
@@ -94,13 +99,35 @@ export class ParamLinkComponent implements OnChanges {
         }
     }
 
-    public ngOnChanges() {
-        this._linkableParams = ServiceFactory.instance.formulaireService.getLinkableParameters(this._param);
+    private updateParamList() {
+        // liste des paramètres liables
+        this._linkableParams = this._formService.getLinkableParameters(this._param);
+
+        // initialisation de l'indice courant
         if (this._linkableParams.length > 0) {
-            this.linkTo(0);
+            if (this._currentIndex === -1)
+                this.linkTo(0);
             this._message = undefined;
         }
-        else
+        else {
+            this._currentIndex = -1;
             this._message = "Aucun paramètre compatible trouvé";
+        }
+    }
+
+    public ngOnChanges() {
+        this.updateParamList();
+    }
+
+    // interface Observer 
+
+    public update(sender: any, data: any) {
+        if (sender instanceof FormulaireService) {
+            switch (data["action"]) {
+                case "createForm":
+                    this.updateParamList();
+                    break;
+            }
+        }
     }
 }
-- 
GitLab