1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 10:29:54 +00:00

Extend set widget to support returning a single result from a filter

This solves the problem with extraneous double square brackets when
using the filtered set widget.
This commit is contained in:
Jermolene 2016-10-18 09:16:47 +01:00
parent d9f301f755
commit b35544bf49
2 changed files with 27 additions and 6 deletions

View File

@ -40,6 +40,7 @@ SetWidget.prototype.execute = function() {
// Get our parameters
this.setName = this.getAttribute("name","currentTiddler");
this.setFilter = this.getAttribute("filter");
this.setSelect = this.getAttribute("select");
this.setValue = this.getAttribute("value");
this.setEmptyValue = this.getAttribute("emptyValue");
// Set context variable
@ -56,7 +57,15 @@ SetWidget.prototype.getValue = function() {
if(this.setFilter) {
var results = this.wiki.filterTiddlers(this.setFilter,this);
if(!this.setValue) {
value = $tw.utils.stringifyList(results);
var select;
if(this.setSelect) {
select = parseInt(this.setSelect,10);
}
if(select !== undefined) {
value = results[select] || "";
} else {
value = $tw.utils.stringifyList(results);
}
}
if(results.length === 0 && this.setEmptyValue !== undefined) {
value = this.setEmptyValue;
@ -72,7 +81,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of
*/
SetWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.name || changedAttributes.filter || changedAttributes.value || changedAttributes.emptyValue ||
if(changedAttributes.name || changedAttributes.filter || changedAttributes.select ||changedAttributes.value || changedAttributes.emptyValue ||
(this.setFilter && this.getValue() != this.variables[this.setName].value)) {
this.refreshSelf();
return true;

View File

@ -1,8 +1,9 @@
title: SetWidget
created: 201311151827
modified: 20150220162217000
tags: Widgets
caption: set
created: 20131115182700000
modified: 20161017122456014
tags: Widgets
title: SetWidget
type: text/vnd.tiddlywiki
! Introduction
@ -16,6 +17,7 @@ The content of the `<$set>` widget is the scope for the value assigned to the va
|name |The name of the variable to assign (defaults to "currentTiddler") |
|value |The value to assign to the variable if the filter is missing or not empty |
|filter |An optional filter to be evaluated and assigned to the variable (see below) |
|select |<<.from-version "5.1.14">> An optional zero-based index of the item to return from the filter output (see below) |
|emptyValue |The value to assign to the variable if the filter is present and evaluates to an empty list (see below) |
!! Simple Variable Assignment
@ -55,3 +57,13 @@ This form of the set variable widget evaluates the filter and assigns the result
<$text text=<<myVariable>>/>
</$set>
```
!! Filtered Item Variable Assignment
<<.from-version "5.1.14">> This form of the set variable widget evaluates the filter and assigns the specified result to the variable as a single item (ie, not using double square brackets for titles containing spaces).
```
<$set name="myVariable" filter="[tag[HelloThere]]" select="0">
<$text text=<<myVariable>>/>
</$set>
```