From 385c7e207cb82ad5dd50382ba71da3328dbdfbbc Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sun, 27 Apr 2014 20:03:33 +0100 Subject: [PATCH] Refactor wiki.filterTiddlers() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we pass a widget instead of the current tiddler title. We can use widget.getVariable(“currentTiddler”) to get the current tiddler. --- core/modules/filters.js | 33 +++++++++++++++-------------- core/modules/filters/all/current.js | 5 +++-- core/modules/filters/is/current.js | 7 +++--- core/modules/filters/list.js | 3 ++- core/modules/widgets/count.js | 2 +- core/modules/widgets/encrypt.js | 10 ++------- core/modules/widgets/list.js | 2 +- 7 files changed, 30 insertions(+), 32 deletions(-) diff --git a/core/modules/filters.js b/core/modules/filters.js index 729307c8b..b8ca1dae6 100644 --- a/core/modules/filters.js +++ b/core/modules/filters.js @@ -149,22 +149,22 @@ exports.getFilterOperators = function() { return this.filterOperators; }; -exports.filterTiddlers = function(filterString,currTiddlerTitle,source) { +exports.filterTiddlers = function(filterString,widget,source) { var fn = this.compileFilter(filterString); - return fn.call(this,source,currTiddlerTitle); + return fn.call(this,source,widget); }; /* -Compile a filter into a function with the signature fn(source,currTiddlerTitle) where: +Compile a filter into a function with the signature fn(source,widget) where: source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title) -currTiddlerTitle: the optional name of the current tiddler +widget: an optional widget node for retrieving the current tiddler etc. */ exports.compileFilter = function(filterString) { var filterParseTree; try { filterParseTree = this.parseFilter(filterString); } catch(e) { - return function(source,currTiddlerTitle) { + return function(source,widget) { return ["Filter error: " + e]; }; } @@ -176,9 +176,10 @@ exports.compileFilter = function(filterString) { var self = this; $tw.utils.each(filterParseTree,function(operation) { // Create a function for the chain of operators in the operation - var operationSubFunction = function(source,currTiddlerTitle) { + var operationSubFunction = function(source,widget) { var accumulator = source, - results = []; + results = [], + currTiddlerTitle = widget && widget.getVariable("currentTiddler"); $tw.utils.each(operation.operators,function(operator) { var operand = operator.operand, operatorFunction; @@ -200,7 +201,7 @@ exports.compileFilter = function(filterString) { regexp: operator.regexp },{ wiki: self, - currTiddlerTitle: currTiddlerTitle + widget: widget }); accumulator = self.makeTiddlerIterator(results); }); @@ -210,25 +211,25 @@ exports.compileFilter = function(filterString) { operationFunctions.push((function() { switch(operation.prefix || "") { case "": // No prefix means that the operation is unioned into the result - return function(results,source,currTiddlerTitle) { - $tw.utils.pushTop(results,operationSubFunction(source,currTiddlerTitle)); + return function(results,source,widget) { + $tw.utils.pushTop(results,operationSubFunction(source,widget)); }; case "-": // The results of this operation are removed from the main result - return function(results,source,currTiddlerTitle) { - $tw.utils.removeArrayEntries(results,operationSubFunction(source,currTiddlerTitle)); + return function(results,source,widget) { + $tw.utils.removeArrayEntries(results,operationSubFunction(source,widget)); }; case "+": // This operation is applied to the main results so far - return function(results,source,currTiddlerTitle) { + return function(results,source,widget) { // This replaces all the elements of the array, but keeps the actual array so that references to it are preserved source = self.makeTiddlerIterator(results); results.splice(0,results.length); - $tw.utils.pushTop(results,operationSubFunction(source,currTiddlerTitle)); + $tw.utils.pushTop(results,operationSubFunction(source,widget)); }; } })()); }); // Return a function that applies the operations to a source iterator of tiddler titles - return $tw.perf.measure("filter",function filterFunction(source,currTiddlerTitle) { + return $tw.perf.measure("filter",function filterFunction(source,widget) { if(!source) { source = self.each; } else if(typeof source === "object") { // Array or hashmap @@ -236,7 +237,7 @@ exports.compileFilter = function(filterString) { } var results = []; $tw.utils.each(operationFunctions,function(operationFunction) { - operationFunction(results,source,currTiddlerTitle); + operationFunction(results,source,widget); }); return results; }); diff --git a/core/modules/filters/all/current.js b/core/modules/filters/all/current.js index f1d5d9179..82ad8cca3 100644 --- a/core/modules/filters/all/current.js +++ b/core/modules/filters/all/current.js @@ -16,8 +16,9 @@ Filter function for [all[current]] Export our filter function */ exports.current = function(source,prefix,options) { - if(options.currTiddlerTitle) { - return [options.currTiddlerTitle]; + var currTiddlerTitle = options.widget && options.widget.getVariable("currentTiddler"); + if(currTiddlerTitle) { + return [currTiddlerTitle]; } else { return []; } diff --git a/core/modules/filters/is/current.js b/core/modules/filters/is/current.js index 4e014413a..b5b88f616 100644 --- a/core/modules/filters/is/current.js +++ b/core/modules/filters/is/current.js @@ -16,16 +16,17 @@ Filter function for [is[current]] Export our filter function */ exports.current = function(source,prefix,options) { - var results = []; + var results = [], + currTiddlerTitle = options.widget && options.widget.getVariable("currentTiddler"); if(prefix === "!") { source(function(tiddler,title) { - if(title !== options.currTiddlerTitle) { + if(title !== currTiddlerTitle) { results.push(title); } }); } else { source(function(tiddler,title) { - if(title === options.currTiddlerTitle) { + if(title === currTiddlerTitle) { results.push(title); } }); diff --git a/core/modules/filters/list.js b/core/modules/filters/list.js index efc51065a..1b23b9382 100644 --- a/core/modules/filters/list.js +++ b/core/modules/filters/list.js @@ -18,7 +18,8 @@ Export our filter function exports.list = function(source,operator,options) { var results = [], tr = $tw.utils.parseTextReference(operator.operand), - list = options.wiki.getTiddlerList(tr.title || options.currTiddlerTitle,tr.field,tr.index); + currTiddlerTitle = options.widget && options.widget.getVariable("currentTiddler"), + list = options.wiki.getTiddlerList(tr.title || currTiddlerTitle,tr.field,tr.index); if(operator.prefix === "!") { source(function(tiddler,title) { if(list.indexOf(title) === -1) { diff --git a/core/modules/widgets/count.js b/core/modules/widgets/count.js index f5d71eb9b..7522a40cd 100644 --- a/core/modules/widgets/count.js +++ b/core/modules/widgets/count.js @@ -43,7 +43,7 @@ CountWidget.prototype.execute = function() { this.filter = this.getAttribute("filter"); // Execute the filter if(this.filter) { - this.currentCount = this.wiki.filterTiddlers(this.filter,this.getVariable("currentTiddler")).length; + this.currentCount = this.wiki.filterTiddlers(this.filter,this).length; } else { this.currentCount = undefined; } diff --git a/core/modules/widgets/encrypt.js b/core/modules/widgets/encrypt.js index 3937cd4b8..28dfdaf78 100644 --- a/core/modules/widgets/encrypt.js +++ b/core/modules/widgets/encrypt.js @@ -60,14 +60,8 @@ EncryptWidget.prototype.execute = function() { Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering */ EncryptWidget.prototype.refresh = function(changedTiddlers) { - var changedAttributes = this.computeAttributes(), - affectedTiddlers = this.wiki.filterTiddlers(this.filter,null,changedTiddlers); - if(changedAttributes.filter || affectedTiddlers.length > 0) { - this.refreshSelf(); - return true; - } else { - return false; - } + // We don't need to worry about refreshing because the encrypt widget isn't for interactive use + return false; }; exports.encrypt = EncryptWidget; diff --git a/core/modules/widgets/list.js b/core/modules/widgets/list.js index 9da599f89..fd3b26dd9 100755 --- a/core/modules/widgets/list.js +++ b/core/modules/widgets/list.js @@ -80,7 +80,7 @@ ListWidget.prototype.execute = function() { ListWidget.prototype.getTiddlerList = function() { var defaultFilter = "[!is[system]sort[title]]"; - return this.wiki.filterTiddlers(this.getAttribute("filter",defaultFilter),this.getVariable("currentTiddler")); + return this.wiki.filterTiddlers(this.getAttribute("filter",defaultFilter),this); }; ListWidget.prototype.getEmptyMessage = function() {