98 lines
1.5 KiB
TypeScript
98 lines
1.5 KiB
TypeScript
import { EventSlot } from './EventSlot';
|
|
import { ChangeEvent } from './ChangeEvent';
|
|
import { isClassOf } from '../tools/TypeUtilities';
|
|
|
|
export class Property<T> extends EventSlot<ChangeEvent<T>>
|
|
{
|
|
protected _value:T;
|
|
private _silent:boolean = false;
|
|
|
|
get isSilent()
|
|
{
|
|
return this._silent;
|
|
}
|
|
|
|
constructor( initValue:T )
|
|
{
|
|
super();
|
|
this._value = initValue;
|
|
}
|
|
|
|
toString()
|
|
{
|
|
return this.value + "";
|
|
}
|
|
|
|
get value(){ return this._value; }
|
|
|
|
|
|
set value( v:T )
|
|
{
|
|
if ( this._silent )
|
|
{
|
|
return;
|
|
}
|
|
|
|
if ( this.isEqual( v, this._value ) )
|
|
{
|
|
return;
|
|
}
|
|
|
|
let valueBefore = this._value;
|
|
this._value = v;
|
|
|
|
this.dispatch( new ChangeEvent<T>( valueBefore, v ) );
|
|
}
|
|
|
|
forceDispatch()
|
|
{
|
|
this.dispatch( new ChangeEvent<T>( this._value, this._value ) );
|
|
}
|
|
|
|
silent()
|
|
{
|
|
this._silent = true;
|
|
}
|
|
|
|
unsilent()
|
|
{
|
|
this._silent = false;
|
|
}
|
|
|
|
silentRecursively():boolean
|
|
{
|
|
let silent = this._silent;
|
|
this._silent = true;
|
|
return silent;
|
|
}
|
|
|
|
unsilentRecursively( valueBefore:boolean )
|
|
{
|
|
this._silent = valueBefore;
|
|
}
|
|
|
|
setValueSilently( v:T )
|
|
{
|
|
this._value = v;
|
|
}
|
|
|
|
isEqual( a:T, b:T )
|
|
{
|
|
return a === b;
|
|
}
|
|
|
|
static resolveValue<T>( value:T|Property<T>, alternative:T = null )
|
|
{
|
|
if ( value === null || value === undefined )
|
|
{
|
|
return alternative;
|
|
}
|
|
|
|
if ( isClassOf( value, Property ) )
|
|
{
|
|
return ( value as Property<T> ).value;
|
|
}
|
|
|
|
return value as T;
|
|
}
|
|
} |