removed notes_parent, instead using notes_tree

This commit is contained in:
azivner 2017-11-19 11:28:46 -05:00
parent b22eb2db1e
commit f18d25911b
5 changed files with 69 additions and 60 deletions

View File

@ -0,0 +1,6 @@
UPDATE
notes_tree
SET
note_pid = (SELECT parent.note_id FROM notes_tree parent WHERE notes_tree.note_pid = parent.note_tree_id)
WHERE
note_pid != 'root'

View File

@ -1,28 +0,0 @@
CREATE TABLE [notes_parent] (
[parent_id] VARCHAR(30) NOT NULL,
[child_id] VARCHAR(30) NOT NULL,
date_created INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (parent_id, child_id)
);
INSERT INTO notes_parent (parent_id, child_id, date_created)
SELECT note_pid, note_tree_id, date_modified FROM notes_tree;
CREATE TABLE [notes_tree_mig] (
[note_tree_id] VARCHAR(30) PRIMARY KEY NOT NULL,
[note_id] VARCHAR(30) UNIQUE NOT NULL,
[note_pos] INTEGER NOT NULL,
[is_expanded] BOOLEAN NULL ,
date_modified INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0
);
INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted)
SELECT note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted FROM notes_tree;
DROP TABLE notes_tree;
ALTER TABLE notes_tree_mig RENAME TO notes_tree;
CREATE INDEX `IDX_notes_tree_note_id` ON `notes_tree` (
`note_id`
);

View File

