From 91ee90d827b9a4cd872ab1c990a6df60ad7a001f Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 25 Mar 2018 22:37:02 -0400 Subject: [PATCH] cleanup in tree service --- .../javascripts/dialogs/edit_tree_prefix.js | 3 +- src/public/javascripts/services/bootstrap.js | 7 ++- .../javascripts/services/context_menu.js | 7 ++- src/public/javascripts/services/info.js | 5 +- src/public/javascripts/services/init.js | 3 +- src/public/javascripts/services/tree.js | 59 ++++--------------- 6 files changed, 27 insertions(+), 57 deletions(-) diff --git a/src/public/javascripts/dialogs/edit_tree_prefix.js b/src/public/javascripts/dialogs/edit_tree_prefix.js index 63629d758..41a607804 100644 --- a/src/public/javascripts/dialogs/edit_tree_prefix.js +++ b/src/public/javascripts/dialogs/edit_tree_prefix.js @@ -1,5 +1,6 @@ import treeService from '../services/tree.js'; import server from '../services/server.js'; +import treeCache from "../services/tree_cache.js"; const $dialog = $("#edit-tree-prefix-dialog"); const $form = $("#edit-tree-prefix-form"); @@ -19,7 +20,7 @@ async function showDialog() { const currentNode = treeService.getCurrentNode(); branchId = currentNode.data.branchId; - const branch = await treeService.getBranch(branchId); + const branch = await treeCache.getBranch(branchId); $treePrefixInput.val(branch.prefix).focus(); diff --git a/src/public/javascripts/services/bootstrap.js b/src/public/javascripts/services/bootstrap.js index 69d9c0ca0..a31c0b846 100644 --- a/src/public/javascripts/services/bootstrap.js +++ b/src/public/javascripts/services/bootstrap.js @@ -61,4 +61,9 @@ utils.bindShortcut('alt+l', labelsDialog.showDialog); $("#settings-button").click(settingsDialog.showDialog); -utils.bindShortcut('alt+o', sqlConsoleDialog.showDialog); \ No newline at end of file +utils.bindShortcut('alt+o', sqlConsoleDialog.showDialog); + +if (utils.isElectron()) { + utils.bindShortcut('alt+left', window.history.back); + utils.bindShortcut('alt+right', window.history.forward); +} \ No newline at end of file diff --git a/src/public/javascripts/services/context_menu.js b/src/public/javascripts/services/context_menu.js index a5d96b70d..f15acd69e 100644 --- a/src/public/javascripts/services/context_menu.js +++ b/src/public/javascripts/services/context_menu.js @@ -8,6 +8,7 @@ import treeUtils from './tree_utils.js'; import utils from './utils.js'; import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js'; import infoService from "./info.js"; +import treeCache from "./tree_cache.js"; const $tree = $("#tree"); @@ -103,9 +104,9 @@ const contextMenuSettings = { ], beforeOpen: async (event, ui) => { const node = $.ui.fancytree.getNode(ui.target); - const branch = await treeService.getBranch(node.data.branchId); - const note = await treeService.getNote(node.data.noteId); - const parentNote = await treeService.getNote(branch.parentNoteId); + const branch = await treeCache.getBranch(branchId); + const note = await treeCache.getNote(node.data.noteId); + const parentNote = await treeCache.getNote(branch.parentNoteId); // Modify menu entries depending on node status $tree.contextmenu("enableEntry", "pasteAfter", clipboardIds.length > 0 && (!parentNote || parentNote.type !== 'search')); diff --git a/src/public/javascripts/services/info.js b/src/public/javascripts/services/info.js index b20f6262d..48ab4eae4 100644 --- a/src/public/javascripts/services/info.js +++ b/src/public/javascripts/services/info.js @@ -1,7 +1,8 @@ import messagingService from "./messaging.js"; +import utils from "./utils.js"; function showMessage(message) { - console.log(now(), "message: ", message); + console.log(utils.now(), "message: ", message); $.notify({ // options @@ -14,7 +15,7 @@ function showMessage(message) { } function showError(message, delay = 10000) { - console.log(now(), "error: ", message); + console.log(utils.now(), "error: ", message); $.notify({ // options diff --git a/src/public/javascripts/services/init.js b/src/public/javascripts/services/init.js index 67002d996..54e9dd5fd 100644 --- a/src/public/javascripts/services/init.js +++ b/src/public/javascripts/services/init.js @@ -6,6 +6,7 @@ import treeUtils from './tree_utils.js'; import utils from './utils.js'; import server from './server.js'; import bundleService from './bundle.js'; +import treeCache from "./tree_cache.js"; // hot keys are active also inside inputs and content editables jQuery.hotkeys.options.filterInputAcceptingElements = false; @@ -218,7 +219,7 @@ $(document).ready(() => { if (utils.isElectron()) { require('electron').ipcRenderer.on('create-day-sub-note', async function(event, parentNoteId) { // this might occur when day note had to be created - if (!await treeService.noteExists(parentNoteId)) { + if (!await treeCache.getNote(parentNoteId)) { await treeService.reload(); } diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index c6dfaef2a..93a772031 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -24,16 +24,6 @@ let instanceName = null; // should have better place let startNotePath = null; -async function getNote(noteId) { - const note = await treeCache.getNote(noteId); - - if (!note) { - infoService.throwError(`Can't find title for noteId='${noteId}'`); - } - - return note; -} - async function getNoteTitle(noteId, parentNoteId = null) { utils.assertArguments(noteId); @@ -403,7 +393,7 @@ function clearSelectedNodes() { async function treeInitialized() { const noteId = treeUtils.getNoteIdFromNotePath(startNotePath); - if ((await treeCache.getNote(noteId)) === undefined) { + if (!await treeCache.getNote(noteId)) { // note doesn't exist so don't try to activate it startNotePath = null; } @@ -609,7 +599,7 @@ function initFancyTree(branch) { dnd: dragAndDropSetup, lazyLoad: function(event, data) { const noteId = data.node.data.noteId; - data.result = getNote(noteId).then(note => { + data.result = treeCache.getNote(noteId).then(note => { if (note.type === 'search') { return loadSearchNote(noteId); } @@ -760,7 +750,9 @@ async function getAutocompleteItems(parentNoteId, notePath, titlePath) { async function setNoteTitle(noteId, title) { utils.assertArguments(noteId); - getNote(noteId).title = title; + const note = await treeCache.getNote(noteId); + + note.title = title; for (const clone of getNodesByNoteId(noteId)) { await setNodeTitleWithPrefix(clone); @@ -846,18 +838,10 @@ async function sortAlphabetically(noteId) { await reload(); } -async function noteExists(noteId) { - return !!(await treeCache.getNote(noteId)); -} - function getInstanceName() { return instanceName; } -async function getBranch(branchId) { - return await treeCache.getBranch(branchId); -} - messagingService.subscribeToMessages(syncData => { if (syncData.some(sync => sync.entityName === 'branches') || syncData.some(sync => sync.entityName === 'notes')) { @@ -868,33 +852,27 @@ messagingService.subscribeToMessages(syncData => { } }); -$(document).bind('keydown', 'ctrl+o', e => { +utils.bindShortcut('ctrl+o', () => { const node = getCurrentNode(); const parentNoteId = node.data.parentNoteId; const isProtected = treeUtils.getParentProtectedStatus(node); createNote(node, parentNoteId, 'after', isProtected); - - e.preventDefault(); }); -$(document).bind('keydown', 'ctrl+p', e => { +utils.bindShortcut('ctrl+p', () => { const node = getCurrentNode(); createNote(node, node.data.noteId, 'into', node.data.isProtected); - - e.preventDefault(); }); -$(document).bind('keydown', 'ctrl+del', e => { +utils.bindShortcut('ctrl+del', () => { const node = getCurrentNode(); treeChangesService.deleteNodes([node]); - - e.preventDefault(); }); -$(document).bind('keydown', 'ctrl+.', scrollToCurrentNote); +utils.bindShortcut('ctrl+.', scrollToCurrentNote); $(window).bind('hashchange', function() { const notePath = getNotePathFromAddress(); @@ -906,20 +884,6 @@ $(window).bind('hashchange', function() { } }); -if (utils.isElectron()) { - $(document).bind('keydown', 'alt+left', e => { - window.history.back(); - - e.preventDefault(); - }); - - $(document).bind('keydown', 'alt+right', e => { - window.history.forward(); - - e.preventDefault(); - }); -} - $createTopLevelNoteButton.click(createNewTopLevelNote); $collapseTreeButton.click(collapseTree); $scrollToCurrentNoteButton.click(scrollToCurrentNote); @@ -946,8 +910,5 @@ export default { setParentChildRelation, getSelectedNodes, sortAlphabetically, - noteExists, - getInstanceName, - getBranch, - getNote + getInstanceName }; \ No newline at end of file