mirror of
https://github.com/flarum/framework.git
synced 2025-02-23 07:05:26 +08:00
parent
eb49e1d4e8
commit
76eaf8cb2d
@ -3,22 +3,38 @@ export default function sortTags(tags) {
|
|||||||
const aPos = a.position();
|
const aPos = a.position();
|
||||||
const bPos = b.position();
|
const bPos = b.position();
|
||||||
|
|
||||||
|
// If they're both secondary tags, sort them by their discussions count,
|
||||||
|
// descending.
|
||||||
|
if (aPos === null && bPos === null)
|
||||||
|
return b.discussionsCount() - a.discussionsCount();
|
||||||
|
|
||||||
|
// If just one is a secondary tag, then the primary tag should
|
||||||
|
// come first.
|
||||||
|
if (bPos === null) return -1;
|
||||||
|
if (aPos === null) return 1;
|
||||||
|
|
||||||
|
// If we've made it this far, we know they're both primary tags. So we'll
|
||||||
|
// need to see if they have parents.
|
||||||
const aParent = a.parent();
|
const aParent = a.parent();
|
||||||
const bParent = b.parent();
|
const bParent = b.parent();
|
||||||
|
|
||||||
if (aPos === null && bPos === null) {
|
// If they both have the same parent, then their positions are local,
|
||||||
return b.discussionsCount() - a.discussionsCount();
|
// so we can compare them directly.
|
||||||
} else if (bPos === null) {
|
if (aParent === bParent) return aPos - bPos;
|
||||||
return -1;
|
|
||||||
} else if (aPos === null) {
|
// If they are both child tags, then we will compare the positions of their
|
||||||
return 1;
|
// parents.
|
||||||
} else if (aParent === bParent) {
|
else if (aParent && bParent)
|
||||||
return aPos - bPos;
|
return aParent.position() - bParent.position();
|
||||||
} else if (aParent) {
|
|
||||||
|
// If we are comparing a child tag with its parent, then we let the parent
|
||||||
|
// come first. If we are comparing an unrelated parent/child, then we
|
||||||
|
// compare both of the parents.
|
||||||
|
else if (aParent)
|
||||||
return aParent === b ? 1 : aParent.position() - bPos;
|
return aParent === b ? 1 : aParent.position() - bPos;
|
||||||
} else if (bParent) {
|
|
||||||
|
else if (bParent)
|
||||||
return bParent === a ? -1 : aPos - bParent.position();
|
return bParent === a ? -1 : aPos - bParent.position();
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user