From e36c09ec425cdd5906d617424728ff8f72bbd585 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Tue, 22 May 2018 12:15:04 +0200 Subject: [PATCH] =?UTF-8?q?classe=20Props=20:=20ajout=20de=20la=20m=C3=A9t?= =?UTF-8?q?hode=20setProps()=20pour=20fixer=20la=20valeur=20de=20toutes=20?= =?UTF-8?q?les=20propri=C3=A9t=C3=A9s=20en=20m=C3=AAme=20temps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/session_nub.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/session_nub.ts b/src/session_nub.ts index d1f320d1..fb9a96d3 100644 --- a/src/session_nub.ts +++ b/src/session_nub.ts @@ -30,12 +30,28 @@ export class Props implements IObservable { return this._props[key]; } + /** + * notification de changement de la valeur d'une propriété + * @param prop nom de la propriété modifiée + * @param val nouvelle valeur + * @param sender objet ayant changé la valeur + */ private notifyPropChange(prop: string, val: any, sender: any) { this.notifyObservers({ "action": "propertyChange", "name": prop, "value": val - }, sender) + }, sender); + } + + /** + * notification de changement de la valeur de toutes les propriétés + * @param sender objet ayant changé les valeurs + */ + private notifyPropsChange(sender: any) { + this.notifyObservers({ + "action": "propertiesChange", + }, sender); } public setPropValue(key: string, val: any, sender?: any): boolean { @@ -48,6 +64,49 @@ export class Props implements IObservable { return changed; } + /** + * compare 2 objets (clés et valeurs) + * @return true s'il existe une différence + */ + private compareObjects(o1: { [key: string]: any }, o2: { [key: string]: any }) { + const oldKeys: string[] = Object.keys(o1).sort(); + const newKeys: string[] = Object.keys(o2).sort(); + + // nombre de clés + let changed = oldKeys.length !== newKeys.length; + if (changed) + return true; + + // nom des clés + for (const i in oldKeys) + if (oldKeys[i] !== newKeys[i]) + return true; + + // valeurs + for (const i in o1) + if (o1[i] != o2[i]) + return true; + + return false; + } + + /** + * fixe la valeur de toutes les propriétés + * @param props nouvelles valeurs + * @param sender objet modificateur + */ + public setProps(props: {}, sender?: any) { + if (props instanceof Props) + var p = props.props; + else + p = props; + const changed = this.compareObjects(this._props, p); + if (changed) { + this._props = p; + this.notifyPropsChange(sender); + } + } + public get props() { return this._props; } -- GitLab