mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-25 01:20:30 +00:00
Extend checkbox widget to toggle fields
This commit is contained in:
parent
4743d0a863
commit
f08f57c5d2
@ -55,22 +55,52 @@ CheckboxWidget.prototype.render = function(parent,nextSibling) {
|
|||||||
|
|
||||||
CheckboxWidget.prototype.getValue = function() {
|
CheckboxWidget.prototype.getValue = function() {
|
||||||
var tiddler = this.wiki.getTiddler(this.checkboxTitle);
|
var tiddler = this.wiki.getTiddler(this.checkboxTitle);
|
||||||
return tiddler ? tiddler.hasTag(this.checkboxTag) : false;
|
if(tiddler) {
|
||||||
|
if(this.checkboxTag) {
|
||||||
|
return tiddler.hasTag(this.checkboxTag);
|
||||||
|
}
|
||||||
|
if(this.checkboxField) {
|
||||||
|
var value = tiddler.fields[this.checkboxField] || this.checkboxDefault || "";
|
||||||
|
if(value === this.checkboxChecked) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(value === this.checkboxUnchecked) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckboxWidget.prototype.handleChangeEvent = function(event) {
|
CheckboxWidget.prototype.handleChangeEvent = function(event) {
|
||||||
var checked = this.inputDomNode.checked,
|
var checked = this.inputDomNode.checked,
|
||||||
tiddler = this.wiki.getTiddler(this.checkboxTitle);
|
tiddler = this.wiki.getTiddler(this.checkboxTitle),
|
||||||
if(tiddler && tiddler.hasTag(this.checkboxTag) !== checked) {
|
newFields = {},
|
||||||
var newTags = (tiddler.fields.tags || []).slice(0),
|
hasChanged = false;
|
||||||
pos = newTags.indexOf(this.checkboxTag);
|
if(tiddler) {
|
||||||
|
// Set the tag if specified
|
||||||
|
if(this.checkboxTag && tiddler.hasTag(this.checkboxTag) !== checked) {
|
||||||
|
newFields.tags = (tiddler.fields.tags || []).slice(0);
|
||||||
|
var pos = newFields.tags.indexOf(this.checkboxTag);
|
||||||
if(pos !== -1) {
|
if(pos !== -1) {
|
||||||
newTags.splice(pos,1);
|
newFields.tags.splice(pos,1);
|
||||||
}
|
}
|
||||||
if(checked) {
|
if(checked) {
|
||||||
newTags.push(this.checkboxTag);
|
newFields.tags.push(this.checkboxTag);
|
||||||
|
}
|
||||||
|
hasChanged = true;
|
||||||
|
}
|
||||||
|
// Set the field if specified
|
||||||
|
if(this.checkboxField) {
|
||||||
|
var value = checked ? this.checkboxChecked : this.checkboxUnchecked;
|
||||||
|
if(tiddler.fields[this.checkboxField] !== value) {
|
||||||
|
newFields[this.checkboxField] = value;
|
||||||
|
hasChanged = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(hasChanged) {
|
||||||
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,newFields,this.wiki.getModificationFields()));
|
||||||
}
|
}
|
||||||
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{tags: newTags},this.wiki.getModificationFields()));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,6 +111,10 @@ CheckboxWidget.prototype.execute = function() {
|
|||||||
// Get the parameters from the attributes
|
// Get the parameters from the attributes
|
||||||
this.checkboxTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
this.checkboxTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
||||||
this.checkboxTag = this.getAttribute("tag");
|
this.checkboxTag = this.getAttribute("tag");
|
||||||
|
this.checkboxField = this.getAttribute("field");
|
||||||
|
this.checkboxChecked = this.getAttribute("checked");
|
||||||
|
this.checkboxUnchecked = this.getAttribute("unchecked");
|
||||||
|
this.checkboxDefault = this.getAttribute("default");
|
||||||
// Make the child widgets
|
// Make the child widgets
|
||||||
this.makeChildWidgets();
|
this.makeChildWidgets();
|
||||||
};
|
};
|
||||||
@ -90,7 +124,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of
|
|||||||
*/
|
*/
|
||||||
CheckboxWidget.prototype.refresh = function(changedTiddlers) {
|
CheckboxWidget.prototype.refresh = function(changedTiddlers) {
|
||||||
var changedAttributes = this.computeAttributes();
|
var changedAttributes = this.computeAttributes();
|
||||||
if(changedAttributes.tiddler || changedAttributes.tag || changedAttributes["class"]) {
|
if(changedAttributes.tiddler || changedAttributes.tag || changedAttributes.field || changedAttributes.checked || changedAttributes.unchecked || changedAttributes["default"] || changedAttributes["class"]) {
|
||||||
this.refreshSelf();
|
this.refreshSelf();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
title: CheckboxWidget
|
title: CheckboxWidget
|
||||||
created: 201310241419
|
created: 201310241419
|
||||||
modified: 201310300837
|
modified: 201407110837
|
||||||
tags: widget
|
tags: widget
|
||||||
|
|
||||||
! Introduction
|
! Introduction
|
||||||
|
|
||||||
The checkbox widget displays an HTML `<input type="checkbox">` element that is dynamically bound to the presence or absence of a specified tag on a specified tiddler.
|
The checkbox widget displays an HTML `<input type="checkbox">` element that is dynamically bound to either:
|
||||||
|
|
||||||
|
* the presence or absence of a specified tag on a specified tiddler
|
||||||
|
* the value of a specified field of a specified tiddler
|
||||||
|
|
||||||
! Content and Attributes
|
! Content and Attributes
|
||||||
|
|
||||||
@ -14,3 +17,27 @@ The content of the `<$checkbox>` widget is displayed within an HTML `<label>` el
|
|||||||
|!Attribute |!Description |
|
|!Attribute |!Description |
|
||||||
|tiddler |Title of the tiddler to manipulate (defaults to the [[WidgetVariable: currentTiddler]]) |
|
|tiddler |Title of the tiddler to manipulate (defaults to the [[WidgetVariable: currentTiddler]]) |
|
||||||
|tag |The name of the tag to which the checkbox should be bound |
|
|tag |The name of the tag to which the checkbox should be bound |
|
||||||
|
|field |The name of the field to which the checkbox should be bound |
|
||||||
|
|checked |The value of the field corresponding to the checkbox being checked |
|
||||||
|
|unchecked |The value of the field corresponding to the checkbox being unchecked |
|
||||||
|
|default |The default value to use if the field is not defined |
|
||||||
|
|
||||||
|
!! Tag Mode
|
||||||
|
|
||||||
|
Using the checkbox widget in tag mode requires the ''tag'' attribute to specify the name of the tag. The ''tiddler'' attribute specifies the tiddler to target, defaulting to the current tiddler if not present.
|
||||||
|
|
||||||
|
This example creates a checkbox that flips the ''done'' tag on the current tiddler:
|
||||||
|
|
||||||
|
```
|
||||||
|
<$checkbox tag="done">Is it done?</$checkbox>
|
||||||
|
```
|
||||||
|
|
||||||
|
!! Field Mode
|
||||||
|
|
||||||
|
Using the checkbox widget in field mode requires the ''field'' attribute to specify the name of the field. The ''checked'' and ''unchecked'' attributes specify the values to be assigned to the field to correspond to its checked and unchecked states respectively. The ''default'' attribute is used as a fallback value if the field is not defined.
|
||||||
|
|
||||||
|
This example creates a checkbox that is checked if the field ''status'' is equal to ''open'' and unchecked if the field is equal to ''closed''. If the field is undefined then it defaults to ''closed'', meaning that the checkbox will be unchecked if the ''status'' field is missing.
|
||||||
|
|
||||||
|
```
|
||||||
|
<$checkbox field="status" checked="open" unchecked="closed" default="closed">Is it open?</$checkbox>
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user