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() {
|
2022-04-30 08:30:25 +00:00
|
|
|
// Get our attributes into properties of the widget object
|
|
|
|
this.collectAttributes();
|
|
|
|
// Get the parse tree nodes that we are transcluding
|
|
|
|
var target = this.getTransclusionTarget(),
|
|
|
|
parseTreeNodes = target.parseTreeNodes;
|
|
|
|
this.sourceText = target.source;
|
|
|
|
this.sourceType = target.type;
|
|
|
|
// Set context variables for recursion detection
|
|
|
|
var recursionMarker = this.makeRecursionMarker();
|
|
|
|
if(this.recursionMarker === "yes") {
|
|
|
|
this.setVariable("transclusion",recursionMarker);
|
|
|
|
}
|
|
|
|
// Check for recursion
|
|
|
|
if(target.parser) {
|
|
|
|
if(this.parentWidget && this.parentWidget.hasVariable("transclusion",recursionMarker)) {
|
|
|
|
parseTreeNodes = [{type: "element", tag: "span", attributes: {
|
|
|
|
"class": {type: "string", value: "tc-error"}
|
|
|
|
}, children: [
|
|
|
|
{type: "text", text: $tw.language.getString("Error/RecursiveTransclusion")}
|
|
|
|
]}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets(parseTreeNodes);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Collect the attributes we need, in the process determining whether we're being used in legacy mode
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.collectAttributes = function() {
|
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");
|
2022-04-30 08:30:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get transcluded parse tree nodes as an object {parser:,text:,type:}
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.getTransclusionTarget = function() {
|
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
|
2022-04-30 08:30:25 +00:00
|
|
|
});
|
2014-07-12 08:08:52 +00:00
|
|
|
if(parser) {
|
2022-04-30 08:30:25 +00:00
|
|
|
return {
|
|
|
|
parser: parser,
|
|
|
|
parseTreeNodes: parser.tree,
|
|
|
|
text: parser.source,
|
|
|
|
type: parser.type
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
parser: null,
|
|
|
|
parseTreeNodes: this.parseTreeNode.children,
|
|
|
|
text: null,
|
|
|
|
type: null
|
|
|
|
};
|
2014-07-12 08:08:52 +00:00
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
})();
|