2013-10-14 16:48:26 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/macrocall.js
|
2013-10-14 16:48:26 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-14 16:48:26 +00:00
|
|
|
|
|
|
|
Macrocall 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-14 16:48:26 +00:00
|
|
|
|
|
|
|
var MacroCallWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
MacroCallWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
MacroCallWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
MacroCallWidget.prototype.execute = function() {
|
2013-11-04 10:00:02 +00:00
|
|
|
// Get the parse type if specified
|
|
|
|
this.parseType = this.getAttribute("$type","text/vnd.tiddlywiki");
|
|
|
|
this.renderOutput = this.getAttribute("$output","text/html");
|
2013-10-17 15:55:23 +00:00
|
|
|
// Merge together the parameters specified in the parse tree with the specified attributes
|
|
|
|
var params = this.parseTreeNode.params ? this.parseTreeNode.params.slice(0) : [];
|
|
|
|
$tw.utils.each(this.attributes,function(attribute,name) {
|
2013-11-04 10:00:02 +00:00
|
|
|
if(name.charAt(0) !== "$") {
|
2021-05-30 18:20:17 +00:00
|
|
|
params.push({name: name, value: attribute});
|
2013-10-31 21:59:37 +00:00
|
|
|
}
|
2013-10-17 15:55:23 +00:00
|
|
|
});
|
2013-10-14 16:48:26 +00:00
|
|
|
// Get the macro value
|
2017-12-16 09:10:10 +00:00
|
|
|
var macroName = this.parseTreeNode.name || this.getAttribute("$name"),
|
|
|
|
variableInfo = this.getVariableInfo(macroName,{params: params}),
|
|
|
|
text = variableInfo.text,
|
2013-11-04 10:00:02 +00:00
|
|
|
parseTreeNodes;
|
|
|
|
// Are we rendering to HTML?
|
|
|
|
if(this.renderOutput === "text/html") {
|
|
|
|
// If so we'll return the parsed macro
|
2020-12-06 09:43:06 +00:00
|
|
|
// Check if we've already cached parsing this macro
|
2020-12-07 16:05:34 +00:00
|
|
|
var mode = this.parseTreeNode.isBlock ? "blockParser" : "inlineParser",
|
|
|
|
parser;
|
|
|
|
if(variableInfo.srcVariable && variableInfo.srcVariable[mode]) {
|
|
|
|
parser = variableInfo.srcVariable[mode];
|
2020-12-06 09:43:06 +00:00
|
|
|
} else {
|
|
|
|
parser = this.wiki.parseText(this.parseType,text,
|
|
|
|
{parseAsInline: !this.parseTreeNode.isBlock});
|
|
|
|
if(variableInfo.isCacheable && variableInfo.srcVariable) {
|
2020-12-07 16:05:34 +00:00
|
|
|
variableInfo.srcVariable[mode] = parser;
|
2020-12-06 09:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var parseTreeNodes = parser ? parser.tree : [];
|
2017-12-16 09:10:10 +00:00
|
|
|
// Wrap the parse tree in a vars widget assigning the parameters to variables named "__paramname__"
|
|
|
|
var attributes = {};
|
|
|
|
$tw.utils.each(variableInfo.params,function(param) {
|
|
|
|
var name = "__" + param.name + "__";
|
|
|
|
attributes[name] = {
|
|
|
|
name: name,
|
|
|
|
type: "string",
|
|
|
|
value: param.value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
parseTreeNodes = [{
|
|
|
|
type: "vars",
|
|
|
|
attributes: attributes,
|
|
|
|
children: parseTreeNodes
|
|
|
|
}];
|
2020-09-28 10:30:28 +00:00
|
|
|
} else if(this.renderOutput === "text/raw") {
|
|
|
|
parseTreeNodes = [{type: "text", text: text}];
|
2013-11-04 10:00:02 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, we'll render the text
|
2013-11-08 08:51:14 +00:00
|
|
|
var plainText = this.wiki.renderText("text/plain",this.parseType,text,{parentWidget: this});
|
2013-11-04 10:00:02 +00:00
|
|
|
parseTreeNodes = [{type: "text", text: plainText}];
|
|
|
|
}
|
2013-10-14 16:48:26 +00:00
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets(parseTreeNodes);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
MacroCallWidget.prototype.refresh = function(changedTiddlers) {
|
2013-10-17 15:55:23 +00:00
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
if($tw.utils.count(changedAttributes) > 0) {
|
|
|
|
// Rerender ourselves
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
}
|
2013-10-14 16:48:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.macrocall = MacroCallWidget;
|
|
|
|
|
|
|
|
})();
|