mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
Merge branch 'master' into stable
This commit is contained in:
commit
d019d0a690
27
app.js
27
app.js
@ -9,6 +9,7 @@ const session = require('express-session');
|
|||||||
const FileStore = require('session-file-store')(session);
|
const FileStore = require('session-file-store')(session);
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const sessionSecret = require('./services/session_secret');
|
const sessionSecret = require('./services/session_secret');
|
||||||
|
const utils = require('./services/utils');
|
||||||
|
|
||||||
require('./services/ping_job');
|
require('./services/ping_job');
|
||||||
|
|
||||||
@ -71,4 +72,30 @@ require('./services/sync');
|
|||||||
// triggers backup timer
|
// triggers backup timer
|
||||||
require('./services/backup');
|
require('./services/backup');
|
||||||
|
|
||||||
|
if (utils.isElectron()) {
|
||||||
|
const ipcMain = require('electron').ipcMain;
|
||||||
|
|
||||||
|
ipcMain.on('server-request', (event, arg) => {
|
||||||
|
const req = {};
|
||||||
|
req.url = arg.url;
|
||||||
|
req.method = arg.method;
|
||||||
|
req.body = arg.data;
|
||||||
|
req.headers = {};
|
||||||
|
|
||||||
|
const res = {};
|
||||||
|
res.setHeader = function() {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
res.send = function(obj) {
|
||||||
|
event.sender.send('server-response', {
|
||||||
|
requestId: arg.requestId,
|
||||||
|
body: obj
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return app._router.handle(req, res, () => {});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
@ -30,16 +30,9 @@ const editTreePrefix = (function() {
|
|||||||
formEl.submit(() => {
|
formEl.submit(() => {
|
||||||
const prefix = treePrefixInputEl.val();
|
const prefix = treePrefixInputEl.val();
|
||||||
|
|
||||||
$.ajax({
|
server.put('tree/' + noteTreeId + '/setPrefix', {
|
||||||
url: baseApiUrl + 'tree/' + noteTreeId + '/setPrefix',
|
prefix: prefix
|
||||||
type: 'PUT',
|
}).then(() => noteTree.setPrefix(noteTreeId, prefix));
|
||||||
contentType: 'application/json',
|
|
||||||
data: JSON.stringify({
|
|
||||||
prefix: prefix
|
|
||||||
}),
|
|
||||||
success: () => noteTree.setPrefix(noteTreeId, prefix),
|
|
||||||
error: () => showError("Error setting prefix.")
|
|
||||||
});
|
|
||||||
|
|
||||||
dialogEl.dialog("close");
|
dialogEl.dialog("close");
|
||||||
|
|
||||||
|
@ -13,11 +13,7 @@ const eventLog = (function() {
|
|||||||
height: 700
|
height: 700
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await $.ajax({
|
const result = await server.get('event-log');
|
||||||
url: baseApiUrl + 'event-log',
|
|
||||||
type: 'GET',
|
|
||||||
error: () => showError("Error getting event log.")
|
|
||||||
});
|
|
||||||
|
|
||||||
listEl.html('');
|
listEl.html('');
|
||||||
|
|
||||||
|
@ -24,11 +24,7 @@ const noteHistory = (function() {
|
|||||||
listEl.empty();
|
listEl.empty();
|
||||||
contentEl.empty();
|
contentEl.empty();
|
||||||
|
|
||||||
historyItems = await $.ajax({
|
historyItems = await server.get('notes-history/' + noteId);
|
||||||
url: baseApiUrl + 'notes-history/' + noteId,
|
|
||||||
type: 'GET',
|
|
||||||
error: () => showError("Error getting note history.")
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const item of historyItems) {
|
for (const item of historyItems) {
|
||||||
const dateModified = getDateFromTS(item.date_modified_to);
|
const dateModified = getDateFromTS(item.date_modified_to);
|
||||||
|
@ -12,11 +12,7 @@ const recentChanges = (function() {
|
|||||||
height: 700
|
height: 700
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await $.ajax({
|
const result = await server.get('recent-changes/');
|
||||||
url: baseApiUrl + 'recent-changes/',
|
|
||||||
type: 'GET',
|
|
||||||
error: () => showError("Error getting recent changes.")
|
|
||||||
});
|
|
||||||
|
|
||||||
dialogEl.html('');
|
dialogEl.html('');
|
||||||
|
|
||||||
|
@ -10,38 +10,26 @@ const recentNotes = (function() {
|
|||||||
const noteDetailEl = $('#note-detail');
|
const noteDetailEl = $('#note-detail');
|
||||||
let list = [];
|
let list = [];
|
||||||
|
|
||||||
$.ajax({
|
server.get('recent-notes').then(result => {
|
||||||
url: baseApiUrl + 'recent-notes',
|
|
||||||
type: 'GET',
|
|
||||||
error: () => showError("Error getting recent notes.")
|
|
||||||
}).then(result => {
|
|
||||||
list = result.map(r => r.note_tree_id);
|
list = result.map(r => r.note_tree_id);
|
||||||
});
|
});
|
||||||
|
|
||||||
function addRecentNote(notePath) {
|
function addRecentNote(notePath) {
|
||||||
setTimeout(() => {
|
setTimeout(async () => {
|
||||||
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
||||||
if (notePath && notePath === noteTree.getCurrentNotePath()) {
|
if (notePath && notePath === noteTree.getCurrentNotePath()) {
|
||||||
$.ajax({
|
const result = await server.put('recent-notes/' + encodeURIComponent(notePath));
|
||||||
url: baseApiUrl + 'recent-notes/' + encodeURIComponent(notePath),
|
|
||||||
type: 'PUT',
|
list = result.map(r => r.note_path);
|
||||||
error: () => showError("Error setting recent notes.")
|
|
||||||
}).then(result => {
|
|
||||||
list = result.map(r => r.note_path);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}, 1500);
|
}, 1500);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: this should be probably just refresh upon deletion, not explicit delete
|
// FIXME: this should be probably just refresh upon deletion, not explicit delete
|
||||||
function removeRecentNote(notePathIdToRemove) {
|
async function removeRecentNote(notePathIdToRemove) {
|
||||||
$.ajax({
|
const result = server.remove('recent-notes/' + encodeURIComponent(notePathIdToRemove));
|
||||||
url: baseApiUrl + 'recent-notes/' + encodeURIComponent(notePathIdToRemove),
|
|
||||||
type: 'DELETE',
|
list = result.map(r => r.note_path);
|
||||||
error: () => showError("Error removing note from recent notes.")
|
|
||||||
}).then(result => {
|
|
||||||
list = result.map(r => r.note_path);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function showDialog() {
|
function showDialog() {
|
||||||
|
@ -13,11 +13,7 @@ const settings = (function() {
|
|||||||
async function showDialog() {
|
async function showDialog() {
|
||||||
glob.activeDialog = dialogEl;
|
glob.activeDialog = dialogEl;
|
||||||
|
|
||||||
const settings = await $.ajax({
|
const settings = await server.get('settings');
|
||||||
url: baseApiUrl + 'settings',
|
|
||||||
type: 'GET',
|
|
||||||
error: () => showError("Error getting settings.")
|
|
||||||
});
|
|
||||||
|
|
||||||
dialogEl.dialog({
|
dialogEl.dialog({
|
||||||
modal: true,
|
modal: true,
|
||||||
@ -33,20 +29,13 @@ const settings = (function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveSettings(settingName, settingValue) {
|
async function saveSettings(settingName, settingValue) {
|
||||||
return $.ajax({
|
await server.post('settings', {
|
||||||
url: baseApiUrl + 'settings',
|
name: settingName,
|
||||||
type: 'POST',
|
value: settingValue
|
||||||
data: JSON.stringify({
|
|
||||||
name: settingName,
|
|
||||||
value: settingValue
|
|
||||||
}),
|
|
||||||
contentType: "application/json",
|
|
||||||
success: () => {
|
|
||||||
showMessage("Settings change have been saved.");
|
|
||||||
},
|
|
||||||
error: () => alert("Error occurred during saving settings change.")
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
showMessage("Settings change have been saved.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -79,26 +68,19 @@ settings.addModule((function() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$.ajax({
|
server.post('password/change', {
|
||||||
url: baseApiUrl + 'password/change',
|
'current_password': oldPassword,
|
||||||
type: 'POST',
|
'new_password': newPassword1
|
||||||
data: JSON.stringify({
|
}).then(result => {
|
||||||
'current_password': oldPassword,
|
if (result.success) {
|
||||||
'new_password': newPassword1
|
alert("Password has been changed. Trilium will be reloaded after you press OK.");
|
||||||
}),
|
|
||||||
contentType: "application/json",
|
|
||||||
success: result => {
|
|
||||||
if (result.success) {
|
|
||||||
alert("Password has been changed. Trilium will be reloaded after you press OK.");
|
|
||||||
|
|
||||||
// password changed so current protected session is invalid and needs to be cleared
|
// password changed so current protected session is invalid and needs to be cleared
|
||||||
protected_session.resetProtectedSession();
|
protected_session.resetProtectedSession();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
showError(result.message);
|
showError(result.message);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
error: () => showError("Error occurred during changing password.")
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -159,7 +141,7 @@ settings.addModule((async function () {
|
|||||||
const buildDateEl = $("#build-date");
|
const buildDateEl = $("#build-date");
|
||||||
const buildRevisionEl = $("#build-revision");
|
const buildRevisionEl = $("#build-revision");
|
||||||
|
|
||||||
const appInfo = await $.get(baseApiUrl + 'app-info');
|
const appInfo = await server.get('app-info');
|
||||||
|
|
||||||
appVersionEl.html(appInfo.app_version);
|
appVersionEl.html(appInfo.app_version);
|
||||||
dbVersionEl.html(appInfo.db_version);
|
dbVersionEl.html(appInfo.db_version);
|
||||||
|
@ -129,14 +129,4 @@ function showAppIfHidden() {
|
|||||||
// Kick off the CSS transition
|
// Kick off the CSS transition
|
||||||
loaderDiv.style.opacity = 0.0;
|
loaderDiv.style.opacity = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initAjax() {
|
|
||||||
$.ajaxSetup({
|
|
||||||
headers: {
|
|
||||||
'x-protected-session-id': typeof protected_session !== 'undefined' ? protected_session.getProtectedSessionId() : null
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
initAjax();
|
|
@ -17,29 +17,28 @@ $(document).ready(() => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#run-migration").click(() => {
|
$("#run-migration").click(async () => {
|
||||||
$("#run-migration").prop("disabled", true);
|
$("#run-migration").prop("disabled", true);
|
||||||
|
|
||||||
$("#migration-result").show();
|
$("#migration-result").show();
|
||||||
|
|
||||||
$.ajax({
|
const result = await $.ajax({
|
||||||
url: baseApiUrl + 'migration',
|
url: baseApiUrl + 'migration',
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
success: result => {
|
|
||||||
for (const migration of result.migrations) {
|
|
||||||
const row = $('<tr>')
|
|
||||||
.append($('<td>').html(migration.db_version))
|
|
||||||
.append($('<td>').html(migration.name))
|
|
||||||
.append($('<td>').html(migration.success ? 'Yes' : 'No'))
|
|
||||||
.append($('<td>').html(migration.success ? 'N/A' : migration.error));
|
|
||||||
|
|
||||||
if (!migration.success) {
|
|
||||||
row.addClass("danger");
|
|
||||||
}
|
|
||||||
|
|
||||||
$("#migration-table").append(row);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: () => showError("Migration failed with unknown error")
|
error: () => showError("Migration failed with unknown error")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (const migration of result.migrations) {
|
||||||
|
const row = $('<tr>')
|
||||||
|
.append($('<td>').html(migration.db_version))
|
||||||
|
.append($('<td>').html(migration.name))
|
||||||
|
.append($('<td>').html(migration.success ? 'Yes' : 'No'))
|
||||||
|
.append($('<td>').html(migration.success ? 'N/A' : migration.error));
|
||||||
|
|
||||||
|
if (!migration.success) {
|
||||||
|
row.addClass("danger");
|
||||||
|
}
|
||||||
|
|
||||||
|
$("#migration-table").append(row);
|
||||||
|
}
|
||||||
});
|
});
|
@ -93,15 +93,7 @@ const noteEditor = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveNoteToServer(note) {
|
async function saveNoteToServer(note) {
|
||||||
await $.ajax({
|
await server.put('notes/' + note.detail.note_id, note);
|
||||||
url: baseApiUrl + 'notes/' + note.detail.note_id,
|
|
||||||
type: 'PUT',
|
|
||||||
data: JSON.stringify(note),
|
|
||||||
contentType: "application/json",
|
|
||||||
error: () => {
|
|
||||||
showError("Error saving the note!");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
isNoteChanged = false;
|
isNoteChanged = false;
|
||||||
|
|
||||||
@ -130,7 +122,7 @@ const noteEditor = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadNoteToEditor(noteId) {
|
async function loadNoteToEditor(noteId) {
|
||||||
currentNote = await $.get(baseApiUrl + 'notes/' + noteId);
|
currentNote = await server.get('notes/' + noteId);
|
||||||
|
|
||||||
if (isNewNoteCreated) {
|
if (isNewNoteCreated) {
|
||||||
isNewNoteCreated = false;
|
isNewNoteCreated = false;
|
||||||
@ -167,7 +159,7 @@ const noteEditor = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadNote(noteId) {
|
async function loadNote(noteId) {
|
||||||
return await $.get(baseApiUrl + 'notes/' + noteId);
|
return await server.get('notes/' + noteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).ready(() => {
|
$(document).ready(() => {
|
||||||
|
@ -307,15 +307,10 @@ const noteTree = (function() {
|
|||||||
return path.reverse().join('/');
|
return path.reverse().join('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
function setExpandedToServer(noteTreeId, isExpanded) {
|
async function setExpandedToServer(noteTreeId, isExpanded) {
|
||||||
const expandedNum = isExpanded ? 1 : 0;
|
const expandedNum = isExpanded ? 1 : 0;
|
||||||
|
|
||||||
$.ajax({
|
await server.put('notes/' + noteTreeId + '/expanded/' + expandedNum);
|
||||||
url: baseApiUrl + 'notes/' + noteTreeId + '/expanded/' + expandedNum,
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: "application/json",
|
|
||||||
success: result => {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCurrentNotePathToHash(node) {
|
function setCurrentNotePathToHash(node) {
|
||||||
@ -479,7 +474,7 @@ const noteTree = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadTree() {
|
function loadTree() {
|
||||||
return $.get(baseApiUrl + 'tree').then(resp => {
|
return server.get('tree').then(resp => {
|
||||||
startNoteTreeId = resp.start_note_tree_id;
|
startNoteTreeId = resp.start_note_tree_id;
|
||||||
treeLoadTime = resp.tree_load_time;
|
treeLoadTime = resp.tree_load_time;
|
||||||
|
|
||||||
@ -574,16 +569,11 @@ const noteTree = (function() {
|
|||||||
|
|
||||||
const newNoteName = "new note";
|
const newNoteName = "new note";
|
||||||
|
|
||||||
const result = await $.ajax({
|
const result = await server.post('notes/' + parentNoteId + '/children', {
|
||||||
url: baseApiUrl + 'notes/' + parentNoteId + '/children' ,
|
note_title: newNoteName,
|
||||||
type: 'POST',
|
target: target,
|
||||||
data: JSON.stringify({
|
target_note_tree_id: node.data.note_tree_id,
|
||||||
note_title: newNoteName,
|
is_protected: isProtected
|
||||||
target: target,
|
|
||||||
target_note_tree_id: node.data.note_tree_id,
|
|
||||||
is_protected: isProtected
|
|
||||||
}),
|
|
||||||
contentType: "application/json"
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const newNode = {
|
const newNode = {
|
||||||
|
@ -10,11 +10,7 @@ const protected_session = (function() {
|
|||||||
let protectedSessionTimeout = null;
|
let protectedSessionTimeout = null;
|
||||||
let protectedSessionId = null;
|
let protectedSessionId = null;
|
||||||
|
|
||||||
$.ajax({
|
server.get('settings/all').then(settings => {
|
||||||
url: baseApiUrl + 'settings/all',
|
|
||||||
type: 'GET',
|
|
||||||
error: () => showError("Error getting protected session settings.")
|
|
||||||
}).then(settings => {
|
|
||||||
protectedSessionTimeout = settings.protected_session_timeout;
|
protectedSessionTimeout = settings.protected_session_timeout;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -61,7 +57,7 @@ const protected_session = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protectedSessionId = response.protectedSessionId;
|
protectedSessionId = response.protectedSessionId;
|
||||||
initAjax();
|
server.initAjax();
|
||||||
|
|
||||||
dialogEl.dialog("close");
|
dialogEl.dialog("close");
|
||||||
|
|
||||||
@ -88,14 +84,8 @@ const protected_session = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function enterProtectedSession(password) {
|
async function enterProtectedSession(password) {
|
||||||
return await $.ajax({
|
return await server.post('login/protected', {
|
||||||
url: baseApiUrl + 'login/protected',
|
password: password
|
||||||
type: 'POST',
|
|
||||||
contentType: 'application/json',
|
|
||||||
data: JSON.stringify({
|
|
||||||
password: password
|
|
||||||
}),
|
|
||||||
error: () => showError("Error entering protected session.")
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +96,7 @@ const protected_session = (function() {
|
|||||||
function resetProtectedSession() {
|
function resetProtectedSession() {
|
||||||
protectedSessionId = null;
|
protectedSessionId = null;
|
||||||
|
|
||||||
initAjax();
|
server.initAjax();
|
||||||
|
|
||||||
// most secure solution - guarantees nothing remained in memory
|
// most secure solution - guarantees nothing remained in memory
|
||||||
// since this expires because user doesn't use the app, it shouldn't be disruptive
|
// since this expires because user doesn't use the app, it shouldn't be disruptive
|
||||||
@ -154,12 +144,7 @@ const protected_session = (function() {
|
|||||||
async function protectSubTree(noteId, protect) {
|
async function protectSubTree(noteId, protect) {
|
||||||
await ensureProtectedSession(true, true);
|
await ensureProtectedSession(true, true);
|
||||||
|
|
||||||
await $.ajax({
|
await server.put('tree/' + noteId + "/protectSubTree/" + (protect ? 1 : 0));
|
||||||
url: baseApiUrl + 'tree/' + noteId + "/protectSubTree/" + (protect ? 1 : 0),
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: 'application/json',
|
|
||||||
error: () => showError("Request to un/protect sub tree has failed.")
|
|
||||||
});
|
|
||||||
|
|
||||||
showMessage("Request to un/protect sub tree has finished successfully");
|
showMessage("Request to un/protect sub tree has finished successfully");
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ const searchTree = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (e && e.which === $.ui.keyCode.ENTER) {
|
if (e && e.which === $.ui.keyCode.ENTER) {
|
||||||
$.get(baseApiUrl + 'notes?search=' + searchText).then(resp => {
|
server.get('notes?search=' + searchText).then(resp => {
|
||||||
// Pass a string to perform case insensitive matching
|
// Pass a string to perform case insensitive matching
|
||||||
getTree().filterBranches(node => {
|
getTree().filterBranches(node => {
|
||||||
return resp.includes(node.data.note_id);
|
return resp.includes(node.data.note_id);
|
||||||
|
84
public/javascripts/server.js
Normal file
84
public/javascripts/server.js
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
const server = (function() {
|
||||||
|
function initAjax() {
|
||||||
|
$.ajaxSetup({
|
||||||
|
headers: {
|
||||||
|
'x-protected-session-id': typeof protected_session !== 'undefined' ? protected_session.getProtectedSessionId() : null
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function get(url) {
|
||||||
|
return await call('GET', url);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function post(url, data) {
|
||||||
|
return await call('POST', url, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function put(url, data) {
|
||||||
|
return await call('PUT', url, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function remove(url) {
|
||||||
|
return await call('DELETE', url);
|
||||||
|
}
|
||||||
|
|
||||||
|
let i = 1;
|
||||||
|
const reqResolves = {};
|
||||||
|
|
||||||
|
async function call(method, url, data) {
|
||||||
|
if (isElectron()) {
|
||||||
|
const ipc = require('electron').ipcRenderer;
|
||||||
|
const requestId = i++;
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
reqResolves[requestId] = resolve;
|
||||||
|
|
||||||
|
ipc.send('server-request', {
|
||||||
|
requestId: requestId,
|
||||||
|
method: method,
|
||||||
|
url: "/" + baseApiUrl + url,
|
||||||
|
data: data
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return await ajax(url, method, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isElectron()) {
|
||||||
|
const ipc = require('electron').ipcRenderer;
|
||||||
|
|
||||||
|
ipc.on('server-response', (event, arg) => {
|
||||||
|
reqResolves[arg.requestId](arg.body);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ajax(url, method, data) {
|
||||||
|
const options = {
|
||||||
|
url: baseApiUrl + url,
|
||||||
|
type: method
|
||||||
|
};
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
options.data = JSON.stringify(data);
|
||||||
|
options.contentType = "application/json";
|
||||||
|
}
|
||||||
|
|
||||||
|
return await $.ajax(options).catch(e => {
|
||||||
|
showError("Error when calling " + method + " " + url + ": " + e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
initAjax();
|
||||||
|
|
||||||
|
return {
|
||||||
|
get,
|
||||||
|
post,
|
||||||
|
put,
|
||||||
|
remove,
|
||||||
|
initAjax
|
||||||
|
}
|
||||||
|
})();
|
@ -1,21 +1,16 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function syncNow() {
|
async function syncNow() {
|
||||||
$.ajax({
|
const result = await server.post('sync/now');
|
||||||
url: baseApiUrl + 'sync/now',
|
|
||||||
type: 'POST',
|
|
||||||
success: result => {
|
|
||||||
if (result.success) {
|
|
||||||
showMessage("Sync finished successfully.");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (result.message.length > 50) {
|
|
||||||
result.message = result.message.substr(0, 50);
|
|
||||||
}
|
|
||||||
|
|
||||||
showError("Sync failed: " + result.message);
|
if (result.success) {
|
||||||
}
|
showMessage("Sync finished successfully.");
|
||||||
},
|
}
|
||||||
error: () => showError("Sync failed for unknown reason.")
|
else {
|
||||||
});
|
if (result.message.length > 50) {
|
||||||
|
result.message = result.message.substr(0, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
showError("Sync failed: " + result.message);
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,11 +2,7 @@
|
|||||||
|
|
||||||
const treeChanges = (function() {
|
const treeChanges = (function() {
|
||||||
async function moveBeforeNode(node, beforeNode, changeInPath = true) {
|
async function moveBeforeNode(node, beforeNode, changeInPath = true) {
|
||||||
await $.ajax({
|
await server.put('notes/' + node.data.note_tree_id + '/moveBefore/' + beforeNode.data.note_tree_id);
|
||||||
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveBefore/' + beforeNode.data.note_tree_id,
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: "application/json"
|
|
||||||
});
|
|
||||||
|
|
||||||
node.moveTo(beforeNode, 'before');
|
node.moveTo(beforeNode, 'before');
|
||||||
|
|
||||||
@ -18,11 +14,7 @@ const treeChanges = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function moveAfterNode(node, afterNode, changeInPath = true) {
|
async function moveAfterNode(node, afterNode, changeInPath = true) {
|
||||||
await $.ajax({
|
await server.put('notes/' + node.data.note_tree_id + '/moveAfter/' + afterNode.data.note_tree_id);
|
||||||
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + afterNode.data.note_tree_id,
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: "application/json"
|
|
||||||
});
|
|
||||||
|
|
||||||
node.moveTo(afterNode, 'after');
|
node.moveTo(afterNode, 'after');
|
||||||
|
|
||||||
@ -35,11 +27,7 @@ const treeChanges = (function() {
|
|||||||
|
|
||||||
// beware that first arg is noteId and second is noteTreeId!
|
// beware that first arg is noteId and second is noteTreeId!
|
||||||
async function cloneNoteAfter(noteId, afterNoteTreeId) {
|
async function cloneNoteAfter(noteId, afterNoteTreeId) {
|
||||||
const resp = await $.ajax({
|
const resp = await server.put('notes/' + noteId + '/cloneAfter/' + afterNoteTreeId);
|
||||||
url: baseApiUrl + 'notes/' + noteId + '/cloneAfter/' + afterNoteTreeId,
|
|
||||||
type: 'PUT',
|
|
||||||
error: () => showError("Error cloning note.")
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!resp.success) {
|
if (!resp.success) {
|
||||||
alert(resp.message);
|
alert(resp.message);
|
||||||
@ -50,11 +38,7 @@ const treeChanges = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function moveToNode(node, toNode) {
|
async function moveToNode(node, toNode) {
|
||||||
await $.ajax({
|
await server.put('notes/' + node.data.note_tree_id + '/moveTo/' + toNode.data.note_id);
|
||||||
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveTo/' + toNode.data.note_id,
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: "application/json"
|
|
||||||
});
|
|
||||||
|
|
||||||
node.moveTo(toNode);
|
node.moveTo(toNode);
|
||||||
|
|
||||||
@ -69,11 +53,7 @@ const treeChanges = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function cloneNoteTo(childNoteId, parentNoteId) {
|
async function cloneNoteTo(childNoteId, parentNoteId) {
|
||||||
const resp = await $.ajax({
|
const resp = await server.put('notes/' + childNoteId + '/cloneTo/' + parentNoteId);
|
||||||
url: baseApiUrl + 'notes/' + childNoteId + '/cloneTo/' + parentNoteId,
|
|
||||||
type: 'PUT',
|
|
||||||
error: () => showError("Error cloning note.")
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!resp.success) {
|
if (!resp.success) {
|
||||||
alert(resp.message);
|
alert(resp.message);
|
||||||
@ -88,10 +68,7 @@ const treeChanges = (function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await $.ajax({
|
await server.remove('notes/' + node.data.note_tree_id);
|
||||||
url: baseApiUrl + 'notes/' + node.data.note_tree_id,
|
|
||||||
type: 'DELETE'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
|
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
|
||||||
node.getParent().folder = false;
|
node.getParent().folder = false;
|
||||||
@ -118,11 +95,7 @@ const treeChanges = (function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await $.ajax({
|
await server.put('notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id);
|
||||||
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id,
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: "application/json",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
|
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
|
||||||
node.getParent().folder = false;
|
node.getParent().folder = false;
|
||||||
|
@ -105,6 +105,12 @@ async function getLastSyncedPull() {
|
|||||||
return parseInt(await options.getOption('last_synced_pull'));
|
return parseInt(await options.getOption('last_synced_pull'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setLastSyncedPull(syncId) {
|
||||||
|
await sql.doInTransaction(async () => {
|
||||||
|
await options.setOption('last_synced_pull', syncId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function pullSync(syncContext) {
|
async function pullSync(syncContext) {
|
||||||
const lastSyncedPull = await getLastSyncedPull();
|
const lastSyncedPull = await getLastSyncedPull();
|
||||||
|
|
||||||
@ -118,6 +124,8 @@ async function pullSync(syncContext) {
|
|||||||
if (source_id.isLocalSourceId(sync.source_id)) {
|
if (source_id.isLocalSourceId(sync.source_id)) {
|
||||||
log.info("Skipping " + sync.entity_name + " " + sync.entity_id + " because it has local source id.");
|
log.info("Skipping " + sync.entity_name + " " + sync.entity_id + " because it has local source id.");
|
||||||
|
|
||||||
|
await setLastSyncedPull(sync.id);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,9 +157,7 @@ async function pullSync(syncContext) {
|
|||||||
throw new Error("Unrecognized entity type " + sync.entity_name);
|
throw new Error("Unrecognized entity type " + sync.entity_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
await setLastSyncedPull(sync.id);
|
||||||
await options.setOption('last_synced_pull', sync.id);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Finished pull");
|
log.info("Finished pull");
|
||||||
|
@ -302,6 +302,7 @@
|
|||||||
<link href="stylesheets/style.css" rel="stylesheet">
|
<link href="stylesheets/style.css" rel="stylesheet">
|
||||||
|
|
||||||
<script src="javascripts/init.js"></script>
|
<script src="javascripts/init.js"></script>
|
||||||
|
<script src="javascripts/server.js"></script>
|
||||||
|
|
||||||
<!-- Tree scripts -->
|
<!-- Tree scripts -->
|
||||||
<script src="javascripts/note_tree.js"></script>
|
<script src="javascripts/note_tree.js"></script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user