From d6d3aab36ab4a684e5108894e1adfb0dba362fd4 Mon Sep 17 00:00:00 2001 From: Tobias Beer Date: Sat, 17 Dec 2016 12:51:24 +0100 Subject: [PATCH] Allow radio widget to work with indexes in a data tiddler (#2104) * allow radio widget to set an index in a data tiddler * updated RadioWidget docs, with same demo macro as for CheckboxWidget in #2103 * removed docs in widget code (seems the wrong place) * added from version to docs * revert doc maros to master * using wikitext-example-without-html and .tip macro now * fix quotes --- core/modules/widgets/radio.js | 34 ++++++++----------- .../tw5.com/tiddlers/system/doc-macros.tid | 2 +- .../tw5.com/tiddlers/widgets/RadioWidget.tid | 28 ++++++++------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/core/modules/widgets/radio.js b/core/modules/widgets/radio.js index 09a053c5b..daa06ae11 100644 --- a/core/modules/widgets/radio.js +++ b/core/modules/widgets/radio.js @@ -3,22 +3,7 @@ title: $:/core/modules/widgets/radio.js type: application/javascript module-type: widget -Radio widget - -Will set a field to the selected value: - -``` - <$radio field="myfield" value="check 1">one - <$radio field="myfield" value="check 2">two - <$radio field="myfield" value="check 3">three -``` - -|Parameter |Description |h -|tiddler |Name of the tiddler in which the field should be set. Defaults to current tiddler | -|field |The name of the field to be set | -|value |The value to set | -|class |Optional class name(s) | - +Set a field or index at a given tiddler via radio buttons \*/ (function(){ @@ -70,12 +55,20 @@ RadioWidget.prototype.render = function(parent,nextSibling) { }; RadioWidget.prototype.getValue = function() { - var tiddler = this.wiki.getTiddler(this.radioTitle); - return tiddler && tiddler.getFieldString(this.radioField); + var value, + tiddler = this.wiki.getTiddler(this.radioTitle); + if (this.radioIndex) { + value = this.wiki.extractTiddlerDataItem(this.radioTitle,this.radioIndex); + } else { + value = tiddler && tiddler.getFieldString(this.radioField); + } + return value; }; RadioWidget.prototype.setValue = function() { - if(this.radioField) { + if(this.radioIndex) { + this.wiki.setText(this.radioTitle,"",this.radioIndex,this.radioValue); + } else { var tiddler = this.wiki.getTiddler(this.radioTitle), addition = {}; addition[this.radioField] = this.radioValue; @@ -96,6 +89,7 @@ RadioWidget.prototype.execute = function() { // Get the parameters from the attributes this.radioTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler")); this.radioField = this.getAttribute("field","text"); + this.radioIndex = this.getAttribute("index"); this.radioValue = this.getAttribute("value"); this.radioClass = this.getAttribute("class",""); if(this.radioClass !== "") { @@ -111,7 +105,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of */ RadioWidget.prototype.refresh = function(changedTiddlers) { var changedAttributes = this.computeAttributes(); - if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.value || changedAttributes["class"]) { + if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.value || changedAttributes["class"]) { this.refreshSelf(); return true; } else { diff --git a/editions/tw5.com/tiddlers/system/doc-macros.tid b/editions/tw5.com/tiddlers/system/doc-macros.tid index e256bc1cd..86c31e6ea 100644 --- a/editions/tw5.com/tiddlers/system/doc-macros.tid +++ b/editions/tw5.com/tiddlers/system/doc-macros.tid @@ -106,4 +106,4 @@ This is an example tiddler. See [[Table-of-Contents Macros (Examples)]]. -\end +\end \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/widgets/RadioWidget.tid b/editions/tw5.com/tiddlers/widgets/RadioWidget.tid index b0339c656..21b1ca575 100644 --- a/editions/tw5.com/tiddlers/widgets/RadioWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/RadioWidget.tid @@ -1,9 +1,9 @@ +caption: radio created: 20131212195353929 -modified: 20150220161302000 +modified: 20161217112444671 tags: Widgets title: RadioWidget type: text/vnd.tiddlywiki -caption: radio ! Introduction @@ -15,22 +15,26 @@ The content of the `<$radio>` widget is displayed within an HTML `