From f08f57c5d24eb0146ac2cb77472a5fc5f135f1e9 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sat, 12 Jul 2014 09:09:13 +0100 Subject: [PATCH] Extend checkbox widget to toggle fields --- core/modules/widgets/checkbox.js | 56 +++++++++++++++---- .../tiddlers/widgets/CheckboxWidget.tid | 31 +++++++++- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/core/modules/widgets/checkbox.js b/core/modules/widgets/checkbox.js index 07cb95a7a..4ac023bc8 100644 --- a/core/modules/widgets/checkbox.js +++ b/core/modules/widgets/checkbox.js @@ -55,22 +55,52 @@ CheckboxWidget.prototype.render = function(parent,nextSibling) { CheckboxWidget.prototype.getValue = function() { 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) { var checked = this.inputDomNode.checked, - tiddler = this.wiki.getTiddler(this.checkboxTitle); - if(tiddler && tiddler.hasTag(this.checkboxTag) !== checked) { - var newTags = (tiddler.fields.tags || []).slice(0), - pos = newTags.indexOf(this.checkboxTag); - if(pos !== -1) { - newTags.splice(pos,1); + tiddler = this.wiki.getTiddler(this.checkboxTitle), + newFields = {}, + hasChanged = false; + 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) { + newFields.tags.splice(pos,1); + } + if(checked) { + newFields.tags.push(this.checkboxTag); + } + hasChanged = true; } - if(checked) { - newTags.push(this.checkboxTag); + // 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 this.checkboxTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler")); 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 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) { 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(); return true; } else { diff --git a/editions/tw5.com/tiddlers/widgets/CheckboxWidget.tid b/editions/tw5.com/tiddlers/widgets/CheckboxWidget.tid index a17090702..3ac13ba40 100644 --- a/editions/tw5.com/tiddlers/widgets/CheckboxWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/CheckboxWidget.tid @@ -1,11 +1,14 @@ title: CheckboxWidget created: 201310241419 -modified: 201310300837 +modified: 201407110837 tags: widget ! Introduction -The checkbox widget displays an HTML `` element that is dynamically bound to the presence or absence of a specified tag on a specified tiddler. +The checkbox widget displays an HTML `` 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 @@ -14,3 +17,27 @@ The content of the `<$checkbox>` widget is displayed within an HTML `