2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/treenodes/element.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: treenode
|
|
|
|
|
|
|
|
Element nodes
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-05-04 17:49:04 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Node = require("./node.js").Node;
|
|
|
|
|
2012-06-14 16:15:38 +00:00
|
|
|
var Element = function(type,attributes,children,options) {
|
|
|
|
options = options || {};
|
2012-04-30 11:23:03 +00:00
|
|
|
if(this instanceof Element) {
|
|
|
|
this.type = type;
|
|
|
|
this.attributes = attributes || {};
|
2012-05-02 16:26:55 +00:00
|
|
|
this.children = children;
|
2012-06-14 16:15:38 +00:00
|
|
|
this.events = options.events;
|
|
|
|
this.eventHandler = options.eventHandler;
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-06-14 16:15:38 +00:00
|
|
|
return new Element(type,attributes,children,options);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype = new Node();
|
|
|
|
Element.prototype.constructor = Element;
|
|
|
|
|
|
|
|
Element.prototype.clone = function() {
|
2012-05-02 16:26:55 +00:00
|
|
|
var childClones;
|
|
|
|
if(this.children) {
|
|
|
|
childClones = [];
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
childClones.push(this.children[t].clone());
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-06-14 16:15:38 +00:00
|
|
|
return new Element(this.type,this.attributes,childClones,{
|
|
|
|
events: this.events,
|
|
|
|
eventHandler: this.eventHandler
|
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.execute = function(parents,tiddlerTitle) {
|
2012-05-02 16:26:55 +00:00
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].execute(parents,tiddlerTitle);
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.render = function(type) {
|
|
|
|
var isHtml = type === "text/html",
|
2012-05-08 16:42:49 +00:00
|
|
|
output = [],attr,a,v;
|
2012-04-30 11:23:03 +00:00
|
|
|
if(isHtml) {
|
|
|
|
output.push("<",this.type);
|
|
|
|
if(this.attributes) {
|
2012-05-08 18:33:04 +00:00
|
|
|
attr = [];
|
2012-05-08 16:42:49 +00:00
|
|
|
for(a in this.attributes) {
|
|
|
|
attr.push(a);
|
|
|
|
}
|
|
|
|
attr.sort();
|
|
|
|
for(a=0; a<attr.length; a++) {
|
|
|
|
v = this.attributes[attr[a]];
|
2012-04-30 11:23:03 +00:00
|
|
|
if(v !== undefined) {
|
|
|
|
if($tw.utils.isArray(v)) {
|
|
|
|
v = v.join(" ");
|
|
|
|
} else if(typeof v === "object") {
|
|
|
|
var s = [];
|
|
|
|
for(var p in v) {
|
|
|
|
s.push(p + ":" + v[p] + ";");
|
|
|
|
}
|
|
|
|
v = s.join("");
|
|
|
|
}
|
2012-05-08 16:42:49 +00:00
|
|
|
output.push(" ",attr[a],"='",$tw.utils.htmlEncode(v),"'");
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.push(">");
|
|
|
|
}
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
output.push(this.children[t].render(type));
|
|
|
|
}
|
|
|
|
if(isHtml) {
|
|
|
|
output.push("</",this.type,">");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
2012-06-14 16:15:38 +00:00
|
|
|
// Create the element
|
2012-04-30 11:23:03 +00:00
|
|
|
var element = document.createElement(this.type);
|
2012-06-14 16:15:38 +00:00
|
|
|
// Assign the attributes
|
2012-04-30 11:23:03 +00:00
|
|
|
if(this.attributes) {
|
|
|
|
for(var a in this.attributes) {
|
|
|
|
var v = this.attributes[a];
|
|
|
|
if(v !== undefined) {
|
|
|
|
if($tw.utils.isArray(v)) { // Ahem, could there be arrays other than className?
|
|
|
|
element.className = v.join(" ");
|
|
|
|
} else if (typeof v === "object") { // ...or objects other than style?
|
|
|
|
for(var p in v) {
|
2012-06-11 09:41:13 +00:00
|
|
|
element.style[$tw.utils.unHyphenateCss(p)] = v[p];
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.setAttribute(a,v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 16:15:38 +00:00
|
|
|
// Insert it into the DOM tree
|
2012-04-30 11:23:03 +00:00
|
|
|
if(insertBefore) {
|
|
|
|
parentDomNode.insertBefore(element,insertBefore);
|
|
|
|
} else {
|
|
|
|
parentDomNode.appendChild(element);
|
|
|
|
}
|
2012-06-14 16:15:38 +00:00
|
|
|
// Register event handlers
|
|
|
|
if(this.events) {
|
|
|
|
for(var e=0; e<this.events.length; e++) {
|
|
|
|
element.addEventListener(this.events[e],this.eventHandler,false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save a reference to the DOM element
|
2012-04-30 11:23:03 +00:00
|
|
|
this.domNode = element;
|
2012-06-14 16:15:38 +00:00
|
|
|
// Render any child nodes
|
2012-04-30 11:23:03 +00:00
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].renderInDom(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.refresh = function(changes) {
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].refresh(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.refreshInDom = function(changes) {
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].refreshInDom(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-26 17:30:32 +00:00
|
|
|
Element.prototype.addClass = function(className) {
|
|
|
|
if(typeof this.attributes["class"] === "string") {
|
|
|
|
this.attributes["class"] = this.attributes["class"].split(" ");
|
|
|
|
}
|
|
|
|
this.attributes["class"] = this.attributes["class"] || [];
|
|
|
|
this.attributes["class"].push(className);
|
|
|
|
};
|
|
|
|
|
2012-11-03 20:34:39 +00:00
|
|
|
Element.prototype.addStyles = function(styles) {
|
2012-06-14 17:18:32 +00:00
|
|
|
this.attributes.style = this.attributes.style || {};
|
|
|
|
for(var t in styles) {
|
|
|
|
this.attributes.style[t] = styles[t];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
exports.Element = Element;
|
|
|
|
|
|
|
|
})();
|