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

735 lines
15 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
2017-04-10 15:17:56 +00:00
var VnObject = require ('./object');
2017-10-18 16:01:21 +00:00
var Component = require ('./component');
2017-03-23 16:20:51 +00:00
var Type = require ('./type');
2016-09-26 09:28:47 +00:00
2017-10-18 16:01:21 +00:00
var specialAttrs = {
id : 1,
property : 1
};
var objectAttrs = {
for : 1
};
/**
2015-03-06 23:33:54 +00:00
* Creates a object from a XML specification.
2016-12-20 09:32:17 +00:00
*/
2016-09-26 09:28:47 +00:00
module.exports = new Class
({
2017-04-10 15:17:56 +00:00
Extends: VnObject
,_addedMap: {}
,_contexts: null
/**
* Compiles an XML file.
*
2017-10-18 16:01:21 +00:00
* @param {String} path The XML path
* @return {Boolean} %true on success, %false othersise
2016-12-20 09:32:17 +00:00
*/
2017-10-18 16:01:21 +00:00
,compileFile: function (path)
{
2016-09-26 09:28:47 +00:00
this._path = path;
2017-10-18 16:01:21 +00:00
return this.compileDocument (Vn.getXml (path));
2016-09-26 09:28:47 +00:00
}
2017-10-18 16:01:21 +00:00
/**
* Compiles an XML string.
*
* @param {String} xmlString The XML string
* @return {Boolean} %true on success, %false othersise
*/
,compileString: function (xmlString)
2016-09-26 09:28:47 +00:00
{
var parser = new DOMParser ();
2017-10-18 16:01:21 +00:00
var doc = parser.parseFromString (xmlString, 'text/xml');
return this.compileDocument (doc);
2016-09-26 09:28:47 +00:00
}
2017-10-18 16:01:21 +00:00
/**
* Compiles a XML document.
*
* @param {Document} doc The DOM document
* @return {Boolean} %true on success, %false othersise
*/
,compileDocument: function (doc)
2016-09-26 09:28:47 +00:00
{
2017-10-18 16:01:21 +00:00
if (!doc)
2015-03-06 23:33:54 +00:00
return false;
2017-10-18 16:01:21 +00:00
this._compileInit ();
2017-10-18 16:01:21 +00:00
var docElement = doc.documentElement;
2015-03-06 23:33:54 +00:00
if (docElement.tagName !== 'vn')
{
2017-10-18 16:01:21 +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++)
this._compileNode (childs[i]);
2015-03-06 23:33:54 +00:00
this._compileEnd ();
2015-03-06 23:33:54 +00:00
return true;
}
2015-03-09 08:36:54 +00:00
/**
* Compiles a single DOM node.
*
2017-10-18 16:01:21 +00:00
* @param {Node} path The DOM node
* @return {Boolean} %true on success, %false othersise
2016-12-20 09:32:17 +00:00
*/
2017-10-18 16:01:21 +00:00
,compileNode: function (node)
2015-03-09 08:36:54 +00:00
{
2017-10-18 16:01:21 +00:00
this._compileInit ();
this._mainContext = this._compileNode (node).id;
this._compileEnd ();
return true;
2015-03-09 08:36:54 +00:00
}
2017-10-18 16:01:21 +00:00
/**
* Creates a new scope from a compiled XML tree.
*
* @param {Document} dstDocument The document used to create the nodes
* @param {Object} signalData The object where to bind methods
* @param {Scope} parentScope The parent scope or %null for no parent
* @return {Scope} The created scope
*/
,load: function (dstDocument, signalData, parentScope, extraObjects)
{
if (this._contexts === null)
return null;
2017-10-18 16:01:21 +00:00
this._doc = dstDocument || document;
var contexts = this._contexts;
var len = contexts.length;
var objects = new Array (len);
2017-10-18 16:01:21 +00:00
var scope = new Scope (this, objects, signalData, parentScope, extraObjects)
for (var i = 0; i < len; i++)
2015-03-06 23:33:54 +00:00
{
var context = contexts[i];
if (context.tagName)
2017-10-18 16:01:21 +00:00
objects[i] = this.elementInstantiate (context, scope);
else if (context.klass)
2017-10-18 16:01:21 +00:00
objects[i] = this.objectInstantiate (context, scope);
else
2017-10-18 16:01:21 +00:00
objects[i] = this.textInstantiate (context, scope);
2015-03-06 23:33:54 +00:00
}
2017-10-18 16:01:21 +00:00
return scope;
}
2017-10-18 16:01:21 +00:00
,_compileInit: function ()
{
this._path = null;
this._tags = {};
this._contexts = [];
this._contextMap = {};
2017-10-18 16:01:21 +00:00
this._objectLinks = [];
this._mainContext = null;
}
2017-07-05 09:50:42 +00:00
,_compileEnd: function ()
{
2017-10-18 16:01:21 +00:00
var links = this._objectLinks;
for (var i = links.length - 1; i >= 0; i--)
2015-03-06 23:33:54 +00:00
{
2017-10-18 16:01:21 +00:00
var link = links[i];
var context = link.context;
var contextId = this._contextMap[link.objectId];
2017-10-18 16:01:21 +00:00
if (contextId == undefined)
continue;
if (link.prop)
context.objectProps[link.prop] = contextId;
else
2017-10-18 16:01:21 +00:00
context.childs.push (contextId);
2017-10-18 16:01:21 +00:00
links.splice (i, 1);
}
}
,_compileNode: function (node)
{
var context = null;
var tagName = null;
2017-10-18 16:01:21 +00:00
var isElement = node.nodeType === Node.ELEMENT_NODE;
2015-11-09 08:14:33 +00:00
2017-10-18 16:01:21 +00:00
if (isElement)
tagName = node.tagName.toLowerCase ();
else if (node.nodeType !== Node.TEXT_NODE
|| /^[\n\r\t]*$/.test (node.textContent))
return null;
var context =
this.textCompile (node, tagName)
|| this.objectCompile (node, tagName)
|| this.elementCompile (node, tagName);
context.id = this._contexts.length;
2017-10-18 16:01:21 +00:00
if (isElement)
2015-11-09 08:14:33 +00:00
{
var nodeId = node.getAttribute ('id');
if (nodeId)
2017-10-18 16:01:21 +00:00
{
this._contextMap[nodeId] = context.id;
2017-10-18 16:01:21 +00:00
context.nodeId = nodeId;
}
2015-11-09 08:14:33 +00:00
var tags = this._tags[tagName];
if (!tags)
this._tags[tagName] = tags = [];
tags.push (context.id);
2015-11-09 08:14:33 +00:00
}
this._contexts.push (context);
return context;
}
2017-10-18 16:01:21 +00:00
,link: function (scope)
{
2017-10-18 16:01:21 +00:00
var objects = scope.objects;
var links = this._objectLinks;
for (var i = links.length - 1; i >= 0; i--)
{
var link = links[i];
var object = objects[link.context.id];
var objectRef = scope.$(link.objectId);
if (objectRef == null)
{
this._showError ('Referenced unexistent object with id \'%s\'',
link.objectId);
continue;
}
if (link.prop)
object[link.prop] = objectRef;
else
object.appendChild (objectRef);
}
var contexts = this._contexts;
for (var i = 0; i < contexts.length; i++)
{
var context = contexts[i];
var object = objects[i];
if (context.tagName)
this.elementLink (context, object, objects, scope);
else if (context.klass)
this.objectLink (context, object, objects, scope);
}
for (var i = 0; i < contexts.length; i++)
{
var context = contexts[i];
var object = objects[i];
if (context.tagName)
this.elementConnect (context, object, objects, scope);
else if (context.klass)
this.objectConnect (context, object, objects, scope);
}
}
/**
* Creates a object context.
2016-12-20 09:32:17 +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 = {};
2017-10-18 16:01:21 +00:00
var funcProps = {};
var childs = [];
var events = {};
var context = {
klass: klass,
props: props,
objectProps: objectProps,
2017-10-18 16:01:21 +00:00
funcProps: funcProps,
childs: childs,
events: events,
custom: null
};
var a = node.attributes;
2015-11-09 08:14:33 +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
2015-11-19 13:57:23 +00:00
if (this._isEvent (attribute))
2017-10-18 16:01:21 +00:00
events[attribute.substr (3)] = value;
else if (!specialAttrs[attribute])
{
this.propCompile (context, klass, props,
node, attribute, value);
}
}
var childNodes = node.childNodes;
if (childNodes)
for (var i = 0; i < childNodes.length; i++)
2015-07-07 15:27:47 +00:00
{
var child = childNodes[i];
2017-04-07 11:00:33 +00:00
var childContext = null;
var childTagName = null;
2017-04-07 11:00:33 +00:00
if (child.nodeType === Node.ELEMENT_NODE)
childTagName = child.tagName.toLowerCase ();
if (childTagName === 'pointer')
{
2017-10-18 16:01:21 +00:00
this.objectAddLink (context, null, child.getAttribute ('object'));
}
else if (childTagName === 'custom')
{
context.custom = child;
}
else if (childContext = this._compileNode (child))
{
2017-04-07 11:00:33 +00:00
var prop = null;
if (childTagName)
prop = child.getAttribute ('property');
if (prop)
{
prop = prop.replace (/-./g, this._replaceFunc);
objectProps[prop] = childContext.id;
}
else
childs.push (childContext.id);
}
2015-07-07 15:27:47 +00:00
}
return context;
2015-07-07 15:27:47 +00:00
}
,propCompile: function (context, klass, props, node, attribute, value)
{
2015-12-02 17:26:58 +00:00
var isLink = false;
var newValue = null;
var propName = attribute.replace (/-./g, this._replaceFunc);
var propInfo = klass.Properties[propName];
if (!propInfo)
2015-07-03 05:49:45 +00:00
{
this._showError ('Attribute \'%s\' not valid for tag \'%s\'',
attribute, node.tagName);
2015-07-03 05:49:45 +00:00
return;
}
if (!value)
{
this._showError ('Attribute \'%s\' empty on tag \'%s\'',
attribute, node.tagName);
return;
}
2017-10-18 16:01:21 +00:00
var propError = false;
switch (propInfo.type)
{
2017-04-19 06:16:37 +00:00
case null:
newValue = value;
break;
case Boolean:
newValue = (/^(true|1)$/i).test (value);
break;
case Number:
newValue = 0 + new Number (value);
break;
case String:
newValue = this._translateValue (value);
2015-03-06 23:33:54 +00:00
break;
case Function:
2017-10-18 16:01:21 +00:00
context.funcProps[propName] = value;
break;
2017-03-23 16:20:51 +00:00
case Type:
newValue = window[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;
2017-10-18 16:01:21 +00:00
else
propError = true;
}
2015-12-02 17:26:58 +00:00
if (isLink)
2017-10-18 16:01:21 +00:00
this.objectAddLink (context, propName, value);
else if (newValue != null)
2015-12-02 17:26:58 +00:00
props[propName] = newValue;
2017-10-18 16:01:21 +00:00
else if (propError)
this._showError ('Attribute \'%s\' invalid for tag \'%s\'',
attribute, node.tagName);
}
2017-10-18 16:01:21 +00:00
,objectAddLink: function (context, prop, objectId)
{
2017-10-18 16:01:21 +00:00
this._objectLinks.push ({
context: context
,prop: prop
,objectId: objectId
});
}
2017-10-18 16:01:21 +00:00
,objectInstantiate: function (context, scope)
{
2017-10-18 16:01:21 +00:00
var object = new context.klass ();
object.setProperties (context.props);
2017-10-18 16:01:21 +00:00
if (context.nodeId && object instanceof Component)
object.htmlId = scope.getHtmlId (context.nodeId);
return object;
}
2017-10-18 16:01:21 +00:00
,objectLink: function (context, object, objects, scope)
{
var objectProps = context.objectProps;
for (var prop in objectProps)
object[prop] = objects[objectProps[prop]];
var childs = context.childs;
for (var i = 0; i < childs.length; i++)
object.appendChild (objects[childs[i]]);
2017-10-18 16:01:21 +00:00
if (context.custom)
object.loadXml (scope, context.custom);
}
,objectConnect: function (context, object, objects, scope)
{
var funcProps = context.funcProps;
for (var prop in funcProps)
{
var method = scope.getMethod (funcProps[prop], true);
if (method)
object[prop] = method;
}
var events = context.events;
for (var event in events)
2017-10-18 16:01:21 +00:00
{
var method = scope.getMethod (events[event]);
if (method)
object.on (event, method, scope.signalData);
}
}
/**
* Creates a HTML node context.
2016-12-20 09:32:17 +00:00
*/
,elementCompile: function (node, tagName)
{
2017-10-18 16:01:21 +00:00
var props = {};
var objectProps = {};
var childs = [];
var events = {};
var a = node.attributes;
for (var i = 0; i < a.length; i++)
{
var attribute = a[i].nodeName;
var value = a[i].nodeValue;
2015-11-19 13:57:23 +00:00
if (this._isEvent (attribute))
2017-10-18 16:01:21 +00:00
events[attribute.substr (3)] = value;
else if (objectAttrs[attribute])
objectProps[attribute] = value;
else if (!specialAttrs[attribute])
props[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++)
if (childContext = this._compileNode (childNodes[i]))
2017-10-18 16:01:21 +00:00
childs.push (childContext.id);
return {
tagName: tagName,
2017-10-18 16:01:21 +00:00
props: props,
objectProps: objectProps,
childs: childs,
events: events
};
}
2017-10-18 16:01:21 +00:00
,elementInstantiate: function (context, scope)
{
2017-10-18 16:01:21 +00:00
var object = this._doc.createElement (context.tagName);
var props = context.props;
for (var prop in props)
object.setAttribute (prop, props[prop]);
if (context.nodeId)
object.setAttribute ('id', scope.getHtmlId (context.nodeId));
return object;
}
2017-10-18 16:01:21 +00:00
,elementLink: function (context, object, objects, scope)
{
2017-10-18 16:01:21 +00:00
var props = context.objectProps;
for (var prop in props)
{
var objectValue = scope.$(props[prop]);
var htmlId;
if (objectValue instanceof Component)
htmlId = objectValue.htmlId;
if (objectValue instanceof Node)
htmlId = objectValue.id;
object.setAttribute (prop, htmlId);
}
2015-10-14 11:51:43 +00:00
var childs = context.childs;
for (var i = 0; i < childs.length; i++)
{
var child = objects[childs[i]];
2017-10-18 16:01:21 +00:00
if (child instanceof Component)
2016-10-16 14:16:08 +00:00
child = child.node;
if (child instanceof Node)
object.appendChild (child);
}
2017-10-18 16:01:21 +00:00
}
,elementConnect: function (context, object, objects, scope)
{
var events = context.events;
for (var event in events)
2017-10-18 16:01:21 +00:00
{
var method = scope.getMethod (events[event], true);
if (method)
object.addEventListener (event, method);
}
}
/**
* Creates a text node context.
*/
,textCompile: function (node, tagName)
{
if (!tagName)
{
var text = node.textContent;
if (/^[\s]*\\?_.*/.test (text))
text = _(text.trim ().substr (1));
}
else if (tagName === 't')
var text = _(node.firstChild.textContent);
else
return null;
return {text: text};
}
,textInstantiate: function (context)
{
return this._doc.createTextNode (context.text);
}
,_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]);
2017-03-23 16:20:51 +00:00
console.warn.apply (console, logArgs);
}
,_translateValue: function (value)
2015-10-14 11:51:43 +00:00
{
var chr = value.charAt (0);
if (chr === '_')
2015-10-14 11:51:43 +00:00
return _(value.substr (1));
else if (chr === '\\' && value.charAt (1) === '_')
2015-10-14 11:51:43 +00:00
return value.substr (1);
return value;
}
2015-11-19 13:57:23 +00:00
,_isEvent: function (attribute)
{
return /^on-\w+/.test (attribute);
2015-10-14 11:51:43 +00:00
}
,_replaceFunc: function (token)
2015-10-14 11:51:43 +00:00
{
return token.charAt(1).toUpperCase ();
}
2017-10-18 16:01:21 +00:00
,getMain: function (result)
{
return result.objects[this._mainContext];
}
,getById: function (objectId)
{
return this._contextMap[objectId];
}
,getByTagName: function (result, tagName)
{
var tags = this._tags[tagName];
if (tags)
{
var arr = new Array (tags.length);
for (var i = 0; i < tags.length; i++)
arr[i] = result.objects[tags[i]];
return arr;
}
return [];
}
});
2017-10-18 16:01:21 +00:00
var scopeUid = 0;
var Scope = new Class
({
2017-04-10 15:17:56 +00:00
Extends: VnObject
2017-10-18 16:01:21 +00:00
,initialize: function (builder, objects, signalData, parentScope, extraObjects)
{
this.builder = builder;
this.objects = objects;
2017-10-18 16:01:21 +00:00
this.signalData = signalData;
this.parentScope = parentScope;
this.uid = ++scopeUid;
this.extraObjects = extraObjects;
if (!signalData && parentScope)
this.signalData = parentScope.signalData;
this.parent ();
}
,getMain: function ()
{
return this.builder.getMain (this);
}
2015-07-03 05:49:45 +00:00
2017-10-18 16:01:21 +00:00
/**
* Fetchs an element by it's identifier.
*
* @param {String} id The node identifier
*/
,$: function (id)
2015-07-03 05:49:45 +00:00
{
2017-10-18 16:01:21 +00:00
var object;
var index = this.builder.getById (id);
if (index !== undefined)
object = this.objects[index];
else
{
object = this.extraObjects[id];
if (object === undefined && this.parentScope)
object = this.parentScope.getById (id);
}
return object ? object : null;
2015-07-03 05:49:45 +00:00
}
2017-10-18 16:01:21 +00:00
/**
* Fetchs an element by it's identifier.
*
* @param {String} id The node identifier
*/
,getById: function (id)
{
2017-10-18 16:01:21 +00:00
return this.$(id);
}
2015-11-09 08:14:33 +00:00
,getByTagName: function (tagName)
{
return this.builder.getByTagName (this, tagName);
}
,link: function ()
{
this.builder.link (this);
}
2017-10-18 16:01:21 +00:00
,getMethod: function (value, binded)
{
if (this.signalData)
{
var method = this.signalData[value];
if (method && binded)
method = method.bind (this.signalData);
}
else
var method = window[value];
if (method === undefined)
this.builder._showError ('Function \'%s\' not found', value);
return method;
}
,getHtmlId: function (nodeId)
{
return 'vn-'+ this.uid +'-'+ nodeId;
}
2015-08-17 18:02:14 +00:00
,_destroy: function ()
{
var objects = this.objects;
2017-04-19 06:16:37 +00:00
for (var i = objects.length; i--;)
2017-04-10 15:17:56 +00:00
if (objects[i] instanceof VnObject)
2017-10-18 16:01:21 +00:00
{
objects[i].unref ();
2017-10-18 16:01:21 +00:00
objects[i].disconnectByInstance (this.builder.signalData);
}
2015-08-17 18:02:14 +00:00
this.parent ();
}
});