Work on special pages
This commit is contained in:
parent
d9adfd3a91
commit
da7999fb24
7 changed files with 185 additions and 95 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import { Node } from 'node'
|
||||
|
||||
export const ROOT_NODE = '00000000-0000-0000-0000-000000000000'
|
||||
export const ORPHANED_NODE = '00000000-0000-0000-0000-000000000001'
|
||||
export const DELETED_NODE = '00000000-0000-0000-0000-000000000002'
|
||||
|
||||
export class NodeStore {
|
||||
constructor() {//{{{
|
||||
|
|
@ -76,8 +78,7 @@ export class NodeStore {
|
|||
this.sendQueue = new SimpleNodeStore(this.db, 'send_queue')
|
||||
this.nodesHistory = new NodeHistoryStore(this.db, 'nodes_history')
|
||||
this.files = new SimpleNodeStore(this.db, 'files')
|
||||
this.initializeRootNode()
|
||||
.then(() => resolve())
|
||||
resolve()
|
||||
}
|
||||
|
||||
req.onerror = (event) => {
|
||||
|
|
@ -85,37 +86,6 @@ export class NodeStore {
|
|||
}
|
||||
})
|
||||
}//}}}
|
||||
initializeRootNode() {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
// The root node is a magical node which displays as the first node if none is specified.
|
||||
// If not already existing, it will be created.
|
||||
const trx = this.db.transaction('nodes', 'readwrite')
|
||||
const nodes = trx.objectStore('nodes')
|
||||
const getRequest = nodes.get(ROOT_NODE)
|
||||
getRequest.onsuccess = (event) => {
|
||||
// Root node exists - nice!
|
||||
if (event.target.result !== undefined) {
|
||||
resolve(event.target.result)
|
||||
return
|
||||
}
|
||||
|
||||
const putRequest = nodes.put({
|
||||
UUID: ROOT_NODE,
|
||||
Name: 'Notes2',
|
||||
Content: 'Hello, World!',
|
||||
Updated: new Date().toISOString(),
|
||||
ParentUUID: '',
|
||||
})
|
||||
putRequest.onsuccess = (event) => {
|
||||
resolve(event.target.result)
|
||||
}
|
||||
putRequest.onerror = (event) => {
|
||||
reject(event.target.error)
|
||||
}
|
||||
}
|
||||
getRequest.onerror = (event) => reject(event.target.error)
|
||||
})
|
||||
}//}}}
|
||||
purgeCache() {//{{{
|
||||
this.nodes = {}
|
||||
}//}}}
|
||||
|
|
@ -272,74 +242,92 @@ export class NodeStore {
|
|||
}//}}}
|
||||
get(uuid, suppliedNodestore) {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
switch (uuid) {
|
||||
case ROOT_NODE:
|
||||
const rootNode = new Node({ UUID: ROOT_NODE, Name: 'Start', Special: true }, -1)
|
||||
this.nodes[ROOT_NODE] = rootNode
|
||||
resolve(rootNode)
|
||||
return
|
||||
case DELETED_NODE:
|
||||
const deletedNode = new Node({ UUID: DELETED_NODE, Name: 'Deleted nodes', Special: true }, -1)
|
||||
this.nodes[DELETED_NODE] = deletedNode
|
||||
resolve(deletedNode)
|
||||
return
|
||||
}
|
||||
|
||||
// A nodestore can be provided in order to
|
||||
// avoid creating new transactions.
|
||||
let trx
|
||||
let nodeStore = suppliedNodestore
|
||||
|
||||
if (nodeStore === undefined) {
|
||||
trx = this.db.transaction('nodes', 'readonly')
|
||||
nodeStore = trx.objectStore('nodes')
|
||||
trx = this.db.transaction('nodes', 'readonly')
|
||||
nodeStore = trx.objectStore('nodes')
|
||||
}
|
||||
|
||||
const getRequest = nodeStore.get(uuid)
|
||||
|
||||
getRequest.onsuccess = (event) => {
|
||||
// Node not found in IndexedDB.
|
||||
if (event.target.result === undefined) {
|
||||
reject("No such node")
|
||||
return
|
||||
}
|
||||
const node = this.node(uuid, event.target.result, -1)
|
||||
resolve(node)
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
getNodeAncestry(node, accumulated) {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
if (accumulated === undefined)
|
||||
accumulated = []
|
||||
|
||||
const getRequest = nodeStore.get(uuid)
|
||||
const nodeParentIndex = this.db
|
||||
.transaction('nodes', 'readonly')
|
||||
.objectStore('nodes')
|
||||
|
||||
getRequest.onsuccess = (event) => {
|
||||
// Node not found in IndexedDB.
|
||||
if (event.target.result === undefined) {
|
||||
reject("No such node")
|
||||
return
|
||||
}
|
||||
const node = this.node(uuid, event.target.result, -1)
|
||||
resolve(node)
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
getNodeAncestry(node, accumulated) {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
if (accumulated === undefined)
|
||||
accumulated = []
|
||||
if (node.UUID === ROOT_NODE || node.ParentUUID === ROOT_NODE) {
|
||||
resolve(accumulated)
|
||||
return
|
||||
}
|
||||
|
||||
const nodeParentIndex = this.db
|
||||
.transaction('nodes', 'readonly')
|
||||
.objectStore('nodes')
|
||||
if (node.UUID === DELETED_NODE || node.ParentUUID === DELETED_NODE) {
|
||||
resolve(accumulated)
|
||||
return
|
||||
}
|
||||
|
||||
if (node.UUID === ROOT_NODE || node.ParentUUID === ROOT_NODE) {
|
||||
resolve(accumulated)
|
||||
const getRequest = nodeParentIndex.get(node.ParentUUID)
|
||||
getRequest.onsuccess = (event) => {
|
||||
// Node not found in IndexedDB.
|
||||
// Not expected to happen.
|
||||
const parentNodeData = event.target.result
|
||||
if (parentNodeData === undefined) {
|
||||
reject("No such node")
|
||||
return
|
||||
}
|
||||
|
||||
const getRequest = nodeParentIndex.get(node.ParentUUID)
|
||||
getRequest.onsuccess = (event) => {
|
||||
// Node not found in IndexedDB.
|
||||
// Not expected to happen.
|
||||
const parentNodeData = event.target.result
|
||||
if (parentNodeData === undefined) {
|
||||
reject("No such node")
|
||||
return
|
||||
}
|
||||
const parentNode = this.node(parentNodeData.UUID, parentNodeData, -1)
|
||||
this.getNodeAncestry(parentNode, accumulated.concat(parentNode))
|
||||
.then(accumulated => resolve(accumulated))
|
||||
}
|
||||
})
|
||||
|
||||
const parentNode = this.node(parentNodeData.UUID, parentNodeData, -1)
|
||||
this.getNodeAncestry(parentNode, accumulated.concat(parentNode))
|
||||
.then(accumulated => resolve(accumulated))
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
newTransaction(objectStore, mode) {// {{{
|
||||
return this.db.transaction(objectStore, mode)
|
||||
}// }}}
|
||||
|
||||
}//}}}
|
||||
newTransaction(objectStore, mode) {// {{{
|
||||
return this.db.transaction(objectStore, mode)
|
||||
}// }}}
|
||||
|
||||
nodeCount() {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
const t = this.db.transaction('nodes', 'readwrite')
|
||||
const nodeStore = t.objectStore('nodes')
|
||||
const countReq = nodeStore.count()
|
||||
countReq.onsuccess = event => {
|
||||
resolve(event.target.result)
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
nodeCount() {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
const t = this.db.transaction('nodes', 'readwrite')
|
||||
const nodeStore = t.objectStore('nodes')
|
||||
const countReq = nodeStore.count()
|
||||
countReq.onsuccess = event => {
|
||||
resolve(event.target.result)
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
}
|
||||
|
||||
class SimpleNodeStore {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue