2022-05-30 01:30:33 +00:00
|
|
|
|
|
|
|
var LotIface = require('./lot-iface');
|
|
|
|
var Type = require('./type');
|
|
|
|
var Value = require('./value');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A value holder, it emits the changed signal when value is changed.
|
|
|
|
* Also it can be linked with a lot value or another parameter.
|
|
|
|
*/
|
|
|
|
module.exports = new Class({
|
|
|
|
Properties: {
|
|
|
|
/**
|
|
|
|
* The parameter value.
|
|
|
|
*/
|
|
|
|
value: {
|
|
|
|
type: null
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The parameter type.
|
|
|
|
*/
|
|
|
|
type: {
|
|
|
|
type: Type
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Another parameter to bind with.
|
|
|
|
*/
|
|
|
|
param: {
|
|
|
|
type: Object
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* A lot to bind with.
|
|
|
|
*/
|
|
|
|
lot: {
|
|
|
|
type: LotIface
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The field name in the lot.
|
|
|
|
*/
|
|
|
|
name: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Determines whether the link to the lot is unidirectional, ie, a
|
|
|
|
* change in the lot updates the parameter but not viceversa.
|
|
|
|
*/
|
|
|
|
oneWay: {
|
|
|
|
type: Boolean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
,_value: undefined
|
|
|
|
,_type: null
|
|
|
|
,_param: null
|
|
|
|
,_lot: null
|
2022-10-03 12:49:41 +00:00
|
|
|
,_sourceLock: false
|
2022-05-30 01:30:33 +00:00
|
|
|
,_name: null
|
|
|
|
,_oneWay: false
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_setValue(newValue) {
|
2022-05-30 01:30:33 +00:00
|
|
|
if (this._putValue(newValue))
|
|
|
|
this._notifyChanges();
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_putValue(newValue) {
|
2022-05-30 01:30:33 +00:00
|
|
|
if (Value.simpleEquals(newValue, this._value))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this._value = Value.simpleClone(newValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_notifyChanges() {
|
2022-05-30 01:30:33 +00:00
|
|
|
this._refreshLot();
|
|
|
|
this._refreshParam();
|
|
|
|
this.emit('changed', this._value);
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_setType(type) {
|
2022-05-30 01:30:33 +00:00
|
|
|
this._type = type;
|
|
|
|
this._onLotChange();
|
|
|
|
}
|
2022-10-03 12:49:41 +00:00
|
|
|
|
|
|
|
,_onSourceChange(newValue) {
|
|
|
|
if (this._oneTime && this._value !== undefined)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._sourceLock = true;
|
|
|
|
this._setValue(newValue);
|
|
|
|
this._sourceLock = false;
|
|
|
|
}
|
2022-05-30 01:30:33 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_setParam(param) {
|
2022-10-03 12:49:41 +00:00
|
|
|
this.link({_lot: null});
|
2022-05-30 01:30:33 +00:00
|
|
|
this.link({_param: param}, {changed: this._onParamChange});
|
|
|
|
this._refreshParam();
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onParamChange() {
|
2022-10-03 12:49:41 +00:00
|
|
|
if (this._sourceLock || !this._param) return;
|
|
|
|
this._onSourceChange(this._param.value);
|
2022-05-30 01:30:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_refreshParam() {
|
2022-10-03 12:49:41 +00:00
|
|
|
if (this._sourceLock || !this._param || this._oneWay)
|
2022-05-30 01:30:33 +00:00
|
|
|
return;
|
|
|
|
|
2022-10-03 12:49:41 +00:00
|
|
|
this._sourceLock = true;
|
2022-05-30 01:30:33 +00:00
|
|
|
this._param.value = this._value;
|
2022-10-03 12:49:41 +00:00
|
|
|
this._sourceLock = false;
|
2022-05-30 01:30:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_setLot(lot) {
|
2022-10-03 12:49:41 +00:00
|
|
|
this.link({_param: null});
|
2022-05-30 01:30:33 +00:00
|
|
|
this.link({_lot: lot}, {change: this._onLotChange});
|
|
|
|
this._onLotChange();
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onLotChange() {
|
2022-10-03 12:49:41 +00:00
|
|
|
if (this._sourceLock || !this._lot || !this._name)
|
2022-05-30 01:30:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var newValue = this._lot.get(this._name, this._type);
|
2022-10-03 12:49:41 +00:00
|
|
|
this._onSourceChange(newValue);
|
2022-05-30 01:30:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_refreshLot() {
|
2022-10-03 12:49:41 +00:00
|
|
|
if (this._sourceLock || !this._name || !this._lot || this._oneWay)
|
2022-05-30 01:30:33 +00:00
|
|
|
return;
|
|
|
|
|
2022-10-03 12:49:41 +00:00
|
|
|
this._sourceLock = true;
|
2022-05-30 01:30:33 +00:00
|
|
|
this._lot.set(this._name, this._value);
|
2022-10-03 12:49:41 +00:00
|
|
|
this._sourceLock = false;
|
2022-05-30 01:30:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_setName(name) {
|
2022-05-30 01:30:33 +00:00
|
|
|
this._name = name;
|
|
|
|
this._onLotChange();
|
|
|
|
}
|
|
|
|
});
|