1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-19 16:24:51 +00:00

Changed the signature of WikiStore.renderTiddler

The parameters are more sensible and predictable now
This commit is contained in:
Jeremy Ruston 2012-02-01 16:12:49 +00:00
parent 5b301dc30b
commit b0336c527c
4 changed files with 41 additions and 42 deletions

View File

@ -275,7 +275,7 @@ WikiStore.prototype.getCacheForTiddler = function(title,cacheName,initializer) {
// Clear all caches associated with a particular tiddler
WikiStore.prototype.clearCache = function(title) {
if(this.caches.hasOwnProperty("title")) {
if(this.caches.hasOwnProperty(title)) {
delete this.caches[title];
}
};
@ -338,26 +338,35 @@ WikiStore.prototype.renderText = function(type,text,targetType,asTitle) {
};
/*
Render a tiddler to a particular MIME type. Optionally render it with a different tiddler
as the context. This option is used to render a tiddler through a template eg
store.renderTiddler("text/html",templateTitle,tiddlerTitle)
Render a tiddler to a particular MIME type
targetType: target MIME type
title: title of the tiddler to render
template: optional title of the tiddler to use as a template
options: see below
Options include:
noWrap: Suppress the outer refresh wrapper nodes
*/
WikiStore.prototype.renderTiddler = function(targetType,title,asTitle,options) {
WikiStore.prototype.renderTiddler = function(targetType,title,templateTitle,options) {
options = options || {};
if(typeof templateTitle !== "string") {
templateTitle = title;
}
var stitcher = ((targetType === "text/html") && !options.noWrap && !this.disableHtmlWrapperNodes) ? utils.stitchElement : function(a,b,c) {return c.content;},
tiddler = this.getTiddler(title),
renderer = this.compileTiddler(title,targetType),
renditions = this.getCacheForTiddler(title,"renditions",function() {
renderer = this.compileTiddler(templateTitle,targetType),
renditions = this.getCacheForTiddler(templateTitle,"renditions",function() {
return {};
});
if(tiddler) {
if(asTitle && asTitle !== title) {
var asTiddler = this.getTiddler(asTitle);
if(title !== templateTitle) {
var template = this.getTiddler(templateTitle);
return stitcher("div",{
"data-tw-render-tiddler": title,
"data-tw-render-as": asTitle
"data-tw-render-template": templateTitle
},{
content: renderer.render(asTiddler,this)
content: renderer.render(tiddler,this)
});
} else {
if(!renditions[targetType]) {
@ -421,18 +430,20 @@ WikiStore.prototype.refreshDomNode = function(node,changes,renderer,tiddler) {
};
// Get all the various attributes we need
var renderTiddler = node.getAttribute ? node.getAttribute("data-tw-render-tiddler") : null,
renderAs = node.getAttribute ? node.getAttribute("data-tw-render-as") : null,
renderTemplate = node.getAttribute ? node.getAttribute("data-tw-render-template") : null,
macro = node.getAttribute ? node.getAttribute("data-tw-macro") : null,
renderStep = node.getAttribute ? node.getAttribute("data-tw-render-step") : null;
// Is this node the rendering of a tiddler?
if(renderTiddler !== null) {
// Rerender the content of the node if the tiddler being rendered has changed
if((renderTiddler in changes) || (renderAs && renderAs in changes)) {
node.innerHTML = this.renderTiddler("text/html",renderTiddler,renderAs,{noWrap: true});
if(changes.hasOwnProperty(renderTiddler) || (renderTemplate && changes.hasOwnProperty(renderTemplate))) {
node.innerHTML = this.renderTiddler("text/html",renderTiddler,renderTemplate,{noWrap: true});
} else {
// If it hasn't changed, just refresh the child nodes
var asTiddler = renderAs !== null ? this.getTiddler(renderAs) : this.getTiddler(renderTiddler);
refreshChildNodes(node,this.compileTiddler(renderTiddler,"text/html"),asTiddler);
if(typeof renderTemplate !== "string") {
renderTemplate = renderTiddler;
}
refreshChildNodes(node,this.compileTiddler(renderTemplate,"text/html"),this.getTiddler(renderTiddler));
}
// Is this node a macro
} else if(macro !== null) {

View File

@ -25,13 +25,9 @@ var parseStory = function(storyText) {
// Search the children of a node looking for the required tiddler rendering
var searchTiddlerNode = function(node,renderTiddler,renderTemplate) {
var renderAs;
if(renderTemplate) {
renderAs = renderTiddler;
renderTiddler = renderTemplate;
}
while(node !== null) {
if(node.getAttribute && node.getAttribute("data-tw-render-tiddler") === renderTiddler) {
if(!renderAs || (renderAs && node.getAttribute("data-tw-render-as") == renderAs)) {
if(!renderTemplate || (renderTemplate && node.getAttribute("data-tw-render-template") == renderTemplate)) {
return node;
}
}
@ -52,7 +48,7 @@ exports.macro = {
output = [];
for(var t=0; t<tiddlers.length; t++) {
if(params.template) {
output.push(store.renderTiddler(type,params.template,tiddlers[t]));
output.push(store.renderTiddler(type,tiddlers[t],params.template));
} else {
output.push(store.renderTiddler(type,tiddlers[t]));
}
@ -71,9 +67,7 @@ exports.macro = {
if(tiddlerNode === null) {
// If not, render the tiddler
var tmpNode = document.createElement("div");
tmpNode.innerHTML = params.template ?
store.renderTiddler(type,params.template,targetTiddlers[t]) :
store.renderTiddler(type,targetTiddlers[t]);
tmpNode.innerHTML = store.renderTiddler(type,targetTiddlers[t],params.template);
tiddlerNode = tmpNode.firstChild;
node.insertBefore(tiddlerNode,currNode);
} else {

View File

@ -54,33 +54,27 @@ exports.macro = {
"with": {byName: true, type: "text", optional: true, dependentAll: true}
},
render: function(type,tiddler,store,params) {
var renderTitle = params.template,
renderAs = params.target;
// Slightly bewildering logic to sort out the optional parameters
if(renderAs === undefined) {
if(renderTitle === undefined) {
return "";
} else {
renderAs = tiddler.title;
}
} else {
if(renderTitle === undefined) {
renderTitle = renderAs;
}
var renderTitle = params.target,
renderTemplate = params.template;
if(typeof renderTitle !== "string") {
renderTitle = tiddler.title;
}
if(typeof renderTemplate !== "string") {
renderTemplate = renderTitle;
}
if(params["with"]) {
// Parameterised transclusion
var targetTiddler = store.getTiddler(renderTitle),
var targetTiddler = store.getTiddler(renderTemplate),
text = targetTiddler.text;
var withTokens = [params["with"]];
for(var t=0; t<withTokens.length; t++) {
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
text = text.replace(placeholderRegExp,withTokens[t]);
}
return store.renderText(targetTiddler.type,text,type,renderAs);
return store.renderText(targetTiddler.type,text,type,renderTitle);
} else {
// There's no parameterisation, so we can just render the target tiddler directly
return store.renderTiddler(type,renderTitle,renderAs);
return store.renderTiddler(type,renderTitle,renderTemplate);
}
}
};

View File

@ -1 +1 @@
<div data-tw-render-tiddler='EighthTiddler'><div data-tw-macro='tiddler' data-tw-render-step='1'><div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='2'><div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='3'><div data-tw-render-tiddler='SixthTiddler' data-tw-render-as='EighthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>EighthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'></div><br><div data-tw-macro='view' data-tw-render-step='3'></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='4'><div data-tw-render-tiddler='SixthTiddler' data-tw-render-as='FirstTiddler'><div data-tw-macro='view' data-tw-render-step='1'>FirstTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'></div><br><div data-tw-macro='view' data-tw-render-step='3'></div><br></div></div><br></div>
<div data-tw-render-tiddler='EighthTiddler'><div data-tw-macro='tiddler' data-tw-render-step='1'><div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='2'><div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='3'><div data-tw-render-tiddler='EighthTiddler' data-tw-render-template='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>EighthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'></div><br><div data-tw-macro='view' data-tw-render-step='3'></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='4'><div data-tw-render-tiddler='FirstTiddler' data-tw-render-template='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>FirstTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'></div><br><div data-tw-macro='view' data-tw-render-step='3'></div><br></div></div><br></div>