reduced dependencies of utils

This commit is contained in:
azivner 2018-03-25 21:29:35 -04:00
parent ac1b06967f
commit d3316cd09c
17 changed files with 93 additions and 85 deletions

View File

@ -1,6 +1,7 @@
import noteDetailService from '../services/note_detail.js'; import noteDetailService from '../services/note_detail.js';
import utils from '../services/utils.js'; import utils from '../services/utils.js';
import server from '../services/server.js'; import server from '../services/server.js';
import infoService from "../services/info.js";
const $dialog = $("#labels-dialog"); const $dialog = $("#labels-dialog");
const $saveLabelsButton = $("#save-labels-button"); const $saveLabelsButton = $("#save-labels-button");
@ -91,7 +92,7 @@ function LabelsModel() {
addLastEmptyRow(); addLastEmptyRow();
utils.showMessage("Labels have been saved."); infoService.showMessage("Labels have been saved.");
noteDetailService.loadLabelList(); noteDetailService.loadLabelList();
}; };

View File

@ -1,7 +1,7 @@
import treeService from '../services/tree.js'; import treeService from '../services/tree.js';
import messagingService from '../services/messaging.js'; import messagingService from '../services/messaging.js';
import server from '../services/server.js'; import server from '../services/server.js';
import utils from "../services/utils"; import utils from "../services/utils.js";
const $dialog = $("#recent-notes-dialog"); const $dialog = $("#recent-notes-dialog");
const $searchInput = $('#recent-notes-search-input'); const $searchInput = $('#recent-notes-search-input');

View File

