0
1
Fork 0
hedera-web-mindshore/js/vn/scope.js

164 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2022-05-24 21:11:12 +00:00
const VnObject = require('./object');
const kebabToCamel = require('./string-util').kebabToCamel;
2022-05-28 19:27:36 +00:00
let scopeUid = 0;
2022-11-19 13:00:13 +00:00
Vn.nWatchers = 0;
2022-05-28 19:27:36 +00:00
2022-05-24 21:11:12 +00:00
module.exports = new Class({
Extends: VnObject
2022-11-16 01:46:44 +00:00
,initialize(builder, doc, objects, exprValues, thisArg, parent) {
2022-05-24 21:11:12 +00:00
this.builder = builder;
this.objects = objects;
2022-06-18 21:04:34 +00:00
this.exprValues = exprValues;
2022-05-24 21:11:12 +00:00
this.thisArg = thisArg;
2022-06-07 08:19:29 +00:00
this.parent = parent;
2022-05-28 19:27:36 +00:00
this.uid = ++scopeUid;
2022-05-28 01:18:06 +00:00
this.$ = parent ? Object.create(parent.$) : {};
2022-11-19 13:00:13 +00:00
Vn.nWatchers += exprValues.length;
2022-05-24 21:11:12 +00:00
2022-06-07 08:19:29 +00:00
if (parent) {
2022-06-18 21:04:34 +00:00
parent.ref();
// XXX: Keep commented until optimized
//parent.on('change', this.onChange, this);
2022-06-07 08:19:29 +00:00
if (!thisArg) this.thisArg = parent.thisArg;
}
2022-06-18 21:04:34 +00:00
const contexts = builder._contexts;
for (let i = 0; i < contexts.length; i++) {
const context = contexts[i];
objects[i] = context.compiler.instantiate(doc, context, this);
}
2022-05-24 21:11:12 +00:00
}
2022-11-16 01:46:44 +00:00
,link(extraObjects) {
2022-05-24 21:11:12 +00:00
var contextMap = this.builder._contextMap;
for (var id in extraObjects)
2022-05-28 01:18:06 +00:00
this.$[id] = extraObjects[id];
2022-05-24 21:11:12 +00:00
for (var id in contextMap)
2022-05-28 01:18:06 +00:00
this.$[id] = this.objects[contextMap[id]];
2022-05-24 21:11:12 +00:00
2022-06-18 21:04:34 +00:00
const builder = this.builder;
const contexts = builder._contexts;
const objects = this.objects;
for (const compiler of builder._compilers)
compiler.preLink(this);
2022-06-07 08:19:29 +00:00
2022-06-18 21:04:34 +00:00
for (let i = 0; i < contexts.length; i++) {
const context = contexts[i];
context.compiler.link(context, objects[i], objects, this);
}
for (let i = 0; i < contexts.length; i++) {
const context = contexts[i];
context.compiler.connect(context, objects[i], objects, this);
}
for (const compiler of builder._compilers)
compiler.postLink(this);
this.digest();
2022-06-07 08:19:29 +00:00
for (const object of this.objects)
if (object.assignLot)
2022-06-18 21:04:34 +00:00
object.on('change', this.onChange, this);
2022-06-07 08:19:29 +00:00
}
2022-06-18 21:04:34 +00:00
,digest() {
const exprContexts = this.builder._exprContexts;
const exprValues = this.exprValues;
const objects = this.objects;
for (let i = 0; i < exprContexts.length; i++) {
const exprContext = exprContexts[i];
let newValue;
if (exprContext.template) {
const values = [];
let isEmpty = false;
for (expr of exprContext.exprs) {
const value = this.execExpr(expr);
if (value == null) {
isEmpty = true;
break;
}
values.push(value);
}
if (!isEmpty) {
let k = 0;
newValue = exprContext.template.replace(/{{\d+}}/g, function() {
return values[k++];
});
} else
newValue = '';
} else
newValue = this.execExpr(exprContext.expr);
if (newValue !== exprValues[i]) {
const context = exprContext.context;
context.compiler.setProperty(objects[context.id],
exprContext.property, newValue);
exprValues[i] = newValue;
}
}
}
,execExpr(expr) {
try {
return expr.call(this.thisArg, this.$);
// eslint-disable-next-line no-empty
} catch (e) {}
}
,onChange() {
this.emit('change');
this.digest();
2022-05-24 21:11:12 +00:00
}
2022-11-16 01:46:44 +00:00
,getMain() {
2022-06-18 21:04:34 +00:00
return this.objects[this.builder._mainContext];
2022-05-24 21:11:12 +00:00
}
2022-11-16 01:46:44 +00:00
,getById(objectId) {
2022-05-28 01:18:06 +00:00
if (!objectId) return null;
return this.$[kebabToCamel(objectId)];
2022-05-24 21:11:12 +00:00
}
2022-11-16 01:46:44 +00:00
,getByTagName(tagName) {
2022-06-18 21:04:34 +00:00
const tags = this.builder._tags[tagName];
if (tags) {
const arr = new Array(tags.length);
for (let i = 0; i < tags.length; i++)
arr[i] = this.objects[tags[i]];
return arr;
}
return [];
2022-05-24 21:11:12 +00:00
}
2022-11-16 01:46:44 +00:00
,getHtmlId(nodeId) {
2022-05-24 21:11:12 +00:00
return 'vn-'+ this.uid +'-'+ nodeId;
}
2022-11-16 01:46:44 +00:00
,_destroy() {
2022-11-19 13:00:13 +00:00
Vn.nWatchers -= this.exprValues.length;
2022-06-07 08:19:29 +00:00
for (const object of this.objects)
if (object instanceof VnObject) {
object.disconnectByInstance(this);
object._destroy();
2022-06-07 08:19:29 +00:00
}
if (this.parent)
2022-06-07 08:19:29 +00:00
this.parent.disconnectByInstance(this);
2022-05-24 21:11:12 +00:00
2022-06-06 16:02:17 +00:00
VnObject.prototype._destroy.call(this);
2022-05-24 21:11:12 +00:00
}
});