tree utils as a module

This commit is contained in:
azivner 2017-11-04 22:18:36 -04:00
parent 900f169dab
commit 30df3cba1c
10 changed files with 82 additions and 65 deletions

View File

@ -4,7 +4,7 @@ const contextMenu = (function() {
const treeEl = $("#tree"); const treeEl = $("#tree");
function pasteAfter(node) { function pasteAfter(node) {
const subjectNode = getNodeByKey(noteTree.getClipboardNoteId()); const subjectNode = treeUtils.getNodeByKey(noteTree.getClipboardNoteId());
treeChanges.moveAfterNode(subjectNode, node); treeChanges.moveAfterNode(subjectNode, node);
@ -12,7 +12,7 @@ const contextMenu = (function() {
} }
function pasteInto(node) { function pasteInto(node) {
const subjectNode = getNodeByKey(noteTree.getClipboardNoteId()); const subjectNode = treeUtils.getNodeByKey(noteTree.getClipboardNoteId());
treeChanges.moveToNode(subjectNode, node); treeChanges.moveToNode(subjectNode, node);
@ -56,8 +56,8 @@ const contextMenu = (function() {
const node = $.ui.fancytree.getNode(ui.target); const node = $.ui.fancytree.getNode(ui.target);
if (ui.cmd === "insertNoteHere") { if (ui.cmd === "insertNoteHere") {
const parentKey = getParentKey(node); const parentKey = treeUtils.getParentKey(node);
const encryption = getParentEncryption(node); const encryption = treeUtils.getParentEncryption(node);
noteEditor.createNote(node, parentKey, 'after', encryption); noteEditor.createNote(node, parentKey, 'after', encryption);
} }

View File

@ -21,7 +21,7 @@ const addLink = (function() {
linkTitleEl.val(''); linkTitleEl.val('');
function setDefaultLinkTitle(noteId) { function setDefaultLinkTitle(noteId) {
const noteTitle = getNoteTitle(noteId); const noteTitle = treeUtils.getNoteTitle(noteId);
linkTitleEl.val(noteTitle); linkTitleEl.val(noteTitle);
} }

View File

@ -28,7 +28,7 @@ const jumpToNote = (function() {
const noteId = link.getNodeIdFromLabel(val); const noteId = link.getNodeIdFromLabel(val);
if (noteId) { if (noteId) {
getNodeByKey(noteId).setActive(); treeUtils.activateNode(noteId);
dialogEl.dialog('close'); dialogEl.dialog('close');
} }

View File

@ -40,7 +40,7 @@ const recentNotes = (function() {
const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId()); const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId());
$.each(recNotes, (key, valueNoteId) => { $.each(recNotes, (key, valueNoteId) => {
const noteTitle = getFullName(valueNoteId); const noteTitle = treeUtils.getFullName(valueNoteId);
if (!noteTitle) { if (!noteTitle) {
return; return;
@ -66,7 +66,7 @@ const recentNotes = (function() {
function setActiveNoteBasedOnRecentNotes() { function setActiveNoteBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes(); const noteId = getSelectedNoteIdFromRecentNotes();
getNodeByKey(noteId).setActive(); treeUtils.activateNode(noteId);
dialogEl.dialog('close'); dialogEl.dialog('close');
} }
@ -74,7 +74,7 @@ const recentNotes = (function() {
function addLinkBasedOnRecentNotes() { function addLinkBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes(); const noteId = getSelectedNoteIdFromRecentNotes();
const linkTitle = getNoteTitle(noteId); const linkTitle = treeUtils.getNoteTitle(noteId);
dialogEl.dialog("close"); dialogEl.dialog("close");

View File

@ -42,7 +42,7 @@ const encryption = (function() {
open: () => { open: () => {
if (!modal) { if (!modal) {
// dialog steals focus for itself, which is not what we want for non-modal (viewing) // dialog steals focus for itself, which is not what we want for non-modal (viewing)
getNodeByKey(noteEditor.getCurrentNoteId()).setFocus(); treeUtils.getNodeByKey(noteEditor.getCurrentNoteId()).setFocus();
} }
} }
}); });
@ -105,7 +105,7 @@ const encryption = (function() {
} }
for (const noteId of glob.allNoteIds) { for (const noteId of glob.allNoteIds) {
const note = getNodeByKey(noteId); const note = treeUtils.getNodeByKey(noteId);
if (note.data.encryption > 0) { if (note.data.encryption > 0) {
const title = decryptString(note.data.note_title); const title = decryptString(note.data.note_title);
@ -375,7 +375,7 @@ const encryption = (function() {
function updateSubTreeRecursively(noteId, updateCallback, successCallback) { function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
updateNoteSynchronously(noteId, updateCallback, successCallback); updateNoteSynchronously(noteId, updateCallback, successCallback);
const node = getNodeByKey(noteId); const node = treeUtils.getNodeByKey(noteId);
if (!node || !node.getChildren()) { if (!node || !node.getChildren()) {
return; return;
} }

View File

@ -25,7 +25,7 @@ const link = (function() {
function createNoteLink(noteId) { function createNoteLink(noteId) {
const noteLink = $("<a>", { const noteLink = $("<a>", {
href: 'javascript:', href: 'javascript:',
text: getFullName(noteId) text: treeUtils.getFullName(noteId)
}).attr('action', 'note') }).attr('action', 'note')
.attr('note-id', noteId); .attr('note-id', noteId);
@ -41,7 +41,7 @@ const link = (function() {
} }
if (noteId) { if (noteId) {
getNodeByKey(noteId).setActive(); treeUtils.activateNode(noteId);
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
$("[role='tooltip']").remove(); $("[role='tooltip']").remove();

View File

@ -78,7 +78,7 @@ const noteEditor = (function() {
const title = noteTitleEl.val(); const title = noteTitleEl.val();
getNodeByKey(note.detail.note_id).setTitle(title); treeUtils.getNodeByKey(note.detail.note_id).setTitle(title);
note.detail.note_title = title; note.detail.note_title = title;
} }
@ -155,7 +155,7 @@ const noteEditor = (function() {
} }
function setTreeBasedOnEncryption(note) { function setTreeBasedOnEncryption(note) {
const node = getNodeByKey(note.detail.note_id); const node = treeUtils.getNodeByKey(note.detail.note_id);
node.toggleClass("encrypted", note.detail.encryption > 0); node.toggleClass("encrypted", note.detail.encryption > 0);
} }

View File

@ -58,8 +58,8 @@ const noteTree = (function() {
function initFancyTree(notes, startNoteId) { function initFancyTree(notes, startNoteId) {
const keybindings = { const keybindings = {
"insert": node => { "insert": node => {
const parentKey = getParentKey(node); const parentKey = treeUtils.getParentKey(node);
const encryption = getParentEncryption(node); const encryption = treeUtils.getParentEncryption(node);
noteEditor.createNote(node, parentKey, 'after', encryption); noteEditor.createNote(node, parentKey, 'after', encryption);
}, },
@ -227,7 +227,7 @@ const noteTree = (function() {
$(document).bind('keydown', 'alt+c', collapseTree); $(document).bind('keydown', 'alt+c', collapseTree);
function scrollToCurrentNote() { function scrollToCurrentNote() {
const node = getNodeByKey(noteEditor.getCurrentNoteId()); const node = treeUtils.getNodeByKey(noteEditor.getCurrentNoteId());
if (node) { if (node) {
node.makeVisible({scrollIntoView: true}); node.makeVisible({scrollIntoView: true});

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
const treeUtils = (function() {
const treeEl = $("#tree"); const treeEl = $("#tree");
function getParentKey(node) { function getParentKey(node) {
@ -14,8 +15,14 @@ function getNodeByKey(noteId) {
return treeEl.fancytree('getNodeByKey', noteId); return treeEl.fancytree('getNodeByKey', noteId);
} }
function activateNode(noteId) {
const node = treeUtils.getNodeByKey(noteId);
node.setActive();
}
function getNoteTitle(noteId) { function getNoteTitle(noteId) {
const note = getNodeByKey(noteId); const note = treeUtils.getNodeByKey(noteId);
if (!note) { if (!note) {
return; return;
} }
@ -30,7 +37,7 @@ function getNoteTitle(noteId) {
} }
function getFullName(noteId) { function getFullName(noteId) {
let note = getNodeByKey(noteId); let note = treeUtils.getNodeByKey(noteId);
if (note === null) { if (note === null) {
return "[unknown]"; return "[unknown]";
@ -59,3 +66,13 @@ function getFullName(noteId) {
return path.reverse().join(" > "); return path.reverse().join(" > ");
} }
return {
getParentKey,
getParentEncryption,
getNodeByKey,
activateNode,
getNoteTitle,
getFullName
};
})();

View File

@ -20,7 +20,7 @@ function getAutocompleteItems(noteIds) {
const autocompleteItems = []; const autocompleteItems = [];
for (const noteId of noteIds) { for (const noteId of noteIds) {
const fullName = getFullName(noteId); const fullName = treeUtils.getFullName(noteId);
if (fullName !== null) { if (fullName !== null) {
autocompleteItems.push({ autocompleteItems.push({