hedera-web/js/vn/lot-query.js

123 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-05-30 01:30:33 +00:00
var Lot = require('./lot');
var LotIface = require('./lot-iface');
var Spec = require('./spec');
var Value = require('./value');
module.exports = new Class({
Extends: Lot
,Tag: 'vn-lot-query'
,Properties: {
fields: {
type: Array
2022-11-16 01:46:44 +00:00
,set(x) {
2022-05-30 01:30:33 +00:00
this._fields = x;
this._onSourceChange();
}
2022-11-16 01:46:44 +00:00
,get() {
2022-05-30 01:30:33 +00:00
return this._fields;
}
},
source: {
type: LotIface
2022-11-16 01:46:44 +00:00
,set(x) {
2022-05-30 01:30:33 +00:00
this.link({_source: x}, {change: this._onSourceChange});
this._onSourceChange();
}
2022-11-16 01:46:44 +00:00
,get() {
2022-05-30 01:30:33 +00:00
return this._source;
}
}
}
2022-11-16 01:46:44 +00:00
,initialize(props) {
2022-05-30 01:30:33 +00:00
Object.assign(this, {
_fields: null,
_source: null,
_lockSource: false,
_specs: {}
});
2022-06-06 16:02:17 +00:00
Lot.prototype.initialize.call(this, props);
2022-05-30 01:30:33 +00:00
}
2022-11-16 01:46:44 +00:00
,appendChild(child) {
2022-05-30 01:30:33 +00:00
if (!(child instanceof Spec))
throw new Error('VnLotQuery: Child must be a Vn.Spec instance');
this._specs[child.name] = child;
this._onSourceChange();
}
2022-11-16 01:46:44 +00:00
,_onSourceChange() {
2022-05-30 01:30:33 +00:00
if (this._lockSource)
return;
var params = this._source ? this._source.params : {};
var myParams = {};
for (var key in this._specs)
myParams[key] = Value.simpleClone(params[key]);
this.assign(myParams);
}
2022-11-16 01:46:44 +00:00
,assign(params) {
2022-05-30 01:30:33 +00:00
params = this.transformParams(params);
var diff = Value.partialDiff(this._params, params);
this._assign(diff);
}
2022-11-16 01:46:44 +00:00
,setAll(params) {
2022-05-30 01:30:33 +00:00
params = this.transformParams(params);
var diff = Value.diff(this._params, params);
this._assign(diff);
}
2022-11-16 01:46:44 +00:00
,_assign(diff) {
2022-05-30 01:30:33 +00:00
if (diff) {
Object.assign(this._params, diff);
if (this.source) {
this._lockSource = true;
this.source.assign(diff);
this._lockSource = false;
}
this._paramsChanged(diff);
this.changed(diff);
}
}
2022-11-16 01:46:44 +00:00
,transformParams(params) {
2022-05-30 01:30:33 +00:00
var newParams = {};
for (var key in this._specs) {
var spec = this._specs[key];
if (params[key])
newParams[key] = cast(params[key], spec.type);
}
return Object.assign(params, newParams);
}
});
function cast(value, type) {
switch (type) {
case Boolean:
return (/^(true|1)$/i).test(value);
case Number:
return 0 + new Number(value);
case Date:
var date = new Date(value);
date.setHours(0, 0, 0, 0);
return date;
case String:
return value;
default:
if (type instanceof Object)
return JSON.parse(value);
else
return value;
}
}