1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 17:10:29 +00:00

Refactor things so that widgets can opt not to generate an element

They have to have a single child renderer instead
This commit is contained in:
Jeremy Ruston 2013-01-03 20:51:13 +00:00
parent 1c559660d9
commit 89d44e27df

View File

@ -114,70 +114,82 @@ ElementRenderer.prototype.getAttribute = function(name,defaultValue) {
}; };
ElementRenderer.prototype.render = function(type) { ElementRenderer.prototype.render = function(type) {
var isHtml = type === "text/html", // Check if our widget is providing an element
output = [],attr,a,v; if(this.widget.tag) {
if(isHtml) { var isHtml = type === "text/html",
output.push("<",this.widget.tag); output = [],attr,a,v;
if(this.widget.attributes) { if(isHtml) {
attr = []; output.push("<",this.widget.tag);
for(a in this.widget.attributes) { if(this.widget.attributes) {
attr.push(a); attr = [];
} for(a in this.widget.attributes) {
attr.sort(); attr.push(a);
for(a=0; a<attr.length; a++) { }
v = this.widget.attributes[attr[a]]; attr.sort();
if(v !== undefined) { for(a=0; a<attr.length; a++) {
if($tw.utils.isArray(v)) { v = this.widget.attributes[attr[a]];
v = v.join(" "); if(v !== undefined) {
} else if(typeof v === "object") { if($tw.utils.isArray(v)) {
var s = []; v = v.join(" ");
for(var p in v) { } else if(typeof v === "object") {
s.push(p + ":" + v[p] + ";"); var s = [];
for(var p in v) {
s.push(p + ":" + v[p] + ";");
}
v = s.join("");
} }
v = s.join(""); output.push(" ",attr[a],"='",$tw.utils.htmlEncode(v),"'");
} }
output.push(" ",attr[a],"='",$tw.utils.htmlEncode(v),"'");
} }
} }
} if(!this.widget.children || this.widget.children.length === 0) {
if(!this.widget.children || this.widget.children.length === 0) { output.push("/");
output.push("/");
}
output.push(">");
}
if(this.widget.children && this.widget.children.length > 0) {
$tw.utils.each(this.widget.children,function(node) {
if(node.render) {
output.push(node.render(type));
} }
}); output.push(">");
if(isHtml) {
output.push("</",this.widget.tag,">");
} }
if(this.widget.children && this.widget.children.length > 0) {
$tw.utils.each(this.widget.children,function(node) {
if(node.render) {
output.push(node.render(type));
}
});
if(isHtml) {
output.push("</",this.widget.tag,">");
}
}
return output.join("");
} else {
// Just render our first child if we're not generating an element
return this.widget.children[0].render(type);
} }
return output.join("");
}; };
ElementRenderer.prototype.renderInDom = function() { ElementRenderer.prototype.renderInDom = function() {
// Create the element // Check if our widget is providing an element
this.domNode = document.createElement(this.widget.tag); if(this.widget.tag) {
// Assign the attributes // Create the element
this.assignAttributes(); this.domNode = document.createElement(this.widget.tag);
// Render any child nodes // Assign any specified event handlers
var self = this; $tw.utils.addEventListeners(this.domNode,this.widget.events);
$tw.utils.each(this.widget.children,function(node) { // Assign the attributes
if(node.renderInDom) { this.assignAttributes();
self.domNode.appendChild(node.renderInDom()); // Render any child nodes
var self = this;
$tw.utils.each(this.widget.children,function(node) {
if(node.renderInDom) {
self.domNode.appendChild(node.renderInDom());
}
});
// Call postRenderInDom if the widget provides it
if(this.widget.postRenderInDom) {
this.widget.postRenderInDom();
} }
}); // Return the dom node
// Assign any specified event handlers return this.domNode;
$tw.utils.addEventListeners(this.domNode,this.widget.events); } else {
// Call postRenderInDom if the widget provides it // If we're not generating an element, just render our first child
if(this.widget.postRenderInDom) { return this.widget.children[0].renderInDom();
this.widget.postRenderInDom();
} }
// Return the dom node
return this.domNode;
}; };
ElementRenderer.prototype.assignAttributes = function() { ElementRenderer.prototype.assignAttributes = function() {