From 5dd3d97760083f7c6ba9a6cd6186525edb3fb836 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 12 Jul 2017 16:59:56 +0100 Subject: [PATCH] Extend the set widget with tiddler, field and index attributes --- core/modules/widgets/setvariable.js | 20 ++++++++++++--- .../tw5.com/tiddlers/widgets/SetWidget.tid | 25 +++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/core/modules/widgets/setvariable.js b/core/modules/widgets/setvariable.js index 76844b62d..3da727066 100755 --- a/core/modules/widgets/setvariable.js +++ b/core/modules/widgets/setvariable.js @@ -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; diff --git a/editions/tw5.com/tiddlers/widgets/SetWidget.tid b/editions/tw5.com/tiddlers/widgets/SetWidget.tid index bf1ec571f..214aab9cc 100644 --- a/editions/tw5.com/tiddlers/widgets/SetWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/SetWidget.tid @@ -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=<>/> ``` + +!! 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=<>/> + +``` + +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=<> field={{$:/currentField}}> +<$text text=<>/> + +```