From 62881a5aaa1db5caebd7fb2aa3bc58885dcdce98 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Thu, 24 Mar 2022 23:07:41 +0700 Subject: [PATCH] WIP on implementing filter attr for checkboxes --- core/modules/widgets/checkbox.js | 18 ++++++++++++------ editions/test/tiddlers/tests/test-widget.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/core/modules/widgets/checkbox.js b/core/modules/widgets/checkbox.js index e835552eb..24ed5def7 100644 --- a/core/modules/widgets/checkbox.js +++ b/core/modules/widgets/checkbox.js @@ -59,7 +59,7 @@ CheckboxWidget.prototype.render = function(parent,nextSibling) { CheckboxWidget.prototype.getValue = function() { var tiddler = this.wiki.getTiddler(this.checkboxTitle); - if(tiddler) { + if(tiddler || this.checkboxFilter) { if(this.checkboxTag) { if(this.checkboxInvertTag) { return !tiddler.hasTag(this.checkboxTag); @@ -90,12 +90,17 @@ CheckboxWidget.prototype.getValue = function() { return false; } } - if(this.checkboxListField) { + if(this.checkboxListField || this.checkboxFilter) { + // Same logic applies to lists and filters var list; - if($tw.utils.hop(tiddler.fields,this.checkboxListField)) { - list = tiddler.getFieldList(this.checkboxListField); + if(this.checkboxListField) { + if($tw.utils.hop(tiddler.fields,this.checkboxListField)) { + list = tiddler.getFieldList(this.checkboxListField); + } else { + list = $tw.utils.parseStringArray(this.checkboxDefault || "") || []; + } } else { - list = $tw.utils.parseStringArray(this.checkboxDefault || "") || []; + list = this.wiki.filterTiddlers(this.checkboxFilter,this) || []; } if(list.indexOf(this.checkboxChecked) !== -1) { return true; @@ -233,6 +238,7 @@ CheckboxWidget.prototype.execute = function() { this.checkboxField = this.getAttribute("field"); this.checkboxIndex = this.getAttribute("index"); this.checkboxListField = this.getAttribute("listField"); + this.checkboxFilter = this.getAttribute("filter"); this.checkboxChecked = this.getAttribute("checked"); this.checkboxUnchecked = this.getAttribute("unchecked"); this.checkboxDefault = this.getAttribute("default"); @@ -248,7 +254,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.invertTag || changedAttributes.field || changedAttributes.index || changedAttributes.checked || changedAttributes.unchecked || changedAttributes["default"] || changedAttributes["class"] || changedAttributes.disabled) { + if(changedAttributes.tiddler || changedAttributes.tag || changedAttributes.invertTag || changedAttributes.field || changedAttributes.index || changedAttributes.listField || changedAttributes.filter || changedAttributes.checked || changedAttributes.unchecked || changedAttributes["default"] || changedAttributes["class"] || changedAttributes.disabled) { this.refreshSelf(); return true; } else { diff --git a/editions/test/tiddlers/tests/test-widget.js b/editions/test/tiddlers/tests/test-widget.js index e126c5d29..63e048800 100755 --- a/editions/test/tiddlers/tests/test-widget.js +++ b/editions/test/tiddlers/tests/test-widget.js @@ -269,6 +269,24 @@ describe("Widget module", function() { startsOutChecked: false, expectedChange: { "Colors": { colors: "orange yellow green" } } }, + { + testName: "filter mode 1", + tiddlers: [{title: "Colors", colors: "red orange yellow"}], + widgetText: "\\define checkActions() <$action-listops $tiddler='Colors' $field='colors' $subfilter='-red green'/>\n" + + "\\define uncheckActions() <$action-listops $tiddler='Colors' $field='colors' $subfilter='red -green'/>\n" + + "<$checkbox filter='[list[Colors!!colors]]' checked='green' unchecked='red' default='green' checkactions=<> uncheckactions=<> />", + startsOutChecked: false, + expectedChange: { "Colors": { colors: "orange yellow green" } } + }, + { + testName: "filter mode 2", + tiddlers: [{title: "Colors", colors: "green orange yellow"}], + widgetText: "\\define checkActions() <$action-listops $tiddler='Colors' $field='colors' $subfilter='-red green'/>\n" + + "\\define uncheckActions() <$action-listops $tiddler='Colors' $field='colors' $subfilter='red -green'/>\n" + + "<$checkbox filter='[list[Colors!!colors]]' checked='green' unchecked='red' default='green' checkactions=<> uncheckactions=<> />", + startsOutChecked: true, + expectedChange: { "Colors": { colors: "orange yellow red" } } + }, ]; for (const data of checkboxTestData) {