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

add invert parameter to checkbox plugin

for when you want a tag to denote the lack of a check rather than the
presence of one.
This commit is contained in:
James Welford Anderson 2015-06-26 05:10:13 +09:00
parent f3c066ff48
commit 379181ded5

View File

@ -58,7 +58,11 @@ CheckboxWidget.prototype.getValue = function() {
var tiddler = this.wiki.getTiddler(this.checkboxTitle);
if(tiddler) {
if(this.checkboxTag) {
return tiddler.hasTag(this.checkboxTag);
if(this.checkboxInvert) {
return !tiddler.hasTag(this.checkboxTag);
} else {
return tiddler.hasTag(this.checkboxTag);
}
}
if(this.checkboxField) {
var value = tiddler.fields[this.checkboxField] || this.checkboxDefault || "";
@ -90,15 +94,23 @@ CheckboxWidget.prototype.handleChangeEvent = function(event) {
tiddler = this.wiki.getTiddler(this.checkboxTitle),
fallbackFields = {text: ""},
newFields = {title: this.checkboxTitle},
hasChanged = false;
hasChanged = false,
tagCheck = false;
if(this.checkboxTag && this.checkboxInvert === "yes") {
tagCheck = tiddler.hasTag(this.checkboxTag) === checked;
} else {
tagCheck = tiddler.hasTag(this.checkboxTag) !== checked;
}
// Set the tag if specified
if(this.checkboxTag && (!tiddler || tiddler.hasTag(this.checkboxTag) !== checked)) {
if(this.checkboxTag && (!tiddler || tagCheck)) {
newFields.tags = tiddler ? (tiddler.fields.tags || []).slice(0) : [];
var pos = newFields.tags.indexOf(this.checkboxTag);
if(pos !== -1) {
newFields.tags.splice(pos,1);
}
if(checked) {
if(this.checkboxInvert === "yes" && !checked) {
newFields.tags.push(this.checkboxTag);
} else if(this.checkboxInvert !== "yes" && checked) {
newFields.tags.push(this.checkboxTag);
}
hasChanged = true;
@ -128,6 +140,7 @@ CheckboxWidget.prototype.execute = function() {
this.checkboxUnchecked = this.getAttribute("unchecked");
this.checkboxDefault = this.getAttribute("default");
this.checkboxClass = this.getAttribute("class","");
this.checkboxInvert = this.getAttribute("invert","");
// Make the child widgets
this.makeChildWidgets();
};
@ -152,4 +165,4 @@ CheckboxWidget.prototype.refresh = function(changedTiddlers) {
exports.checkbox = CheckboxWidget;
})();
})();