Notes/src/public/app/setup.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-07-22 14:49:59 +02:00
import utils from "./services/utils.js";
import macInit from './services/mac_init.js';
macInit.init();
2018-04-02 22:33:54 -04:00
function SetupModel() {
if (syncInProgress) {
setInterval(checkOutstandingSyncs, 1000);
}
const serverAddress = `${location.protocol}//${location.host}`;
$("#current-host").html(serverAddress);
this.step = ko.observable(syncInProgress ? "sync-in-progress" : "setup-type");
this.setupType = ko.observable();
this.setupNewDocument = ko.observable(false);
this.setupSyncFromDesktop = ko.observable(false);
this.setupSyncFromServer = ko.observable(false);
2018-07-25 08:30:41 +02:00
this.syncServerHost = ko.observable();
this.syncProxy = ko.observable();
this.password = ko.observable();
2018-07-22 14:49:59 +02:00
this.setupTypeSelected = () => !!this.setupType();
this.selectSetupType = () => {
2021-12-28 22:59:38 +01:00
if (this.setupType() === 'new-document') {
this.step('new-document-in-progress');
$.post('api/setup/new-document').then(() => {
window.location.replace("./setup");
});
}
else {
this.step(this.setupType());
}
};
2018-07-22 14:49:59 +02:00
this.back = () => {
this.step("setup-type");
this.setupType("");
2018-07-22 14:49:59 +02:00
};
this.finish = async () => {
2021-12-28 22:59:38 +01:00
const syncServerHost = this.syncServerHost();
const syncProxy = this.syncProxy();
const password = this.password();
2021-12-28 22:59:38 +01:00
if (!syncServerHost) {
showAlert("Trilium server address can't be empty");
return;
}
2021-12-28 22:59:38 +01:00
if (!password) {
showAlert("Password can't be empty");
return;
}
2021-12-28 22:59:38 +01:00
// not using server.js because it loads too many dependencies
const resp = await $.post('api/setup/sync-from-server', {
syncServerHost: syncServerHost,
syncProxy: syncProxy,
password: password
});
if (resp.result === 'success') {
this.step('sync-in-progress');
2021-12-28 22:59:38 +01:00
setInterval(checkOutstandingSyncs, 1000);
2021-12-28 22:59:38 +01:00
hideAlert();
}
else {
showAlert(`Sync setup failed: ${resp.error}`);
2018-07-22 14:49:59 +02:00
}
};
}
2017-12-03 22:29:23 -05:00
async function checkOutstandingSyncs() {
const { outstandingPullCount, initialized } = await $.get('api/sync/stats');
if (initialized) {
2020-08-27 22:03:56 +02:00
if (utils.isElectron()) {
2021-11-16 22:43:08 +01:00
const remote = utils.dynamicRequire('@electron/remote');
2020-08-27 22:03:56 +02:00
remote.app.relaunch();
remote.app.exit(0);
}
else {
2021-08-24 22:59:51 +02:00
utils.reloadFrontendApp();
2020-08-27 22:03:56 +02:00
}
}
2019-12-28 12:55:53 +01:00
else {
$("#outstanding-syncs").html(outstandingPullCount);
2019-12-28 12:55:53 +01:00
}
}
2017-12-03 22:29:23 -05:00
function showAlert(message) {
$("#alert").html(message);
$("#alert").show();
}
function hideAlert() {
$("#alert").hide();
}
2018-07-22 14:49:59 +02:00
ko.applyBindings(new SetupModel(), document.getElementById('setup-dialog'));
2020-08-27 22:03:56 +02:00
$("#setup-dialog").show();