From 808eacf3c20f1fa79201b1e34bff2e18a93a6a67 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Thu, 25 Jan 2018 14:54:21 +0100
Subject: [PATCH] =?UTF-8?q?NgParameter=20:=20impl=C3=A9mente=20Observable?=
 =?UTF-8?q?=20pour=20pr=C3=A9venir=20NgParamInputComponent=20des=20changem?=
 =?UTF-8?q?ents=20de=20valeur?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../generic-input/generic-input.component.ts  | 11 ++++--
 .../ngparam-input/ngparam-input.component.ts  | 11 +++++-
 src/app/formulaire/ngparam.ts                 | 39 ++++++++++++++++++-
 3 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/src/app/components/generic-input/generic-input.component.ts b/src/app/components/generic-input/generic-input.component.ts
index 2077a6bbb..64487d22e 100644
--- a/src/app/components/generic-input/generic-input.component.ts
+++ b/src/app/components/generic-input/generic-input.component.ts
@@ -150,8 +150,13 @@ export abstract class GenericInputComponent extends BaseComponent {
 
     public set model(v: any) {
         this.setAndValidateModel(v);
+        this.updateAndValidateUI();
+    }
 
-        // MAJ UI
+    /**
+     * MAJ et validation de l'UI
+     */
+    protected updateAndValidateUI() {
         this._uiValue = this.modelToUI(this.getModelValue());
         this.validateUI();
     }
@@ -175,8 +180,8 @@ export abstract class GenericInputComponent extends BaseComponent {
      * @see BaseComponent
      */
     protected afterFirstViewChecked() {
-        this._uiValue = this.modelToUI(this.getModelValue());
-        this.validate();
+         this.updateAndValidateUI();
+        this.validateModel();
     }
 
     /**
diff --git a/src/app/components/ngparam-input/ngparam-input.component.ts b/src/app/components/ngparam-input/ngparam-input.component.ts
index 064101c27..ed085de0f 100644
--- a/src/app/components/ngparam-input/ngparam-input.component.ts
+++ b/src/app/components/ngparam-input/ngparam-input.component.ts
@@ -8,12 +8,13 @@ import { ComputeNodeType, ParamDefinition, NumericalString, Message, MessageCode
 import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
 import { NgParameter } from "../../formulaire/ngparam";
 import { GenericInputComponent } from "../generic-input/generic-input.component";
+import { Observer, IObservable } from "../../services/observer";
 
 @Component({
     selector: "ngparam-input",
     templateUrl: "../generic-input/generic-input.component.html"
 })
-export class NgParamInputComponent extends GenericInputComponent {
+export class NgParamInputComponent extends GenericInputComponent implements Observer {
     /**
      * paramètre géré
      */
@@ -88,5 +89,13 @@ export class NgParamInputComponent extends GenericInputComponent {
             if (this._paramDef.isDefined)
                 this._model = this._paramDef.getValue();
         super.afterFirstViewChecked();
+        this._paramDef.addObserver(this);
+    }
+
+    public update(sender: IObservable, data: any): void {
+        if (data["action"] == "value") {
+            this._model = data["value"];
+            this.updateAndValidateUI();
+        }
     }
 }
diff --git a/src/app/formulaire/ngparam.ts b/src/app/formulaire/ngparam.ts
index 7dd3fda87..dd7fb6554 100644
--- a/src/app/formulaire/ngparam.ts
+++ b/src/app/formulaire/ngparam.ts
@@ -4,6 +4,7 @@ import { InputField } from "./input-field";
 import { Dependency } from "./dependency";
 import { DependencyConditionType } from "./dependency-condition";
 import { ValueDependencyCondition } from "./value-dependency-condition";
+import { Observable, IObservable, Observer } from "../services/observer";
 
 export enum ParamRadioConfig {
     /**
@@ -41,7 +42,7 @@ export enum ParamValueMode {
 /**
  * classe englobante de ParamDefinition (champs supplémentaires pour l'affichage, radio boutons, ...)
  */
-export class NgParameter extends InputField {
+export class NgParameter extends InputField implements IObservable {
     public unit: string;
     public radioConfig: ParamRadioConfig;
     public radioState: ParamRadioConfig;
@@ -72,8 +73,15 @@ export class NgParameter extends InputField {
      */
     private _valueList: number[];
 
+    /**
+     * implémentation par délégation de IObservable
+     */
+    private _observable: Observable;
+
     constructor(private _paramDef: ParamDefinition, formId: number) {
         super(_paramDef.computeNodeType, _paramDef.symbol, formId);
+        this._observable = new Observable();
+        this._observable.sender = this;
     }
 
     get symbol(): string {
@@ -90,6 +98,12 @@ export class NgParameter extends InputField {
 
     public setValue(val: number) {
         this._paramDef.v = val;
+        this.notifyObservers(
+            {
+                "action": "value",
+                "value": val
+            }
+        )
     }
 
     get isDefined(): boolean {
@@ -270,4 +284,27 @@ export class NgParameter extends InputField {
                 throw "NgParameter.verifyDependency() : type de condition '" + DependencyConditionType[d.masterCondition.type] + "' non pris en charge";
         }
     }
+
+    // interface IObservable
+
+    /**
+     * ajoute un observateur à la liste
+     */
+    public addObserver(o: Observer) {
+        this._observable.addObserver(o);
+    }
+
+    /**
+     * supprime un observateur de la liste
+     */
+    public removeObserver(o: Observer) {
+        this._observable.removeObserver(o);
+    }
+
+    /**
+     * notifie un événement aux observateurs
+     */
+    public notifyObservers(data: any) {
+        this._observable.notifyObservers(data);
+    }
 }
-- 
GitLab