2014-06-12 17:01:33 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/importvariables.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Import variable definitions from other tiddlers
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var ImportVariablesWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
ImportVariablesWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
ImportVariablesWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
ImportVariablesWidget.prototype.execute = function(tiddlerList) {
|
2020-01-30 12:53:26 +00:00
|
|
|
var widgetPointer = this;
|
2020-04-23 08:10:52 +00:00
|
|
|
// Got to flush all the accumulated variables
|
|
|
|
this.variables = new this.variablesConstructor();
|
2014-06-12 17:01:33 +00:00
|
|
|
// Get our parameters
|
|
|
|
this.filter = this.getAttribute("filter");
|
|
|
|
// Compute the filter
|
|
|
|
this.tiddlerList = tiddlerList || this.wiki.filterTiddlers(this.filter,this);
|
|
|
|
// Accumulate the <$set> widgets from each tiddler
|
|
|
|
$tw.utils.each(this.tiddlerList,function(title) {
|
2022-01-27 17:35:18 +00:00
|
|
|
var parser = widgetPointer.wiki.parseTiddler(title,{parseAsInline:true});
|
2014-06-12 17:01:33 +00:00
|
|
|
if(parser) {
|
|
|
|
var parseTreeNode = parser.tree[0];
|
|
|
|
while(parseTreeNode && parseTreeNode.type === "set") {
|
2020-01-30 12:53:26 +00:00
|
|
|
var node = {
|
2014-06-12 17:01:33 +00:00
|
|
|
type: "set",
|
|
|
|
attributes: parseTreeNode.attributes,
|
2018-06-15 07:31:02 +00:00
|
|
|
params: parseTreeNode.params,
|
|
|
|
isMacroDefinition: parseTreeNode.isMacroDefinition
|
2020-01-30 12:53:26 +00:00
|
|
|
};
|
|
|
|
if (parseTreeNode.isMacroDefinition) {
|
|
|
|
// Macro definitions can be folded into
|
|
|
|
// current widget instead of adding
|
|
|
|
// another link to the chain.
|
|
|
|
var widget = widgetPointer.makeChildWidget(node);
|
|
|
|
widget.computeAttributes();
|
|
|
|
widget.execute();
|
2020-03-19 20:32:51 +00:00
|
|
|
// We SHALLOW copy over all variables
|
|
|
|
// in widget. We can't use
|
|
|
|
// $tw.utils.assign, because that copies
|
|
|
|
// up the prototype chain, which we
|
|
|
|
// don't want.
|
|
|
|
$tw.utils.each(Object.keys(widget.variables), function(key) {
|
|
|
|
widgetPointer.variables[key] = widget.variables[key];
|
|
|
|
});
|
2020-01-30 12:53:26 +00:00
|
|
|
} else {
|
2020-04-23 08:10:52 +00:00
|
|
|
widgetPointer.children = [widgetPointer.makeChildWidget(node)];
|
|
|
|
// No more regenerating children for
|
|
|
|
// this widget. If it needs to refresh,
|
|
|
|
// it'll do so along with the the whole
|
|
|
|
// importvariable tree.
|
|
|
|
if (widgetPointer != this) {
|
|
|
|
widgetPointer.makeChildWidgets = function(){};
|
|
|
|
}
|
2020-01-30 12:53:26 +00:00
|
|
|
widgetPointer = widgetPointer.children[0];
|
|
|
|
}
|
2020-01-03 10:39:37 +00:00
|
|
|
parseTreeNode = parseTreeNode.children && parseTreeNode.children[0];
|
2014-06-12 17:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-01-30 12:53:26 +00:00
|
|
|
|
|
|
|
if (widgetPointer != this) {
|
|
|
|
widgetPointer.parseTreeNode.children = this.parseTreeNode.children;
|
2014-06-12 17:01:33 +00:00
|
|
|
} else {
|
2020-01-30 12:53:26 +00:00
|
|
|
widgetPointer.makeChildWidgets();
|
2014-06-12 17:01:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
ImportVariablesWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
// Recompute our attributes and the filter list
|
|
|
|
var changedAttributes = this.computeAttributes(),
|
|
|
|
tiddlerList = this.wiki.filterTiddlers(this.getAttribute("filter"),this);
|
|
|
|
// Refresh if the filter has changed, or the list of tiddlers has changed, or any of the tiddlers in the list has changed
|
|
|
|
function haveListedTiddlersChanged() {
|
|
|
|
var changed = false;
|
|
|
|
tiddlerList.forEach(function(title) {
|
|
|
|
if(changedTiddlers[title]) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
if(changedAttributes.filter || !$tw.utils.isArrayEqual(this.tiddlerList,tiddlerList) || haveListedTiddlersChanged()) {
|
|
|
|
// Compute the filter
|
|
|
|
this.removeChildDomNodes();
|
|
|
|
this.execute(tiddlerList);
|
|
|
|
this.renderChildren(this.parentDomNode,this.findNextSiblingDomNode());
|
|
|
|
return true;
|
|
|
|
} else {
|
2021-05-30 18:20:17 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
2014-06-12 17:01:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.importvariables = ImportVariablesWidget;
|
|
|
|
|
|
|
|
})();
|