trilium/src/public/javascripts/dialogs/options.js

267 lines
7.2 KiB
JavaScript
Raw Normal View History

"use strict";
import protectedSessionHolder from '../services/protected_session_holder.js';
import server from '../services/server.js';
2018-03-26 09:29:35 +08:00
import infoService from "../services/info.js";
import zoomService from "../services/zoom.js";
2018-06-09 22:34:51 +08:00
import utils from "../services/utils.js";
const $dialog = $("#options-dialog");
const $tabs = $("#options-tabs");
2017-11-05 02:31:53 +08:00
2018-03-28 10:42:46 +08:00
const tabHandlers = [];
2018-03-28 10:42:46 +08:00
function addTabHandler(handler) {
tabHandlers.push(handler);
}
2017-11-05 05:03:15 +08:00
async function showDialog() {
glob.activeDialog = $dialog;
const options = await server.get('options');
2017-09-13 10:23:57 +08:00
$dialog.dialog({
modal: true,
width: 900
});
2017-09-13 10:23:57 +08:00
$tabs.tabs();
2018-03-28 10:42:46 +08:00
for (const handler of tabHandlers) {
if (handler.optionsLoaded) {
handler.optionsLoaded(options);
2017-11-05 02:31:53 +08:00
}
}
}
2017-11-05 02:31:53 +08:00
async function saveOptions(options) {
await server.put('options', options);
infoService.showMessage("Options change have been saved.");
}
2017-11-05 02:31:53 +08:00
export default {
showDialog,
saveOptions
};
2017-11-05 02:31:53 +08:00
addTabHandler((function() {
const $themeSelect = $("#theme-select");
const $zoomFactorSelect = $("#zoom-factor-select");
const $html = $("html");
function optionsLoaded(options) {
$themeSelect.val(options.theme);
2018-06-09 22:34:51 +08:00
if (utils.isElectron()) {
$zoomFactorSelect.val(options.zoomFactor);
}
else {
$zoomFactorSelect.prop('disabled', true);
}
}
$themeSelect.change(function() {
const newTheme = $(this).val();
$html.attr("class", "theme-" + newTheme);
server.put('options/theme/' + newTheme);
});
$zoomFactorSelect.change(function() {
const newZoomFactor = $(this).val();
zoomService.setZoomFactorAndSave(newZoomFactor);
});
return {
optionsLoaded
};
})());
2018-03-28 10:42:46 +08:00
addTabHandler((function() {
const $form = $("#change-password-form");
const $oldPassword = $("#old-password");
const $newPassword1 = $("#new-password1");
const $newPassword2 = $("#new-password2");
2017-11-05 02:31:53 +08:00
function optionsLoaded(options) {
2017-09-13 10:23:57 +08:00
}
$form.submit(() => {
const oldPassword = $oldPassword.val();
const newPassword1 = $newPassword1.val();
const newPassword2 = $newPassword2.val();
2017-11-05 02:31:53 +08:00
$oldPassword.val('');
$newPassword1.val('');
$newPassword2.val('');
2017-11-05 02:31:53 +08:00
if (newPassword1 !== newPassword2) {
alert("New passwords are not the same.");
return false;
}
server.post('password/change', {
'current_password': oldPassword,
'new_password': newPassword1
}).then(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
protectedSessionHolder.resetProtectedSession();
}
else {
2018-03-26 09:29:35 +08:00
infoService.showError(result.message);
}
2017-11-05 02:31:53 +08:00
});
return false;
2017-09-13 10:23:57 +08:00
});
2017-11-05 02:31:53 +08:00
return {
optionsLoaded
2017-11-05 02:31:53 +08:00
};
})());
2018-03-28 10:42:46 +08:00
addTabHandler((function() {
const $form = $("#protected-session-timeout-form");
const $protectedSessionTimeout = $("#protected-session-timeout-in-seconds");
function optionsLoaded(options) {
$protectedSessionTimeout.val(options['protectedSessionTimeout']);
2017-11-05 02:31:53 +08:00
}
$form.submit(() => {
const protectedSessionTimeout = $protectedSessionTimeout.val();
saveOptions({ 'protectedSessionTimeout': protectedSessionTimeout }).then(() => {
protectedSessionHolder.setProtectedSessionTimeout(protectedSessionTimeout);
2017-11-05 02:31:53 +08:00
});
return false;
});
2017-11-05 02:31:53 +08:00
return {
optionsLoaded
2017-11-05 02:31:53 +08:00
};
})());
2018-03-28 10:42:46 +08:00
addTabHandler((function () {
const $form = $("#note-revision-snapshot-time-interval-form");
const $timeInterval = $("#note-revision-snapshot-time-interval-in-seconds");
2017-11-05 02:31:53 +08:00
function optionsLoaded(options) {
$timeInterval.val(options['noteRevisionSnapshotTimeInterval']);
2017-11-05 02:31:53 +08:00
}
$form.submit(() => {
saveOptions({ 'noteRevisionSnapshotTimeInterval': $timeInterval.val() });
2017-11-05 02:31:53 +08:00
return false;
});
2017-11-14 12:35:23 +08:00
return {
optionsLoaded
2017-11-14 12:35:23 +08:00
};
})());
2018-03-28 10:42:46 +08:00
addTabHandler((async function () {
const $appVersion = $("#app-version");
const $dbVersion = $("#db-version");
const $syncVersion = $("#sync-version");
const $buildDate = $("#build-date");
const $buildRevision = $("#build-revision");
2017-11-14 12:35:23 +08:00
const appInfo = await server.get('app-info');
2017-11-14 12:35:23 +08:00
2018-04-05 11:04:31 +08:00
$appVersion.html(appInfo.appVersion);
2018-04-03 09:47:46 +08:00
$dbVersion.html(appInfo.dbVersion);
$syncVersion.html(appInfo.syncVersion);
2018-04-05 11:04:31 +08:00
$buildDate.html(appInfo.buildDate);
$buildRevision.html(appInfo.buildRevision);
$buildRevision.attr('href', 'https://github.com/zadam/trilium/commit/' + appInfo.buildRevision);
2017-11-21 13:25:53 +08:00
return {};
})());
addTabHandler((function() {
const $form = $("#sync-setup-form");
const $syncServerHost = $("#sync-server-host");
const $syncServerTimeout = $("#sync-server-timeout");
const $syncProxy = $("#sync-proxy");
function optionsLoaded(options) {
$syncServerHost.val(options['syncServerHost']);
$syncServerTimeout.val(options['syncServerTimeout']);
$syncProxy.val(options['syncProxy']);
}
$form.submit(() => {
saveOptions({
'syncServerHost': $syncServerHost.val(),
'syncServerTimeout': $syncServerTimeout.val(),
'syncProxy': $syncProxy.val()
});
return false;
});
return {
optionsLoaded
};
})());
2018-03-28 10:42:46 +08:00
addTabHandler((async function () {
const $forceFullSyncButton = $("#force-full-sync-button");
const $fillSyncRowsButton = $("#fill-sync-rows-button");
const $anonymizeButton = $("#anonymize-button");
const $cleanupSoftDeletedButton = $("#cleanup-soft-deleted-items-button");
const $cleanupUnusedImagesButton = $("#cleanup-unused-images-button");
const $vacuumDatabaseButton = $("#vacuum-database-button");
$forceFullSyncButton.click(async () => {
await server.post('sync/force-full-sync');
2018-03-26 09:29:35 +08:00
infoService.showMessage("Full sync triggered");
});
$fillSyncRowsButton.click(async () => {
await server.post('sync/fill-sync-rows');
2018-03-26 09:29:35 +08:00
infoService.showMessage("Sync rows filled successfully");
});
2017-12-16 13:05:37 +08:00
$anonymizeButton.click(async () => {
2017-12-16 13:05:37 +08:00
await server.post('anonymization/anonymize');
2018-03-26 09:29:35 +08:00
infoService.showMessage("Created anonymized database");
2017-12-16 13:05:37 +08:00
});
$cleanupSoftDeletedButton.click(async () => {
if (confirm("Do you really want to clean up soft-deleted items?")) {
await server.post('cleanup/cleanup-soft-deleted-items');
2018-03-26 09:29:35 +08:00
infoService.showMessage("Soft deleted items have been cleaned up");
}
});
$cleanupUnusedImagesButton.click(async () => {
2018-01-08 03:07:59 +08:00
if (confirm("Do you really want to clean up unused images?")) {
await server.post('cleanup/cleanup-unused-images');
2018-03-26 09:29:35 +08:00
infoService.showMessage("Unused images have been cleaned up");
2018-01-08 03:07:59 +08:00
}
});
$vacuumDatabaseButton.click(async () => {
await server.post('cleanup/vacuum-database');
2018-03-26 09:29:35 +08:00
infoService.showMessage("Database has been vacuumed");
});
2017-11-21 13:25:53 +08:00
return {};
2017-11-05 02:31:53 +08:00
})());