Improvements and fixes to search

This commit is contained in:
Jeremy Ruston 2012-10-17 14:57:13 +01:00
parent bf94a6dc65
commit 915caf2a18
2 changed files with 27 additions and 13 deletions

View File

@ -211,12 +211,12 @@ exports.operators = {
selector: function(operator) {
var op = operator.prefix === "!" ? "true" : "false";
return "var term = this.getTiddler(\"" + $tw.utils.stringify(operator.operand) + "\").fields.text;" +
"$tw.utils.pushTop(subResults,this.search(term,source," + op + "));";
"$tw.utils.pushTop(subResults,this.search(term,{titles: source, invert: " + op + ", exclude: [\"" + $tw.utils.stringify(operator.operand) + "\"]}));";
},
filter: function(operator) {
var op = operator.prefix === "!" ? "true" : "false";
return "var term = this.getTiddler(\"" + $tw.utils.stringify(operator.operand) + "\").fields.text;" +
"$tw.utils.pushTop(subResults,this.search(term,subResults," + op + "));";
"subResults = this.search(term,{titles: subResults, invert: " + op + ", exclude: [\"" + $tw.utils.stringify(operator.operand) + "\"]});"
}
},
"field": { // Special handler for field comparisons

View File

@ -596,32 +596,46 @@ exports.saveWiki = function(options) {
/*
Return an array of tiddler titles that match a search string
text: The text string to search for
titles: Optional hashmap or array of tiddler titles to limit search
exclude: Optional; if true returns tiddlers that do not contain the specified string
options: see below
Options available:
titles: Hashmap or array of tiddler titles to limit search
exclude: An array of tiddler titles to exclude from the search
invert: If true returns tiddlers that do not contain the specified string
*/
exports.search = function(text,titles,exclude) {
exports.search = function(text,options) {
options = options || {};
var me = this;
// Function to check a given tiddler for the search term
var searchTiddler = function(title) {
var tiddler = me.getTiddler(title);
return tiddler ? tiddler.fields.text.indexOf(text) !== -1 : false;
var tiddler = me.getTiddler(title),
match = tiddler ? tiddler.fields.text.indexOf(text) !== -1 : false;
return options.invert ? !match : match;
}
// Loop through all the tiddlers doing the search
var results = [],t;
if($tw.utils.isArray(titles)) {
for(t=0; t<titles.length; t++) {
if(searchTiddler(titles[t])) {
results.push(titles[t]);
if($tw.utils.isArray(options.titles)) {
for(t=0; t<options.titles.length; t++) {
if(searchTiddler(options.titles[t])) {
results.push(options.titles[t]);
}
}
} else {
titles = titles || this.tiddlers;
for(t in titles) {
var source = options.titles || this.tiddlers;
for(t in source) {
if(searchTiddler(t)) {
results.push(t);
}
}
}
// Remove any of the results we have to exclude
if(options.exclude) {
for(t=0; t<options.exclude.length; t++) {
var p = results.indexOf(options.exclude[t]);
if(p !== -1) {
results.splice(p,1);
}
}
}
return results;
};