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:
|
|
|
|
|
|
|
|
{{{
|
2013-01-03 16:27:55 +00:00
|
|
|
<$transclude target="Foo"/>
|
2012-12-13 21:34:31 +00:00
|
|
|
}}}
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
{{{
|
2013-01-03 16:27:55 +00:00
|
|
|
<$transclude template="Foo"/>
|
2012-12-13 21:34:31 +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 widget will access the fields of the tiddler doing the transcluding.
|
|
|
|
|
|
|
|
The `target` and `template` attributes may be combined:
|
|
|
|
|
|
|
|
{{{
|
2013-01-03 16:27:55 +00:00
|
|
|
<$transclude template="Bar" target="Foo"/>
|
2012-12-13 21:34:31 +00:00
|
|
|
}}}
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
var TranscludeWidget = function(renderer) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
2013-01-03 16:27:55 +00:00
|
|
|
this.generate();
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-03 16:27:55 +00:00
|
|
|
TranscludeWidget.prototype.generate = function() {
|
2013-06-04 11:02:18 +00:00
|
|
|
var self = this,
|
|
|
|
tr, templateParseTree, templateTiddler;
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get the render target details
|
2013-05-15 16:32:17 +00:00
|
|
|
this.targetTitle = this.renderer.getAttribute("target",this.renderer.tiddlerTitle);
|
2013-01-15 17:50:47 +00:00
|
|
|
this.targetField = this.renderer.getAttribute("field");
|
|
|
|
this.targetIndex = this.renderer.getAttribute("index");
|
2012-12-13 21:34:31 +00:00
|
|
|
// 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
|
2013-05-15 16:32:17 +00:00
|
|
|
if(this.renderer.renderTree.checkContextRecursion(this.renderer.parentRenderer,{
|
2013-01-15 17:50:47 +00:00
|
|
|
tiddlerTitle: this.targetTitle,
|
|
|
|
templateTitle: this.templateTitle
|
|
|
|
})) {
|
2012-12-13 21:34:31 +00:00
|
|
|
templateParseTree = [{type: "text", text: "Tiddler recursion error in transclude widget"}];
|
|
|
|
} else {
|
2013-01-03 16:27:55 +00:00
|
|
|
var parser;
|
2013-01-15 17:50:47 +00:00
|
|
|
if(this.targetField === "text" || (!this.targetField && !this.targetIndex)) {
|
|
|
|
parser = this.renderer.renderTree.wiki.parseTiddler(this.templateTitle,{parseAsInline: !this.renderer.parseTreeNode.isBlock});
|
2013-01-03 16:27:55 +00:00
|
|
|
} else {
|
2013-01-15 17:50:47 +00:00
|
|
|
var tiddler,text;
|
|
|
|
if(this.targetField) {
|
|
|
|
tiddler = this.renderer.renderTree.wiki.getTiddler(this.targetTitle);
|
2013-01-03 16:27:55 +00:00
|
|
|
text = tiddler ? tiddler.fields[this.targetField] : "";
|
2013-01-15 17:50:47 +00:00
|
|
|
if(text === undefined) {
|
|
|
|
text = "";
|
|
|
|
}
|
|
|
|
parser = this.renderer.renderTree.wiki.parseText("text/vnd.tiddlywiki",text,{parseAsInline: !this.renderer.parseTreeNode.isBlock});
|
|
|
|
} else if(this.targetIndex) {
|
|
|
|
text = this.renderer.renderTree.wiki.extractTiddlerDataItem(this.targetTitle,this.targetIndex,"");
|
2013-02-03 23:02:23 +00:00
|
|
|
parser = this.renderer.renderTree.wiki.parseText("text/vnd.tiddlywiki",text);
|
2013-01-03 16:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
templateParseTree = parser ? parser.tree : [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set up the attributes for the wrapper element
|
2013-01-03 16:27:55 +00:00
|
|
|
var classes = ["tw-transclude"];
|
2012-12-20 17:19:28 +00:00
|
|
|
if(this.renderer.hasAttribute("class")) {
|
|
|
|
$tw.utils.pushTop(classes,this.renderer.getAttribute("class").split(" "));
|
|
|
|
}
|
2013-04-03 13:29:12 +00:00
|
|
|
if(!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle) && !this.renderer.renderTree.wiki.isShadowTiddler(this.targetTitle)) {
|
2012-12-13 21:34:31 +00:00
|
|
|
$tw.utils.pushTop(classes,"tw-tiddler-missing");
|
|
|
|
}
|
2013-05-15 16:32:17 +00:00
|
|
|
// Save the context for this renderer node
|
|
|
|
this.renderer.context = {
|
2012-12-13 21:34:31 +00:00
|
|
|
tiddlerTitle: this.targetTitle,
|
2013-05-15 16:32:17 +00:00
|
|
|
templateTitle: this.templateTitle
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
2013-06-09 18:27:15 +00:00
|
|
|
// Initialise events
|
|
|
|
this.events = [];
|
|
|
|
// Trap and update tag modification events
|
|
|
|
this.events.push({name: "tw-remove-tag", handlerFunction: function(event) {
|
|
|
|
event.currentTag = self.targetTitle;
|
|
|
|
return true;
|
|
|
|
}});
|
2013-01-03 16:27:55 +00:00
|
|
|
// Set the element
|
|
|
|
this.tag = this.renderer.parseTreeNode.isBlock ? "div" : "span";
|
|
|
|
this.attributes = {};
|
|
|
|
if(classes.length > 0) {
|
|
|
|
this.attributes["class"] = classes.join(" ");
|
|
|
|
}
|
|
|
|
if(this.renderer.hasAttribute("style")) {
|
|
|
|
this.attributes.style = this.renderer.getAttribute("style");
|
|
|
|
}
|
|
|
|
if(this.renderer.hasAttribute("tooltip")) {
|
|
|
|
this.attributes.title = this.renderer.getAttribute("tooltip");
|
|
|
|
}
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,templateParseTree);
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
TranscludeWidget.prototype.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]) {
|
2013-01-03 20:54:34 +00:00
|
|
|
$tw.utils.toggleClass(this.renderer.domNode,"tw-tiddler-missing",!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle));
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
|
2013-01-17 11:29:21 +00:00
|
|
|
if(changedAttributes.target || changedAttributes.template || (this.templateTitle && changedTiddlers[this.templateTitle])) {
|
2013-01-03 16:27:55 +00:00
|
|
|
// Regenerate and rerender the widget and replace the existing DOM node
|
|
|
|
this.generate();
|
|
|
|
var oldDomNode = this.renderer.domNode,
|
|
|
|
newDomNode = this.renderer.renderInDom();
|
|
|
|
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
|
2012-12-13 21:34:31 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
exports.transclude = TranscludeWidget;
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
})();
|