mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-04-06 10:46:57 +00:00
Improve wiki.sortByList()
Previously list-before and list-after didn’t work if they referenced entries that hadn’t yet been placed
This commit is contained in:
parent
fe0ed5e4ce
commit
dad60cda05
@ -497,7 +497,7 @@ exports.sortByList = function(array,listTitle) {
|
||||
var list = this.getTiddlerList(listTitle);
|
||||
if(!array || array.length === 0) {
|
||||
return [];
|
||||
} else if(list) {
|
||||
} else {
|
||||
var titles = [], t, title;
|
||||
// First place any entries that are present in the list
|
||||
for(t=0; t<list.length; t++) {
|
||||
@ -510,36 +510,43 @@ exports.sortByList = function(array,listTitle) {
|
||||
for(t=0; t<array.length; t++) {
|
||||
title = array[t];
|
||||
if(list.indexOf(title) === -1) {
|
||||
// Entry isn't in the list yet, so either append or insert;
|
||||
// obey list-before and list-after if present for relative insertion point
|
||||
var tiddler = this.getTiddler(title),
|
||||
pos = -1;
|
||||
if(tiddler) {
|
||||
var beforeTitle = tiddler.fields["list-before"];
|
||||
if(beforeTitle === "") {
|
||||
pos = 0;
|
||||
} else if(beforeTitle) {
|
||||
pos = list.indexOf(beforeTitle);
|
||||
} else {
|
||||
var afterTitle = tiddler.fields["list-after"];
|
||||
if(afterTitle) {
|
||||
pos = list.indexOf(afterTitle);
|
||||
if(pos >= 0) {
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(pos >= 0) {
|
||||
titles.splice(pos,0,title);
|
||||
} else {
|
||||
titles.push(title);
|
||||
}
|
||||
titles.push(title);
|
||||
}
|
||||
}
|
||||
// Finally obey the list-before and list-after fields of each tiddler in turn
|
||||
var sortedTitles = titles.slice(0);
|
||||
for(t=0; t<sortedTitles.length; t++) {
|
||||
title = sortedTitles[t];
|
||||
var currPos = titles.indexOf(title),
|
||||
newPos = -1,
|
||||
tiddler = this.getTiddler(title);
|
||||
if(tiddler) {
|
||||
var beforeTitle = tiddler.fields["list-before"],
|
||||
afterTitle = tiddler.fields["list-after"];
|
||||
if(beforeTitle === "") {
|
||||
newPos = 0;
|
||||
} else if(beforeTitle) {
|
||||
newPos = titles.indexOf(beforeTitle);
|
||||
} else if(afterTitle) {
|
||||
newPos = titles.indexOf(afterTitle);
|
||||
if(newPos >= 0) {
|
||||
++newPos;
|
||||
}
|
||||
}
|
||||
if(newPos === -1) {
|
||||
newPos = currPos;
|
||||
}
|
||||
if(newPos !== currPos) {
|
||||
titles.splice(currPos,1);
|
||||
if(newPos >= currPos) {
|
||||
newPos--;
|
||||
}
|
||||
titles.splice(newPos,0,title);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return titles;
|
||||
} else {
|
||||
return array;
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user