mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-26 00:46:52 +00:00
Experimental support for custom filter operators
Just as we can define custom widgets we can also define custom parameterised filter operators
This commit is contained in:
parent
a9938a6c67
commit
e01dfa1507
@ -244,13 +244,15 @@ exports.compileFilter = function(filterString) {
|
||||
var operands = [],
|
||||
operatorFunction;
|
||||
if(!operator.operator) {
|
||||
// Use the "title" operator if no operator is specified
|
||||
operatorFunction = filterOperators.title;
|
||||
} else if(!filterOperators[operator.operator]) {
|
||||
operatorFunction = filterOperators.field;
|
||||
// Unknown operators treated as "unknown" - at run time we can distinguish between a custom operator and falling back to the default "field" operator
|
||||
operatorFunction = filterOperators.unknown;
|
||||
} else {
|
||||
// Use the operator function
|
||||
operatorFunction = filterOperators[operator.operator];
|
||||
}
|
||||
|
||||
$tw.utils.each(operator.operands,function(operand) {
|
||||
if(operand.indirect) {
|
||||
operand.value = self.getTextReference(operand.text,"",currTiddlerTitle);
|
||||
|
59
core/modules/filters/unknown.js
Normal file
59
core/modules/filters/unknown.js
Normal file
@ -0,0 +1,59 @@
|
||||
/*\
|
||||
title: $:/core/modules/filters/unknown.js
|
||||
type: application/javascript
|
||||
module-type: filteroperator
|
||||
|
||||
Filter operator for handling unknown filter operators
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var fieldFilterOperatorFn = require("$:/core/modules/filters/field.js").field;
|
||||
|
||||
/*
|
||||
Export our filter function
|
||||
*/
|
||||
exports.unknown = function(source,operator,options) {
|
||||
var customDefinitionTitle = "[" + operator.operator + "[]]",
|
||||
customDefinition = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(customDefinitionTitle);
|
||||
if(customDefinition && customDefinition.srcVariable) {
|
||||
var variables = Object.create(null);
|
||||
$tw.utils.each(customDefinition.srcVariable.variableParams,function(param,index) {
|
||||
var value = operator.operands[index];
|
||||
if(value === undefined) {
|
||||
value = param["default"] || "";
|
||||
}
|
||||
variables[param.name] = value;
|
||||
});
|
||||
var getVariable = function(name,opts) {
|
||||
if(name in variables) {
|
||||
return variables[name];
|
||||
} else {
|
||||
return options.widget.getVariable(name,opts);
|
||||
};
|
||||
};
|
||||
var getVariableInfo = function(name,opts) {
|
||||
return options.widget.getVariableInfo(name,opts);
|
||||
}
|
||||
var list = options.wiki.filterTiddlers(customDefinition.srcVariable.value,{getVariable: getVariable,getVariableInfo: getVariableInfo},source);
|
||||
if(operator.prefix === "!") {
|
||||
var results = [];
|
||||
source(function(tiddler,title) {
|
||||
if(list.indexOf(title) === -1) {
|
||||
results.push(title);
|
||||
}
|
||||
});
|
||||
return results;
|
||||
} else {
|
||||
return list;
|
||||
}
|
||||
} else {
|
||||
return fieldFilterOperatorFn(source,operator,options);
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
@ -0,0 +1,24 @@
|
||||
title: CustomOperators/NestedParameterised
|
||||
description: Nested parameterised custom operator usage
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
\whitespace trim
|
||||
\function [dividebysomething[]](first:ignored,factor:0.5)
|
||||
[divide[2]multiply<factor>]
|
||||
\end
|
||||
|
||||
\function [multiplebysomething[]](first:ignored,factor:2)
|
||||
[multiply[2]dividebysomething[],<factor>]
|
||||
\end
|
||||
|
||||
<$text text={{{ [[123]multiplebysomething[]] }}}/>
|
||||
-
|
||||
<$text text={{{ [[123]multiplebysomething[x],[4]] }}}/>
|
||||
|
||||
+
|
||||
title: ExpectedResult
|
||||
|
||||
<p>246-492</p>
|
@ -0,0 +1,20 @@
|
||||
title: CustomOperators/Parameterised
|
||||
description: Parameterised custom operator usage
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
\whitespace trim
|
||||
\function [multiplybysomething[]](first:ignored,factor:2)
|
||||
[multiply[2]multiply<factor>]
|
||||
\end
|
||||
|
||||
<$text text={{{ [[123]multiplybysomething[]] }}}/>
|
||||
-
|
||||
<$text text={{{ [[123]multiplybysomething[x],[4]] }}}/>
|
||||
|
||||
+
|
||||
title: ExpectedResult
|
||||
|
||||
<p>492-984</p>
|
@ -0,0 +1,16 @@
|
||||
title: CustomOperators/Simple
|
||||
description: Simple custom operator usage
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
\function [multiplybytwo[]]()
|
||||
[multiply[2]]
|
||||
\end
|
||||
|
||||
<$text text={{{ [[123]multiplybytwo[]] }}}/>
|
||||
+
|
||||
title: ExpectedResult
|
||||
|
||||
<p>246</p>
|
Loading…
Reference in New Issue
Block a user