mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 10:07:19 +00:00
Refactor wiki.filterTiddlers()
Now we pass a widget instead of the current tiddler title. We can use widget.getVariable(“currentTiddler”) to get the current tiddler.
This commit is contained in:
parent
61c204366f
commit
385c7e207c
@ -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;
|
||||
});
|
||||
|
@ -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 [];
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user