2013-10-12 16:05:13 +00:00
|
|
|
/*\
|
2013-11-15 18:31:39 +00:00
|
|
|
title: $:/core/modules/widgets/set.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
|
|
|
|
2013-11-15 18:31:39 +00:00
|
|
|
Set variable widget
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(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
|
|
|
|
2013-11-15 18:31:39 +00:00
|
|
|
var SetWidget = function(parseTreeNode,options) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
2013-11-15 18:31:39 +00:00
|
|
|
SetWidget.prototype = new Widget();
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
2013-11-15 18:31:39 +00:00
|
|
|
SetWidget.prototype.render = function(parent,nextSibling) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
2013-11-15 18:31:39 +00:00
|
|
|
SetWidget.prototype.execute = function() {
|
2013-10-12 16:05:13 +00:00
|
|
|
// Get our parameters
|
2013-10-28 23:40:45 +00:00
|
|
|
this.setName = this.getAttribute("name","currentTiddler");
|
2014-11-13 21:39:15 +00:00
|
|
|
this.setFilter = this.getAttribute("filter");
|
2013-10-12 16:05:13 +00:00
|
|
|
this.setValue = this.getAttribute("value");
|
2014-11-13 21:39:15 +00:00
|
|
|
this.setEmptyValue = this.getAttribute("emptyValue");
|
2013-10-12 16:05:13 +00:00
|
|
|
// Set context variable
|
2014-11-13 21:39:15 +00:00
|
|
|
var value = this.setValue;
|
|
|
|
if(this.setFilter) {
|
|
|
|
var results = this.wiki.filterTiddlers(this.setFilter,this);
|
|
|
|
if(!this.setValue) {
|
|
|
|
value = $tw.utils.stringifyList(results);
|
|
|
|
}
|
|
|
|
if(results.length === 0 && this.setEmptyValue !== undefined) {
|
|
|
|
value = this.setEmptyValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setVariable(this.setName,value,this.parseTreeNode.params);
|
2013-10-12 16:05:13 +00:00
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
2013-11-15 18:31:39 +00:00
|
|
|
SetWidget.prototype.refresh = function(changedTiddlers) {
|
2013-10-12 16:05:13 +00:00
|
|
|
var changedAttributes = this.computeAttributes();
|
2014-11-13 21:39:15 +00:00
|
|
|
if(changedAttributes.name || changedAttributes.filter || changedAttributes.value || changedAttributes.emptyValue) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-15 18:31:39 +00:00
|
|
|
exports.setvariable = SetWidget;
|
|
|
|
exports.set = SetWidget;
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
})();
|