From 19e19a2f4291f1ef6d33eea4965bfe472638d095 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 8 May 2012 16:02:24 +0100 Subject: [PATCH] Added sort operator to filter syntax --- core/modules/commands/dump.js | 4 ++-- core/modules/macros/list.js | 2 +- core/modules/wiki.filters.js | 32 +++++++++++++++++++++----------- core/modules/wiki.js | 29 ++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/core/modules/commands/dump.js b/core/modules/commands/dump.js index 48e57d251..2f989e691 100644 --- a/core/modules/commands/dump.js +++ b/core/modules/commands/dump.js @@ -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=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 === "!" ? "!" : "="; diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 18ad10f2f..8d22c7aee 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -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