Notes2/static/js/app.mjs

189 lines
5.5 KiB
JavaScript
Raw Normal View History

2024-11-28 18:11:14 +01:00
import { h, Component, createRef } from 'preact'
2024-11-29 09:15:42 +01:00
import { signal } from 'preact/signals'
2024-11-28 18:11:14 +01:00
import htm from 'htm'
import { API } from 'api'
2024-12-02 15:26:35 +01:00
import { Node, NodeUI } from 'node'
2024-11-28 18:11:14 +01:00
const html = htm.bind(h)
export class Notes2 {
constructor() {//{{{
2024-11-29 09:15:42 +01:00
this.startNode = null
2024-12-03 06:53:31 +01:00
this.tree = null
2024-12-02 15:26:35 +01:00
this.nodeUI = createRef()
2024-12-03 06:53:31 +01:00
this.nodeModified = signal(false)
2024-11-29 09:15:42 +01:00
this.setStartNode()
2024-11-28 18:11:14 +01:00
}//}}}
render() {//{{{
return html`
2024-11-30 17:10:46 +01:00
<${Tree} ref=${this.tree} app=${this} />
2024-12-03 06:53:31 +01:00
<div class="nodeui">
<${NodeUI} app=${this} ref=${this.nodeUI} />
</div>
2024-11-28 18:11:14 +01:00
`
}//}}}
2024-11-29 09:15:42 +01:00
setStartNode() {//{{{
2024-12-03 06:53:31 +01:00
/*
2024-12-02 15:26:35 +01:00
const urlParams = new URLSearchParams(window.location.search)
const nodeID = urlParams.get('node')
2024-12-03 06:53:31 +01:00
*/
const parts = document.URL.split('#')
const nodeID = parts[1]
2024-12-02 15:26:35 +01:00
this.startNode = new Node(this, nodeID ? Number.parseInt(nodeID) : 0)
2024-11-29 09:15:42 +01:00
}//}}}
2024-11-28 18:11:14 +01:00
2024-12-01 10:21:29 +01:00
treeGet() {//{{{
2024-11-28 18:11:14 +01:00
const req = {}
2024-11-29 09:15:42 +01:00
API.query('POST', '/node/tree', req)
2024-11-28 18:11:14 +01:00
.then(response => {
2024-11-30 17:10:46 +01:00
console.log(response.Nodes)
nodeStore.add(response.Nodes)
2024-11-28 18:11:14 +01:00
})
.catch(e => console.log(e.type, e.error))
2024-12-01 10:21:29 +01:00
}//}}}
2024-11-28 18:11:14 +01:00
}
2024-11-29 09:15:42 +01:00
class Tree extends Component {
constructor(props) {//{{{
super(props)
this.treeNodes = {}
this.treeNodeComponents = {}
this.treeTrunk = []
this.selectedTreeNode = null
2024-12-03 06:53:31 +01:00
this.props.app.tree = this
2024-11-29 09:15:42 +01:00
this.retrieve()
}//}}}
render({ app }) {//{{{
2024-12-02 15:26:35 +01:00
const renderedTreeTrunk = this.treeTrunk.map(node => {
2024-11-29 09:15:42 +01:00
this.treeNodeComponents[node.ID] = createRef()
2024-12-02 15:26:35 +01:00
return html`<${TreeNode} key=${`treenode_${node.ID}`} tree=${this} node=${node} ref=${this.treeNodeComponents[node.ID]} selected=${node.ID === app.startNode.ID} />`
2024-11-29 09:15:42 +01:00
})
return html`<div id="tree">${renderedTreeTrunk}</div>`
}//}}}
retrieve(callback = null) {//{{{
2024-11-30 17:10:46 +01:00
nodeStore.getTreeNodes()
2024-11-29 09:15:42 +01:00
.then(res => {
this.treeNodes = {}
this.treeNodeComponents = {}
this.treeTrunk = []
this.selectedTreeNode = null
// A tree of nodes is built. This requires the list of nodes
// returned from the server to be sorted in such a way that
// a parent node always appears before a child node.
// The server uses a recursive SQL query delivering this.
2024-12-02 15:26:35 +01:00
for (const nodeData of res) {
const node = new Node(
2024-11-29 09:15:42 +01:00
this,
nodeData.ID,
)
node.Children = []
node.Crumbs = []
node.Files = []
node.Level = nodeData.Level
node.Name = nodeData.Name
node.ParentID = nodeData.ParentID
node.Updated = nodeData.Updated
node.UserID = nodeData.UserID
this.treeNodes[node.ID] = node
2024-12-02 15:26:35 +01:00
if (node.ParentID === 0)
2024-11-29 09:15:42 +01:00
this.treeTrunk.push(node)
else if (this.treeNodes[node.ParentID] !== undefined)
this.treeNodes[node.ParentID].Children.push(node)
2024-12-02 15:26:35 +01:00
}
2024-11-29 09:15:42 +01:00
// When starting with an explicit node value, expanding all nodes
// on its path gives the user a sense of location. Not necessarily working
// as the start node isn't guaranteed to have returned data yet.
// XXX this.crumbsUpdateNodes()
this.forceUpdate()
if (callback)
callback()
})
.catch(e => { console.log(e); console.log(e.type, e.error); alert(e.error) })
}//}}}
setSelected(node) {//{{{
2024-12-03 06:53:31 +01:00
return // TODO
2024-11-29 09:15:42 +01:00
if (this.selectedTreeNode)
this.selectedTreeNode.selected.value = false
this.selectedTreeNode = this.treeNodeComponents[node.ID].current
this.selectedTreeNode.selected.value = true
this.selectedTreeNode.expanded.value = true
this.expandToTrunk(node.ID)
}//}}}
crumbsUpdateNodes(node) {//{{{
2024-12-03 06:53:31 +01:00
console.log('crumbs', this.props.app.startNode.Crumbs)
2024-12-02 15:26:35 +01:00
for (const crumb in this.props.app.startNode.Crumbs) {
2024-11-29 09:15:42 +01:00
// Start node is loaded before the tree.
2024-12-02 15:26:35 +01:00
const node = this.treeNodes[crumb.ID]
2024-11-29 09:15:42 +01:00
if (node)
node._expanded = true
// Tree is done before the start node.
2024-12-02 15:26:35 +01:00
const component = this.treeNodeComponents[crumb.ID]
if (component?.component.current)
2024-11-29 09:15:42 +01:00
component.current.expanded.value = true
2024-12-02 15:26:35 +01:00
}
2024-11-29 09:15:42 +01:00
// Will be undefined when called from tree initialization
// (as tree nodes aren't rendered yet)
if (node !== undefined)
this.setSelected(node)
}//}}}
expandToTrunk(nodeID) {//{{{
let node = this.treeNodes[nodeID]
if (node === undefined)
return
node = this.treeNodes[node.ParentID]
while (node !== undefined) {
this.treeNodeComponents[node.ID].current.expanded.value = true
node = this.treeNodes[node.ParentID]
}
}//}}}
}
class TreeNode extends Component {
constructor(props) {//{{{
super(props)
this.selected = signal(props.selected)
this.expanded = signal(this.props.node._expanded)
}//}}}
render({ tree, node }) {//{{{
2024-12-02 15:26:35 +01:00
const children = node.Children.map(node => {
2024-11-29 09:15:42 +01:00
tree.treeNodeComponents[node.ID] = createRef()
2024-12-02 15:26:35 +01:00
return html`<${TreeNode} key=${`treenode_${node.ID}`} tree=${tree} node=${node} ref=${tree.treeNodeComponents[node.ID]} selected=${node.ID === tree.props.app.startNode.ID} />`
2024-11-29 09:15:42 +01:00
})
let expandImg = ''
2024-12-02 15:26:35 +01:00
if (node.Children.length === 0)
2024-11-29 09:15:42 +01:00
expandImg = html`<img src="/images/${window._VERSION}/leaf.svg" />`
else {
if (this.expanded.value)
expandImg = html`<img src="/images/${window._VERSION}/expanded.svg" />`
else
expandImg = html`<img src="/images/${window._VERSION}/collapsed.svg" />`
}
2024-12-02 15:26:35 +01:00
const selected = (this.selected.value ? 'selected' : '')
2024-11-29 09:15:42 +01:00
return html`
<div class="node">
2024-12-02 15:26:35 +01:00
<div class="expand-toggle" onclick=${() => { this.expanded.value ^= true }}>${expandImg}</div>
<div class="name ${selected}" onclick=${() => window._notes2.current.nodeUI.current.goToNode(node.ID)}>${node.Name}</div>
2024-11-29 09:15:42 +01:00
<div class="children ${node.Children.length > 0 && this.expanded.value ? 'expanded' : 'collapsed'}">${children}</div>
</div>`
}//}}}
}
2024-12-02 15:26:35 +01:00
// vim: foldmethod=marker