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

652 lines
15 KiB
JavaScript
Raw Normal View History

2022-05-24 21:11:12 +00:00
const VnObject = require('./object');
const Scope = require('./scope');
const kebabToCamel = require('./string-util').kebabToCamel;
2016-09-26 09:28:47 +00:00
/**
2015-03-06 23:33:54 +00:00
* Creates a object from a XML specification.
2022-05-24 10:18:44 +00:00
*/
module.exports = new Class({
2022-05-24 21:11:12 +00:00
Extends: VnObject
,_contexts: null
/**
* Compiles an XML file.
*
2022-05-24 21:11:12 +00:00
* @param {String} path The XML path
* @return {Boolean} %true on success, %false othersise
2022-05-24 10:18:44 +00:00
*/
2022-05-24 21:11:12 +00:00
,compileFile: function(path) {
2016-09-26 09:28:47 +00:00
this._path = path;
2022-05-24 21:11:12 +00:00
return this.compileDocument(Vn.getXml(path));
}
/**
* Compiles an XML string.
*
* @param {String} xmlString The XML string
* @return {Boolean} %true on success, %false othersise
*/
,compileString: function(xmlString) {
2022-05-24 10:18:44 +00:00
var parser = new DOMParser();
2022-05-24 21:11:12 +00:00
var doc = parser.parseFromString(xmlString, 'text/xml');
return this.compileDocument(doc);
}
2015-03-06 23:33:54 +00:00
2022-05-24 21:11:12 +00:00
/**
* Compiles a XML document.
*
* @param {Document} doc The DOM document
* @return {Boolean} %true on success, %false othersise
*/
,compileDocument: function(doc, exprArgs) {
if (!doc)
return false;
2022-05-24 21:11:12 +00:00
this._preCompile(exprArgs);
var docElement = doc.documentElement;
2015-03-06 23:33:54 +00:00
2022-05-24 10:18:44 +00:00
if (docElement.tagName !== 'vn') {
2022-05-24 21:11:12 +00:00
this.showError('The toplevel tag should be named \'vn\'');
this._contexts = null;
2015-03-06 23:33:54 +00:00
return false;
}
2015-03-06 23:33:54 +00:00
var childs = docElement.childNodes;
if (childs)
for (var i = 0; i < childs.length; i++)
2022-05-24 21:11:12 +00:00
this._compile(childs[i]);
2015-03-06 23:33:54 +00:00
2022-05-24 21:11:12 +00:00
this._postCompile();
2015-03-06 23:33:54 +00:00
return true;
}
2015-03-09 08:36:54 +00:00
/**
* Compiles a single DOM node.
*
* @path Node The DOM node
* @return %true on success, %false othersise
2022-05-24 10:18:44 +00:00
*/
2022-05-24 21:11:12 +00:00
,compileNode: function(node, exprArgs) {
this._preCompile(exprArgs);
this._mainContext = this._compile(node).id;
this._postCompile();
return true;
2015-03-09 08:36:54 +00:00
}
2022-05-24 21:11:12 +00:00
/**
* Called before starting to compile nodes.
*/
,_preCompile: function(exprArgs) {
this._path = null;
this._tags = {};
this._contexts = [];
this._contextMap = {};
this._links = [];
this._mainContext = null;
this._baseExprArgs = ['_', '$'];
if (exprArgs)
this._baseExprArgs = this._baseExprArgs.concat(exprArgs);
this._baseEventArgs = this._baseExprArgs.concat(['$event']);
this._exprArgs = this._baseExprArgs.join(',');
this._eventArgs = this._baseEventArgs.join(',');
}
2015-03-09 08:36:54 +00:00
2022-05-24 21:11:12 +00:00
/**
* Called after all nodes have been compiled.
*/
,_postCompile: function() {}
/**
* Compiles a node.
*/
,_compile: function(node) {
let context = null;
let tagName = null;
const isElement = node.nodeType === Node.ELEMENT_NODE;
if (isElement)
tagName = node.tagName.toLowerCase();
else if (node.nodeType !== Node.TEXT_NODE
|| /^[\n\r\t]*$/.test(node.textContent))
return null;
2022-05-24 21:11:12 +00:00
context =
this.textCompile(node, tagName)
|| this.objectCompile(node, tagName)
|| this.elementCompile(node, tagName);
context.id = this._contexts.length;
if (isElement) {
var nodeId = node.getAttribute('id');
2022-05-24 21:11:12 +00:00
if (nodeId)
this._contextMap[kebabToCamel(nodeId)] = context.id;
var tags = this._tags[tagName];
if (!tags)
this._tags[tagName] = tags = [];
tags.push(context.id);
}
this._contexts.push(context);
return context;
}
,getMain: function(scope) {
return scope.objects[this._mainContext];
}
,getByTagName: function(scope, tagName) {
var tags = this._tags[tagName];
if (tags) {
var arr = new Array(tags.length);
for (var i = 0; i < tags.length; i++)
arr[i] = scope.objects[tags[i]];
return arr;
}
return [];
}
,load: function(dstDocument, thisArg, parentScope) {
if (this._contexts === null)
return null;
const contexts = this._contexts;
const len = contexts.length;
const objects = new Array(len);
const doc = dstDocument ? dstDocument : document;
2022-05-24 10:18:44 +00:00
for (var i = 0; i < len; i++) {
var context = contexts[i];
if (context.tagName)
2022-05-24 21:11:12 +00:00
objects[i] = this.elementInstantiate(doc, context);
else if (context.klass)
2022-05-24 21:11:12 +00:00
objects[i] = this.objectInstantiate(doc, context);
else
2022-05-24 21:11:12 +00:00
objects[i] = this.textInstantiate(doc, context);
2015-03-06 23:33:54 +00:00
}
2022-05-24 21:11:12 +00:00
return new Scope(this, objects, thisArg, parentScope);
}
2022-05-24 21:11:12 +00:00
,link: function(scope, exprScope) {
const objects = scope.objects;
const links = this._links;
2016-10-04 15:27:49 +00:00
2022-05-24 21:11:12 +00:00
// Pre-link
for (var i = links.length - 1; i >= 0; i--) {
const link = links[i];
const object = objects[link.context.id];
const objectRef = scope._$[link.objectId];
if (objectRef === undefined) {
this.showError('Referenced unexistent object with id \'%s\'',
link.objectId);
continue;
}
if (link.prop)
object[link.prop] = objectRef;
else
object.appendChild(objectRef);
2015-03-06 23:33:54 +00:00
}
2022-05-24 21:11:12 +00:00
// Post-link
const baseExprScope = [
_,
scope._$
].concat(exprScope);
2022-05-24 10:18:44 +00:00
2022-05-24 21:11:12 +00:00
this.linkExpr(scope, baseExprScope);
const contexts = this._contexts;
2022-05-24 10:18:44 +00:00
for (var i = 0; i < contexts.length; i++) {
2022-05-24 21:11:12 +00:00
const context = contexts[i];
const object = objects[i];
if (context.tagName)
2022-05-24 21:11:12 +00:00
this.elementLink(context, object, objects, scope, baseExprScope);
else if (context.klass)
2022-05-24 21:11:12 +00:00
this.objectLink(context, object, objects, scope, baseExprScope);
}
}
2022-05-24 10:18:44 +00:00
2022-05-24 21:11:12 +00:00
,linkExpr(scope, baseScope, exprScope) {
2022-05-24 10:18:44 +00:00
const contexts = this._contexts;
2022-05-24 21:11:12 +00:00
const objects = scope.objects;
2022-05-24 10:18:44 +00:00
2022-05-24 21:11:12 +00:00
exprScope = baseScope.concat(exprScope);
2022-05-24 10:18:44 +00:00
for (let i = 0; i < contexts.length; i++) {
const context = contexts[i];
const object = objects[i];
if (context.exprs) {
const values = [];
for (expr of context.exprs) {
let value = undefined;
try {
2022-05-24 21:11:12 +00:00
value = expr.apply(scope.thisArg, exprScope);
2022-05-24 10:18:44 +00:00
} catch (e) {
console.warn('Expression error:', e.message);
continue;
}
values.push(value);
}
let k = 0;
const text = context.text.replace(/{{\d+}}/g, function() {
return values[k++];
});
object.textContent = text;
} else {
const dynProps = context.dynProps;
for (const prop in dynProps) {
let value = undefined;
try {
2022-05-24 21:11:12 +00:00
value = dynProps[prop].apply(scope.thisArg, exprScope);
2022-05-24 10:18:44 +00:00
} catch (e) {
console.warn('Expression error:', e.message);
continue;
}
if (context.tagName)
object.setAttribute(prop, value);
else
object[prop] = value;
}
}
}
}
2022-05-24 21:11:12 +00:00
,showError: function(error) {
var path = this._path ? this._path : 'Node';
var logArgs = ['Vn.Builder: %s: '+ error, path];
for (var i = 1; i < arguments.length; i++)
logArgs.push(arguments[i]);
console.warn.apply(null, logArgs);
}
2022-05-24 21:11:12 +00:00
,_addLink: function(context, prop, objectId) {
this._links.push({
context
,prop
,objectId: kebabToCamel(objectId)
});
}
2022-05-24 21:11:12 +00:00
,fnExpr(expr) {
return new Function(this._exprArgs,
'"use strict"; return ' + expr + ';'
);
}
,matchExpr(value) {
const match = /^{{(.*)}}$/.exec(value);
if (!match) return null;
return this.fnExpr(match[1]);
}
2015-11-09 08:14:33 +00:00
2022-05-24 21:11:12 +00:00
,_translateValue: function(value) {
var chr = value.charAt(0);
2022-05-24 21:11:12 +00:00
if (chr === '_')
return _(value.substr(1));
else if (chr === '\\' && value.charAt(1) === '_')
return value.substr(1);
return value;
}
2022-05-24 21:11:12 +00:00
,_getMethod: function(value) {
let method;
2015-11-09 08:14:33 +00:00
2022-05-24 21:11:12 +00:00
if (this.isIdentifier(value)) {
// XXX: Compatibility with old events
method = value;
} else {
try {
method = new Function(this._eventArgs,
'"use strict"; return ' + value + ';'
);
} catch (err) {
this.showError(`Method: ${err.message}: ${value}`);
}
2015-11-09 08:14:33 +00:00
}
2022-05-24 21:11:12 +00:00
return method;
}
,_isEvent: function(attribute) {
return /^on-\w+/.test(attribute);
}
2022-05-24 21:11:12 +00:00
,isIdentifier: function(value) {
return /^[a-zA-Z_$][\w$]*$/.test(value);
}
,_replaceFunc: function(token) {
return token.charAt(1).toUpperCase();
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TextNode
/**
* Creates a text node context.
2022-05-24 10:18:44 +00:00
*/
,textCompile: function(node, tagName) {
if (!tagName) {
let text = node.textContent;
if (/{{.*}}/.test(text)) {
let i = 0;
const self = this;
const exprs = [];
text = text.replace(/{{((?:(?!}}).)*)}}/g, function(match, capture) {
exprs.push(self.fnExpr(capture));
return `{{${i++}}}`;
});
return {text, exprs};
} else
return {text};
} else if (tagName === 't')
return {text: _(node.firstChild.textContent)};
else
return null;
}
2022-05-24 21:11:12 +00:00
,textInstantiate: function(doc, context) {
return doc.createTextNode(context.exprs ? '' : context.text);
}
2022-05-24 21:11:12 +00:00
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Vn.Object
/**
* Creates a object context.
2022-05-24 10:18:44 +00:00
*/
,objectCompile: function(node, tagName) {
2016-09-26 09:28:47 +00:00
var klass = vnCustomTags[tagName];
if (!klass)
return null;
var props = {};
var objectProps = {};
var childs = [];
var events = {};
var context = {
klass: klass,
props: props,
2022-05-24 21:11:12 +00:00
dynProps: {},
funcProps: {},
objectProps: objectProps,
childs: childs,
events: events,
custom: null
};
var a = node.attributes;
2015-11-09 08:14:33 +00:00
2022-05-24 10:18:44 +00:00
for (var i = 0; i < a.length; i++) {
var attribute = a[i].nodeName;
var value = a[i].nodeValue;
2015-11-09 08:14:33 +00:00
2022-05-24 10:18:44 +00:00
if (this._isEvent(attribute)) {
var handler = this._getMethod(value)
2015-11-19 13:57:23 +00:00
if (handler)
2022-05-24 10:18:44 +00:00
events[attribute.substr(3)] = handler;
} else if (!/^(id|property)$/.test(attribute)) {
2022-05-24 21:11:12 +00:00
this.propCompile(context, klass, props,
node, attribute, value);
}
}
var childNodes = node.childNodes;
if (childNodes)
2022-05-24 10:18:44 +00:00
for (var i = 0; i < childNodes.length; i++) {
var child = childNodes[i];
var isElement = child.nodeType === Node.ELEMENT_NODE;
2022-05-24 10:18:44 +00:00
var childTagName = isElement ? child.tagName.toLowerCase() : null;
var childContext;
2022-05-24 10:18:44 +00:00
if (childTagName === 'pointer') {
this._addLink(context, null, child.getAttribute('object'));
} else if (childTagName === 'custom') {
context.custom = child;
2022-05-24 21:11:12 +00:00
} else if (childContext = this._compile(child)) {
2022-05-24 10:18:44 +00:00
var prop = isElement ? child.getAttribute('property') : null;
2022-05-24 10:18:44 +00:00
if (prop) {
prop = prop.replace(/-./g, this._replaceFunc);
objectProps[prop] = childContext.id;
2022-05-24 10:18:44 +00:00
} else
childs.push(childContext.id);
}
2015-07-07 15:27:47 +00:00
}
return context;
2015-07-07 15:27:47 +00:00
}
2022-05-24 21:11:12 +00:00
,propCompile: function(context, klass, props, node, attribute, value) {
let isLink = false;
let propError = false;
let newValue = null;
const propName = attribute.replace(/-./g, this._replaceFunc);
const propInfo = klass.Properties[propName];
2022-05-24 10:18:44 +00:00
if (!propInfo) {
2022-05-24 21:11:12 +00:00
this.showError('Attribute \'%s\' not valid for tag \'%s\'',
attribute, node.tagName);
2015-07-03 05:49:45 +00:00
return;
}
2022-05-24 10:18:44 +00:00
if (!value) {
2022-05-24 21:11:12 +00:00
this.showError('Attribute \'%s\' empty on tag \'%s\'',
attribute, node.tagName);
return;
}
2022-05-24 10:18:44 +00:00
const expr = this.matchExpr(value);
if (expr) {
2022-05-24 21:11:12 +00:00
context.dynProps[propName] = expr;
2022-05-24 10:18:44 +00:00
} else {
switch (propInfo.type) {
case Boolean:
2022-05-24 10:18:44 +00:00
newValue = (/^(true|1)$/i).test(value);
break;
case Number:
2022-05-24 10:18:44 +00:00
newValue = 0 + new Number(value);
break;
case String:
2022-05-24 10:18:44 +00:00
newValue = this._translateValue(value);
2015-03-06 23:33:54 +00:00
break;
case Function:
2022-05-24 21:11:12 +00:00
context.funcProps[propName] = this._getMethod(value);
break;
default:
if (propInfo.enumType)
newValue = propInfo.enumType[value];
2015-12-02 17:26:58 +00:00
else if (propInfo.type instanceof Function)
isLink = true;
2022-05-24 21:11:12 +00:00
else
propError = true;
2022-05-24 10:18:44 +00:00
}
if (isLink)
this._addLink(context, propName, value);
else if (newValue !== null && newValue !== undefined)
props[propName] = newValue;
2022-05-24 21:11:12 +00:00
else if (propError)
this.showError('Attribute \'%s\' invalid for tag \'%s\'',
2022-05-24 10:18:44 +00:00
attribute, node.tagName);
}
}
2022-05-24 21:11:12 +00:00
,objectInstantiate: function(doc, context) {
2022-05-24 10:18:44 +00:00
return new context.klass();
}
2022-05-24 21:11:12 +00:00
,objectLink: function(context, object, objects, scope, exprScope) {
2022-05-24 10:18:44 +00:00
object.setProperties(context.props);
2022-05-24 21:11:12 +00:00
const objectProps = context.objectProps;
for (const prop in objectProps)
object[prop] = objects[objectProps[prop]];
2022-05-24 21:11:12 +00:00
const childs = context.childs;
for (let i = 0; i < childs.length; i++)
2022-05-24 10:18:44 +00:00
object.appendChild(objects[childs[i]]);
2022-05-24 21:11:12 +00:00
const funcProps = context.funcProps;
for (const prop in funcProps) {
let method;
const handler = funcProps[prop];
if (typeof handler === 'string') {
// XXX: Compatibility with old expressions
method = scope.thisArg[handler];
if (!method)
this.showError(`Function '${handler}' not found`);
method = method.bind(scope.thisArg);
} else {
method = function() {
handler.apply(scope.thisArg, exprScope);
};
}
if (method)
object[prop] = method;
}
const events = context.events;
for (const event in events) {
let listener;
const handler = events[event];
if (typeof handler === 'string') {
// XXX: Compatibility with old expressions
listener = scope.thisArg[handler];
if (!listener)
this.showError(`Function '${handler}' not found`);
} else {
listener = function() {
handler.apply(scope.thisArg, exprScope.concat(arguments));
};
}
if (listener)
object.on(event, listener, scope.thisArg);
}
if (context.custom)
2022-05-24 21:11:12 +00:00
object.loadXml(scope, context.custom);
}
2022-05-24 21:11:12 +00:00
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Element
/**
* Creates a HTML node context.
2022-05-24 10:18:44 +00:00
*/
,elementCompile: function(node, tagName) {
var attributes = {};
2022-05-24 10:18:44 +00:00
var dynProps = {};
var childs = [];
var events = {};
var handler;
var a = node.attributes;
2022-05-24 10:18:44 +00:00
for (var i = 0; i < a.length; i++) {
var attribute = a[i].nodeName;
var value = a[i].nodeValue;
2022-05-24 10:18:44 +00:00
2022-05-24 21:11:12 +00:00
if (this._isEvent(attribute)) {
2022-05-24 10:18:44 +00:00
var handler = this._getMethod(value);
2022-05-24 21:11:12 +00:00
if (handler) events[attribute.substr(3)] = handler;
} else if (attribute !== 'id') {
const expr = this.matchExpr(value);
if (expr)
dynProps[attribute] = expr;
else
attributes[attribute] = this._translateValue(value);
}
}
2015-10-14 11:51:43 +00:00
var childContext;
var childNodes = node.childNodes;
if (childNodes)
for (var i = 0; i < childNodes.length; i++)
2022-05-24 21:11:12 +00:00
if (childContext = this._compile(childNodes[i]))
2022-05-24 10:18:44 +00:00
childs.push(childContext.id);
return {
2022-05-24 10:18:44 +00:00
tagName,
attributes,
dynProps,
childs,
events
};
}
2022-05-24 21:11:12 +00:00
,elementInstantiate: function(doc, context) {
return doc.createElement(context.tagName);
}
2022-05-24 21:11:12 +00:00
,elementLink: function(context, object, objects, scope, exprScope) {
const attributes = context.attributes;
for (const attribute in attributes)
2022-05-24 10:18:44 +00:00
object.setAttribute(attribute, attributes[attribute]);
2015-10-14 11:51:43 +00:00
2022-05-24 21:11:12 +00:00
const childs = context.childs;
2022-05-24 10:18:44 +00:00
for (var i = 0; i < childs.length; i++) {
2022-05-24 21:11:12 +00:00
let child = objects[childs[i]];
if (child instanceof Htk.Widget)
2016-10-16 14:16:08 +00:00
child = child.node;
if (child instanceof Node)
2022-05-24 10:18:44 +00:00
object.appendChild(child);
}
2022-05-24 21:11:12 +00:00
const events = context.events;
for (const event in events) {
let listener;
const handler = events[event];
if (typeof handler === 'string') {
// XXX: Compatibility with old expressions
listener = scope.thisArg[handler];
if (!listener)
this.showError(`Function '${handler}' not found`);
listener = listener.bind(scope.thisArg);
} else {
listener = function(e) {
handler.apply(scope.thisArg, exprScope.concat(e));
};
}
2015-08-17 18:02:14 +00:00
2022-05-24 21:11:12 +00:00
if (listener)
object.addEventListener(event, listener);
}
}
});