1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-10 16:10:02 +00:00

Add sorting by tiddler lists

The tag and tagging filters now sort their results by the list field of
the tag, if present
This commit is contained in:
Jeremy Ruston 2013-08-08 17:39:34 +01:00
parent 554168fcf1
commit a9a2ae2223
5 changed files with 36 additions and 4 deletions

View File

@ -40,6 +40,10 @@ exports.tag = function(source,operator,options) {
checkTiddler(title);
});
}
// Sort the results if we are matching a tag
if(operator.prefix !== "!") {
results = options.wiki.sortByList(results,operator.operand);
}
return results;
};

View File

@ -411,6 +411,7 @@ exports.getShadowTitles = function() {
Retrieves a list of the tiddler titles that are tagged with a given tag
*/
exports.getTiddlersWithTag = function(tag) {
// Get the list associated with the tag
var titles = [];
for(var title in this.tiddlers) {
var tiddler = this.tiddlers[title];
@ -418,7 +419,34 @@ exports.getTiddlersWithTag = function(tag) {
titles.push(title);
}
}
return titles;
return this.sortByList(titles,tag);
};
/*
Sorts an array of tiddler titles according to an ordered list
*/
exports.sortByList = function(array,listTitle) {
var list = this.getTiddlerList(listTitle);
if(list) {
var titles = [], t, title;
// First place any entries that are present in the list
for(t=0; t<list.length; t++) {
title = list[t];
if(array.indexOf(title) !== -1) {
titles.push(title);
}
}
// Then place any remaining entries
for(t=0; t<array.length; t++) {
title = array[t];
if(list.indexOf(title) === -1) {
titles.push(title);
}
}
return titles;
} else {
return array;
}
};
/*

View File

@ -4,7 +4,7 @@ title: $:/core/ui/TagTemplate
<$reveal state="$:/state/tagpopup" type="popup" position="below" qualifyTiddlerTitles="yes" ><div class="tw-drop-down">
<$view field="title" format="link" />
----
<$list filter="[is[current]tagging[]sort[title]]">
<$list filter="[is[current]tagging[]]">
<$view field="title" format="link" />
</$list>
</div></$reveal>

View File

@ -114,7 +114,7 @@ describe("Filter tests", function() {
it("should handle the tagging operator", function() {
expect(wiki.filterTiddlers("[[one]tagging[]sort[title]]").join(",")).toBe("Tiddler Three,TiddlerOne");
expect(wiki.filterTiddlers("[[one]tagging[]]").join(",")).toBe("TiddlerOne,Tiddler Three");
expect(wiki.filterTiddlers("[[one]tagging[]]").join(",")).toBe("Tiddler Three,TiddlerOne");
expect(wiki.filterTiddlers("[[two]tagging[]sort[title]]").join(",")).toBe("$:/TiddlerTwo,Tiddler Three");
expect(wiki.filterTiddlers("[is[current]tagging[]sort[title]]","one").join(",")).toBe("Tiddler Three,TiddlerOne");
});

View File

@ -1,4 +1,4 @@
title: introduction
modified: 201306041707
color: plum
list: HelloThere Acknowledgements Improvements Demos [[TiddlyWiki5 Editions]] Docs DeveloperDocs