Fixed initial node when visiting page

This commit is contained in:
Magnus Åhall 2026-06-08 10:34:55 +02:00
parent 303c7d9b2d
commit b1c85d96e9
3 changed files with 29 additions and 17 deletions

View file

@ -1,6 +1,7 @@
import { ROOT_NODE } from 'node_store'
import { CustomHTMLElement } from './lib/custom_html_element.mjs'
import { Color, Solver } from './lib/css_colorize.mjs'
import { Node } from './page_node.mjs'
// TreeExpandedHandler is responsible for collapsing or expanding
// the node tree, wide view or narrow "mobile" view.
@ -152,21 +153,21 @@ export class N2Tree extends CustomHTMLElement {
let result = solver.solve()
// console.log(result.filter)
}// }}}
render() {// {{{
async render() {// {{{
if (this.rendered)
alert('Tree should only be rendered once.')
for (const node of this.treeTrunk) {
const treenode = new N2TreeNode(this, node)
this.treeNodeComponents[node.UUID] = treenode
this.elTreenodes.appendChild(treenode.render())
}
this.expandedNodes[ROOT_NODE] = true
const startnode = await nodeStore.get(ROOT_NODE)
const starttreenode = new N2TreeNode(this, startnode, null)
this.treeNodeComponents[startnode.UUID] = starttreenode
this.elTreenodes.appendChild(starttreenode.render())
this.rendered = true
return this
}// }}}
reset() {// {{{
console.log('tree reset')
this.treeNodeComponents = {}
this.treeTrunk = []
this.rendered = false
@ -306,7 +307,7 @@ export class N2Tree extends CustomHTMLElement {
return
}
if (n.isFirstSibling() && n.getParent().UUID !== ROOT_NODE) {
if (n.isFirstSibling()) {
_mbus.dispatch("GO_TO_NODE", { nodeUUID: n.getParent()?.UUID, dontPush: false, dontExpand: true })
return
}
@ -358,8 +359,6 @@ export class N2Tree extends CustomHTMLElement {
if (n.isFirstSibling()) {
parent = n.getParent()
if (parent?.UUID === ROOT_NODE)
return
_mbus.dispatch("GO_TO_NODE", { nodeUUID: parent?.UUID, dontPush: false, dontExpand: true })
return
}
@ -562,7 +561,9 @@ export class N2TreeNode extends CustomHTMLElement {
}
// The expand icon <img> is only changed to not get a flickering when re-rendering.
if (!this.node.hasChildren())
if (this.node.UUID === ROOT_NODE)
this.setImgSrc(this.elExpand, `/images/${window._VERSION}/icon_home.svg`)
else if (!this.node.hasChildren())
this.setImgSrc(this.elExpand, `/images/${window._VERSION}/leaf.svg`)
else if (this.tree.getNodeExpanded(this.node.UUID))
this.setImgSrc(this.elExpand, `/images/${window._VERSION}/expanded.svg`)
@ -578,6 +579,7 @@ export class N2TreeNode extends CustomHTMLElement {
if (treenode === undefined) {
treenode = new N2TreeNode(this.tree, node, this)
this.tree.treeNodeComponents[node.UUID] = treenode
_mbus.dispatch(`NODE_COMPONENT_EXIST_${node.UUID}`)
}
return treenode
})