@ -3,6 +3,7 @@
import protectedSessionHolder from '../services/protected_session_holder.js'; import protectedSessionHolder from '../services/protected_session_holder.js';
import utils from '../services/utils.js'; import utils from '../services/utils.js';
import server from '../services/server.js'; import server from '../services/server.js';
import infoService from "../services/info.js";
const $dialog = $("#settings-dialog"); const $dialog = $("#settings-dialog");
const $tabs = $("#settings-tabs"); const $tabs = $("#settings-tabs");
@ -38,7 +39,7 @@ async function saveSettings(settingName, settingValue) {
value: settingValue value: settingValue
}); });
utils.showMessage("Settings change have been saved."); infoService.showMessage("Settings change have been saved.");
} }
export default { export default {
@ -80,7 +81,7 @@ addModule((function() {
protectedSessionHolder.resetProtectedSession(); protectedSessionHolder.resetProtectedSession();
} }
else { else {
utils.showError(result.message); infoService.showError(result.message);
} }
}); });
@ -164,27 +165,27 @@ addModule((async function () {
$forceFullSyncButton.click(async () => { $forceFullSyncButton.click(async () => {
await server.post('sync/force-full-sync'); await server.post('sync/force-full-sync');
utils.showMessage("Full sync triggered"); infoService.showMessage("Full sync triggered");
}); });
$fillSyncRowsButton.click(async () => { $fillSyncRowsButton.click(async () => {
await server.post('sync/fill-sync-rows'); await server.post('sync/fill-sync-rows');
utils.showMessage("Sync rows filled successfully"); infoService.showMessage("Sync rows filled successfully");
}); });
$anonymizeButton.click(async () => { $anonymizeButton.click(async () => {
await server.post('anonymization/anonymize'); await server.post('anonymization/anonymize');
utils.showMessage("Created anonymized database"); infoService.showMessage("Created anonymized database");
}); });
$cleanupSoftDeletedButton.click(async () => { $cleanupSoftDeletedButton.click(async () => {
if (confirm("Do you really want to clean up soft-deleted items?")) { if (confirm("Do you really want to clean up soft-deleted items?")) {
await server.post('cleanup/cleanup-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?")) { if (confirm("Do you really want to clean up unused images?")) {
await server.post('cleanup/cleanup-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 () => { $vacuumDatabaseButton.click(async () => {
await server.post('cleanup/vacuum-database'); await server.post('cleanup/vacuum-database');
utils.showMessage("Database has been vacuumed"); infoService.showMessage("Database has been vacuumed");
}); });
return {}; return {};

View File

@ -1,5 +1,6 @@
import utils from '../services/utils.js'; import utils from '../services/utils.js';
import server from '../services/server.js'; import server from '../services/server.js';
import infoService from "../services/info.js";
const $dialog = $("#sql-console-dialog"); const $dialog = $("#sql-console-dialog");
const $query = $('#sql-console-query'); const $query = $('#sql-console-query');
@ -60,11 +61,11 @@ async function execute(e) {
}); });
if (!result.success) { if (!result.success) {
utils.showError(result.error); infoService.showError(result.error);
return; return;
} }
else { else {
utils.showMessage("Query was executed successfully."); infoService.showMessage("Query was executed successfully.");
} }
const rows = result.rows; const rows = result.rows;

View File

@ -1,4 +1,4 @@
import ScriptContext from "./script_context"; import ScriptContext from "./script_context.js";
async function executeBundle(bundle) { async function executeBundle(bundle) {
const apiContext = ScriptContext(bundle.note, bundle.allNotes); const apiContext = ScriptContext(bundle.note, bundle.allNotes);

View File

@ -7,6 +7,7 @@ import treeChangesService from './tree_changes.js';
import treeUtils from './tree_utils.js'; import treeUtils from './tree_utils.js';
import utils from './utils.js'; import utils from './utils.js';
import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js'; import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js';
import infoService from "./info.js";
const $tree = $("#tree"); const $tree = $("#tree");
@ -33,7 +34,7 @@ async function pasteAfter(node) {
// just do nothing // just do nothing
} }
else { else {
utils.throwError("Unrecognized clipboard mode=" + clipboardMode); infoService.throwError("Unrecognized clipboard mode=" + clipboardMode);
} }
} }
@ -56,7 +57,7 @@ async function pasteInto(node) {
// just do nothing // just do nothing
} }
else { 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); clipboardIds = nodes.map(node => node.data.noteId);
clipboardMode = 'copy'; clipboardMode = 'copy';
utils.showMessage("Note(s) have been copied into clipboard."); infoService.showMessage("Note(s) have been copied into clipboard.");
} }
function cut(nodes) { function cut(nodes) {
clipboardIds = nodes.map(node => node.key); clipboardIds = nodes.map(node => node.key);
clipboardMode = 'cut'; clipboardMode = 'cut';
utils.showMessage("Note(s) have been cut into clipboard."); infoService.showMessage("Note(s) have been cut into clipboard.");
} }
const contextMenuSettings = { const contextMenuSettings = {

View File

@ -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
}

View File

@ -1,4 +1,5 @@
import utils from './utils.js'; import utils from './utils.js';
import infoService from "./info.js";
const $changesToPushCount = $("#changes-to-push-count"); const $changesToPushCount = $("#changes-to-push-count");
@ -45,10 +46,10 @@ function handleMessage(event) {
$changesToPushCount.html(message.changesToPushCount); $changesToPushCount.html(message.changesToPushCount);
} }
else if (message.type === 'sync-hash-check-failed') { 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') { 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(); await connectionBrokenNotification.close();
connectionBrokenNotification = null; connectionBrokenNotification = null;
utils.showMessage("Re-connected to server"); infoService.showMessage("Re-connected to server");
} }
ws.send(JSON.stringify({ ws.send(JSON.stringify({

View File

@ -6,6 +6,7 @@ import utils from './utils.js';
import server from './server.js'; import server from './server.js';
import messagingService from "./messaging.js"; import messagingService from "./messaging.js";
import bundleService from "./bundle.js"; import bundleService from "./bundle.js";
import infoService from "./info.js";
const $noteTitle = $("#note-title"); const $noteTitle = $("#note-title");
@ -109,7 +110,7 @@ function updateNoteFromInputs(note) {
// nothing // nothing
} }
else { else {
utils.throwError("Unrecognized type: " + note.detail.type); infoService.throwError("Unrecognized type: " + note.detail.type);
} }
const title = $noteTitle.val(); const title = $noteTitle.val();
@ -124,7 +125,7 @@ async function saveNoteToServer(note) {
isNoteChanged = false; isNoteChanged = false;
utils.showMessage("Saved!"); infoService.showMessage("Saved!");
} }
function setNoteBackgroundIfProtected(note) { function setNoteBackgroundIfProtected(note) {
@ -318,7 +319,7 @@ function focus() {
// do nothing // do nothing
} }
else { 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()); await server.post('script/run/' + getCurrentNoteId());
} }
utils.showMessage("Note executed"); infoService.showMessage("Note executed");
} }
} }
@ -368,7 +369,7 @@ function getAttachmentUrl() {
messagingService.subscribeToMessages(syncData => { messagingService.subscribeToMessages(syncData => {
if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getCurrentNoteId())) { 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(); reload();
} }

View File

@ -1,6 +1,7 @@
import treeService from './tree.js'; import treeService from './tree.js';
import noteDetail from './note_detail.js'; import noteDetail from './note_detail.js';
import utils from './utils.js'; import utils from './utils.js';
import infoService from "./info.js";
const $executeScriptButton = $("#execute-script-button"); const $executeScriptButton = $("#execute-script-button");
const noteTypeModel = new NoteTypeModel(); 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 // ignore and do nothing, "type" will be hidden since it's not possible to switch to and from search
} }
else { else {
utils.throwError('Unrecognized type: ' + type); infoService.throwError('Unrecognized type: ' + type);
} }
}; };

