2015-01-23 13:09:30 +00:00
|
|
|
/**
|
2015-03-06 23:33:54 +00:00
|
|
|
* Creates a object from a XML specification.
|
2015-01-23 13:09:30 +00:00
|
|
|
**/
|
|
|
|
Vn.Builder = new Class
|
|
|
|
({
|
2015-07-28 19:14:26 +00:00
|
|
|
Extends: Vn.Object
|
|
|
|
,objectMap: {}
|
2015-01-23 13:09:30 +00:00
|
|
|
,tags: {}
|
2015-10-14 11:51:43 +00:00
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++ Deprecated
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
,loadXml: function (xmlDoc)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
if (!xmlDoc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var docElement = xmlDoc.documentElement;
|
|
|
|
|
|
|
|
if (docElement.tagName !== 'vn')
|
|
|
|
return false;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
this.contexts = [];
|
2015-03-06 23:33:54 +00:00
|
|
|
var childs = docElement.childNodes;
|
|
|
|
|
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
|
|
|
this.loadNode (childs[i], null);
|
|
|
|
|
|
|
|
this.resolveProperties ();
|
|
|
|
delete this.contexts;
|
|
|
|
return true;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
|
|
|
|
,loadXmlFromNode: function (node)
|
|
|
|
{
|
|
|
|
this.contexts = [];
|
2015-03-19 19:36:11 +00:00
|
|
|
var mainNode = this.loadNode (node);
|
2015-03-09 08:36:54 +00:00
|
|
|
this.resolveProperties ();
|
|
|
|
delete this.contexts;
|
|
|
|
return mainNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
,add: function (id, object)
|
|
|
|
{
|
|
|
|
this.objectMap[id] = object;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,loadNode: function (node)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
var customNode;
|
2015-01-23 13:09:30 +00:00
|
|
|
var htmlNode = null;
|
2015-03-06 23:33:54 +00:00
|
|
|
var tagName = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (node.tagName)
|
|
|
|
tagName = node.tagName.toLowerCase ();
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
if (tagName === 't')
|
|
|
|
{
|
|
|
|
htmlNode = document.createTextNode (_(node.firstChild.textContent));
|
|
|
|
}
|
|
|
|
else if (!tagName)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-06 23:33:54 +00:00
|
|
|
htmlNode = document.importNode (node, false);
|
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
else if ((customNode = this.loadCustomNode (node, null)))
|
|
|
|
{
|
|
|
|
if (customNode instanceof Htk.Widget)
|
|
|
|
htmlNode = customNode.getNode ();
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
else
|
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
htmlNode = document.createElement (tagName);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var a = node.attributes;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
for (var i = 0; i < a.length; i++)
|
2015-03-27 19:10:49 +00:00
|
|
|
{
|
|
|
|
var nodeName = a[i].nodeName;
|
|
|
|
var nodeValue = a[i].nodeValue;
|
|
|
|
|
2015-07-07 15:27:47 +00:00
|
|
|
if (/^on-\w+/.test (nodeName))
|
2015-03-27 19:10:49 +00:00
|
|
|
{
|
|
|
|
var method = this.getMethod (nodeValue);
|
|
|
|
htmlNode.addEventListener (
|
|
|
|
nodeName.substr (3), method.bind (this.signalData));
|
|
|
|
}
|
|
|
|
else if (nodeName === 'id')
|
|
|
|
{
|
|
|
|
this.objectMap[nodeValue] = htmlNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
htmlNode.setAttribute (nodeName,
|
|
|
|
this.translateValue (nodeValue));
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
var childs = node.childNodes;
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
2015-03-06 23:33:54 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
var htmlChild = this.loadNode (childs[i]);
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
if (htmlChild)
|
2015-03-06 23:33:54 +00:00
|
|
|
htmlNode.appendChild (htmlChild);
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
return htmlNode;
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,loadCustomNode: function (node, parentContext)
|
|
|
|
{
|
|
|
|
if (!node.tagName)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var tagName = node.tagName.toLowerCase ();
|
|
|
|
var klass = Vn.customTags[tagName];
|
|
|
|
|
|
|
|
if (!klass)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var customNode = new klass ();
|
|
|
|
|
|
|
|
if (!this.tags[tagName])
|
|
|
|
this.tags[tagName] = [];
|
|
|
|
|
|
|
|
this.tags[tagName].push (customNode);
|
|
|
|
|
|
|
|
var context = {
|
|
|
|
node: node
|
|
|
|
,parent: parentContext
|
|
|
|
,object: customNode
|
|
|
|
,klass: klass
|
|
|
|
};
|
|
|
|
this.contexts.push (context);
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
var nodeId = node.getAttribute ('id');
|
|
|
|
|
|
|
|
if (nodeId)
|
|
|
|
this.objectMap[nodeId] = customNode;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
var childs = node.childNodes;
|
|
|
|
|
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
|
|
|
this.loadCustomNode (childs[i], context);
|
|
|
|
|
|
|
|
return customNode;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
,resolveProperties: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.contexts.length; i++)
|
|
|
|
{
|
|
|
|
var c = this.contexts[i];
|
|
|
|
var a = c.node.attributes;
|
|
|
|
|
|
|
|
for (var j = 0; j < a.length; j++)
|
2015-07-07 15:27:47 +00:00
|
|
|
this.setAttribute (c, a[j].nodeName, a[j].nodeValue);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (c.parent)
|
|
|
|
{
|
|
|
|
var parentProperty = c.node.getAttribute ('property');
|
|
|
|
|
|
|
|
if (!parentProperty)
|
|
|
|
parentProperty = c.parent.klass.Child;
|
|
|
|
|
|
|
|
if (parentProperty)
|
|
|
|
this.setProperty (c.parent, parentProperty, c.object);
|
|
|
|
|
|
|
|
if (c.klass.Parent)
|
|
|
|
this.setProperty (c, c.klass.Parent, c.parent.object);
|
|
|
|
}
|
|
|
|
|
|
|
|
c.object.loadXml (this, c.node);
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
|
|
|
|
,setAttribute: function (c, attribute, value)
|
|
|
|
{
|
|
|
|
if (/^on-\w+/.test (attribute))
|
|
|
|
{
|
|
|
|
var method = this.getMethod (value);
|
|
|
|
|
|
|
|
if (method)
|
|
|
|
c.object.on (attribute.substr (3), method, this.signalData);
|
|
|
|
}
|
|
|
|
else if (!/^(id|property)$/.test (attribute))
|
|
|
|
{
|
|
|
|
this.setProperty (c, attribute, value)
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-07-07 15:27:47 +00:00
|
|
|
,setProperty: function (c, attribute, value)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-07-07 15:27:47 +00:00
|
|
|
var propName = attribute.replace (/-./g, this.replaceFunc);
|
2015-01-23 13:09:30 +00:00
|
|
|
var prop = c.klass.Properties[propName];
|
|
|
|
|
2015-07-03 05:49:45 +00:00
|
|
|
if (!prop)
|
|
|
|
{
|
2015-07-07 15:27:47 +00:00
|
|
|
console.warn ('Vn.Builder: Attribute \'%s\' not valid for tag \'%s\'',
|
|
|
|
attribute, c.node.tagName);
|
2015-07-03 05:49:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!value)
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (prop.type)
|
|
|
|
{
|
|
|
|
case Boolean:
|
|
|
|
value = (/^(true|1)$/i).test (value);
|
|
|
|
break;
|
|
|
|
case Number:
|
|
|
|
value = 0 + new Number (value);
|
|
|
|
break;
|
|
|
|
case String:
|
2015-03-06 23:33:54 +00:00
|
|
|
value = this.translateValue (value);
|
|
|
|
break;
|
2015-01-23 13:09:30 +00:00
|
|
|
case Function:
|
2015-06-30 12:06:19 +00:00
|
|
|
{
|
|
|
|
var method = this.getMethod (value);
|
|
|
|
value = method ? method.bind (this.signalData) : null;
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
2015-06-30 12:06:19 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
default:
|
|
|
|
if (prop.type instanceof Function)
|
|
|
|
{
|
|
|
|
if (typeof value == 'string')
|
|
|
|
value = this.get (value);
|
|
|
|
if (!(value instanceof prop.type))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (prop.enumType)
|
|
|
|
value = prop.enumType[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value !== undefined)
|
|
|
|
c.object[propName] = value;
|
2015-07-10 12:30:08 +00:00
|
|
|
else
|
|
|
|
console.warn ('Vn.Builder: Empty attribute \'%s\' on tag \'%s\'',
|
|
|
|
attribute, c.node.tagName);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-27 19:10:49 +00:00
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++ Alpha
|
|
|
|
|
|
|
|
,compile: function (node, dstDocument)
|
|
|
|
{
|
|
|
|
this.contexts = [];
|
|
|
|
this.contextMap = {};
|
2015-11-05 07:30:19 +00:00
|
|
|
this.propLinks = [];
|
|
|
|
this.childLinks = [];
|
2015-10-14 11:51:43 +00:00
|
|
|
this.document = dstDocument ? dstDocument : document;
|
|
|
|
|
|
|
|
this.compileRec (node, null);
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < this.propLinks.length; i++)
|
|
|
|
{
|
|
|
|
var pl = this.propLinks[i];
|
|
|
|
var contextId = this.contextMap[pl.value];
|
|
|
|
|
|
|
|
if (contextId)
|
|
|
|
{
|
|
|
|
pl.context.objectProps[pl.prop] = contextId;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var object = this.parentBuilder.get (pl.value);
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
{
|
|
|
|
pl.context.props[pl.prop] = object;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.warn ('Vn.Builder: Referenced unexistent object with id \'%s\'',
|
|
|
|
pl.value);
|
|
|
|
}
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
delete this.propLinks;
|
|
|
|
|
|
|
|
for (var i = 0; i < this.childLinks.length; i++)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
var cl = this.childLinks[i];
|
|
|
|
var contextId = this.contextMap[pl.value];
|
|
|
|
|
|
|
|
if (contextId)
|
|
|
|
pl.context.childs.push (contextId);
|
|
|
|
else
|
|
|
|
console.warn ('Vn.Builder: Referenced unexistent object with id \'%s\'',
|
|
|
|
pl.value);
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
delete this.childLinks;
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
,compileRec: function (node)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
|
|
|
var tagName = null;
|
|
|
|
|
|
|
|
if (node.tagName)
|
|
|
|
tagName = node.tagName.toLowerCase ();
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
var nextId = this.contexts.length;
|
|
|
|
var context =
|
|
|
|
createTextTemplate (nextId, node, tagName)
|
|
|
|
|| createObjectTemplate (nextId, node, tagName)
|
|
|
|
|| createHtmlTemplate (nextId, node, tagName);
|
2015-10-14 11:51:43 +00:00
|
|
|
|
|
|
|
this.contexts.push (context);
|
2015-11-05 07:30:19 +00:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
,load: function (thisData)
|
|
|
|
{
|
|
|
|
var contexts = this.contexts;
|
|
|
|
var objects = new Array (contexts.length);
|
|
|
|
|
|
|
|
for (var i = 0; i < contexts.length; i++)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
var context = contexts[i];
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
if (context.func)
|
|
|
|
objects[i] = context.func (context);
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
for (var i = 0; i < contexts.length; i++)
|
|
|
|
{
|
|
|
|
var context = contexts[i];
|
|
|
|
|
|
|
|
if (context.linkFunc)
|
|
|
|
context.linkFunc (context, objects[i]);
|
|
|
|
}
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
2015-11-05 07:30:19 +00:00
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
/**
|
|
|
|
* Creates a text node template.
|
|
|
|
**/
|
2015-11-05 07:30:19 +00:00
|
|
|
,createTextTemplate: function (contextId, node, tagName)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
|
|
|
if (tagName === 't')
|
|
|
|
var text = _(node.firstChild.textContent);
|
|
|
|
else if (!tagName)
|
|
|
|
var text = node.textContent;
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
return {
|
|
|
|
id: contextId,
|
|
|
|
text: text,
|
|
|
|
func: this.createTextInstance
|
|
|
|
};
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
,createTextInstance: function (context)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
return this.document.createTextNode (context.text);
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a object template.
|
|
|
|
**/
|
2015-11-05 07:30:19 +00:00
|
|
|
,createObjectTemplate: function (contextId, node, tagName)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
|
|
|
var id = null;
|
|
|
|
var handler;
|
|
|
|
var props = {};
|
2015-11-05 07:30:19 +00:00
|
|
|
var objectProps = {};
|
|
|
|
var childs = [];
|
2015-10-14 11:51:43 +00:00
|
|
|
var events = null;
|
|
|
|
var klass = Vn.customTags[tagName];
|
|
|
|
|
|
|
|
if (!klass)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var a = node.attributes;
|
|
|
|
|
|
|
|
for (var i = 0; i < a.length; i++)
|
|
|
|
{
|
|
|
|
var attribute = a[i].nodeName;
|
|
|
|
var value = a[i].nodeValue;
|
|
|
|
|
|
|
|
if (attribute === 'id')
|
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
this.contextMap[value] = contextId;
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
else if ((handler = this.getEventHandler (attribute, value)))
|
|
|
|
{
|
|
|
|
if (!events)
|
|
|
|
events = {};
|
|
|
|
|
|
|
|
events[attribute.substr (3)] = handler;
|
|
|
|
}
|
|
|
|
else if (attribute !== 'property')
|
|
|
|
{
|
|
|
|
this.createPropTemplate (context, klass, props,
|
|
|
|
node, attribute, value);
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
var context = {
|
|
|
|
id: contextId,
|
|
|
|
func: this.createObjectInstance,
|
|
|
|
linkFunc: this.createObjectLink,
|
2015-10-14 11:51:43 +00:00
|
|
|
klass: klass,
|
2015-11-05 07:30:19 +00:00
|
|
|
props: props,
|
2015-10-14 11:51:43 +00:00
|
|
|
events: events,
|
2015-11-05 07:30:19 +00:00
|
|
|
objectProps: objectProps,
|
|
|
|
childs: childs
|
2015-10-14 11:51:43 +00:00
|
|
|
};
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
var childs = node.childNodes;
|
|
|
|
|
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
|
|
|
{
|
|
|
|
var child = childs[i];
|
|
|
|
var childTagName = child.tagName.toLowerCase ();
|
|
|
|
|
|
|
|
if (childTagName === 'pointer')
|
|
|
|
{
|
|
|
|
this.childLinks.push ({
|
|
|
|
context: context,
|
|
|
|
objectId: child.getAttribute ('object')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (childTagName === 'custom')
|
|
|
|
{
|
|
|
|
context.custom = child.firstElementChild;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var childContext = this.compileRec (child);
|
|
|
|
var prop = child.getAttribute ('property');
|
|
|
|
|
|
|
|
if (prop)
|
|
|
|
objectProps[prop] = childContext.id;
|
|
|
|
else
|
|
|
|
childs.push (childContext.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,createPropTemplate: function (context, klass, props, node, attribute, value)
|
|
|
|
{
|
|
|
|
var newValue = null;
|
|
|
|
var propName = attribute.replace (/-./g, this.replaceFunc);
|
|
|
|
var propInfo = klass.Properties[propName];
|
|
|
|
|
|
|
|
if (!propInfo)
|
|
|
|
{
|
|
|
|
console.warn ('Vn.Builder: Attribute \'%s\' not valid for tag \'%s\'',
|
|
|
|
attribute, node.tagName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
console.warn ('Vn.Builder: Attribute \'%s\' empty on tag \'%s\'',
|
|
|
|
attribute, node.tagName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (propInfo.type)
|
|
|
|
{
|
|
|
|
case Boolean:
|
|
|
|
newValue = (/^(true|1)$/i).test (value);
|
|
|
|
break;
|
|
|
|
case Number:
|
|
|
|
newValue = 0 + new Number (value);
|
|
|
|
break;
|
|
|
|
case String:
|
|
|
|
newValue = this.translateValue (value);
|
|
|
|
break;
|
|
|
|
case Function:
|
|
|
|
newValue = this.getMethod (value);
|
|
|
|
break;
|
|
|
|
case Vn.Enum:
|
|
|
|
newValue = propInfo.enumType[value];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newValue !== null && newValue !== undefined)
|
|
|
|
{
|
|
|
|
props[propName] = newValue;
|
|
|
|
}
|
|
|
|
else if (propInfo.type instanceof Function)
|
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
this.propLinks.push ({
|
|
|
|
context: context,
|
|
|
|
prop: attribute,
|
|
|
|
value: value
|
|
|
|
});
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
console.warn ('Vn.Builder: Attribute \'%s\' invalid for tag \'%s\'',
|
|
|
|
attribute, node.tagName);
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
,createObjectInstance: function (context)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
var object = new context.klass (context.props);
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
var events = context.events;
|
2015-10-14 11:51:43 +00:00
|
|
|
|
|
|
|
for (var event in events)
|
|
|
|
object.on (event,
|
|
|
|
events[event].bind (this.signalData));
|
2015-11-05 07:30:19 +00:00
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
,createObjectLink: function (context, object)
|
|
|
|
{
|
|
|
|
var objectProps = context.objectProps;
|
|
|
|
|
|
|
|
if (objectProps)
|
|
|
|
for (var prop in objectProps)
|
|
|
|
object[prop] = this.objects[objectProps[prop].id];
|
|
|
|
|
|
|
|
var childs = context.childs;
|
|
|
|
|
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
|
|
|
object.appendChild (childs[i]);
|
|
|
|
|
|
|
|
if (context.custom)
|
|
|
|
object.loadXml (context.custom);
|
|
|
|
}
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
/**
|
|
|
|
* Creates a HTML node template.
|
|
|
|
**/
|
2015-11-05 07:30:19 +00:00
|
|
|
,createHtmlTemplate: function (contextId, node, tagName)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
|
|
|
var handler;
|
|
|
|
var events = null;
|
2015-11-05 07:30:19 +00:00
|
|
|
var childs = [];
|
2015-10-14 11:51:43 +00:00
|
|
|
var htmlNode = this.document.createElement (tagName);
|
|
|
|
|
|
|
|
var a = node.attributes;
|
|
|
|
|
|
|
|
for (var i = 0; i < a.length; i++)
|
|
|
|
{
|
|
|
|
var attribute = a[i].nodeName;
|
|
|
|
var value = a[i].nodeValue;
|
|
|
|
|
|
|
|
if (attribute === 'id')
|
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
this.contextMap[value] = contextId;
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
else if ((handler = this.getEventHandler (attribute, value)))
|
|
|
|
{
|
|
|
|
if (!events)
|
|
|
|
events = {};
|
|
|
|
|
|
|
|
events[attribute.substr (3)] = handler;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
htmlNode.setAttribute (nodeName,
|
|
|
|
this.translateValue (nodeValue));
|
|
|
|
}
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
var childNodes = node.childNodes;
|
|
|
|
|
|
|
|
if (childNodes)
|
|
|
|
for (var i = 0; i < childNodes.length; i++)
|
|
|
|
{
|
|
|
|
var childContext = this.compileRec (childNodes[i]);
|
|
|
|
childs.push (childContext.id);
|
|
|
|
}
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
return {
|
2015-11-05 07:30:19 +00:00
|
|
|
id: contextId,
|
|
|
|
func: this.createHtmlInstance,
|
|
|
|
linkFunc: this.createHtmlLink,
|
2015-10-14 11:51:43 +00:00
|
|
|
node: htmlNode,
|
|
|
|
events: events,
|
2015-11-05 07:30:19 +00:00
|
|
|
childs: childs
|
2015-10-14 11:51:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
,createHtmlInstance: function (context)
|
2015-10-14 11:51:43 +00:00
|
|
|
{
|
2015-11-05 07:30:19 +00:00
|
|
|
var object = new context.node.cloneNode (false);
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-11-05 07:30:19 +00:00
|
|
|
var events = context.events;
|
2015-10-14 11:51:43 +00:00
|
|
|
|
|
|
|
for (var event in events)
|
2015-11-05 07:30:19 +00:00
|
|
|
object.addEventListener (event,
|
2015-10-14 11:51:43 +00:00
|
|
|
events[event].bind (this.signalData));
|
2015-11-05 07:30:19 +00:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
,createHtmlLink: function (context, object)
|
|
|
|
{
|
|
|
|
var childs = context.childs;
|
|
|
|
|
|
|
|
if (childs)
|
|
|
|
for (var i = 0; i < childs.length; i++)
|
|
|
|
object.appendChild (this.objects[childs[i]]);
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++ Utilities
|
|
|
|
|
|
|
|
,translateValue: function (value)
|
|
|
|
{
|
|
|
|
var chr = value.charAt (0);
|
|
|
|
|
|
|
|
if (chr == '_')
|
|
|
|
return _(value.substr (1));
|
|
|
|
else if (chr == '\\' && value.charAt (1) == '_')
|
|
|
|
return value.substr (1);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
,getMethod: function (value)
|
|
|
|
{
|
|
|
|
if (this.signalData)
|
|
|
|
var methodName = 'this.signalData.'+ value;
|
|
|
|
else
|
|
|
|
var methodName = value;
|
|
|
|
|
|
|
|
var method;
|
|
|
|
|
|
|
|
try {
|
|
|
|
method = eval (methodName);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
method = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method == null)
|
|
|
|
console.warn ('Vn.Builder: Function \'%s\' not found',
|
|
|
|
value);
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
,getEventHandler: function (attribute, value)
|
|
|
|
{
|
|
|
|
if (!(/^on-\w+/.test (attribute)))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return this.getMethod (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
,replaceFunc: function (token)
|
|
|
|
{
|
|
|
|
return token.charAt(1).toUpperCase ();
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,setParent: function (parentBuilder)
|
|
|
|
{
|
|
|
|
this.parentBuilder = parentBuilder;
|
|
|
|
|
|
|
|
if (parentBuilder)
|
|
|
|
this.signalData = parentBuilder.signalData;
|
|
|
|
}
|
2015-07-03 05:49:45 +00:00
|
|
|
|
|
|
|
,$: function (objectId)
|
|
|
|
{
|
|
|
|
return this.get (objectId);
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
,get: function (objectId)
|
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
var object = this.objectMap[objectId];
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
return object;
|
|
|
|
if (this.parentBuilder)
|
|
|
|
return this.parentBuilder.get (objectId);
|
|
|
|
|
|
|
|
return null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,getObjects: function (tagName)
|
|
|
|
{
|
|
|
|
if (this.tags[tagName])
|
|
|
|
return this.tags[tagName];
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2015-08-17 18:02:14 +00:00
|
|
|
,_destroy: function ()
|
2015-07-28 19:14:26 +00:00
|
|
|
{
|
|
|
|
for (var tag in this.tags)
|
|
|
|
{
|
|
|
|
var objects = this.tags[tag];
|
2015-08-17 18:02:14 +00:00
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
for (var i = 0; i < objects.length; i++)
|
|
|
|
objects[i].unref ();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parent ();
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|
|
|
|
|