mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 15:42:59 +00:00 
			
		
		
		
	Extend set variable widget to work with filters
This commit is contained in:
		| @@ -39,9 +39,21 @@ Compute the internal state of the widget | ||||
| SetWidget.prototype.execute = function() { | ||||
| 	// Get our parameters | ||||
| 	this.setName = this.getAttribute("name","currentTiddler"); | ||||
| 	this.setFilter = this.getAttribute("filter"); | ||||
| 	this.setValue = this.getAttribute("value"); | ||||
| 	this.setEmptyValue = this.getAttribute("emptyValue"); | ||||
| 	// Set context variable | ||||
| 	this.setVariable(this.setName,this.setValue,this.parseTreeNode.params); | ||||
| 	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); | ||||
| 	// Construct the child widgets | ||||
| 	this.makeChildWidgets(); | ||||
| }; | ||||
| @@ -51,7 +63,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.value) { | ||||
| 	if(changedAttributes.name || changedAttributes.filter || changedAttributes.value || changedAttributes.emptyValue) { | ||||
| 		this.refreshSelf(); | ||||
| 		return true; | ||||
| 	} else { | ||||
|   | ||||
							
								
								
									
										57
									
								
								editions/prerelease/tiddlers/SetWidget.tid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								editions/prerelease/tiddlers/SetWidget.tid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| title: SetWidget | ||||
| created: 201311151827 | ||||
| modified: 20141113212520422 | ||||
| tags: Widgets | ||||
| caption: set | ||||
|  | ||||
| ! Introduction | ||||
|  | ||||
| The set variable widget assigns a value to a specified [[variable|WidgetVariables]]. The new value of the variable is availale to the content within the set variable widget. | ||||
|  | ||||
| ! Content and Attributes | ||||
|  | ||||
| The content of the `<$set>` widget is the scope for the value assigned to the variable. | ||||
|  | ||||
| |!Attribute |!Description | | ||||
| |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) | | ||||
| |emptyValue |The value to assign to the variable if the filter is present and evaluates to an empty list (see below) | | ||||
|  | ||||
| !! Simple Variable Assignment | ||||
|  | ||||
| The simplest way of using set variable widget assigns a string to a variable. The following example assigns a literal string | ||||
|  | ||||
| ``` | ||||
| <$set name="myVariable" value="Some text"> | ||||
| <<myVariable>> | ||||
| </$set> | ||||
| ``` | ||||
|  | ||||
| Both the name and value attributes can be transcluded. For example: | ||||
|  | ||||
| ``` | ||||
| <$set name=<<anotherVariable>> value={{template!!text}}> | ||||
| <<myVariable>> | ||||
| </$set> | ||||
| ``` | ||||
|  | ||||
| !! Conditional Variable Assignment | ||||
|  | ||||
| This form of the set variable widget chooses one of two specified values according to whether a filter evaluates to an empty list. Here's an example that sets a variable according to whether the current tiddler is called "myMagicTitle": | ||||
|  | ||||
| ``` | ||||
| <$set name="myVariable" filter="[all[current]field:title[myMagicTitle]]" value="It's magic" emptyValue="It's not magic"> | ||||
| <<myVariable>> | ||||
| </$set> | ||||
| ``` | ||||
|  | ||||
| !! Filtered List Variable Assignment | ||||
|  | ||||
| This form of the set variable widget evaluates the filter and assigns the result to the variable as a space-separated list (using double square brackets for titles containing spaces). | ||||
|  | ||||
| ``` | ||||
| <$set name="myVariable" filter="[tag[Introduction]]"> | ||||
| <<myVariable>> | ||||
| </$set> | ||||
| ``` | ||||
		Reference in New Issue
	
	Block a user
	 Jermolene
					Jermolene