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 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();
};

View file

@ -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');

View file

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

View file

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

View file

@ -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);

View file

@ -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 = {

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 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({

View file

@ -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();
}

View file

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

View file

@ -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();

View file

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

View file

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

View file

@ -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) {

View file

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

View file

@ -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) {

View file

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