2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/transclude.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
The transclude widget includes another tiddler into the tiddler being rendered.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
target: the title of the tiddler to transclude
|
|
|
|
template: the title of the tiddler to use as a template for the transcluded tiddler
|
|
|
|
|
|
|
|
The simplest case is to just supply a target tiddler:
|
|
|
|
|
|
|
|
{{{
|
|
|
|
<_transclude target="Foo"/>
|
|
|
|
}}}
|
|
|
|
|
|
|
|
This will render the tiddler Foo within the current tiddler. If the tiddler Foo includes
|
|
|
|
the view widget (or other widget 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
|
|
|
|
widget are those of the tiddler doing the transcluding, then you can instead specify the tiddler
|
|
|
|
as a template:
|
|
|
|
|
|
|
|
{{{
|
|
|
|
<_transclude template="Foo"/>
|
|
|
|
}}}
|
|
|
|
|
|
|
|
The effect is the same as the previous example: the text of the tiddler Foo is rendered. The
|
|
|
|
difference is that the view widget will access the fields of the tiddler doing the transcluding.
|
|
|
|
|
|
|
|
The `target` and `template` attributes may be combined:
|
|
|
|
|
|
|
|
{{{
|
|
|
|
<_transclude template="Bar" target="Foo"/>
|
|
|
|
}}}
|
|
|
|
|
|
|
|
Here, the text of the tiddler `Bar` will be transcluded, with the widgets within it accessing the fields
|
|
|
|
of the tiddler `Foo`.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2012-12-14 16:01:37 +00:00
|
|
|
exports.name = "transclude";
|
|
|
|
|
|
|
|
exports.init = function(renderer) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
|
|
|
this.generateChildNodes();
|
|
|
|
};
|
|
|
|
|
2012-12-14 16:01:37 +00:00
|
|
|
exports.generateChildNodes = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
var tr, templateParseTree, templateTiddler;
|
|
|
|
// Get the render target details
|
|
|
|
this.targetTitle = this.renderer.getAttribute("target",this.renderer.getContextTiddlerTitle());
|
|
|
|
// Get the render tree for the template
|
|
|
|
this.templateTitle = undefined;
|
|
|
|
if(this.renderer.parseTreeNode.children && this.renderer.parseTreeNode.children.length > 0) {
|
|
|
|
// Use the child nodes as the template if we've got them
|
|
|
|
templateParseTree = this.renderer.parseTreeNode.children;
|
|
|
|
} else {
|
|
|
|
this.templateTitle = this.renderer.getAttribute("template",this.targetTitle);
|
|
|
|
// Check for recursion
|
|
|
|
if(this.renderer.checkContextRecursion({
|
|
|
|
tiddlerTitle: this.targetTitle,
|
|
|
|
templateTitle: this.templateTitle
|
|
|
|
})) {
|
|
|
|
templateParseTree = [{type: "text", text: "Tiddler recursion error in transclude widget"}];
|
|
|
|
} else {
|
2012-12-28 22:08:32 +00:00
|
|
|
var parser = this.renderer.renderTree.wiki.parseTiddler(this.templateTitle,{parseAsInline: !this.renderer.parseTreeNode.isBlock});
|
2012-12-13 21:34:31 +00:00
|
|
|
templateParseTree = parser ? parser.tree : [];
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 17:19:28 +00:00
|
|
|
// Create the wrapper node
|
|
|
|
var node = {
|
|
|
|
type: "element",
|
|
|
|
tag: this.renderer.parseTreeNode.isBlock ? "div" : "span",
|
|
|
|
children: templateParseTree
|
|
|
|
};
|
2012-12-13 21:34:31 +00:00
|
|
|
// Set up the attributes for the wrapper element
|
|
|
|
var classes = [];
|
2012-12-20 17:19:28 +00:00
|
|
|
if(this.renderer.hasAttribute("class")) {
|
|
|
|
$tw.utils.pushTop(classes,this.renderer.getAttribute("class").split(" "));
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
if(!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle)) {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddler-missing");
|
|
|
|
}
|
2012-12-20 17:19:28 +00:00
|
|
|
if(classes.length > 0) {
|
|
|
|
$tw.utils.addClassToParseTreeNode(node,classes.join(" "));
|
|
|
|
}
|
|
|
|
if(this.renderer.hasAttribute("style")) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(node,"style",this.renderer.getAttribute("style"));
|
|
|
|
}
|
|
|
|
if(this.renderer.hasAttribute("tooltip")) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(node,"title",this.renderer.getAttribute("tooltip"));
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
// Create the renderers for the wrapper and the children
|
|
|
|
var newRenderContext = {
|
|
|
|
tiddlerTitle: this.targetTitle,
|
|
|
|
templateTitle: this.templateTitle,
|
|
|
|
parentContext: this.renderer.renderContext
|
|
|
|
};
|
2012-12-20 17:19:28 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(newRenderContext,[node]);
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2012-12-14 16:01:37 +00:00
|
|
|
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Set the class for missing tiddlers
|
2012-12-14 19:31:37 +00:00
|
|
|
if(this.targetTitle && changedTiddlers[this.targetTitle]) {
|
2012-12-13 21:34:31 +00:00
|
|
|
$tw.utils.toggleClass(this.children[0].domNode,"tw-tiddler-missing",!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle));
|
|
|
|
}
|
|
|
|
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
|
|
|
|
if(changedAttributes.target || changedAttributes.template || (this.targetTitle && changedTiddlers[this.targetTitle]) || (this.templateTitle && changedTiddlers[this.templateTitle])) {
|
|
|
|
// Remove old child nodes
|
|
|
|
$tw.utils.removeChildren(this.parentElement);
|
|
|
|
// Regenerate and render children
|
|
|
|
this.generateChildNodes();
|
|
|
|
var self = this;
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.renderInDom) {
|
|
|
|
self.parentElement.appendChild(node.renderInDom());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// We don't need to refresh ourselves, so just refresh any child nodes
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|