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