1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Extend the filter mechanism to allow operands to be indirected through a tiddler text reference

In other words, as well as `[sort[myfield]]` to sort by the field
called `myfield`, one can do `[sort{MyTiddler}]` to sort by the field
whose name is in the text of the tiddler `MyTiddler`.
This commit is contained in:
Jeremy Ruston 2013-05-27 17:57:37 +01:00
parent 9193937125
commit f4a3b25d3e

View File

@ -20,7 +20,7 @@ Parses an operation within a filter string
Returns the new start position, after the parsed operation Returns the new start position, after the parsed operation
*/ */
function parseFilterOperation(operators,filterString,p) { function parseFilterOperation(operators,filterString,p) {
var operator, operand, bracketPos; var operator, operand, bracketPos, curlyBracketPos;
// Skip the starting square bracket // Skip the starting square bracket
if(filterString.charAt(p++) !== "[") { if(filterString.charAt(p++) !== "[") {
throw "Missing [ in filter expression"; throw "Missing [ in filter expression";
@ -34,18 +34,27 @@ function parseFilterOperation(operators,filterString,p) {
} }
// Get the operator name // Get the operator name
bracketPos = filterString.indexOf("[",p); bracketPos = filterString.indexOf("[",p);
if(bracketPos === -1) { curlyBracketPos = filterString.indexOf("{",p);
if((bracketPos === -1) && (curlyBracketPos === -1)) {
throw "Missing [ in filter expression"; throw "Missing [ in filter expression";
} }
if(bracketPos === -1 || (curlyBracketPos !== -1 && curlyBracketPos < bracketPos)) {
// Curly brackets
operator.indirect = true;
operator.operator = filterString.substring(p,curlyBracketPos);
p = curlyBracketPos + 1;
} else {
// Square brackets
operator.operator = filterString.substring(p,bracketPos); operator.operator = filterString.substring(p,bracketPos);
p = bracketPos + 1;
}
if(operator.operator === "") { if(operator.operator === "") {
operator.operator = "title"; operator.operator = "title";
} }
p = bracketPos + 1;
// Get the operand // Get the operand
bracketPos = filterString.indexOf("]",p); bracketPos = filterString.indexOf(operator.indirect ? "}" : "]",p);
if(bracketPos === -1) { if(bracketPos === -1) {
throw "Missing ] in filter expression"; throw "Missing closing bracket in filter expression";
} }
operator.operand = filterString.substring(p,bracketPos); operator.operand = filterString.substring(p,bracketPos);
p = bracketPos + 1; p = bracketPos + 1;
@ -137,8 +146,19 @@ exports.compileFilter = function(filterString) {
$tw.utils.each(operation.operators,function(operator) { $tw.utils.each(operation.operators,function(operator) {
var operatorFunction = filterOperators[operator.operator] || filterOperators.field || function(source,operator,operations) { var operatorFunction = filterOperators[operator.operator] || filterOperators.field || function(source,operator,operations) {
return ["Filter Error: unknown operator '" + operator.operator + "'"]; return ["Filter Error: unknown operator '" + operator.operator + "'"];
}; },
results = operatorFunction(accumulator,operator,{wiki: self, currTiddlerTitle: currTiddlerTitle}); operand = operator.operand;
if(operator.indirect) {
operand = self.getTextReference(operator.operand,"",currTiddlerTitle);
}
results = operatorFunction(accumulator,{
operator: operator.operator,
operand: operand,
prefix: operator.prefix
},{
wiki: self,
currTiddlerTitle: currTiddlerTitle
});
accumulator = results; accumulator = results;
}); });
return results; return results;