note tree refactorings

This commit is contained in:
azivner 2018-03-12 23:14:09 -04:00
parent 834661c461
commit e5c0acbb43
3 changed files with 49 additions and 26 deletions

View File

@ -19,8 +19,9 @@ const editTreePrefix = (function() {
const currentNode = noteTree.getCurrentNode(); const currentNode = noteTree.getCurrentNode();
noteTreeId = currentNode.data.noteTreeId; noteTreeId = currentNode.data.noteTreeId;
const nt = noteTree.getNoteTree(noteTreeId);
$treePrefixInput.val(currentNode.data.prefix).focus(); $treePrefixInput.val(nt.prefix).focus();
const noteTitle = noteTree.getNoteTitle(currentNode.data.noteId); const noteTitle = noteTree.getNoteTitle(currentNode.data.noteId);

View File

@ -89,11 +89,18 @@ const noteTree = (function() {
notesTreeMap[noteTreeId].prefix = prefix; notesTreeMap[noteTreeId].prefix = prefix;
getNodesByNoteTreeId(noteTreeId).map(node => { getNodesByNoteTreeId(noteTreeId).map(node => setNodeTitleWithPrefix(node));
node.data.prefix = prefix; }
treeUtils.setNodeTitleWithPrefix(node); function setNodeTitleWithPrefix(node) {
}); const noteTitle = getNoteTitle(node.data.noteId);
const noteTree = notesTreeMap[node.data.noteTreeId];
const prefix = noteTree.prefix;
const title = (prefix ? (prefix + " - ") : "") + noteTitle;
node.setTitle(escapeHtml(title));
} }
function removeParentChildRelation(parentNoteId, childNoteId) { function removeParentChildRelation(parentNoteId, childNoteId) {
@ -138,6 +145,7 @@ const noteTree = (function() {
notesTreeMap[note.noteTreeId] = note; notesTreeMap[note.noteTreeId] = note;
noteIdToNote[note.noteId] = { noteIdToNote[note.noteId] = {
noteId: note.noteId,
title: note.title, title: note.title,
isProtected: note.isProtected, isProtected: note.isProtected,
type: note.type, type: note.type,
@ -182,24 +190,26 @@ const noteTree = (function() {
const noteList = []; const noteList = [];
for (const noteId of childNoteIds) { for (const noteId of childNoteIds) {
const note = getNote(noteId);
const noteTreeId = getNoteTreeId(parentNoteId, noteId); const noteTreeId = getNoteTreeId(parentNoteId, noteId);
const noteTree = notesTreeMap[noteTreeId]; const noteTree = notesTreeMap[noteTreeId];
const title = (noteTree.prefix ? (noteTree.prefix + " - ") : "") + getNote(noteTree.noteId).title; const title = (noteTree.prefix ? (noteTree.prefix + " - ") : "") + note.title;
const node = { const node = {
noteId: noteTree.noteId, noteId: noteId,
parentNoteId: noteTree.parentNoteId, parentNoteId: noteTree.parentNoteId,
noteTreeId: noteTree.noteTreeId, noteTreeId: noteTree.noteTreeId,
isProtected: noteTree.isProtected, isProtected: note.isProtected,
prefix: noteTree.prefix,
title: escapeHtml(title), title: escapeHtml(title),
extraClasses: getExtraClasses(noteTree), extraClasses: getExtraClasses(note),
refKey: noteTree.noteId, refKey: noteId,
expanded: noteTree.isExpanded expanded: noteTree.isExpanded
}; };
if (parentToChildren[noteId] && parentToChildren[noteId].length > 0) { const hasChildren = parentToChildren[noteId] && parentToChildren[noteId].length > 0;
if (hasChildren || note.type === 'search') {
node.folder = true; node.folder = true;
if (node.expanded) { if (node.expanded) {
@ -626,9 +636,15 @@ const noteTree = (function() {
}, },
dnd: dragAndDropSetup, dnd: dragAndDropSetup,
lazyLoad: function(event, data){ lazyLoad: function(event, data){
const node = data.node.data; const noteId = data.node.data.noteId;
const note = getNote(noteId);
data.result = prepareNoteTreeInner(node.noteId); if (note.type === 'search') {
data.result = loadSearchNote(noteId);
}
else {
data.result = prepareNoteTreeInner(noteId);
}
}, },
clones: { clones: {
highlightActiveClones: true highlightActiveClones: true
@ -638,6 +654,16 @@ const noteTree = (function() {
$tree.contextmenu(contextMenu.contextMenuSettings); $tree.contextmenu(contextMenu.contextMenuSettings);
} }
async function loadSearchNote(noteId) {
const note = await server.get('notes/' + noteId);
const json = JSON.parse(note.detail.content);
const noteIds = await server.get('notes?search=' + encodeURIComponent(json.searchString));
console.log("Found: ", noteIds);
}
function getTree() { function getTree() {
return $tree.fancytree('getTree'); return $tree.fancytree('getTree');
} }
@ -755,7 +781,7 @@ const noteTree = (function() {
getNote(noteId).title = title; getNote(noteId).title = title;
getNodesByNoteId(noteId).map(clone => treeUtils.setNodeTitleWithPrefix(clone)); getNodesByNoteId(noteId).map(clone => setNodeTitleWithPrefix(clone));
} }
async function createNewTopLevelNote() { async function createNewTopLevelNote() {
@ -787,6 +813,7 @@ const noteTree = (function() {
notesTreeMap[result.noteTreeId] = result; notesTreeMap[result.noteTreeId] = result;
noteIdToNote[result.noteId] = { noteIdToNote[result.noteId] = {
noteId: result.noteId,
title: result.title, title: result.title,
isProtected: result.isProtected, isProtected: result.isProtected,
type: result.type, type: result.type,
@ -844,6 +871,10 @@ const noteTree = (function() {
return instanceName; return instanceName;
} }
function getNoteTree(noteTreeId) {
return notesTreeMap[noteTreeId];
}
$(document).bind('keydown', 'ctrl+o', e => { $(document).bind('keydown', 'ctrl+o', e => {
const node = getCurrentNode(); const node = getCurrentNode();
const parentNoteId = node.data.parentNoteId; const parentNoteId = node.data.parentNoteId;
@ -919,6 +950,7 @@ const noteTree = (function() {
getSelectedNodes, getSelectedNodes,
sortAlphabetically, sortAlphabetically,
noteExists, noteExists,
getInstanceName getInstanceName,
getNoteTree
}; };
})(); })();

View File

@ -31,20 +31,10 @@ const treeUtils = (function() {
return path.reverse().join("/"); return path.reverse().join("/");
} }
function setNodeTitleWithPrefix(node) {
const noteTitle = noteTree.getNoteTitle(node.data.noteId);
const prefix = node.data.prefix;
const title = (prefix ? (prefix + " - ") : "") + noteTitle;
node.setTitle(escapeHtml(title));
}
return { return {
getParentProtectedStatus, getParentProtectedStatus,
getNodeByKey, getNodeByKey,
getNotePath, getNotePath,
getNoteIdFromNotePath, getNoteIdFromNotePath,
setNodeTitleWithPrefix
}; };
})(); })();