2018-07-22 20:49:59 +08:00
|
|
|
import utils from "./services/utils.js";
|
2019-01-10 05:08:24 +08:00
|
|
|
import macInit from './services/mac_init.js';
|
|
|
|
|
|
|
|
macInit.init();
|
2018-04-03 10:33:54 +08:00
|
|
|
|
2018-07-21 14:55:24 +08:00
|
|
|
function SetupModel() {
|
2018-07-24 14:12:36 +08:00
|
|
|
if (syncInProgress) {
|
|
|
|
setInterval(checkOutstandingSyncs, 1000);
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:57:36 +08:00
|
|
|
const serverAddress = location.protocol + '//' + location.host;
|
|
|
|
|
|
|
|
$("#current-host").html(serverAddress);
|
|
|
|
|
2018-07-24 14:12:36 +08:00
|
|
|
this.step = ko.observable(syncInProgress ? "sync-in-progress" : "setup-type");
|
2018-07-21 14:55:24 +08:00
|
|
|
this.setupType = ko.observable();
|
|
|
|
|
|
|
|
this.setupNewDocument = ko.observable(false);
|
|
|
|
this.setupSyncFromDesktop = ko.observable(false);
|
|
|
|
this.setupSyncFromServer = ko.observable(false);
|
|
|
|
|
|
|
|
this.username = ko.observable();
|
|
|
|
this.password1 = ko.observable();
|
|
|
|
this.password2 = ko.observable();
|
|
|
|
|
2019-08-11 16:28:49 +08:00
|
|
|
this.theme = ko.observable("white");
|
|
|
|
this.theme.subscribe(function(newTheme) {
|
|
|
|
const $body = $("body");
|
|
|
|
|
|
|
|
for (const clazz of Array.from($body[0].classList)) { // create copy to safely iterate over while removing classes
|
|
|
|
if (clazz.startsWith("theme-")) {
|
|
|
|
$body.removeClass(clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$body.addClass("theme-" + newTheme);
|
|
|
|
});
|
|
|
|
|
2018-07-25 14:30:41 +08:00
|
|
|
this.syncServerHost = ko.observable();
|
|
|
|
this.syncProxy = ko.observable();
|
2018-07-22 20:49:59 +08:00
|
|
|
|
|
|
|
this.instanceType = utils.isElectron() ? "desktop" : "server";
|
|
|
|
|
2019-01-04 05:32:36 +08:00
|
|
|
this.setupTypeSelected = () => !!this.setupType();
|
2018-07-21 14:55:24 +08:00
|
|
|
|
|
|
|
this.selectSetupType = () => {
|
2019-01-04 05:32:36 +08:00
|
|
|
this.step(this.setupType());
|
2018-07-21 14:55:24 +08:00
|
|
|
};
|
|
|
|
|
2018-07-22 20:49:59 +08:00
|
|
|
this.back = () => {
|
|
|
|
this.step("setup-type");
|
|
|
|
|
2019-01-04 05:32:36 +08:00
|
|
|
this.setupType("");
|
2018-07-22 20:49:59 +08:00
|
|
|
};
|
2018-07-21 14:55:24 +08:00
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
this.finish = async () => {
|
2019-01-04 05:32:36 +08:00
|
|
|
if (this.setupType() === 'new-document') {
|
2018-07-21 14:55:24 +08:00
|
|
|
const username = this.username();
|
|
|
|
const password1 = this.password1();
|
|
|
|
const password2 = this.password2();
|
2019-08-11 16:28:49 +08:00
|
|
|
const theme = this.theme();
|
2018-07-21 14:55:24 +08:00
|
|
|
|
|
|
|
if (!username) {
|
|
|
|
showAlert("Username can't be empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password1) {
|
|
|
|
showAlert("Password can't be empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (password1 !== password2) {
|
|
|
|
showAlert("Both password fields need be identical.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
// not using server.js because it loads too many dependencies
|
|
|
|
$.post('/api/setup/new-document', {
|
2018-07-21 14:55:24 +08:00
|
|
|
username: username,
|
2019-08-11 16:28:49 +08:00
|
|
|
password: password1,
|
|
|
|
theme: theme
|
2018-07-21 14:55:24 +08:00
|
|
|
}).then(() => {
|
|
|
|
window.location.replace("/");
|
|
|
|
});
|
|
|
|
}
|
2019-01-04 05:32:36 +08:00
|
|
|
else if (this.setupType() === 'sync-from-server') {
|
2018-07-25 14:30:41 +08:00
|
|
|
const syncServerHost = this.syncServerHost();
|
|
|
|
const syncProxy = this.syncProxy();
|
2018-07-22 20:49:59 +08:00
|
|
|
const username = this.username();
|
|
|
|
const password = this.password1();
|
|
|
|
|
2018-07-25 14:30:41 +08:00
|
|
|
if (!syncServerHost) {
|
2018-07-22 20:49:59 +08:00
|
|
|
showAlert("Trilium server address can't be empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!username) {
|
|
|
|
showAlert("Username can't be empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password) {
|
|
|
|
showAlert("Password can't be empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
// not using server.js because it loads too many dependencies
|
2019-02-23 06:03:20 +08:00
|
|
|
const resp = await $.post('api/setup/sync-from-server', {
|
2018-07-25 14:30:41 +08:00
|
|
|
syncServerHost: syncServerHost,
|
|
|
|
syncProxy: syncProxy,
|
2018-07-23 01:56:20 +08:00
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
});
|
2018-07-24 03:15:32 +08:00
|
|
|
|
|
|
|
if (resp.result === 'success') {
|
|
|
|
this.step('sync-in-progress');
|
|
|
|
|
|
|
|
setInterval(checkOutstandingSyncs, 1000);
|
2018-07-26 04:54:37 +08:00
|
|
|
|
|
|
|
hideAlert();
|
2018-07-24 03:15:32 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-07-26 04:54:37 +08:00
|
|
|
showAlert('Sync setup failed: ' + resp.error);
|
2018-07-24 03:15:32 +08:00
|
|
|
}
|
2018-07-22 20:49:59 +08:00
|
|
|
}
|
2018-07-21 14:55:24 +08:00
|
|
|
};
|
|
|
|
}
|
2017-12-04 11:29:23 +08:00
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
async function checkOutstandingSyncs() {
|
2018-07-24 14:12:36 +08:00
|
|
|
const { stats, initialized } = await $.get('/api/sync/stats');
|
|
|
|
|
|
|
|
if (initialized) {
|
|
|
|
window.location.replace("/");
|
|
|
|
}
|
|
|
|
|
2018-07-24 03:15:32 +08:00
|
|
|
const totalOutstandingSyncs = stats.outstandingPushes + stats.outstandingPulls;
|
|
|
|
|
|
|
|
$("#outstanding-syncs").html(totalOutstandingSyncs);
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:29:23 +08:00
|
|
|
function showAlert(message) {
|
|
|
|
$("#alert").html(message);
|
|
|
|
$("#alert").show();
|
2018-07-21 14:55:24 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 04:54:37 +08:00
|
|
|
function hideAlert() {
|
|
|
|
$("#alert").hide();
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:49:59 +08:00
|
|
|
ko.applyBindings(new SetupModel(), document.getElementById('setup-dialog'));
|
|
|
|
|
|
|
|
$("#setup-dialog").show();
|