2013-10-12 16:05:13 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/transclude.js
|
2013-10-12 16:05:13 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
Transclude widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
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"));
|
2014-07-17 17:41:20 +00:00
|
|
|
this.transcludeSubTiddler = this.getAttribute("subtiddler");
|
2013-10-12 16:05:13 +00:00
|
|
|
this.transcludeField = this.getAttribute("field");
|
|
|
|
this.transcludeIndex = this.getAttribute("index");
|
2014-02-12 17:59:28 +00:00
|
|
|
this.transcludeMode = this.getAttribute("mode");
|
2020-11-07 09:51:01 +00:00
|
|
|
this.recursionMarker = this.getAttribute("recursionMarker","yes");
|
2013-10-12 16:05:13 +00:00
|
|
|
// Parse the text reference
|
2014-02-12 17:59:28 +00:00
|
|
|
var parseAsInline = !this.parseTreeNode.isBlock;
|
|
|
|
if(this.transcludeMode === "inline") {
|
|
|
|
parseAsInline = true;
|
|
|
|
} else if(this.transcludeMode === "block") {
|
|
|
|
parseAsInline = false;
|
|
|
|
}
|
2013-11-08 08:51:14 +00:00
|
|
|
var parser = this.wiki.parseTextReference(
|
2013-10-12 16:05:13 +00:00
|
|
|
this.transcludeTitle,
|
|
|
|
this.transcludeField,
|
|
|
|
this.transcludeIndex,
|
2014-07-17 17:41:20 +00:00
|
|
|
{
|
|
|
|
parseAsInline: parseAsInline,
|
|
|
|
subTiddler: this.transcludeSubTiddler
|
|
|
|
}),
|
2014-02-12 21:38:37 +00:00
|
|
|
parseTreeNodes = parser ? parser.tree : this.parseTreeNode.children;
|
2021-06-02 20:45:06 +00:00
|
|
|
this.sourceText = parser ? parser.source : null;
|
|
|
|
this.parserType = parser? parser.type : null;
|
2014-07-12 08:08:52 +00:00
|
|
|
// Set context variables for recursion detection
|
|
|
|
var recursionMarker = this.makeRecursionMarker();
|
2020-11-07 09:51:01 +00:00
|
|
|
if(this.recursionMarker === "yes") {
|
|
|
|
this.setVariable("transclusion",recursionMarker);
|
|
|
|
}
|
2014-07-12 08:08:52 +00:00
|
|
|
// Check for recursion
|
|
|
|
if(parser) {
|
|
|
|
if(this.parentWidget && this.parentWidget.hasVariable("transclusion",recursionMarker)) {
|
2014-09-24 09:50:38 +00:00
|
|
|
parseTreeNodes = [{type: "element", tag: "span", attributes: {
|
|
|
|
"class": {type: "string", value: "tc-error"}
|
|
|
|
}, children: [
|
2016-05-17 20:58:47 +00:00
|
|
|
{type: "text", text: $tw.language.getString("Error/RecursiveTransclusion")}
|
2014-09-24 09:50:38 +00:00
|
|
|
]}];
|
2014-07-12 08:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
// Construct the child widgets
|
2021-04-02 08:47:38 +00:00
|
|
|
this.makeChildWidgets(parseTreeNodes);
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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 || "");
|
2014-08-07 14:43:48 +00:00
|
|
|
output.push("|");
|
|
|
|
output.push(this.transcludeSubTiddler || "");
|
2013-10-12 16:05:13 +00:00
|
|
|
output.push("}");
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
2021-06-02 20:45:06 +00:00
|
|
|
TranscludeWidget.prototype.parserNeedsRefresh = function() {
|
|
|
|
var parserInfo = this.wiki.getTextReferenceParserInfo(this.transcludeTitle,this.transcludeField,this.transcludeIndex,{subTiddler:this.transcludeSubTiddler});
|
|
|
|
return (this.sourceText === undefined || parserInfo.sourceText !== this.sourceText || parserInfo.parserType !== this.parserType)
|
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
/*
|
|
|
|
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();
|
2021-06-02 20:45:06 +00:00
|
|
|
if(($tw.utils.count(changedAttributes) > 0) || (changedTiddlers[this.transcludeTitle] && this.parserNeedsRefresh())) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
2021-05-30 18:20:17 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.transclude = TranscludeWidget;
|
|
|
|
|
|
|
|
})();
|