diff --git a/core/modules/widgets/setvariable.js b/core/modules/widgets/setvariable.js index f75fa2c98..76844b62d 100755 --- a/core/modules/widgets/setvariable.js +++ b/core/modules/widgets/setvariable.js @@ -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; diff --git a/editions/tw5.com/tiddlers/widgets/SetWidget.tid b/editions/tw5.com/tiddlers/widgets/SetWidget.tid index c4204be59..bf1ec571f 100644 --- a/editions/tw5.com/tiddlers/widgets/SetWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/SetWidget.tid @@ -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=<>/> ``` + +!! 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=<>/> + +```