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

Extend the set widget with tiddler, field and index attributes

This commit is contained in:
Jermolene 2017-07-12 16:59:56 +01:00
parent c25a1eef33
commit 5dd3d97760
2 changed files with 40 additions and 5 deletions

View File

@ -41,6 +41,9 @@ SetWidget.prototype.execute = function() {
this.setName = this.getAttribute("name","currentTiddler");
this.setFilter = this.getAttribute("filter");
this.setSelect = this.getAttribute("select");
this.setTiddler = this.getAttribute("tiddler");
this.setField = this.getAttribute("field");
this.setIndex = this.getAttribute("index");
this.setValue = this.getAttribute("value");
this.setEmptyValue = this.getAttribute("emptyValue");
// Set context variable
@ -54,7 +57,18 @@ Get the value to be assigned
*/
SetWidget.prototype.getValue = function() {
var value = this.setValue;
if(this.setFilter) {
if(this.setTiddler) {
var tiddler = this.wiki.getTiddler(this.setTiddler);
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) {
var results = this.wiki.filterTiddlers(this.setFilter,this);
if(!this.setValue) {
var select;
@ -73,7 +87,7 @@ SetWidget.prototype.getValue = function() {
} else if(!value && this.setEmptyValue) {
value = this.setEmptyValue;
}
return value;
return value || "";
};
/*
@ -81,7 +95,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.select ||changedAttributes.value || changedAttributes.emptyValue ||
if(changedAttributes.name || changedAttributes.filter || changedAttributes.select || changedAttributes.tiddler || (this.setTiddler && changedTiddlers[this.setTiddler]) || changedAttributes.field || changedAttributes.index || changedAttributes.value || changedAttributes.emptyValue ||
(this.setFilter && this.getValue() != this.variables[this.setName].value)) {
this.refreshSelf();
return true;

View File

@ -1,6 +1,6 @@
caption: set
created: 20131115182700000
modified: 20161017122456014
modified: 20170712153850528
tags: Widgets
title: SetWidget
type: text/vnd.tiddlywiki
@ -16,9 +16,12 @@ The content of the `<$set>` widget is the scope for the value assigned to the va
|!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 |
|tiddler |<<.from-version "5.1.15">> Optional title of the tiddler from which to read the value |
|field |<<.from-version "5.1.15">> Optional field of the tiddler from which to read the value (only used if ''tiddler'' is used) |
|index |<<.from-version "5.1.15">> Optional index of the tiddler from which to read the value (only used if ''tiddler'' is used) |
|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 specified value is missing or empty (see below) |
!! Simple Variable Assignment
@ -67,3 +70,21 @@ This form of the set variable widget evaluates the filter and assigns the result
<$text text=<<myVariable>>/>
</$set>
```
!! Transcluded Variable Assignment
<<.from-version "5.1.15">> This form of the set variable widget obtains the value to assign to the variable from a value in a tiddler field or index. For example:
```
<$set name="myVariable" tiddler="HelloThere" field="text">
<$text text=<<myVariable>>/>
</$set>
```
The example above could also be written as `<$set name="myVariable" value={{HelloThere!!text}}>`. The advantage of using the ''tiddler'' attribute is that the tiddler title and field or index can themselves be computed. For example:
```
<$set name="myVariable" tiddler=<<myTiddler>> field={{$:/currentField}}>
<$text text=<<myVariable>>/>
</$set>
```