1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-06 04:03:16 +00:00

Update the tiddler macro to have a templateText parameter

This commit is contained in:
Jeremy Ruston 2012-10-15 18:46:47 +01:00
parent 4ad8859864
commit 1c699c7118

View File

@ -51,6 +51,7 @@ exports.info = {
params: { params: {
target: {byName: "default", type: "tiddler"}, target: {byName: "default", type: "tiddler"},
template: {byName: true, type: "tiddler"}, template: {byName: true, type: "tiddler"},
templateText: {byName: true, type: "text"},
"with": {byName: true, type: "text", dependentAll: true} "with": {byName: true, type: "text", dependentAll: true}
} }
}; };
@ -70,75 +71,73 @@ exports.evaluateDependencies = function() {
}; };
exports.executeMacro = function() { exports.executeMacro = function() {
var renderTitle = this.params.target, var renderTitle, renderTemplateTitle, renderTemplateTree,
renderTemplate = this.params.template, children, parseTree,
children,
childrenClone = [],
t, t,
parents = this.parents.slice(0), parents = this.parents.slice(0),
parseOptions = {}; parseOptions = {};
// If there's no render title specified then use the current tiddler title // If there's no render title specified then use the current tiddler title
if(typeof renderTitle !== "string") { if(this.hasParameter("target")) {
renderTitle = this.params.target;
} else {
renderTitle = this.tiddlerTitle; renderTitle = this.tiddlerTitle;
} }
// If there's no template specified then use the target tiddler title // Get the render tree for the template
if(typeof renderTemplate !== "string") { if(this.hasParameter("templateText")) {
renderTemplate = renderTitle; renderTemplateTree = this.wiki.parseText("text/x-tiddlywiki",this.params.templateText).tree;
}
// Check for recursion
if(parents.indexOf(renderTemplate) !== -1) {
children = [$tw.Tree.errorNode("Tiddler recursion error in <<tiddler>> macro")];
} else { } else {
// Check for substitution parameters if(this.hasParameter("template")) {
if(this.hasParameter("with")) { renderTemplateTitle = this.params.template;
parseOptions["with"] = [undefined,this.params["with"]]; // TODO: Allow for more than one with: parameter } else {
renderTemplateTitle = renderTitle;
}
// Check for recursion
if(parents.indexOf(this.params.templateTitle) !== -1) {
renderTemplateTree = $tw.Tree.errorNode("Tiddler recursion error in <<transclude>> macro");
} else {
parents.push(renderTemplateTitle);
renderTemplateTree = [];
// Check for substitution parameters
if(this.hasParameter("with")) {
parseOptions["with"] = [undefined,this.params["with"]]; // TODO: Allow for more than one with: parameter
}
parseTree = this.wiki.parseTiddler(renderTemplateTitle,parseOptions);
children = parseTree ? parseTree.tree : [];
for(t=0; t<children.length; t++) {
renderTemplateTree.push(children[t].clone());
}
} }
// Render the target tiddler
var parseTree = this.wiki.parseTiddler(renderTemplate,parseOptions);
children = parseTree ? parseTree.tree : [];
}
// Update the stack of tiddler titles for recursion detection
parents.push(renderTemplate);
// Clone the children
for(t=0; t<children.length; t++) {
childrenClone.push(children[t].clone());
} }
// Execute macros within the children // Execute macros within the children
for(t=0; t<childrenClone.length; t++) { for(t=0; t<renderTemplateTree.length; t++) {
childrenClone[t].execute(parents,renderTitle); renderTemplateTree[t].execute(parents,renderTitle);
} }
// Set up the attributes for the wrapper element // Set up the attributes for the wrapper element
var attributes = { var attributes = {
"data-tiddler-target": renderTitle,
"data-tiddler-template": renderTemplate,
"class": ["tw-tiddler-frame"] "class": ["tw-tiddler-frame"]
}; };
if(!this.wiki.tiddlerExists(renderTitle)) { if(!this.wiki.tiddlerExists(renderTitle)) {
attributes["class"].push("tw-tiddler-missing"); attributes["class"].push("tw-tiddler-missing");
} }
// Return the children // Return the children
return $tw.Tree.Element(this.isBlock ? "div" : "span",attributes,childrenClone); return $tw.Tree.Element(this.isBlock ? "div" : "span",attributes,renderTemplateTree);
}; };
exports.refreshInDom = function(changes) { exports.refreshInDom = function(changes) {
var t; var renderTitle;
// Set the class for missing tiddlers // Set the class for missing tiddlers
var renderTitle = this.params.target; if(this.hasParameter("target")) {
if(typeof renderTitle !== "string") { renderTitle = this.params.target;
renderTitle = this.params.template; } else {
renderTitle = this.tiddlerTitle;
} }
if(renderTitle) { if(renderTitle) {
$tw.utils.toggleClass(this.child.domNode,"tw-tiddler-missing",!this.wiki.tiddlerExists(renderTitle)); $tw.utils.toggleClass(this.child.domNode,"tw-tiddler-missing",!this.wiki.tiddlerExists(renderTitle));
} }
// Rerender the tiddler if it is impacted by the changes // Rerender the tiddler if it is impacted by the changes
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) { if(this.dependencies.hasChanged(changes,this.renderTitle)) {
// Manually reexecute and rerender this macro // Manually reexecute and rerender this macro
var parent = this.child.domNode.parentNode, this.reexecuteInDom();
nextSibling = this.child.domNode.nextSibling;
parent.removeChild(this.child.domNode);
this.execute(this.parents,this.tiddlerTitle);
this.child.renderInDom(parent,nextSibling);
this.domNode = this.child.domNode;
} else { } else {
this.child.refreshInDom(changes); this.child.refreshInDom(changes);
} }