mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 15:42:59 +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:
		| @@ -40,6 +40,7 @@ SetWidget.prototype.execute = function() { | |||||||
| 	// Get our parameters | 	// Get our parameters | ||||||
| 	this.setName = this.getAttribute("name","currentTiddler"); | 	this.setName = this.getAttribute("name","currentTiddler"); | ||||||
| 	this.setFilter = this.getAttribute("filter"); | 	this.setFilter = this.getAttribute("filter"); | ||||||
|  | 	this.setSelect = this.getAttribute("select"); | ||||||
| 	this.setValue = this.getAttribute("value"); | 	this.setValue = this.getAttribute("value"); | ||||||
| 	this.setEmptyValue = this.getAttribute("emptyValue"); | 	this.setEmptyValue = this.getAttribute("emptyValue"); | ||||||
| 	// Set context variable | 	// Set context variable | ||||||
| @@ -56,7 +57,15 @@ SetWidget.prototype.getValue = function() { | |||||||
| 	if(this.setFilter) { | 	if(this.setFilter) { | ||||||
| 		var results = this.wiki.filterTiddlers(this.setFilter,this); | 		var results = this.wiki.filterTiddlers(this.setFilter,this); | ||||||
| 		if(!this.setValue) { | 		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) { | 		if(results.length === 0 && this.setEmptyValue !== undefined) { | ||||||
| 			value = this.setEmptyValue; | 			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) { | SetWidget.prototype.refresh = function(changedTiddlers) { | ||||||
| 	var changedAttributes = this.computeAttributes(); | 	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.setFilter && this.getValue() != this.variables[this.setName].value)) { | ||||||
| 		this.refreshSelf(); | 		this.refreshSelf(); | ||||||
| 		return true; | 		return true; | ||||||
|   | |||||||
| @@ -1,8 +1,9 @@ | |||||||
| title: SetWidget |  | ||||||
| created: 201311151827 |  | ||||||
| modified: 20150220162217000 |  | ||||||
| tags: Widgets |  | ||||||
| caption: set | caption: set | ||||||
|  | created: 20131115182700000 | ||||||
|  | modified: 20161017122456014 | ||||||
|  | tags: Widgets | ||||||
|  | title: SetWidget | ||||||
|  | type: text/vnd.tiddlywiki | ||||||
|  |  | ||||||
| ! Introduction | ! 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") | | |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 | | |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) | | |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) | | |emptyValue |The value to assign to the variable if the filter is present and evaluates to an empty list (see below) | | ||||||
|  |  | ||||||
| !! Simple Variable Assignment | !! Simple Variable Assignment | ||||||
| @@ -55,3 +57,13 @@ This form of the set variable widget evaluates the filter and assigns the result | |||||||
| <$text text=<<myVariable>>/> | <$text text=<<myVariable>>/> | ||||||
| </$set> | </$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> | ||||||
|  | ``` | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jermolene
					Jermolene