View File

@ -3,6 +3,7 @@ import noteDetail from './note_detail.js';
import utils from './utils.js'; import utils from './utils.js';
import server from './server.js'; import server from './server.js';
import protectedSessionHolder from './protected_session_holder.js'; import protectedSessionHolder from './protected_session_holder.js';
import infoService from "./info.js";
const $dialog = $("#protected-session-password-dialog"); const $dialog = $("#protected-session-password-dialog");
const $passwordForm = $("#protected-session-password-form"); const $passwordForm = $("#protected-session-password-form");
@ -48,7 +49,7 @@ async function setupProtectedSession() {
const response = await enterProtectedSession(password); const response = await enterProtectedSession(password);
if (!response.success) { if (!response.success) {
utils.showError("Wrong password."); infoService.showError("Wrong password.");
return; return;
} }
@ -123,7 +124,7 @@ async function protectSubTree(noteId, protect) {
await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0)); 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(); treeService.reload();
noteDetail.reload(); noteDetail.reload();

View File

@ -1,5 +1,6 @@
import protectedSessionHolder from './protected_session_holder.js'; import protectedSessionHolder from './protected_session_holder.js';
import utils from './utils.js'; import utils from './utils.js';
import infoService from "./info.js";
function getHeaders() { function getHeaders() {
let protectedSessionId = null; let protectedSessionId = null;
@ -74,8 +75,8 @@ async function ajax(url, method, data) {
return await $.ajax(options).catch(e => { return await $.ajax(options).catch(e => {
const message = "Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText; const message = "Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText;
utils.showError(message); infoService.showError(message);
utils.throwError(message); infoService.throwError(message);
}); });
} }

View File

@ -1,17 +1,18 @@
import utils from './utils.js'; import utils from './utils.js';
import infoService from "./info.js";
async function syncNow() { async function syncNow() {
const result = await server.post('sync/now'); const result = await server.post('sync/now');
if (result.success) { if (result.success) {
utils.showMessage("Sync finished successfully."); infoService.showMessage("Sync finished successfully.");
} }
else { else {
if (result.message.length > 50) { if (result.message.length > 50) {
result.message = result.message.substr(0, 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) { async function forceNoteSync(noteId) {
const result = await server.post('sync/force-note-sync/' + 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 { export default {

View File

@ -11,6 +11,7 @@ import server from './server.js';
import recentNotesDialog from '../dialogs/recent_notes.js'; import recentNotesDialog from '../dialogs/recent_notes.js';
import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js'; import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js';
import treeCache from './tree_cache.js'; import treeCache from './tree_cache.js';
import infoService from "./info.js";
const $tree = $("#tree"); const $tree = $("#tree");
const $parentList = $("#parent-list"); const $parentList = $("#parent-list");
@ -27,7 +28,7 @@ async function getNote(noteId) {
const note = await treeCache.getNote(noteId); const note = await treeCache.getNote(noteId);
if (!note) { if (!note) {
utils.throwError(`Can't find title for noteId='${noteId}'`); infoService.throwError(`Can't find title for noteId='${noteId}'`);
} }
return note; return note;
@ -296,7 +297,7 @@ async function showParentList(noteId, node) {
const parents = await note.getParentNotes(); const parents = await note.getParentNotes();
if (!parents.length) { 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) { if (parents.length <= 1) {
@ -355,7 +356,7 @@ async function getSomeNotePath(note) {
const parents = await cur.getParentNotes(); const parents = await cur.getParentNotes();
if (!parents.length) { if (!parents.length) {
utils.throwError("Can't find parents for " + cur); infoService.throwError("Can't find parents for " + cur);
} }
cur = parents[0]; cur = parents[0];
@ -831,12 +832,12 @@ async function createNote(node, parentNoteId, target, isProtected) {
node.renderTitle(); node.renderTitle();
} }
else { else {
utils.throwError("Unrecognized target: " + target); infoService.throwError("Unrecognized target: " + target);
} }
clearSelectedNodes(); // to unmark previously active node clearSelectedNodes(); // to unmark previously active node
utils.showMessage("Created!"); infoService.showMessage("Created!");
} }
async function sortAlphabetically(noteId) { async function sortAlphabetically(noteId) {

View File

@ -1,6 +1,7 @@
import utils from "./utils.js"; import utils from "./utils.js";
import Branch from "../entities/branch.js"; import Branch from "../entities/branch.js";
import NoteShort from "../entities/note_short.js"; import NoteShort from "../entities/note_short.js";
import infoService from "./info.js";
class TreeCache { class TreeCache {
load(noteRows, branchRows) { load(noteRows, branchRows) {
@ -59,7 +60,7 @@ class TreeCache {
const branch = this.childParentToBranch[key]; const branch = this.childParentToBranch[key];
if (!branch) { if (!branch) {
utils.throwError("Cannot find branch for child-parent=" + key); infoService.throwError("Cannot find branch for child-parent=" + key);
} }
return branch; return branch;

View File

@ -1,6 +1,7 @@
import treeService from './tree.js'; import treeService from './tree.js';
import utils from './utils.js'; import utils from './utils.js';
import server from './server.js'; import server from './server.js';
import infoService from "./info.js";
async function moveBeforeNode(nodesToMove, beforeNode) { async function moveBeforeNode(nodesToMove, beforeNode) {
for (const nodeToMove of nodesToMove) { for (const nodeToMove of nodesToMove) {
@ -86,7 +87,7 @@ async function deleteNodes(nodes) {
treeService.reload(); treeService.reload();
utils.showMessage("Note(s) has been deleted."); infoService.showMessage("Note(s) has been deleted.");
} }
async function moveNodeUpInHierarchy(node) { async function moveNodeUpInHierarchy(node) {

View File

@ -1,41 +1,7 @@
import messagingService from './messaging.js';
function reloadApp() { function reloadApp() {
window.location.reload(true); 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) { function parseDate(str) {
try { try {
return new Date(Date.parse(str)); return new Date(Date.parse(str));
@ -80,17 +46,11 @@ function isElectron() {
function assertArguments() { function assertArguments() {
for (const i in arguments) { for (const i in arguments) {
if (!arguments[i]) { 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) { function isTopLevelNode(node) {
return isRootNode(node.getParent()); return isRootNode(node.getParent());
} }
@ -236,9 +196,6 @@ function bindShortcut(keyboardShortcut, handler) {
export default { export default {
reloadApp, reloadApp,
showMessage,
showError,
throwError,
parseDate, parseDate,
padNum, padNum,
formatTime, formatTime,
@ -249,7 +206,6 @@ export default {
now, now,
isElectron, isElectron,
assertArguments, assertArguments,
assert,
isTopLevelNode, isTopLevelNode,
isRootNode, isRootNode,
escapeHtml, escapeHtml,