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");
|
2016-10-18 08:16:47 +00:00
|
|
|
this.setSelect = this.getAttribute("select");
|
2017-07-12 15:59:56 +00:00
|
|
|
this.setTiddler = this.getAttribute("tiddler");
|
2018-03-16 20:35:41 +00:00
|
|
|
this.setSubTiddler = this.getAttribute("subtiddler");
|
2017-07-12 15:59:56 +00:00
|
|
|
this.setField = this.getAttribute("field");
|
|
|
|
this.setIndex = this.getAttribute("index");
|
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
|
2015-09-21 15:24:26 +00:00
|
|
|
this.setVariable(this.setName,this.getValue(),this.parseTreeNode.params);
|
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the value to be assigned
|
|
|
|
*/
|
|
|
|
SetWidget.prototype.getValue = function() {
|
2014-11-13 21:39:15 +00:00
|
|
|
var value = this.setValue;
|
2017-07-12 15:59:56 +00:00
|
|
|
if(this.setTiddler) {
|
2018-03-16 20:35:41 +00:00
|
|
|
var tiddler;
|
|
|
|
if(this.setSubTiddler) {
|
|
|
|
tiddler = this.wiki.getSubTiddler(this.setTiddler,this.setSubTiddler);
|
|
|
|
} else {
|
|
|
|
tiddler = this.wiki.getTiddler(this.setTiddler);
|
|
|
|
}
|
2017-07-12 15:59:56 +00:00
|
|
|
if(!tiddler) {
|
|
|
|
value = this.setEmptyValue;
|
|
|
|
} else if(this.setField) {
|
|
|
|
value = tiddler.getFieldString(this.setField) || this.setEmptyValue;
|
|
|
|
} else if(this.setIndex) {
|
|
|
|
value = this.wiki.extractTiddlerDataItem(this.setTiddler,this.setIndex,this.setEmptyValue);
|
|
|
|
} else {
|
|
|
|
value = tiddler.fields.text || this.setEmptyValue ;
|
|
|
|
}
|
|
|
|
} else if(this.setFilter) {
|
2014-11-13 21:39:15 +00:00
|
|
|
var results = this.wiki.filterTiddlers(this.setFilter,this);
|
2018-03-15 14:12:33 +00:00
|
|
|
if(this.setValue == null) {
|
2016-10-18 08:16:47 +00:00
|
|
|
var select;
|
|
|
|
if(this.setSelect) {
|
|
|
|
select = parseInt(this.setSelect,10);
|
|
|
|
}
|
|
|
|
if(select !== undefined) {
|
|
|
|
value = results[select] || "";
|
|
|
|
} else {
|
|
|
|
value = $tw.utils.stringifyList(results);
|
|
|
|
}
|
2014-11-13 21:39:15 +00:00
|
|
|
}
|
|
|
|
if(results.length === 0 && this.setEmptyValue !== undefined) {
|
|
|
|
value = this.setEmptyValue;
|
|
|
|
}
|
2015-12-09 22:33:07 +00:00
|
|
|
} else if(!value && this.setEmptyValue) {
|
|
|
|
value = this.setEmptyValue;
|
2014-11-13 21:39:15 +00:00
|
|
|
}
|
2017-07-12 15:59:56 +00:00
|
|
|
return value || "";
|
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
|
|
|
|
*/
|
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();
|
2017-07-12 15:59:56 +00:00
|
|
|
if(changedAttributes.name || changedAttributes.filter || changedAttributes.select || changedAttributes.tiddler || (this.setTiddler && changedTiddlers[this.setTiddler]) || changedAttributes.field || changedAttributes.index || changedAttributes.value || changedAttributes.emptyValue ||
|
2015-09-21 15:24:26 +00:00
|
|
|
(this.setFilter && this.getValue() != this.variables[this.setName].value)) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
2015-09-21 15:24:26 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-15 18:31:39 +00:00
|
|
|
exports.setvariable = SetWidget;
|
|
|
|
exports.set = SetWidget;
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
})();
|