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

Beginnings of refactoring the transclude macro

We want to make it selectively refresh for better performance
This commit is contained in:
Jeremy Ruston 2012-10-15 18:46:33 +01:00
parent 80cbe2a98f
commit 4ad8859864

View File

@ -18,30 +18,33 @@ exports.info = {
params: {
filter: {byPos: 0, type: "filter"},
title: {byPos: 1, type: "tiddler"},
templateTitle: {byName: true, type: "text"},
templateTitle: {byName: true, type: "tiddler"},
templateText: {byName: true, type: "text"},
emptyMessage: {byName: true, type: "text"}
}
};
exports.executeMacro = function() {
console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle);
var titles,templateTiddler,templateText,t,title,templateParseTree,
nodes,node,c,
parents = this.parents.slice(0);
parents.push(this.tiddlerTitle);
// Get the tiddlers we're transcluding
/*
Return the list of tiddlers to be transcluded
*/
exports.getTiddlerList = function() {
if(this.hasParameter("filter")) {
titles = this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle);
return this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle);
} else if(this.hasParameter("title")) {
titles = [this.params.title];
return [this.params.title];
} else {
titles = [this.tiddlerTitle];
return [this.tiddlerTitle];
}
// Get the template
};
/*
Get the parse tree of the template text to be used
parents: array of tiddler titles of parents in the render tree
*/
exports.getTemplateParseTree = function(parents) {
if(this.hasParameter("templateText")) {
// Parse the template
templateParseTree = this.wiki.parseText("text/x-tiddlywiki",this.params.templateText);
return this.wiki.parseText("text/x-tiddlywiki",this.params.templateText);
} else {
if(this.hasParameter("templateTitle")) {
// Check for recursion
@ -49,13 +52,26 @@ console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle);
return $tw.Tree.errorNode("Tiddler recursion error in <<transclude>> macro");
}
parents.push(this.params.templateTitle);
templateParseTree = this.wiki.parseTiddler(this.params.templateTitle);
return this.wiki.parseTiddler(this.params.templateTitle);
} else {
templateParseTree = this.wiki.parseText("text/x-tiddlywiki","<<view text wikified>>");
return this.wiki.parseText("text/x-tiddlywiki","<<view text wikified>>");
}
}
};
exports.executeMacro = function() {
console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle);
var templateTiddler,templateText,t,title,templateParseTree,
nodes,node,c,
parents = this.parents.slice(0);
// Clear the tiddler list
this.tiddlerList = this.getTiddlerList();
// Ensure we don't recurse back into ourselves
parents.push(this.tiddlerTitle);
// Get the template
templateParseTree = this.getTemplateParseTree(parents);
// Use the empty message if the list is empty
if(titles.length === 0 && this.hasParameter("emptyMessage")) {
if(this.tiddlerList.length === 0 && this.hasParameter("emptyMessage")) {
nodes = this.wiki.parseText("text/x-tiddlywiki",this.params.emptyMessage).tree;
for(c=0; c<nodes.length; c++) {
nodes[c].execute(this.parents,this.tiddlerTitle);
@ -63,8 +79,8 @@ console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle);
} else {
// Render the tiddlers through the template
nodes = [];
for(t=0; t<titles.length; t++) {
title = titles[t];
for(t=0; t<this.tiddlerList.length; t++) {
title = this.tiddlerList[t];
for(c=0; c<templateParseTree.tree.length; c++) {
node = templateParseTree.tree[c].clone();
node.execute(parents,title);
@ -80,4 +96,19 @@ console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle);
return $tw.Tree.Element("div",attributes,nodes);
};
exports.refreshInDom = function(changes) {
console.log("Refreshing transclude macro",this.params.filter,this.tiddlerTitle);
var doRefresh = false;
// Do a refresh if any of our parameters have changed
doRefresh = doRefresh || (this.hasParameter("templateTitle") && $tw.utils.hop(changes,this.params.templateTitle));
// Check if we need to do a full refresh
if(doRefresh) {
// Re-execute the macro to refresh it
this.reexecuteInDom();
} else {
// Do a selective refresh
}
};
})();