@ -11,6 +11,7 @@ const noteTree = (function() {
let childToParents = {}; let childToParents = {};
let counter = 1; let counter = 1;
let noteTreeIdToKey = {}; let noteTreeIdToKey = {};
let parentChildToNoteTreeId = {};
function getNoteTreeIdFromKey(key) { function getNoteTreeIdFromKey(key) {
const node = treeUtils.getNodeByKey(key); const node = treeUtils.getNodeByKey(key);
@ -34,38 +35,54 @@ const noteTree = (function() {
clipboardNoteTreeId = cbNoteId; clipboardNoteTreeId = cbNoteId;
} }
function prepareNoteTree(notes, notesParent) { function getNoteTreeId(parentNoteId, childNoteId) {
const key = parentNoteId + "-" + childNoteId;
const noteTreeId = parentChildToNoteTreeId[key];
if (!noteTreeId) {
throw new Error("Can't find note tree id for parent=" + parentNoteId + ", child=" + childNoteId);
}
return noteTreeId;
}
function prepareNoteTree(notes) {
parentToChildren = {}; parentToChildren = {};
childToParents = {}; childToParents = {};
notesMap = {}; notesMap = {};
for (const note of notes) { for (const note of notes) {
notesMap[note.note_tree_id] = note; notesMap[note.note_tree_id] = note;
}
for (const np of notesParent) { const key = note.note_pid + "-" + note.note_id;
if (!parentToChildren[np.parent_id]) {
parentToChildren[np.parent_id] = []; parentChildToNoteTreeId[key] = note.note_tree_id;
if (!parentToChildren[note.note_pid]) {
parentToChildren[note.note_pid] = [];
} }
parentToChildren[np.parent_id].push(np.child_id); parentToChildren[note.note_pid].push(note.note_id);
if (!childToParents[np.child_id]) { if (!childToParents[note.note_id]) {
childToParents[np.child_id] = []; childToParents[note.note_id] = [];
} }
childToParents[np.child_id].push(np.parent_id); childToParents[note.note_id].push(note.note_pid);
} }
glob.allNoteIds = Object.keys(notesMap); glob.allNoteIds = Object.keys(notesMap);
return prepareNoteTreeInner(parentToChildren['root']); return prepareNoteTreeInner('root');
} }
function prepareNoteTreeInner(noteTreeIds) { function prepareNoteTreeInner(parentNoteId) {
const childNoteIds = parentToChildren[parentNoteId];
const noteList = []; const noteList = [];
for (const noteTreeId of noteTreeIds) { for (const childNoteId of childNoteIds) {
const noteTreeId = getNoteTreeId(parentNoteId, childNoteId);
const note = notesMap[noteTreeId]; const note = notesMap[noteTreeId];
note.title = note.note_title; note.title = note.note_title;
@ -79,11 +96,11 @@ const noteTree = (function() {
noteTreeIdToKey[noteTreeId] = note.key; noteTreeIdToKey[noteTreeId] = note.key;
if (parentToChildren[noteTreeId] && parentToChildren[noteTreeId].length > 0) { if (parentToChildren[note.note_id] && parentToChildren[note.note_id].length > 0) {
note.folder = true; note.folder = true;
if (note.expanded) { if (note.expanded) {
note.children = prepareNoteTreeInner(parentToChildren[noteTreeId], notesMap, parentToChildren); note.children = prepareNoteTreeInner(note.note_id);
} }
else { else {
note.lazy = true; note.lazy = true;
@ -99,21 +116,40 @@ const noteTree = (function() {
async function activateNode(notePath) { async function activateNode(notePath) {
const path = notePath.split("/").reverse(); const path = notePath.split("/").reverse();
if (!notesMap[path[0]]) {
console.log("Requested note doesn't exist.");
return;
}
const effectivePath = []; const effectivePath = [];
let childNoteId = null;
for (const noteTreeId of path) { for (const parentNoteId of path) {
effectivePath.push(noteTreeId); if (childNoteId !== null) {
const parents = childToParents[childNoteId];
if (!parents.includes(parentNoteId)) {
console.log("Did not find parent " + parentNoteId + " for child " + childNoteId);
if (parents.length > 0) {
childNoteId = parents[0];
effectivePath.push(childNoteId);
console.log("Choosing parent " + childNoteId + " instead.");
continue;
}
else {
console.log("No parents, can't activate node.");
return;
}
}
}
effectivePath.push(parentNoteId);
childNoteId = parentNoteId;
} }
const runPath = effectivePath.reverse(); const runPath = effectivePath.reverse();
let parentNoteId = 'root';
for (let i = 0; i < runPath.length; i++) { for (let i = 0; i < runPath.length; i++) {
const noteTreeId = runPath[i]; const childNoteId = runPath[i];
const noteTreeId = getNoteTreeId(parentNoteId, childNoteId);
const node = treeUtils.getNodeByNoteTreeId(noteTreeId); const node = treeUtils.getNodeByNoteTreeId(noteTreeId);
@ -123,6 +159,8 @@ const noteTree = (function() {
else { else {
await node.setActive(); await node.setActive();
} }
parentNoteId = childNoteId;
} }
} }
@ -201,6 +239,7 @@ const noteTree = (function() {
setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false); setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false);
}, },
init: (event, data) => { init: (event, data) => {
showAppIfHidden();
if (startNoteTreeId) { if (startNoteTreeId) {
activateNode(startNoteTreeId); activateNode(startNoteTreeId);
} }

View File

@ -58,8 +58,8 @@ const treeUtils = (function() {
const path = []; const path = [];
while (node) { while (node) {
if (node.data.note_tree_id) { if (node.data.note_id) {
path.push(node.data.note_tree_id); path.push(node.data.note_id);
} }
node = node.getParent(); node = node.getParent();

View File

@ -21,13 +21,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
+ "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 " + "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 "
+ "order by note_pos"); + "order by note_pos");
const notes_parent = await sql.getResults("" +
"select parent_id, child_id " +
"from notes_parent " +
"join notes_tree as child on child.note_tree_id = notes_parent.child_id " +
"left join notes_tree as parent on parent.note_tree_id = notes_parent.parent_id " +
"where child.is_deleted = 0 and (parent.is_deleted = 0 or notes_parent.parent_id = 'root')");
const dataKey = protected_session.getDataKey(req); const dataKey = protected_session.getDataKey(req);
for (const note of notes) { for (const note of notes) {
@ -38,7 +31,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
res.send({ res.send({
notes: notes, notes: notes,
notes_parent: notes_parent,
start_note_tree_id: await options.getOption('start_note_tree_id'), start_note_tree_id: await options.getOption('start_note_tree_id'),
tree_load_time: utils.nowTimestamp() tree_load_time: utils.nowTimestamp()
}); });