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

221 lines
4.0 KiB
JavaScript
Raw Normal View History

/**
2015-03-06 23:33:54 +00:00
* Creates a object from a XML specification.
**/
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
,getNode: function ()
{
return this.mainNode;
}
2015-03-06 23:33:54 +00:00
,loadXml: function (xmlDoc)
{
2015-03-06 23:33:54 +00:00
if (!xmlDoc)
return false;
var docElement = xmlDoc.documentElement;
if (docElement.tagName !== 'vn')
return false;
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-03-06 23:33:54 +00:00
,loadNode: function (node, parentContext)
{
var htmlNode = null;
2015-03-06 23:33:54 +00:00
var tagName = null;
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-03-06 23:33:54 +00:00
htmlNode = document.importNode (node, false);
}
else
{
var vnObject = null;
var context = null;
var htmlNode = null;
var klass = Vn.customTags[tagName];
var nodeId = node.getAttribute ('id');
2015-03-06 23:33:54 +00:00
if (klass)
{
var vnObject = new klass ();
if (vnObject instanceof Htk.Widget)
htmlNode = vnObject.getNode ();
2015-03-06 23:33:54 +00:00
if (!this.tags[tagName])
this.tags[tagName] = [];
2015-03-06 23:33:54 +00:00
this.tags[tagName].push (vnObject);
2015-03-06 23:33:54 +00:00
context = {
node: node
,parent: parentContext
,object: vnObject
,klass: klass
};
this.contexts.push (context);
}
else
{
htmlNode = document.createElement (tagName);
2015-03-06 23:33:54 +00:00
vnObject = htmlNode;
var a = node.attributes;
for (var i = 0; i < a.length; i++)
2015-03-06 23:33:54 +00:00
if (a[i].nodeName !== 'id')
htmlNode.setAttribute (a[i].nodeName,
this.translateValue (a[i].nodeValue));
}
2015-03-06 23:33:54 +00:00
if (nodeId)
this.objectMap[nodeId] = vnObject;
var childs = node.childNodes;
2015-03-06 23:33:54 +00:00
if (childs)
for (var i = 0; i < childs.length; i++)
2015-03-06 23:33:54 +00:00
{
var htmlChild =
this.loadNode (childs[i], context);
if (htmlChild && htmlNode)
htmlNode.appendChild (htmlChild);
}
}
2015-03-06 23:33:54 +00:00
return htmlNode;
}
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;
}
,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++)
{
var prop = a[j].nodeName.replace (/-./g, this.replaceFunc);
this.setProperty (c, prop, a[j].nodeValue);
}
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-03-06 23:33:54 +00:00
value = this.translateValue (value);
break;
case Function:
break;
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;
}
,get: function (objectId)
{
return this.objectMap[objectId];
}
,getObjects: function (tagName)
{
if (this.tags[tagName])
return this.tags[tagName];
return [];
}
});