Refactoring of node model

This commit is contained in:
Magnus Åhall 2023-06-22 07:10:26 +02:00
parent 910a7a15c7
commit 33b10e6527

View File

@ -93,16 +93,22 @@ export class NodeUI extends Component {
case 'S':
if(evt.ctrlKey || (evt.shiftKey && evt.altKey))
this.saveNode()
else
handled = false
break
case 'N':
if((evt.ctrlKey && evt.AltKey) || (evt.shiftKey && evt.altKey))
this.createNode()
else
handled = false
break
case 'U':
if((evt.ctrlKey && evt.altKey) || (evt.shiftKey && evt.altKey))
this.upload.value = true
else
handled = false
default:
handled = false
@ -142,61 +148,32 @@ export class NodeUI extends Component {
let name = prompt("Name")
if(!name)
return
this.props.app.request('/node/create', {
Name: name.trim(),
ParentID: this.node.value.ID,
})
.then(res=>{
this.goToNode(res.Node.ID)
})
.catch(this.props.app.responseError)
this.node.value.create(name, nodeID=>this.goToNode(nodeID))
}//}}}
saveNode() {//{{{
let content = this.nodeContent.current.contentDiv.current.value
this.props.app.request('/node/update', {
NodeID: this.node.value.ID,
Content: content,
})
.then(res=>{
this.props.app.nodeModified.value = false
})
.catch(this.props.app.responseError)
this.node.value.save(content, ()=>this.props.app.nodeModified.value = false)
}//}}}
renameNode() {//{{{
let name = prompt("New name")
if(!name)
return
this.props.app.request('/node/rename', {
Name: name.trim(),
NodeID: this.node.value.ID,
})
.then(_=>{
this.node.value.rename(name, ()=>{
this.goToNode(this.node.value.ID)
this.menu.value = false
})
.catch(this.props.app.responseError)
}//}}}
deleteNode() {//{{{
if(!confirm("Do you want to delete this note and all sub-notes?"))
return
this.props.app.request('/node/delete', {
NodeID: this.node.value.ID,
})
.then(_=>{
this.node.value.delete(()=>{
this.goToNode(this.node.value.ParentID)
this.menu.value = false
})
.catch(this.props.app.responseError)
}//}}}
retrieveTree() {//{{{
this.props.app.request('/node/tree', { StartNodeID: this.node.value.ID })
.then(res=>{
this.tree.value = res.Nodes
})
.catch(this.props.app.responseError)
this.node.value.children(children=>this.tree.value = children)
}//}}}
renderTree(tree) {//{{{
return tree.map(node=>html`<div class="node" style="margin-left: ${(node.Level+1) * 32}px">${node.Name}</div>`)
@ -261,6 +238,46 @@ class Node {
})
.catch(this.app.responseError)
}//}}}
delete(callback) {//{{{
this.app.request('/node/delete', {
NodeID: this.ID,
})
.then(callback)
.catch(this.app.responseError)
}//}}}
create(name, callback) {//{{{
this.app.request('/node/create', {
Name: name.trim(),
ParentID: this.ID,
})
.then(res=>{
callback(res.Node.ID)
})
.catch(this.app.responseError)
}//}}}
save(content, callback) {//{{{
this.app.request('/node/update', {
NodeID: this.ID,
Content: content,
})
.then(callback)
.catch(this.app.responseError)
}//}}}
rename(name, callback) {//{{{
this.app.request('/node/rename', {
Name: name.trim(),
NodeID: this.ID,
})
.then(callback)
.catch(this.app.responseError)
}//}}}
children(callback) {//{{{
this.app.request('/node/tree', { StartNodeID: this.ID })
.then(res=>{
callback(res.Nodes)
})
.catch(this.app.responseError)
}//}}}
}
class Menu extends Component {