2012-06-19 07:57:29 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/macros/transclude.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
|
|
|
Transclude macro
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "transclude",
|
2012-10-15 16:18:32 +00:00
|
|
|
dependentAll: true, // Tiddlers containing <<transclude>> macro are dependent on every tiddler
|
2012-06-19 07:57:29 +00:00
|
|
|
params: {
|
|
|
|
filter: {byPos: 0, type: "filter"},
|
|
|
|
title: {byPos: 1, type: "tiddler"},
|
2012-10-15 18:02:49 +00:00
|
|
|
template: {byName: true, type: "tiddler"},
|
2012-06-19 09:40:30 +00:00
|
|
|
templateText: {byName: true, type: "text"},
|
|
|
|
emptyMessage: {byName: true, type: "text"}
|
2012-06-19 07:57:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-15 17:46:33 +00:00
|
|
|
/*
|
|
|
|
Return the list of tiddlers to be transcluded
|
|
|
|
*/
|
|
|
|
exports.getTiddlerList = function() {
|
2012-06-19 07:57:29 +00:00
|
|
|
if(this.hasParameter("filter")) {
|
2012-10-15 17:46:33 +00:00
|
|
|
return this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle);
|
2012-06-19 07:57:29 +00:00
|
|
|
} else if(this.hasParameter("title")) {
|
2012-10-15 17:46:33 +00:00
|
|
|
return [this.params.title];
|
2012-06-19 07:57:29 +00:00
|
|
|
} else {
|
2012-10-15 17:46:33 +00:00
|
|
|
return [this.tiddlerTitle];
|
2012-06-19 07:57:29 +00:00
|
|
|
}
|
2012-10-15 17:46:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
2012-06-19 07:57:29 +00:00
|
|
|
if(this.hasParameter("templateText")) {
|
|
|
|
// Parse the template
|
2012-10-15 17:46:33 +00:00
|
|
|
return this.wiki.parseText("text/x-tiddlywiki",this.params.templateText);
|
2012-06-19 07:57:29 +00:00
|
|
|
} else {
|
2012-10-15 18:02:49 +00:00
|
|
|
if(this.hasParameter("template")) {
|
2012-06-19 07:57:29 +00:00
|
|
|
// Check for recursion
|
2012-10-15 18:02:49 +00:00
|
|
|
if(parents.indexOf(this.params.template) !== -1) {
|
2012-06-19 07:57:29 +00:00
|
|
|
return $tw.Tree.errorNode("Tiddler recursion error in <<transclude>> macro");
|
2012-06-19 09:59:58 +00:00
|
|
|
}
|
2012-10-15 18:02:49 +00:00
|
|
|
parents.push(this.params.template);
|
|
|
|
return this.wiki.parseTiddler(this.params.template);
|
2012-06-19 07:57:29 +00:00
|
|
|
} else {
|
2012-10-15 17:46:33 +00:00
|
|
|
return this.wiki.parseText("text/x-tiddlywiki","<<view text wikified>>");
|
2012-06-19 07:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 17:46:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2012-06-19 09:40:30 +00:00
|
|
|
// Use the empty message if the list is empty
|
2012-10-15 17:46:33 +00:00
|
|
|
if(this.tiddlerList.length === 0 && this.hasParameter("emptyMessage")) {
|
2012-06-19 09:40:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Render the tiddlers through the template
|
|
|
|
nodes = [];
|
2012-10-15 17:46:33 +00:00
|
|
|
for(t=0; t<this.tiddlerList.length; t++) {
|
|
|
|
title = this.tiddlerList[t];
|
2012-06-19 09:40:30 +00:00
|
|
|
for(c=0; c<templateParseTree.tree.length; c++) {
|
|
|
|
node = templateParseTree.tree[c].clone();
|
|
|
|
node.execute(parents,title);
|
|
|
|
nodes.push(node);
|
|
|
|
}
|
2012-06-19 07:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return
|
|
|
|
var attributes = {"class": ["tw-transclusion"]};
|
|
|
|
if(this.classes) {
|
|
|
|
$tw.utils.pushTop(attributes["class"],this.classes);
|
|
|
|
}
|
|
|
|
return $tw.Tree.Element("div",attributes,nodes);
|
|
|
|
};
|
|
|
|
|
2012-10-15 17:46:33 +00:00
|
|
|
|
|
|
|
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
|
2012-10-15 18:02:49 +00:00
|
|
|
doRefresh = doRefresh || (this.hasParameter("template") && $tw.utils.hop(changes,this.params.template));
|
2012-10-15 17:46:33 +00:00
|
|
|
// 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
|
2012-10-16 09:14:11 +00:00
|
|
|
this.reexecuteInDom();
|
2012-10-15 17:46:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-19 07:57:29 +00:00
|
|
|
})();
|