hedera-web/web/js/vn/builder.js

355 lines
6.5 KiB
JavaScript
Raw Normal View History

/**
2015-03-06 23:33:54 +00:00
* Creates a object from a XML specification.
**/
2015-11-09 08:14:33 +00:00
Vn.BuilderOld = new Class
({
Extends: Vn.Object
,objectMap: {}
,tags: {}
2015-10-14 11:51:43 +00:00
2015-11-09 08:14:33 +00:00
,load: function ()
{
return this;
}
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-09 08:36:54 +00:00
,loadXmlFromNode: function (node)
{
this.contexts = [];
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;
}
,loadNode: function (node)
{
var customNode;
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);
}
2015-11-09 08:14:33 +00:00
else if ((customNode = this.loadCustomNode (node, tagName, null)))
{
if (customNode instanceof Htk.Widget)
htmlNode = customNode.getNode ();
}
2015-03-06 23:33:54 +00:00
else
{
htmlNode = document.createElement (tagName);
var a = node.attributes;
for (var i = 0; i < a.length; i++)
{
var nodeName = a[i].nodeName;
var nodeValue = a[i].nodeValue;
2015-07-07 15:27:47 +00:00
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
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]);
2015-03-06 23:33:54 +00:00
if (htmlChild)
2015-03-06 23:33:54 +00:00
htmlNode.appendChild (htmlChild);
}
}
2015-03-06 23:33:54 +00:00
return htmlNode;
}
2015-11-09 08:14:33 +00:00
,loadCustomNode: function (node, tagName, parentContext)
{
2015-11-09 08:14:33 +00:00
if (!tagName)
return null;
2015-11-09 08:14:33 +00:00
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
2015-11-09 08:14:33 +00:00
,custom: null
};
this.contexts.push (context);
var nodeId = node.getAttribute ('id');
if (nodeId)
this.objectMap[nodeId] = customNode;
var childs = node.childNodes;
if (childs)
for (var i = 0; i < childs.length; i++)
2015-11-09 08:14:33 +00:00
{
var childTagName = childs[i].tagName;
if (!childTagName)
continue;
childTagName = childTagName.toLowerCase ();
var customChild = this.loadCustomNode (
childs[i], childTagName, context);
2015-11-09 08:14:33 +00:00
if (!customChild && childTagName === 'custom')
context.custom = childs[i];
}
return customNode;
}
,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-11-09 08:14:33 +00:00
if (c.custom)
c.object.loadXml (this, c.custom);
if (c.parent)
{
var parentProperty = c.node.getAttribute ('property');
2015-11-09 08:14:33 +00:00
if (parentProperty)
this.setProperty (c.parent, parentProperty, c.object);
2015-11-09 08:14:33 +00:00
else
c.parent.object.appendChild (c.object);
}
}
}
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-07-07 15:27:47 +00:00
,setProperty: function (c, attribute, value)
{
2015-07-07 15:27:47 +00:00
var propName = attribute.replace (/-./g, this.replaceFunc);
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)
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:
{
var method = this.getMethod (value);
value = method ? method.bind (this.signalData) : null;
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;
2015-07-10 12:30:08 +00:00
else
console.warn ('Vn.Builder: Empty attribute \'%s\' on tag \'%s\'',
attribute, c.node.tagName);
}
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 ();
}
,setParent: function (parentBuilder)
{
this.parentBuilder = parentBuilder;
if (parentBuilder)
this.signalData = parentBuilder.signalData;
}
2015-07-03 05:49:45 +00:00
,$: function (objectId)
{
2015-11-09 08:14:33 +00:00
return this.getById (objectId);
2015-07-03 05:49:45 +00:00
}
2015-11-09 08:14:33 +00:00
,getById: function (objectId)
{
var object = this.objectMap[objectId];
if (object)
return object;
if (this.parentBuilder)
return this.parentBuilder.get (objectId);
return null;
}
2015-11-09 08:14:33 +00:00
,getByTagName: function (tagName)
{
if (this.tags[tagName])
return this.tags[tagName];
return [];
}
2015-08-17 18:02:14 +00:00
,_destroy: function ()
{
for (var tag in this.tags)
{
var objects = this.tags[tag];
2015-08-17 18:02:14 +00:00
for (var i = 0; i < objects.length; i++)
objects[i].unref ();
}
this.parent ();
}
});