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
|
|
|
|
({
|
|
|
|
objectMap: {}
|
|
|
|
,tags: {}
|
|
|
|
|
|
|
|
,destroy: function ()
|
|
|
|
{
|
|
|
|
for (var key in this.objectMap)
|
|
|
|
{
|
|
|
|
var object = this.objectMap[key];
|
|
|
|
|
|
|
|
if (object.destroy instanceof Function)
|
|
|
|
this.objectMap[key].destroy ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (/on-\w+/.test (nodeName))
|
|
|
|
{
|
|
|
|
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-03-27 19:10:49 +00:00
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
,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;
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,getMethod: function (value)
|
|
|
|
{
|
|
|
|
if (this.signalData)
|
|
|
|
var methodName = 'this.signalData.'+ value;
|
|
|
|
else
|
|
|
|
var methodName = value;
|
|
|
|
|
|
|
|
return eval (methodName);
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
,replaceFunc: function (token)
|
|
|
|
{
|
|
|
|
return token.charAt(1).toUpperCase ();
|
|
|
|
}
|
|
|
|
|
|
|
|
,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-03-27 19:10:49 +00:00
|
|
|
var nodeName = a[j].nodeName;
|
|
|
|
var nodeValue = a[j].nodeValue;
|
|
|
|
|
|
|
|
if (/on-\w+/.test (nodeName))
|
|
|
|
{
|
|
|
|
var method = this.getMethod (nodeValue);
|
|
|
|
c.object.on (nodeName.substr (3), method, this.signalData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var prop = nodeName.replace (/-./g, this.replaceFunc);
|
|
|
|
this.setProperty (c, prop, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
,setProperty: function (c, propName, value)
|
|
|
|
{
|
|
|
|
var prop = c.klass.Properties[propName];
|
|
|
|
|
|
|
|
if (!prop || !value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (prop.type)
|
|
|
|
{
|
|
|
|
case Boolean:
|
|
|
|
value = (/^(true|1)$/i).test (value);
|
|
|
|
break;
|
|
|
|
case Number:
|
|
|
|
value = 0 + new Number (value);
|
|
|
|
break;
|
|
|
|
case String:
|
2015-06-30 12:06:19 +00:00
|
|
|
case Object:
|
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-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
,setParent: function (parentBuilder)
|
|
|
|
{
|
|
|
|
this.parentBuilder = parentBuilder;
|
|
|
|
|
|
|
|
if (parentBuilder)
|
|
|
|
this.signalData = parentBuilder.signalData;
|
|
|
|
}
|
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 [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|