2013-10-12 16:05:13 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/new_widgets/transclude.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: new_widget
|
|
|
|
|
|
|
|
Transclude widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/new_widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var TranscludeWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.execute = function() {
|
|
|
|
// Get our parameters
|
2013-10-30 13:36:44 +00:00
|
|
|
this.transcludeTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
2013-10-12 16:05:13 +00:00
|
|
|
this.transcludeField = this.getAttribute("field");
|
|
|
|
this.transcludeIndex = this.getAttribute("index");
|
|
|
|
// Check for recursion
|
|
|
|
var recursionMarker = this.makeRecursionMarker();;
|
|
|
|
if(this.parentWidget && this.parentWidget.hasVariable("transclusion",recursionMarker)) {
|
|
|
|
this.makeChildWidgets([{type: "text", text: "Tiddler recursion error in transclude widget"}]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set context variables for recursion detection
|
|
|
|
this.setVariable("transclusion",recursionMarker);
|
|
|
|
// Parse the text reference
|
|
|
|
var parser = this.wiki.new_parseTextReference(
|
|
|
|
this.transcludeTitle,
|
|
|
|
this.transcludeField,
|
|
|
|
this.transcludeIndex,
|
|
|
|
{parseAsInline: !this.parseTreeNode.isBlock}),
|
|
|
|
parseTreeNodes = parser ? parser.tree : [];
|
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets(parseTreeNodes);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compose a string comprising the title, field and/or index to identify this transclusion for recursion detection
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.makeRecursionMarker = function() {
|
|
|
|
var output = [];
|
|
|
|
output.push("{");
|
2013-10-28 23:40:45 +00:00
|
|
|
output.push(this.getVariable("currentTiddler",{defaultValue: ""}));
|
2013-10-14 12:06:07 +00:00
|
|
|
output.push("|");
|
2013-10-12 16:05:13 +00:00
|
|
|
output.push(this.transcludeTitle || "");
|
|
|
|
output.push("|");
|
|
|
|
output.push(this.transcludeField || "");
|
|
|
|
output.push("|");
|
|
|
|
output.push(this.transcludeIndex || "");
|
|
|
|
output.push("}");
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2013-10-30 13:36:44 +00:00
|
|
|
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedTiddlers[this.transcludeTitle]) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.transclude = TranscludeWidget;
|
|
|
|
|
|
|
|
})();
|