2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/macros/tiddler.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
|
|
|
The tiddler macros transcludes another macro into the tiddler being rendered.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
target: the title of the tiddler to transclude
|
|
|
|
template: the title of the tiddler to use as a template for the transcluded tiddler
|
|
|
|
with: optional parameters to be substituted into the rendered tiddler
|
|
|
|
|
|
|
|
The simplest case is to just supply a target tiddler:
|
|
|
|
|
2012-10-26 13:01:27 +00:00
|
|
|
{{{
|
2012-04-30 11:23:03 +00:00
|
|
|
<<tiddler Foo>> or <<transclude target:Foo>>
|
2012-10-26 13:01:27 +00:00
|
|
|
}}}
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
This will render the tiddler Foo within the current tiddler. If the tiddler Foo includes
|
|
|
|
the view macro (or other macros that reference the fields of the current tiddler), then the
|
|
|
|
fields of the tiddler Foo will be accessed.
|
|
|
|
|
|
|
|
If you want to transclude the tiddler as a template, so that the fields referenced by the view
|
|
|
|
macro are those of the tiddler doing the transcluding, then you can instead specify the tiddler
|
|
|
|
as a template:
|
|
|
|
|
2012-10-26 13:01:27 +00:00
|
|
|
{{{
|
2012-04-30 11:23:03 +00:00
|
|
|
<<tiddler template:Foo>>
|
2012-10-26 13:01:27 +00:00
|
|
|
}}}
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
The effect is the same as the previous example: the text of the tiddler Foo is rendered. The
|
|
|
|
difference is that the view macro will access the fields of the tiddler doing the transcluding.
|
|
|
|
|
|
|
|
The `target` and `template` parameters may be combined:
|
|
|
|
|
2012-10-26 13:01:27 +00:00
|
|
|
{{{
|
2012-04-30 11:23:03 +00:00
|
|
|
<<tiddler target:Foo template:Bar>>
|
2012-10-26 13:01:27 +00:00
|
|
|
}}}
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
Here, the text of the tiddler `Bar` will be transcluded, with the macros within it accessing the fields
|
|
|
|
of the tiddler `Foo`.
|
|
|
|
|
|
|
|
Finally, the `with` parameter is used to substitute values for the special markers $1, $2, $3 etc. The
|
|
|
|
substitutions are performed on the tiddler whose text is being rendered: either the tiddler named in
|
|
|
|
the `template` parameter or, if that parameter is missing, the tiddler named in the `target` parameter.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-05-04 17:49:04 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "tiddler",
|
|
|
|
cascadeParams: true, // Cascade names of named parameters to following anonymous parameters
|
|
|
|
params: {
|
|
|
|
target: {byName: "default", type: "tiddler"},
|
2012-11-07 16:11:47 +00:00
|
|
|
targetVia: {byName: true, type: "tiddler"},
|
2012-04-30 11:23:03 +00:00
|
|
|
template: {byName: true, type: "tiddler"},
|
2012-10-15 17:46:47 +00:00
|
|
|
templateText: {byName: true, type: "text"},
|
2012-04-30 11:23:03 +00:00
|
|
|
"with": {byName: true, type: "text", dependentAll: true}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.evaluateDependencies = function() {
|
|
|
|
var dependencies = new $tw.Dependencies(),
|
|
|
|
template = this.srcParams.template;
|
|
|
|
if(template === undefined) {
|
|
|
|
template = this.srcParams.target;
|
|
|
|
}
|
|
|
|
if(typeof template === "function") {
|
|
|
|
dependencies.dependentAll = true;
|
|
|
|
} else {
|
|
|
|
dependencies.addDependency(template,true);
|
|
|
|
}
|
2012-11-07 16:11:47 +00:00
|
|
|
var targetVia = this.srcParams.targetVia;
|
|
|
|
if(typeof targetVia === "function") {
|
|
|
|
dependencies.dependentAll = true;
|
|
|
|
} else {
|
|
|
|
dependencies.addDependency(targetVia,true);
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
return dependencies;
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
exports.executeMacro = function() {
|
2012-10-15 17:46:47 +00:00
|
|
|
var renderTitle, renderTemplateTitle, renderTemplateTree,
|
|
|
|
children, parseTree,
|
2012-04-30 11:23:03 +00:00
|
|
|
t,
|
2012-08-25 13:16:33 +00:00
|
|
|
parents = this.parents.slice(0),
|
|
|
|
parseOptions = {};
|
2012-04-30 11:23:03 +00:00
|
|
|
// If there's no render title specified then use the current tiddler title
|
2012-10-15 17:46:47 +00:00
|
|
|
if(this.hasParameter("target")) {
|
|
|
|
renderTitle = this.params.target;
|
2012-11-07 16:11:47 +00:00
|
|
|
} else if(this.hasParameter("targetVia")) {
|
|
|
|
renderTitle = this.wiki.getTextReference(this.params.targetVia,null,this.tiddlerTitle);
|
2012-10-15 17:46:47 +00:00
|
|
|
} else {
|
2012-04-30 11:23:03 +00:00
|
|
|
renderTitle = this.tiddlerTitle;
|
|
|
|
}
|
2012-10-15 17:46:47 +00:00
|
|
|
// Get the render tree for the template
|
|
|
|
if(this.hasParameter("templateText")) {
|
|
|
|
renderTemplateTree = this.wiki.parseText("text/x-tiddlywiki",this.params.templateText).tree;
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-10-15 17:46:47 +00:00
|
|
|
if(this.hasParameter("template")) {
|
|
|
|
renderTemplateTitle = this.params.template;
|
|
|
|
} else {
|
|
|
|
renderTemplateTitle = renderTitle;
|
|
|
|
}
|
|
|
|
// Check for recursion
|
|
|
|
if(parents.indexOf(this.params.templateTitle) !== -1) {
|
2012-10-16 17:49:23 +00:00
|
|
|
renderTemplateTree = $tw.Tree.errorNode("Tiddler recursion error in <<tiddler>> macro");
|
2012-10-15 17:46:47 +00:00
|
|
|
} 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());
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Execute macros within the children
|
2012-10-15 17:46:47 +00:00
|
|
|
for(t=0; t<renderTemplateTree.length; t++) {
|
|
|
|
renderTemplateTree[t].execute(parents,renderTitle);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
// Set up the attributes for the wrapper element
|
|
|
|
var attributes = {
|
2012-10-24 21:28:25 +00:00
|
|
|
"class": []
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
if(!this.wiki.tiddlerExists(renderTitle)) {
|
|
|
|
attributes["class"].push("tw-tiddler-missing");
|
|
|
|
}
|
|
|
|
// Return the children
|
2012-10-15 17:46:47 +00:00
|
|
|
return $tw.Tree.Element(this.isBlock ? "div" : "span",attributes,renderTemplateTree);
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
exports.refreshInDom = function(changes) {
|
2012-10-15 17:46:47 +00:00
|
|
|
var renderTitle;
|
2012-04-30 11:23:03 +00:00
|
|
|
// Set the class for missing tiddlers
|
2012-10-15 17:46:47 +00:00
|
|
|
if(this.hasParameter("target")) {
|
|
|
|
renderTitle = this.params.target;
|
|
|
|
} else {
|
|
|
|
renderTitle = this.tiddlerTitle;
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
if(renderTitle) {
|
2012-06-09 17:36:32 +00:00
|
|
|
$tw.utils.toggleClass(this.child.domNode,"tw-tiddler-missing",!this.wiki.tiddlerExists(renderTitle));
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
// Rerender the tiddler if it is impacted by the changes
|
2012-10-15 17:46:47 +00:00
|
|
|
if(this.dependencies.hasChanged(changes,this.renderTitle)) {
|
2012-04-30 11:23:03 +00:00
|
|
|
// Manually reexecute and rerender this macro
|
2012-10-15 17:46:47 +00:00
|
|
|
this.reexecuteInDom();
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-06-09 17:36:32 +00:00
|
|
|
this.child.refreshInDom(changes);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|