-
Grand Francois authored81a070b9
import { Nub } from "./nub";
import { IObservable, Observer, Observable } from "./util/observer";
/**
* gestion d'un ensemble de propriétés (clé/valeur)
*/
export class Props implements IObservable {
// implémentation de IObservable par délégation
private _observable: Observable;
constructor(private _props: any = {}) {
this._observable = new Observable();
}
public hasProperties(props: Props | {}): boolean {
const keys = Object.keys(this._props);
const p = props instanceof Props ? props._props : props;
// if (keys.length != Object.keys(p).length)
// return false;
for (const k of keys)
if (this._props[k] !== p[k])
return false;
return true;
}
public getPropValue(key: string): any {
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);
}
/**
* 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 {
const oldValue = this._props[key];
const changed = oldValue !== val;
if (changed) {
this._props[key] = val;
this.notifyPropChange(key, val, sender);
}
return changed;
}
/**
* compare 2 objets (clés et valeurs)
* @return true s'il existe une différence
*/
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
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 = {};
for (const k in p)
this._props[k] = p[k];
this.notifyPropsChange(sender);
}
}
public get props() {
return this._props;
}
public clone(): Props {
const res = new Props();
for (const k in this._props)
res._props[k] = this._props[k];
return res;
}
public toString(): string {
let res = "[";
for (const k in this._props) {
if (res != "[")
res += ", ";
res += `${k}:${this._props[k]}`;
}
res += "]"
return res;
}
// interface IObservable
/**
* ajoute un observateur à la liste
*/
public addObserver(o: Observer) {
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
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, sender?: any) {
this._observable.notifyObservers(data, sender);
}
}
/**
* Nub utilisé dans une session
*/
export class SessionNub {
private _props: Props;
constructor(private _nub: Nub, props: Props | {}) {
if (this._nub == undefined)
throw new Error(`NgNub.constructor() : argument invalide`);
if (props instanceof Props)
this._props = props.clone();
else
this._props = new Props(props);
}
public get nub() {
return this._nub;
}
public get uid(): number {
return this._nub.uid;
}
public get properties() {
return this._props;
}
public hasProperties(p: Props | {}): boolean {
return this._props.hasProperties(p);
}
}