hedera-web/js/vn/component.js

146 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-10-18 16:01:21 +00:00
var NodeBuilder = require ('./node-builder');
var nativeEvents = {
click : 1
,mousedown : 1
,focusout : 1
};
module.exports = new Class
({
Extends: NodeBuilder
,Properties:
{
/**
* Main HTML node that represents the component
*/
node:
{
type: Object
,get: function ()
{
this.renderBase ();
return this._node;
}
},
/**
* CSS classes to be appendend to the node classes.
*/
class:
{
type: String
,set: function (x)
{
this._cssClass = x;
this._refreshClass ();
}
,get: function ()
{
return this._node.className;
}
},
/**
* The HTML id of the element.
*/
htmlId:
{
type: String
,set: function (x)
{
this._htmlId = x;
if (this._node)
this._node.id = x;
}
,get: function ()
{
return this._htmlId;
}
2017-10-20 17:09:06 +00:00
},
2017-10-18 16:01:21 +00:00
}
,_node: null
,scope: null
2017-10-20 17:09:06 +00:00
,$: {}
2017-10-18 16:01:21 +00:00
,initialize: function (props)
{
this.doc = document;
this.renderBase ();
this.parent (props);
}
,on: function (id, callback, instance)
{
if (nativeEvents[id])
this.node.addEventListener (id,
callback.bind (instance, this));
else
this.parent (id, callback, instance);
}
,createRoot: function (tagName)
{
this._node = this.createElement (tagName);
if (this.htmlId)
this._node.id = this.htmlId;
return this._node;
}
,renderBase: function ()
{
if (this._node)
return;
this.render ();
this._refreshClass ();
}
,_refreshClass: function ()
{
if (this._node && this._cssClass)
this._node.className = this._cssClass +' '+ this._node.className;
}
,remove: function ()
{
Vn.Node.remove (this._node);
}
,loadTemplateFromFile: function (path)
{
var builder = new Vn.Builder ();
builder.compileFile (path);
this.loadScope (builder);
}
,loadTemplateFromString: function (xmlString)
{
var builder = new Vn.Builder ();
builder.compileString (xmlString);
this.loadScope (builder);
}
,loadScope: function (builder, parentScope, extraObjects)
{
var scope = builder.load (this.doc, this, parentScope, extraObjects);
this.scope = scope;
scope.link ();
2017-10-20 17:09:06 +00:00
this.$ = scope.$;
this._node = this.$.main;
2017-10-18 16:01:21 +00:00
return scope;
}
,_destroy: function ()
{
2017-10-20 17:09:06 +00:00
if (this.scope)
this.scope.unref ();
2017-10-18 16:01:21 +00:00
this.parent ();
}
});