Sorting of "folders" before leafs

This commit is contained in:
Magnus Åhall 2026-06-02 18:50:29 +02:00
parent 2f27aeffb3
commit cc69f7194e
3 changed files with 42 additions and 8 deletions

View file

@ -257,8 +257,15 @@ customElements.define('n2-nodeui', N2PageNodeUI)
export class Node {
static sort(a, b) {//{{{
if (a.data.Name < b.data.Name) return -1
if (a.data.Name > b.data.Name) return 0
// Nodes with children ("folders") are sorted first.
if (a._has_children && !b._has_children) return -1
if (!a._has_children && b._has_children) return 1
// Otherwise sort by lowercased name.
const an = a.data.Name.toLowerCase()
const bn = b.data.Name.toLowerCase()
if (an < bn) return -1
if (an > bn) return 1
return 0
}//}}}
static create(name, parentUUID) {// {{{
@ -286,6 +293,7 @@ export class Node {
this.ParentUUID = nodeData.ParentUUID
this._children_fetched = false
this._has_children = null // this will be set by nodeStore.getTreeNodes
this.Children = []
this.Ancestors = []
@ -322,6 +330,7 @@ export class Node {
this.Children.sort(Node.sort)
const numChildren = this.Children.length
this.setHasChildren(numChildren > 0)
for (let i = 0; i < numChildren; i++) {
if (i > 0)
this.Children[i]._sibling_before = this.Children[i - 1]
@ -336,8 +345,11 @@ export class Node {
return this.Children
}//}}}
setHasChildren(v) {// {{{
this._has_children = v
}// }}}
hasChildren() {//{{{
return this.Children.length > 0
return this._has_children
}//}}}
getSiblingBefore() {// {{{
return this._sibling_before