2022-04-30 11:44:26 +00:00
|
|
|
const { parse } = require("../parsers/wikiparser/rules/transcludeinline");
|
|
|
|
|
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:54:55 +00:00
|
|
|
// Get our attributes, string parameters, and slot values into properties of the widget object
|
2022-04-30 08:30:25 +00:00
|
|
|
this.collectAttributes();
|
2022-04-30 08:54:55 +00:00
|
|
|
this.collectStringParameters();
|
|
|
|
this.collectSlotValueParameters();
|
2022-04-30 08:30:25 +00:00
|
|
|
// Get the parse tree nodes that we are transcluding
|
|
|
|
var target = this.getTransclusionTarget(),
|
|
|
|
parseTreeNodes = target.parseTreeNodes;
|
|
|
|
this.sourceText = target.source;
|
|
|
|
this.sourceType = target.type;
|
2022-04-30 08:54:55 +00:00
|
|
|
// Wrap the transcluded content if required
|
|
|
|
if(this.slotValueParseTrees["ts-wrapper"]) {
|
|
|
|
this.slotValueParseTrees["ts-wrapped"] = parseTreeNodes;
|
|
|
|
parseTreeNodes = this.slotValueParseTrees["ts-wrapper"];
|
|
|
|
this.sourceTest = undefined;
|
|
|
|
this.sourceType = undefined;
|
|
|
|
}
|
2022-04-30 08:30:25 +00:00
|
|
|
// 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() {
|
2022-04-30 08:54:55 +00:00
|
|
|
var self = this;
|
|
|
|
// Detect legacy mode
|
|
|
|
this.legacyMode = true;
|
|
|
|
$tw.utils.each(this.attributes,function(value,name) {
|
|
|
|
if(name.charAt(0) === "$") {
|
|
|
|
self.legacyMode = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Get the attributes for the appropriate mode
|
|
|
|
if(this.legacyMode) {
|
|
|
|
this.transcludeTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
|
|
|
this.transcludeSubTiddler = this.getAttribute("subtiddler");
|
|
|
|
this.transcludeField = this.getAttribute("field");
|
|
|
|
this.transcludeIndex = this.getAttribute("index");
|
|
|
|
this.transcludeMode = this.getAttribute("mode");
|
|
|
|
this.recursionMarker = this.getAttribute("recursionMarker","yes");
|
|
|
|
} else {
|
|
|
|
this.transcludeVariable = this.getAttribute("$variable");
|
|
|
|
this.transcludeType = this.getAttribute("$type");
|
|
|
|
this.transcludeTitle = this.getAttribute("$tiddler",this.getVariable("currentTiddler"));
|
|
|
|
this.transcludeSubTiddler = this.getAttribute("$subtiddler");
|
|
|
|
this.transcludeField = this.getAttribute("$field");
|
|
|
|
this.transcludeIndex = this.getAttribute("$index");
|
|
|
|
this.transcludeMode = this.getAttribute("$mode");
|
|
|
|
this.recursionMarker = this.getAttribute("$recursionMarker","yes");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Collect string parameters
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.collectStringParameters = function() {
|
|
|
|
var self = this;
|
|
|
|
this.stringParametersByName = Object.create(null);
|
|
|
|
if(!this.legacyMode) {
|
|
|
|
$tw.utils.each(this.attributes,function(value,name) {
|
|
|
|
if(name.charAt(0) === "$") {
|
|
|
|
if(name.charAt(1) === "$") {
|
|
|
|
// Attributes starting $$ represent parameters starting with a single $
|
|
|
|
name = name.slice(1);
|
|
|
|
} else {
|
|
|
|
// Attributes starting with a single $ are reserved for the widget
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.stringParametersByName[name] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Collect slot value parameters
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.collectSlotValueParameters = function() {
|
|
|
|
var self = this;
|
|
|
|
this.slotValueParseTrees = Object.create(null);
|
|
|
|
if(this.legacyMode) {
|
|
|
|
this.slotValueParseTrees["ts-missing"] = this.parseTreeNode.children;
|
|
|
|
} else {
|
|
|
|
var noValueWidgetsFound = true,
|
|
|
|
searchParseTreeNodes = function(nodes) {
|
|
|
|
$tw.utils.each(nodes,function(node) {
|
|
|
|
if(node.type === "value" && node.tag === "$value") {
|
|
|
|
if(node.attributes["$name"] && node.attributes["$name"].type === "string") {
|
|
|
|
var slotValueName = node.attributes["$name"].value;
|
|
|
|
self.slotValueParseTrees[slotValueName] = node.children;
|
|
|
|
}
|
|
|
|
noValueWidgetsFound = false;
|
|
|
|
} else {
|
|
|
|
searchParseTreeNodes(node.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
searchParseTreeNodes(this.parseTreeNode.children);
|
|
|
|
if(noValueWidgetsFound) {
|
|
|
|
this.slotValueParseTrees["ts-missing"] = this.parseTreeNode.children;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
2022-04-30 08:54:55 +00:00
|
|
|
var parser;
|
|
|
|
if(this.transcludeVariable) {
|
2022-04-30 11:44:26 +00:00
|
|
|
var variableInfo = this.getVariableInfo(this.transcludeVariable).srcVariable;
|
|
|
|
parser = this.wiki.parseText(this.transcludeType,variableInfo.value || "",{parseAsInline: !this.parseTreeNode.isBlock});
|
|
|
|
if(parser && variableInfo.isFunctionDefinition) {
|
|
|
|
parser = {
|
|
|
|
tree: [
|
|
|
|
{
|
|
|
|
type: "parameters",
|
|
|
|
name: "$parameters",
|
|
|
|
children: parser.tree,
|
|
|
|
attributes: {},
|
|
|
|
orderedAttributes: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
$tw.utils.each(variableInfo.variableParams,function(param,index) {
|
|
|
|
var attr = {name: param.name, type: "string", value: param["default"]};
|
|
|
|
parser.tree[0].attributes[param.name] = attr;
|
|
|
|
parser.tree[0].orderedAttributes.push(attr);
|
|
|
|
});
|
|
|
|
}
|
2022-04-30 08:54:55 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
});
|
2022-04-30 08:54:55 +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,
|
2022-04-30 08:54:55 +00:00
|
|
|
parseTreeNodes: (this.slotValueParseTrees["ts-missing"] || []),
|
2022-04-30 08:30:25 +00:00
|
|
|
text: null,
|
|
|
|
type: null
|
|
|
|
};
|
2014-07-12 08:08:52 +00:00
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
2022-04-30 08:54:55 +00:00
|
|
|
/*
|
|
|
|
Fetch the value of a parameter
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.getTransclusionParameter = function(name,index,defaultValue) {
|
|
|
|
if(name in this.stringParametersByName) {
|
|
|
|
return this.stringParametersByName[name];
|
|
|
|
} else {
|
|
|
|
var name = "" + index;
|
|
|
|
if(name in this.stringParametersByName) {
|
|
|
|
return this.stringParametersByName[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fetch the value of a parameter identified by its position
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.getTransclusionParameterByPosition = function(index,defaultValue) {
|
|
|
|
if(index in this.stringParametersByPosition) {
|
|
|
|
return this.stringParametersByPosition[index];
|
|
|
|
} else {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fetch the value of a slot
|
|
|
|
*/
|
|
|
|
TranscludeWidget.prototype.getTransclusionSlotValue = function(name,defaultParseTreeNodes) {
|
|
|
|
if(name && this.slotValueParseTrees[name]) {
|
|
|
|
return this.slotValueParseTrees[name];
|
|
|
|
} else {
|
|
|
|
return defaultParseTreeNodes || [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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() {
|
2022-04-30 08:54:55 +00:00
|
|
|
var attributes = Object.create(null);
|
|
|
|
$tw.utils.each(this.attributes,function(value,name) {
|
|
|
|
attributes[name] = value;
|
|
|
|
});
|
2013-10-12 16:05:13 +00:00
|
|
|
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("|");
|
2022-04-30 08:54:55 +00:00
|
|
|
output.push(JSON.stringify(attributes));
|
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;
|
|
|
|
|
|
|
|
})();
|