mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-07-06 12:02:49 +00:00
Minor refactoring
This commit is contained in:
parent
02f3065e4f
commit
d7f0c5cb6b
@ -8,23 +8,23 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var widgetClass = require("$:/core/modules/widgets/widget.js").widget;
|
||||
var widgetClass = require("$:/core/modules/widgets/widget.js").widget;
|
||||
|
||||
/* Maximum permitted filter recursion depth */
|
||||
var MAX_FILTER_DEPTH = 300;
|
||||
/* Maximum permitted filter recursion depth */
|
||||
var MAX_FILTER_DEPTH = 300;
|
||||
|
||||
/*
|
||||
Parses an operation (i.e. a run) within a filter string
|
||||
/*
|
||||
Parses an operation (i.e. a run) within a filter string
|
||||
operators: Array of array of operator nodes into which results should be inserted
|
||||
filterString: filter string
|
||||
p: start position within the string
|
||||
Returns the new start position, after the parsed operation
|
||||
*/
|
||||
function parseFilterOperation(operators,filterString,p) {
|
||||
Returns the new start position, after the parsed operation
|
||||
*/
|
||||
function parseFilterOperation(operators,filterString,p) {
|
||||
var nextBracketPos, operator;
|
||||
// Skip the starting square bracket
|
||||
if(filterString.charAt(p++) !== "[") {
|
||||
@ -133,12 +133,12 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
}
|
||||
// Return the parsing position
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Parse a filter string
|
||||
*/
|
||||
exports.parseFilter = function(filterString) {
|
||||
/*
|
||||
Parse a filter string
|
||||
*/
|
||||
exports.parseFilter = function(filterString) {
|
||||
filterString = filterString || "";
|
||||
var results = [], // Array of arrays of operator nodes {operator:,operand:}
|
||||
p = 0, // Current position in the filter string
|
||||
@ -196,42 +196,45 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
};
|
||||
|
||||
exports.getFilterOperators = function() {
|
||||
exports.getFilterOperators = function() {
|
||||
if(!this.filterOperators) {
|
||||
$tw.Wiki.prototype.filterOperators = {};
|
||||
$tw.modules.applyMethods("filteroperator",this.filterOperators);
|
||||
}
|
||||
return this.filterOperators;
|
||||
};
|
||||
};
|
||||
|
||||
exports.getFilterRunPrefixes = function() {
|
||||
exports.getFilterRunPrefixes = function() {
|
||||
if(!this.filterRunPrefixes) {
|
||||
$tw.Wiki.prototype.filterRunPrefixes = {};
|
||||
$tw.modules.applyMethods("filterrunprefix",this.filterRunPrefixes);
|
||||
}
|
||||
return this.filterRunPrefixes;
|
||||
}
|
||||
}
|
||||
|
||||
exports.filterTiddlers = function(filterString,widget,source) {
|
||||
exports.filterTiddlers = function(filterString,widget,source) {
|
||||
var fn = this.compileFilter(filterString);
|
||||
return fn.call(this,source,widget);
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
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)
|
||||
widget: an optional widget node for retrieving the current tiddler etc.
|
||||
*/
|
||||
exports.compileFilter = function(filterString) {
|
||||
/*
|
||||
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)
|
||||
widget: an optional widget node for retrieving the current tiddler etc.
|
||||
*/
|
||||
exports.compileFilter = function(filterString) {
|
||||
// Set up the filter function cache
|
||||
if(!this.filterCache) {
|
||||
this.filterCache = Object.create(null);
|
||||
this.filterCacheCount = 0;
|
||||
}
|
||||
// Use the cached version of this filter function if it exists
|
||||
if(this.filterCache[filterString] !== undefined) {
|
||||
return this.filterCache[filterString];
|
||||
}
|
||||
// Parse the filter string
|
||||
var filterParseTree;
|
||||
try {
|
||||
filterParseTree = this.parseFilter(filterString);
|
||||
@ -334,8 +337,8 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
}
|
||||
})());
|
||||
});
|
||||
// Return a function that applies the operations to a source iterator of tiddler titles
|
||||
var fnMeasured = $tw.perf.measure("filter: " + filterString,function filterFunction(source,widget) {
|
||||
// Make the filter function
|
||||
var fnFilter = function filterFunction(source,widget) {
|
||||
if(!source) {
|
||||
source = self.each;
|
||||
} else if(typeof source === "object") { // Array or hashmap
|
||||
@ -355,7 +358,10 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
}
|
||||
self.filterRecursionCount = self.filterRecursionCount - 1;
|
||||
return results.toArray();
|
||||
});
|
||||
};
|
||||
// Return a function that applies the operations to a source iterator of tiddler titles
|
||||
var fnMeasured = $tw.perf.measure("filter: " + filterString,fnFilter);
|
||||
// Cache the measured filter function
|
||||
if(this.filterCacheCount >= 2000) {
|
||||
// To prevent memory leak, we maintain an upper limit for cache size.
|
||||
// Reset if exceeded. This should give us 95% of the benefit
|
||||
@ -366,7 +372,7 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
this.filterCache[filterString] = fnMeasured;
|
||||
this.filterCacheCount++;
|
||||
return fnMeasured;
|
||||
};
|
||||
};
|
||||
|
||||
})();
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user