2022-04-24 20:24:38 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/parameters.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Widget for definition of transclusion parameters
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget,
|
2022-04-30 09:00:38 +00:00
|
|
|
TranscludeWidget = require("$:/core/modules/widgets/transclude.js").transclude;
|
2022-04-24 20:24:38 +00:00
|
|
|
|
|
|
|
var ParametersWidget = function(parseTreeNode,options) {
|
|
|
|
// Initialise
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
ParametersWidget.prototype = Object.create(Widget.prototype);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
ParametersWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
// Call the constructor
|
|
|
|
Widget.call(this);
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
ParametersWidget.prototype.execute = function() {
|
|
|
|
var self = this;
|
2022-05-31 08:03:20 +00:00
|
|
|
this.parametersDepth = parseInt(this.getAttribute("$depth","1"),10) || 1;
|
|
|
|
// Find the parent transclusions
|
|
|
|
var pointer = this.parentWidget,
|
|
|
|
depth = this.parametersDepth;
|
|
|
|
while(pointer) {
|
|
|
|
if(pointer instanceof TranscludeWidget) {
|
|
|
|
depth--;
|
|
|
|
if(depth === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pointer = pointer.parentWidget;
|
2022-04-24 20:24:38 +00:00
|
|
|
}
|
|
|
|
// Process each parameter
|
2022-05-31 08:03:20 +00:00
|
|
|
if(pointer instanceof TranscludeWidget) {
|
|
|
|
// Get the value for each defined parameter
|
2022-04-26 20:55:43 +00:00
|
|
|
$tw.utils.each($tw.utils.getOrderedAttributesFromParseTreeNode(self.parseTreeNode),function(attr,index) {
|
2022-05-31 08:03:20 +00:00
|
|
|
var name = attr.name;
|
|
|
|
// If the attribute name starts with $$ then reduce to a single dollar
|
|
|
|
if(name.substr(0,2) === "$$") {
|
|
|
|
name = name.substr(1);
|
|
|
|
}
|
|
|
|
var value = pointer.getTransclusionParameter(name,index,self.getAttribute(attr.name,""));
|
2022-05-05 07:20:14 +00:00
|
|
|
self.setVariable(name,value);
|
|
|
|
});
|
2022-05-31 08:03:20 +00:00
|
|
|
// Assign any metaparameters
|
2022-06-03 10:21:32 +00:00
|
|
|
$tw.utils.each(pointer.getTransclusionMetaParameters(),function(getValue,name) {
|
2022-05-31 08:03:20 +00:00
|
|
|
var variableName = self.getAttribute("$" + name);
|
|
|
|
if(variableName !== undefined) {
|
2022-06-03 10:21:32 +00:00
|
|
|
self.setVariable(variableName,getValue(name));
|
2022-05-31 08:03:20 +00:00
|
|
|
}
|
2022-06-03 10:21:32 +00:00
|
|
|
});
|
2022-04-24 20:24:38 +00:00
|
|
|
}
|
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Refresh the widget by ensuring our attributes are up to date
|
|
|
|
*/
|
|
|
|
ParametersWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
if(Object.keys(changedAttributes).length) {
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.parameters = ParametersWidget;
|
|
|
|
|
|
|
|
})();
|