Commit e36c09ec authored by Grand Francois's avatar Grand Francois
Browse files

classe Props : ajout de la méthode setProps() pour fixer la valeur de toutes...

classe Props : ajout de la méthode setProps() pour fixer la valeur de toutes les propriétés en même temps
Showing with 60 additions and 1 deletion
+60 -1
...@@ -30,12 +30,28 @@ export class Props implements IObservable { ...@@ -30,12 +30,28 @@ export class Props implements IObservable {
return this._props[key]; 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) { private notifyPropChange(prop: string, val: any, sender: any) {
this.notifyObservers({ this.notifyObservers({
"action": "propertyChange", "action": "propertyChange",
"name": prop, "name": prop,
"value": val "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 { public setPropValue(key: string, val: any, sender?: any): boolean {
...@@ -48,6 +64,49 @@ export class Props implements IObservable { ...@@ -48,6 +64,49 @@ export class Props implements IObservable {
return changed; 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() { public get props() {
return this._props; return this._props;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment