mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 12:07:19 +00:00
Added sort operator to filter syntax
This commit is contained in:
parent
c63eb4c045
commit
19e19a2f42
@ -50,7 +50,7 @@ Command.prototype.subcommands.tiddler = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Command.prototype.subcommands.tiddlers = 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");
|
this.output.write("Wiki contains these tiddlers:\n");
|
||||||
for(var t=0; t<tiddlers.length; t++) {
|
for(var t=0; t<tiddlers.length; t++) {
|
||||||
this.output.write(tiddlers[t] + "\n");
|
this.output.write(tiddlers[t] + "\n");
|
||||||
@ -59,7 +59,7 @@ Command.prototype.subcommands.tiddlers = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Command.prototype.subcommands.shadows = 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");
|
this.output.write("Wiki contains these shadow tiddlers:\n");
|
||||||
for(var t=0; t<tiddlers.length; t++) {
|
for(var t=0; t<tiddlers.length; t++) {
|
||||||
this.output.write(tiddlers[t] + "\n");
|
this.output.write(tiddlers[t] + "\n");
|
||||||
|
@ -12,7 +12,7 @@ module-type: macro
|
|||||||
|
|
||||||
var handlers = {
|
var handlers = {
|
||||||
all: function(wiki) {
|
all: function(wiki) {
|
||||||
return wiki.sortTiddlers("title","excludeLists");
|
return wiki.getTiddlers("title","excludeLists");
|
||||||
},
|
},
|
||||||
missing: function(wiki) {
|
missing: function(wiki) {
|
||||||
return wiki.getMissingTitles();
|
return wiki.getMissingTitles();
|
||||||
|
@ -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) {
|
exports.compileFilter = function(filterString) {
|
||||||
var filter = this.parseFilter(filterString),
|
var filter = this.parseFilter(filterString),
|
||||||
@ -96,9 +96,10 @@ exports.compileFilter = function(filterString) {
|
|||||||
output.push(operationInfo.epilogue);
|
output.push(operationInfo.epilogue);
|
||||||
}
|
}
|
||||||
output.push(this.filterFragments.epilogue);
|
output.push(this.filterFragments.epilogue);
|
||||||
|
console.log("Compiled filter to ",output.join("\n"));
|
||||||
try {
|
try {
|
||||||
/*jslint evil: true */
|
/*jslint evil: true */
|
||||||
fn = eval(output.join(""));
|
fn = eval(output.join("\n"));
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
throw "Error in filter expression: " + ex;
|
throw "Error in filter expression: " + ex;
|
||||||
}
|
}
|
||||||
@ -106,21 +107,21 @@ exports.compileFilter = function(filterString) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.filterFragments = {
|
exports.filterFragments = {
|
||||||
prologue: "(function(source) {\nvar results = [], subResults;\n",
|
prologue: "(function(source) {\nvar results = [], subResults;",
|
||||||
epilogue: "return results;\n})",
|
epilogue: "return results;\n})",
|
||||||
operation: {
|
operation: {
|
||||||
prefix: {
|
prefix: {
|
||||||
"": {
|
"": {
|
||||||
prologue: "subResults = [];\n",
|
prologue: "subResults = [];",
|
||||||
epilogue: "$tw.utils.pushTop(results,subResults);\n"
|
epilogue: "$tw.utils.pushTop(results,subResults);"
|
||||||
},
|
},
|
||||||
"+": {
|
"+": {
|
||||||
prologue: "subResults = results.slice(0);\nresults.splice(0,results.length);\n",
|
prologue: "subResults = results.slice(0);\nresults.splice(0,results.length);",
|
||||||
epilogue: "$tw.utils.pushTop(results,subResults);\n"
|
epilogue: "$tw.utils.pushTop(results,subResults);"
|
||||||
},
|
},
|
||||||
"-": {
|
"-": {
|
||||||
prologue: "subResults = [];\n",
|
prologue: "subResults = [];",
|
||||||
epilogue: "$tw.utils.removeArrayEntries(results,subResults);\n"
|
epilogue: "$tw.utils.removeArrayEntries(results,subResults);"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,10 +130,10 @@ exports.filterFragments = {
|
|||||||
exports.operators = {
|
exports.operators = {
|
||||||
"title": {
|
"title": {
|
||||||
selector: function(operator) {
|
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) {
|
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": {
|
"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);}}";
|
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": {
|
"field": {
|
||||||
selector: function(operator) {
|
selector: function(operator) {
|
||||||
var op = operator.prefix === "!" ? "!" : "=";
|
var op = operator.prefix === "!" ? "!" : "=";
|
||||||
|
@ -131,7 +131,7 @@ exports.serializeTiddler = function(title,type) {
|
|||||||
/*
|
/*
|
||||||
Return a sorted array of tiddler titles, optionally filtered by a tag
|
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";
|
sortField = sortField || "title";
|
||||||
var tiddlers = [], t, titles = [];
|
var tiddlers = [], t, titles = [];
|
||||||
for(t in this.tiddlers) {
|
for(t in this.tiddlers) {
|
||||||
@ -158,12 +158,35 @@ exports.sortTiddlers = function(sortField,excludeTag) {
|
|||||||
return titles;
|
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 */) {
|
exports.forEachTiddler = function(/* [sortField,[excludeTag,]]callback */) {
|
||||||
var arg = 0,
|
var arg = 0,
|
||||||
sortField = arguments.length > 1 ? arguments[arg++] : null,
|
sortField = arguments.length > 1 ? arguments[arg++] : null,
|
||||||
excludeTag = arguments.length > 2 ? arguments[arg++] : null,
|
excludeTag = arguments.length > 2 ? arguments[arg++] : null,
|
||||||
callback = arguments[arg++],
|
callback = arguments[arg++],
|
||||||
titles = this.sortTiddlers(sortField,excludeTag),
|
titles = this.getTiddlers(sortField,excludeTag),
|
||||||
t, tiddler;
|
t, tiddler;
|
||||||
for(t=0; t<titles.length; t++) {
|
for(t=0; t<titles.length; t++) {
|
||||||
tiddler = this.tiddlers[titles[t]];
|
tiddler = this.tiddlers[titles[t]];
|
||||||
@ -182,7 +205,7 @@ exports.getOrphanTitles = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.getShadowTitles = 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
|
// Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it
|
||||||
|
Loading…
Reference in New Issue
Block a user