From d3316cd09cfb1908d0f17fd80466e76ee332df88 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 25 Mar 2018 21:29:35 -0400 Subject: [PATCH] reduced dependencies of utils --- src/public/javascripts/dialogs/labels.js | 3 +- .../javascripts/dialogs/recent_notes.js | 2 +- src/public/javascripts/dialogs/settings.js | 17 +++---- src/public/javascripts/dialogs/sql_console.js | 5 +- src/public/javascripts/services/bundle.js | 2 +- .../javascripts/services/context_menu.js | 9 ++-- src/public/javascripts/services/info.js | 39 ++++++++++++++++ src/public/javascripts/services/messaging.js | 7 +-- .../javascripts/services/note_detail.js | 11 +++-- src/public/javascripts/services/note_type.js | 3 +- .../javascripts/services/protected_session.js | 5 +- src/public/javascripts/services/server.js | 5 +- src/public/javascripts/services/sync.js | 7 +-- src/public/javascripts/services/tree.js | 11 +++-- src/public/javascripts/services/tree_cache.js | 3 +- .../javascripts/services/tree_changes.js | 3 +- src/public/javascripts/services/utils.js | 46 +------------------ 17 files changed, 93 insertions(+), 85 deletions(-) create mode 100644 src/public/javascripts/services/info.js diff --git a/src/public/javascripts/dialogs/labels.js b/src/public/javascripts/dialogs/labels.js index 87c1482fa..178639519 100644 --- a/src/public/javascripts/dialogs/labels.js +++ b/src/public/javascripts/dialogs/labels.js @@ -1,6 +1,7 @@ import noteDetailService from '../services/note_detail.js'; import utils from '../services/utils.js'; import server from '../services/server.js'; +import infoService from "../services/info.js"; const $dialog = $("#labels-dialog"); const $saveLabelsButton = $("#save-labels-button"); @@ -91,7 +92,7 @@ function LabelsModel() { addLastEmptyRow(); - utils.showMessage("Labels have been saved."); + infoService.showMessage("Labels have been saved."); noteDetailService.loadLabelList(); }; diff --git a/src/public/javascripts/dialogs/recent_notes.js b/src/public/javascripts/dialogs/recent_notes.js index 23464c1e0..d51df4db8 100644 --- a/src/public/javascripts/dialogs/recent_notes.js +++ b/src/public/javascripts/dialogs/recent_notes.js @@ -1,7 +1,7 @@ import treeService from '../services/tree.js'; import messagingService from '../services/messaging.js'; import server from '../services/server.js'; -import utils from "../services/utils"; +import utils from "../services/utils.js"; const $dialog = $("#recent-notes-dialog"); const $searchInput = $('#recent-notes-search-input'); diff --git a/src/public/javascripts/dialogs/settings.js b/src/public/javascripts/dialogs/settings.js index d7a24b766..283067f1e 100644 --- a/src/public/javascripts/dialogs/settings.js +++ b/src/public/javascripts/dialogs/settings.js @@ -3,6 +3,7 @@ import protectedSessionHolder from '../services/protected_session_holder.js'; import utils from '../services/utils.js'; import server from '../services/server.js'; +import infoService from "../services/info.js"; const $dialog = $("#settings-dialog"); const $tabs = $("#settings-tabs"); @@ -38,7 +39,7 @@ async function saveSettings(settingName, settingValue) { value: settingValue }); - utils.showMessage("Settings change have been saved."); + infoService.showMessage("Settings change have been saved."); } export default { @@ -80,7 +81,7 @@ addModule((function() { protectedSessionHolder.resetProtectedSession(); } else { - utils.showError(result.message); + infoService.showError(result.message); } }); @@ -164,27 +165,27 @@ addModule((async function () { $forceFullSyncButton.click(async () => { await server.post('sync/force-full-sync'); - utils.showMessage("Full sync triggered"); + infoService.showMessage("Full sync triggered"); }); $fillSyncRowsButton.click(async () => { await server.post('sync/fill-sync-rows'); - utils.showMessage("Sync rows filled successfully"); + infoService.showMessage("Sync rows filled successfully"); }); $anonymizeButton.click(async () => { await server.post('anonymization/anonymize'); - utils.showMessage("Created anonymized database"); + infoService.showMessage("Created anonymized database"); }); $cleanupSoftDeletedButton.click(async () => { if (confirm("Do you really want to clean up soft-deleted items?")) { await server.post('cleanup/cleanup-soft-deleted-items'); - utils.showMessage("Soft deleted items have been cleaned up"); + infoService.showMessage("Soft deleted items have been cleaned up"); } }); @@ -192,14 +193,14 @@ addModule((async function () { if (confirm("Do you really want to clean up unused images?")) { await server.post('cleanup/cleanup-unused-images'); - utils.showMessage("Unused images have been cleaned up"); + infoService.showMessage("Unused images have been cleaned up"); } }); $vacuumDatabaseButton.click(async () => { await server.post('cleanup/vacuum-database'); - utils.showMessage("Database has been vacuumed"); + infoService.showMessage("Database has been vacuumed"); }); return {}; diff --git a/src/public/javascripts/dialogs/sql_console.js b/src/public/javascripts/dialogs/sql_console.js index 69bb0a2f1..bd69bc091 100644 --- a/src/public/javascripts/dialogs/sql_console.js +++ b/src/public/javascripts/dialogs/sql_console.js @@ -1,5 +1,6 @@ import utils from '../services/utils.js'; import server from '../services/server.js'; +import infoService from "../services/info.js"; const $dialog = $("#sql-console-dialog"); const $query = $('#sql-console-query'); @@ -60,11 +61,11 @@ async function execute(e) { }); if (!result.success) { - utils.showError(result.error); + infoService.showError(result.error); return; } else { - utils.showMessage("Query was executed successfully."); + infoService.showMessage("Query was executed successfully."); } const rows = result.rows; diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.js index cdd655269..578a1ba9a 100644 --- a/src/public/javascripts/services/bundle.js +++ b/src/public/javascripts/services/bundle.js @@ -1,4 +1,4 @@ -import ScriptContext from "./script_context"; +import ScriptContext from "./script_context.js"; async function executeBundle(bundle) { const apiContext = ScriptContext(bundle.note, bundle.allNotes); diff --git a/src/public/javascripts/services/context_menu.js b/src/public/javascripts/services/context_menu.js index ac8e87ed9..a5d96b70d 100644 --- a/src/public/javascripts/services/context_menu.js +++ b/src/public/javascripts/services/context_menu.js @@ -7,6 +7,7 @@ import treeChangesService from './tree_changes.js'; import treeUtils from './tree_utils.js'; import utils from './utils.js'; import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js'; +import infoService from "./info.js"; const $tree = $("#tree"); @@ -33,7 +34,7 @@ async function pasteAfter(node) { // just do nothing } else { - utils.throwError("Unrecognized clipboard mode=" + clipboardMode); + infoService.throwError("Unrecognized clipboard mode=" + clipboardMode); } } @@ -56,7 +57,7 @@ async function pasteInto(node) { // just do nothing } else { - utils.throwError("Unrecognized clipboard mode=" + mode); + infoService.throwError("Unrecognized clipboard mode=" + mode); } } @@ -64,14 +65,14 @@ function copy(nodes) { clipboardIds = nodes.map(node => node.data.noteId); clipboardMode = 'copy'; - utils.showMessage("Note(s) have been copied into clipboard."); + infoService.showMessage("Note(s) have been copied into clipboard."); } function cut(nodes) { clipboardIds = nodes.map(node => node.key); clipboardMode = 'cut'; - utils.showMessage("Note(s) have been cut into clipboard."); + infoService.showMessage("Note(s) have been cut into clipboard."); } const contextMenuSettings = { diff --git a/src/public/javascripts/services/info.js b/src/public/javascripts/services/info.js new file mode 100644 index 000000000..b20f6262d --- /dev/null +++ b/src/public/javascripts/services/info.js @@ -0,0 +1,39 @@ +import messagingService from "./messaging.js"; + +function showMessage(message) { + console.log(now(), "message: ", message); + + $.notify({ + // options + message: message + }, { + // settings + type: 'success', + delay: 3000 + }); +} + +function showError(message, delay = 10000) { + console.log(now(), "error: ", message); + + $.notify({ + // options + message: message + }, { + // settings + type: 'danger', + delay: delay + }); +} + +function throwError(message) { + messagingService.logError(message); + + throw new Error(message); +} + +export default { + showMessage, + showError, + throwError +} \ No newline at end of file diff --git a/src/public/javascripts/services/messaging.js b/src/public/javascripts/services/messaging.js index dc57ed818..b40f7a376 100644 --- a/src/public/javascripts/services/messaging.js +++ b/src/public/javascripts/services/messaging.js @@ -1,4 +1,5 @@ import utils from './utils.js'; +import infoService from "./info.js"; const $changesToPushCount = $("#changes-to-push-count"); @@ -45,10 +46,10 @@ function handleMessage(event) { $changesToPushCount.html(message.changesToPushCount); } else if (message.type === 'sync-hash-check-failed') { - utils.utils.showError("Sync check failed!", 60000); + infoService.showError("Sync check failed!", 60000); } else if (message.type === 'consistency-checks-failed') { - utils.showError("Consistency checks failed! See logs for details.", 50 * 60000); + infoService.showError("Consistency checks failed! See logs for details.", 50 * 60000); } } @@ -91,7 +92,7 @@ setTimeout(() => { await connectionBrokenNotification.close(); connectionBrokenNotification = null; - utils.showMessage("Re-connected to server"); + infoService.showMessage("Re-connected to server"); } ws.send(JSON.stringify({ diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index e3a895c6d..51d3fbeda 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -6,6 +6,7 @@ import utils from './utils.js'; import server from './server.js'; import messagingService from "./messaging.js"; import bundleService from "./bundle.js"; +import infoService from "./info.js"; const $noteTitle = $("#note-title"); @@ -109,7 +110,7 @@ function updateNoteFromInputs(note) { // nothing } else { - utils.throwError("Unrecognized type: " + note.detail.type); + infoService.throwError("Unrecognized type: " + note.detail.type); } const title = $noteTitle.val(); @@ -124,7 +125,7 @@ async function saveNoteToServer(note) { isNoteChanged = false; - utils.showMessage("Saved!"); + infoService.showMessage("Saved!"); } function setNoteBackgroundIfProtected(note) { @@ -318,7 +319,7 @@ function focus() { // do nothing } else { - utils.throwError('Unrecognized type: ' + note.detail.type); + infoService.throwError('Unrecognized type: ' + note.detail.type); } } @@ -343,7 +344,7 @@ async function executeCurrentNote() { await server.post('script/run/' + getCurrentNoteId()); } - utils.showMessage("Note executed"); + infoService.showMessage("Note executed"); } } @@ -368,7 +369,7 @@ function getAttachmentUrl() { messagingService.subscribeToMessages(syncData => { if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getCurrentNoteId())) { - utils.showMessage('Reloading note because of background changes'); + infoService.showMessage('Reloading note because of background changes'); reload(); } diff --git a/src/public/javascripts/services/note_type.js b/src/public/javascripts/services/note_type.js index 0c23ff364..127de49c3 100644 --- a/src/public/javascripts/services/note_type.js +++ b/src/public/javascripts/services/note_type.js @@ -1,6 +1,7 @@ import treeService from './tree.js'; import noteDetail from './note_detail.js'; import utils from './utils.js'; +import infoService from "./info.js"; const $executeScriptButton = $("#execute-script-button"); const noteTypeModel = new NoteTypeModel(); @@ -74,7 +75,7 @@ function NoteTypeModel() { // ignore and do nothing, "type" will be hidden since it's not possible to switch to and from search } else { - utils.throwError('Unrecognized type: ' + type); + infoService.throwError('Unrecognized type: ' + type); } }; diff --git a/src/public/javascripts/services/protected_session.js b/src/public/javascripts/services/protected_session.js index a9afdfb5c..cddbe619d 100644 --- a/src/public/javascripts/services/protected_session.js +++ b/src/public/javascripts/services/protected_session.js @@ -3,6 +3,7 @@ import noteDetail from './note_detail.js'; import utils from './utils.js'; import server from './server.js'; import protectedSessionHolder from './protected_session_holder.js'; +import infoService from "./info.js"; const $dialog = $("#protected-session-password-dialog"); const $passwordForm = $("#protected-session-password-form"); @@ -48,7 +49,7 @@ async function setupProtectedSession() { const response = await enterProtectedSession(password); if (!response.success) { - utils.showError("Wrong password."); + infoService.showError("Wrong password."); return; } @@ -123,7 +124,7 @@ async function protectSubTree(noteId, protect) { await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0)); - utils.showMessage("Request to un/protect sub tree has finished successfully"); + infoService.showMessage("Request to un/protect sub tree has finished successfully"); treeService.reload(); noteDetail.reload(); diff --git a/src/public/javascripts/services/server.js b/src/public/javascripts/services/server.js index c777a7fe0..947ab865b 100644 --- a/src/public/javascripts/services/server.js +++ b/src/public/javascripts/services/server.js @@ -1,5 +1,6 @@ import protectedSessionHolder from './protected_session_holder.js'; import utils from './utils.js'; +import infoService from "./info.js"; function getHeaders() { let protectedSessionId = null; @@ -74,8 +75,8 @@ async function ajax(url, method, data) { return await $.ajax(options).catch(e => { const message = "Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText; - utils.showError(message); - utils.throwError(message); + infoService.showError(message); + infoService.throwError(message); }); } diff --git a/src/public/javascripts/services/sync.js b/src/public/javascripts/services/sync.js index 7dd6cbde0..85e9a58f0 100644 --- a/src/public/javascripts/services/sync.js +++ b/src/public/javascripts/services/sync.js @@ -1,17 +1,18 @@ import utils from './utils.js'; +import infoService from "./info.js"; async function syncNow() { const result = await server.post('sync/now'); if (result.success) { - utils.showMessage("Sync finished successfully."); + infoService.showMessage("Sync finished successfully."); } else { if (result.message.length > 50) { result.message = result.message.substr(0, 50); } - utils.showError("Sync failed: " + result.message); + infoService.showError("Sync failed: " + result.message); } } @@ -20,7 +21,7 @@ $("#sync-now-button").click(syncNow); async function forceNoteSync(noteId) { const result = await server.post('sync/force-note-sync/' + noteId); - utils.showMessage("Note added to sync queue."); + infoService.showMessage("Note added to sync queue."); } export default { diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index e98e6255b..c6dfaef2a 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -11,6 +11,7 @@ import server from './server.js'; import recentNotesDialog from '../dialogs/recent_notes.js'; import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js'; import treeCache from './tree_cache.js'; +import infoService from "./info.js"; const $tree = $("#tree"); const $parentList = $("#parent-list"); @@ -27,7 +28,7 @@ async function getNote(noteId) { const note = await treeCache.getNote(noteId); if (!note) { - utils.throwError(`Can't find title for noteId='${noteId}'`); + infoService.throwError(`Can't find title for noteId='${noteId}'`); } return note; @@ -296,7 +297,7 @@ async function showParentList(noteId, node) { const parents = await note.getParentNotes(); if (!parents.length) { - utils.throwError("Can't find parents for noteId=" + noteId); + infoService.throwError("Can't find parents for noteId=" + noteId); } if (parents.length <= 1) { @@ -355,7 +356,7 @@ async function getSomeNotePath(note) { const parents = await cur.getParentNotes(); if (!parents.length) { - utils.throwError("Can't find parents for " + cur); + infoService.throwError("Can't find parents for " + cur); } cur = parents[0]; @@ -831,12 +832,12 @@ async function createNote(node, parentNoteId, target, isProtected) { node.renderTitle(); } else { - utils.throwError("Unrecognized target: " + target); + infoService.throwError("Unrecognized target: " + target); } clearSelectedNodes(); // to unmark previously active node - utils.showMessage("Created!"); + infoService.showMessage("Created!"); } async function sortAlphabetically(noteId) { diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index 2ce6371f0..ce22d9d40 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -1,6 +1,7 @@ import utils from "./utils.js"; import Branch from "../entities/branch.js"; import NoteShort from "../entities/note_short.js"; +import infoService from "./info.js"; class TreeCache { load(noteRows, branchRows) { @@ -59,7 +60,7 @@ class TreeCache { const branch = this.childParentToBranch[key]; if (!branch) { - utils.throwError("Cannot find branch for child-parent=" + key); + infoService.throwError("Cannot find branch for child-parent=" + key); } return branch; diff --git a/src/public/javascripts/services/tree_changes.js b/src/public/javascripts/services/tree_changes.js index 74183e585..56ceb7d05 100644 --- a/src/public/javascripts/services/tree_changes.js +++ b/src/public/javascripts/services/tree_changes.js @@ -1,6 +1,7 @@ import treeService from './tree.js'; import utils from './utils.js'; import server from './server.js'; +import infoService from "./info.js"; async function moveBeforeNode(nodesToMove, beforeNode) { for (const nodeToMove of nodesToMove) { @@ -86,7 +87,7 @@ async function deleteNodes(nodes) { treeService.reload(); - utils.showMessage("Note(s) has been deleted."); + infoService.showMessage("Note(s) has been deleted."); } async function moveNodeUpInHierarchy(node) { diff --git a/src/public/javascripts/services/utils.js b/src/public/javascripts/services/utils.js index 109446126..32e6a5a1d 100644 --- a/src/public/javascripts/services/utils.js +++ b/src/public/javascripts/services/utils.js @@ -1,41 +1,7 @@ -import messagingService from './messaging.js'; - function reloadApp() { window.location.reload(true); } -function showMessage(message) { - console.log(now(), "message: ", message); - - $.notify({ - // options - message: message - }, { - // settings - type: 'success', - delay: 3000 - }); -} - -function showError(message, delay = 10000) { - console.log(now(), "error: ", message); - - $.notify({ - // options - message: message - }, { - // settings - type: 'danger', - delay: delay - }); -} - -function throwError(message) { - messagingService.logError(message); - - throw new Error(message); -} - function parseDate(str) { try { return new Date(Date.parse(str)); @@ -80,17 +46,11 @@ function isElectron() { function assertArguments() { for (const i in arguments) { if (!arguments[i]) { - throwError(`Argument idx#${i} should not be falsy: ${arguments[i]}`); + throw new Error(`Argument idx#${i} should not be falsy: ${arguments[i]}`); } } } -function assert(expr, message) { - if (!expr) { - throwError(message); - } -} - function isTopLevelNode(node) { return isRootNode(node.getParent()); } @@ -236,9 +196,6 @@ function bindShortcut(keyboardShortcut, handler) { export default { reloadApp, - showMessage, - showError, - throwError, parseDate, padNum, formatTime, @@ -249,7 +206,6 @@ export default { now, isElectron, assertArguments, - assert, isTopLevelNode, isRootNode, escapeHtml,