1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Added sort operator to filter syntax

This commit is contained in:
Jeremy Ruston 2012-05-08 16:02:24 +01:00
parent c63eb4c045
commit 19e19a2f42
4 changed files with 50 additions and 17 deletions

View File

@ -50,7 +50,7 @@ Command.prototype.subcommands.tiddler = function() {
};
Command.prototype.subcommands.tiddlers = function() {
var tiddlers = this.commander.wiki.sortTiddlers();
var tiddlers = this.commander.wiki.getTiddlers();
this.output.write("Wiki contains these tiddlers:\n");
for(var t=0; t<tiddlers.length; t++) {
this.output.write(tiddlers[t] + "\n");
@ -59,7 +59,7 @@ Command.prototype.subcommands.tiddlers = function() {
};
Command.prototype.subcommands.shadows = function() {
var tiddlers = this.commander.wiki.shadows.sortTiddlers();
var tiddlers = this.commander.wiki.shadows.getTiddlers();
this.output.write("Wiki contains these shadow tiddlers:\n");
for(var t=0; t<tiddlers.length; t++) {
this.output.write(tiddlers[t] + "\n");

View File

@ -12,7 +12,7 @@ module-type: macro
var handlers = {
all: function(wiki) {
return wiki.sortTiddlers("title","excludeLists");
return wiki.getTiddlers("title","excludeLists");
},
missing: function(wiki) {
return wiki.getMissingTitles();

View File

@ -69,7 +69,7 @@ exports.filterTiddlers = function(filterString) {
};
/*
Compiling a filter gives a JavaScript function that is invoked as `filter(source)`, where `source` is a hashmap of source tiddler titles (the values don't matter, so it is possible to use a store or a changes object). It returns an array of tiddler titles that satisfy the filter
Compiling a filter gives a JavaScript function that is invoked as `this.filter(source)`, where `source` is a hashmap of source tiddler titles (the values don't matter, so it is possible to use a store or a changes object). It returns an array of tiddler titles that satisfy the filter
*/
exports.compileFilter = function(filterString) {
var filter = this.parseFilter(filterString),
@ -96,9 +96,10 @@ exports.compileFilter = function(filterString) {
output.push(operationInfo.epilogue);
}
output.push(this.filterFragments.epilogue);
console.log("Compiled filter to ",output.join("\n"));
try {
/*jslint evil: true */
fn = eval(output.join(""));
fn = eval(output.join("\n"));
} catch(ex) {
throw "Error in filter expression: " + ex;
}
@ -106,21 +107,21 @@ exports.compileFilter = function(filterString) {
};
exports.filterFragments = {
prologue: "(function(source) {\nvar results = [], subResults;\n",
prologue: "(function(source) {\nvar results = [], subResults;",
epilogue: "return results;\n})",
operation: {
prefix: {
"": {
prologue: "subResults = [];\n",
epilogue: "$tw.utils.pushTop(results,subResults);\n"
prologue: "subResults = [];",
epilogue: "$tw.utils.pushTop(results,subResults);"
},
"+": {
prologue: "subResults = results.slice(0);\nresults.splice(0,results.length);\n",
epilogue: "$tw.utils.pushTop(results,subResults);\n"
prologue: "subResults = results.slice(0);\nresults.splice(0,results.length);",
epilogue: "$tw.utils.pushTop(results,subResults);"
},
"-": {
prologue: "subResults = [];\n",
epilogue: "$tw.utils.removeArrayEntries(results,subResults);\n"
prologue: "subResults = [];",
epilogue: "$tw.utils.removeArrayEntries(results,subResults);"
}
}
}
@ -129,10 +130,10 @@ exports.filterFragments = {
exports.operators = {
"title": {
selector: function(operator) {
return "if($tw.utils.hop(source,\"" + $tw.utils.stringify(operator.operand) + "\")) {$tw.utils.pushTop(subResults,\"" + $tw.utils.stringify(operator.operand) + "\");}\n";
return "if($tw.utils.hop(source,\"" + $tw.utils.stringify(operator.operand) + "\")) {$tw.utils.pushTop(subResults,\"" + $tw.utils.stringify(operator.operand) + "\");}";
},
filter: function(operator) {
return "if(subResults.indexOf(\"" + $tw.utils.stringify(operator.operand) + "\") !== -1) {subResults = [\"" + $tw.utils.stringify(operator.operand) + "\"];} else {subResults = [];}\n";
return "if(subResults.indexOf(\"" + $tw.utils.stringify(operator.operand) + "\") !== -1) {subResults = [\"" + $tw.utils.stringify(operator.operand) + "\"];} else {subResults = [];}";
}
},
"is": {
@ -183,6 +184,15 @@ exports.operators = {
return "for(var r=subResults.length-1; r>=0; r--) {if(this.getTiddler(subResults[r]).fields[\"" + $tw.utils.stringify(operator.operand) + "\"] " + op + "== undefined) {subResults.splice(r,1);}}";
}
},
"sort": {
selector: function(operator) {
throw "Cannot use sort operator at the start of a sort operation";
},
filter: function(operator) {
var desc = operator.prefix === "!" ? "true" : "false";
return "this.sortTiddlers(subResults,\"" + $tw.utils.stringify(operator.operand) + "\"," + desc + ");"
}
},
"field": {
selector: function(operator) {
var op = operator.prefix === "!" ? "!" : "=";

View File

@ -131,7 +131,7 @@ exports.serializeTiddler = function(title,type) {
/*
Return a sorted array of tiddler titles, optionally filtered by a tag
*/
exports.sortTiddlers = function(sortField,excludeTag) {
exports.getTiddlers = function(sortField,excludeTag) {
sortField = sortField || "title";
var tiddlers = [], t, titles = [];
for(t in this.tiddlers) {
@ -158,12 +158,35 @@ exports.sortTiddlers = function(sortField,excludeTag) {
return titles;
};
/*
Sort an array of tiddler titles by a specified field
titles: array of titles (sorted in place)
sortField: name of field to sort by
isDescending: true if the sort should be descending
*/
exports.sortTiddlers = function(titles,sortField,isDescending) {
var self = this;
titles.sort(function(a,b) {
var aa = self.getTiddler(a).fields[sortField] || 0,
bb = self.getTiddler(b).fields[sortField] || 0;
if(aa < bb) {
return isDescending ? +1 : -1;
} else {
if(aa > bb) {
return isDescending ? -1 : +1;
} else {
return 0;
}
}
});
};
exports.forEachTiddler = function(/* [sortField,[excludeTag,]]callback */) {
var arg = 0,
sortField = arguments.length > 1 ? arguments[arg++] : null,
excludeTag = arguments.length > 2 ? arguments[arg++] : null,
callback = arguments[arg++],
titles = this.sortTiddlers(sortField,excludeTag),
titles = this.getTiddlers(sortField,excludeTag),
t, tiddler;
for(t=0; t<titles.length; t++) {
tiddler = this.tiddlers[titles[t]];
@ -182,7 +205,7 @@ exports.getOrphanTitles = function() {
};
exports.getShadowTitles = function() {
return this.shadows ? this.shadows.sortTiddlers() : [];
return this.shadows ? this.shadows.getTiddlers() : [];
};
// Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it