0
1
Fork 0
hedera-web-mindshore/js/vn/lot-query.js

134 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-04-19 06:16:37 +00:00
var Lot = require ('./lot');
var LotIface = require ('./lot-iface');
2017-07-05 09:50:42 +00:00
var Spec = require ('./spec');
var Value = require ('./value');
2017-04-19 06:16:37 +00:00
2017-07-05 09:50:42 +00:00
module.exports = new Class
2017-04-19 06:16:37 +00:00
({
Extends: Lot
,Tag: 'vn-lot-query'
,Properties:
{
fields:
{
type: Array
,set: function (x)
{
this._fields = x;
this._onSourceChange ();
}
,get: function ()
{
return this._fields;
}
},
source:
{
type: LotIface
,set: function (x)
{
this.link ({_source: x}, {change: this._onSourceChange});
this._onSourceChange ();
}
,get: function ()
{
return this._source;
}
}
}
2017-07-05 09:50:42 +00:00
,initialize: function (props)
2017-04-19 06:16:37 +00:00
{
2017-07-05 09:50:42 +00:00
Object.assign (this, {
_fields: null,
_source: null,
2017-08-21 10:20:36 +00:00
_lockSource: false,
2017-07-05 09:50:42 +00:00
_specs: {}
});
this.parent (props);
}
2017-04-19 06:16:37 +00:00
2017-07-05 09:50:42 +00:00
,appendChild: function (child)
{
if (!(child instanceof Spec))
throw new Error ('VnLotQuery: Child must be a Vn.Spec instance');
2017-04-19 06:16:37 +00:00
2017-07-05 09:50:42 +00:00
this._specs[child.name] = child;
2017-04-19 06:16:37 +00:00
}
2017-07-05 09:50:42 +00:00
,_onSourceChange: function ()
2017-04-19 06:16:37 +00:00
{
2017-08-21 10:20:36 +00:00
if (this._lockSource)
return;
2017-07-05 09:50:42 +00:00
var params = this._source ? this._source.params : {};
var myParams = {};
2017-04-19 06:16:37 +00:00
2017-07-05 09:50:42 +00:00
for (var key in this._specs)
myParams[key] = Value.simpleClone (params[key]);
2017-04-19 06:16:37 +00:00
2017-07-05 09:50:42 +00:00
this.assign (myParams);
2017-04-19 06:16:37 +00:00
}
2017-08-21 10:20:36 +00:00
,assign: function (params)
{
params = this.transformParams (params);
var diff = Value.partialDiff (this._params, params);
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);
}
}
,setAll: function (params)
{
this.assign (params);
}
2017-07-05 09:50:42 +00:00
,transformParams: function (params)
2017-04-19 06:16:37 +00:00
{
2017-07-05 09:50:42 +00:00
var newParams = {};
2017-04-19 06:16:37 +00:00
2017-07-05 09:50:42 +00:00
for (var key in this._specs)
2017-04-19 06:16:37 +00:00
{
2017-07-05 09:50:42 +00:00
var spec = this._specs[key];
if (params[key])
newParams[key] = cast (params[key], spec.type);
2017-04-19 06:16:37 +00:00
}
2017-07-05 09:50:42 +00:00
return Object.assign (params, newParams);
2017-04-19 06:16:37 +00:00
}
});
2017-07-05 09:50:42 +00:00
function cast (value, type)
{
switch (type)
{
case Boolean:
2017-08-21 10:20:36 +00:00
return (/^(true|1)$/i).test (value);
2017-07-05 09:50:42 +00:00
case Number:
2017-08-21 10:20:36 +00:00
return 0 + new Number (value);
2017-07-05 09:50:42 +00:00
case Date:
2017-08-21 10:20:36 +00:00
var date = new Date (value);
2017-07-05 09:50:42 +00:00
date.setHours (0, 0, 0, 0);
return date;
default:
if (type instanceof Object)
2017-08-21 10:20:36 +00:00
return JSON.parse (value);
2017-07-05 09:50:42 +00:00
else
2017-08-21 10:20:36 +00:00
return value;
2017-07-05 09:50:42 +00:00
}
}