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

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
This commit is contained in:
Tobias Beer 2016-12-17 12:51:24 +01:00 committed by Jeremy Ruston
parent 424b8a1f68
commit d6d3aab36a
3 changed files with 31 additions and 33 deletions

View File

@ -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>
<$radio field="myfield" value="check 2">two</$radio>
<$radio field="myfield" value="check 3">three</$radio>
```
|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 {

View File

@ -106,4 +106,4 @@ This is an example tiddler. See [[Table-of-Contents Macros (Examples)]].
</blockquote>
</$reveal>
</$list>
\end
\end

View File

@ -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 `<label>` eleme
|!Attribute |!Description |
|tiddler |Title of the tiddler to manipulate (defaults to the [[current tiddler|Current Tiddler]]) |
|field |The name of the field to which the radio button will be bound |
|value |The value for the tiddler field |
|field |The field of the //tiddler// bound to the radio button|
|index|<<.from-version "5.1.14">> The index of the //tiddler// being [[DataTiddler|DataTiddlers]] bound to the radio button<<.tip "takes precedence over //field//">>|
|value |The value for the //field// or //index// of the //tiddler//|
|class |CSS classes to be assigned to the label around the radio button |
! Example
!! Field Mode
This example uses the radio widget to change the `modifier` field of this tiddler:
```
<$radio field="modifier" value="JoeBloggs"> Joe Bloggs</$radio>
<<wikitext-example-without-html """<$radio field="modifier" value="JoeBloggs"> Joe Bloggs</$radio>
<$radio field="modifier" value="JaneBloggs"> Jane Bloggs</$radio>""">>
<$radio field="modifier" value="JaneBloggs"> Jane Bloggs</$radio>
```
It renders as:
!! Index Mode
<$radio field="modifier" value="JoeBloggs"> Joe Bloggs</$radio>
Using the radio widget in index mode requires the //index// attribute to specify the name of the index of a [[DataTiddler|DataTiddlers]] to which the specified //value// is assigned.
<$radio field="modifier" value="JaneBloggs"> Jane Bloggs</$radio>
This example sets the `Tree Frog` index in the tiddler AnimalColours:
<$macrocall $name="wikitext-example-without-html" src="""<$tiddler tiddler="AnimalColours">
<$radio index="Tree Frog" value="green"> green</$radio>
<$radio index="Tree Frog" value="brown"> brown</$radio>
</$tiddler>"""/>