web-base/js/install.js

203 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-02-09 23:02:19 +01:00
const NOT_STARTED = 0;
const PENDING = 1;
2022-02-20 23:17:17 +01:00
const SUCCESSFUL = 2;
2020-02-09 23:02:19 +01:00
const ERROR = 3;
2022-02-20 23:17:17 +01:00
let currentState = PENDING;
2020-02-09 23:02:19 +01:00
function setState(state) {
2021-03-31 13:59:02 +02:00
let li = $("#currentStep");
let icon, color, text;
2022-02-20 23:17:17 +01:00
currentState = state;
2020-02-09 23:02:19 +01:00
switch (state) {
case PENDING:
icon = 'fas fa-spin fa-spinner';
text = "Loading…";
color = "muted";
break;
2022-02-20 23:17:17 +01:00
case SUCCESSFUL:
2020-02-09 23:02:19 +01:00
icon = 'fas fa-check-circle';
2023-03-05 15:30:06 +01:00
text = "Successful";
2020-02-09 23:02:19 +01:00
color = "success";
break;
case ERROR:
icon = 'fas fa-times-circle';
text = "Failed";
color = "danger";
2022-02-20 23:17:17 +01:00
$("#btnRetry").show();
2020-02-09 23:02:19 +01:00
break;
default:
icon = 'far fa-circle';
text = "Pending";
color = "muted";
break;
}
li.find("small").removeClass().addClass("text-" + color).html(text);
li.find("span").removeClass().addClass("text-" + color);
li.find("i").removeClass().addClass(icon);
}
2020-04-02 22:25:13 +02:00
function getCurrentStep() {
return $("#currentStep").index() + 1;
}
2022-02-20 23:17:17 +01:00
function requestCurrentStep(callback) {
$.post("/index.php?status", {}, function(data) {
callback(data.step ?? null);
}, "json").fail(function() {
callback(null);
});
}
2020-02-09 23:02:19 +01:00
function sendRequest(params, done) {
setState(PENDING);
2021-03-31 13:59:02 +02:00
let success = false;
let statusBox = $("#status");
statusBox.hide();
2020-02-09 23:02:19 +01:00
$.post("/index.php", params, function(data) {
2021-03-31 13:59:02 +02:00
if(data.success || data.step !== getCurrentStep()) {
2020-02-09 23:02:19 +01:00
success = true;
window.location.reload();
} else {
setState(ERROR);
2021-03-31 13:59:02 +02:00
statusBox.addClass("alert-danger");
2022-02-20 23:17:17 +01:00
statusBox.html("An error occurred during installation: " + data.msg);
2021-03-31 13:59:02 +02:00
statusBox.show();
2020-02-09 23:02:19 +01:00
}
}, "json").fail(function() {
setState(ERROR);
2021-03-31 13:59:02 +02:00
statusBox.addClass("alert-danger");
2022-02-20 23:17:17 +01:00
statusBox.html("An error occurred during installation. Try <a href=\"/index.php\">restarting the process</a>.");
2021-03-31 13:59:02 +02:00
statusBox.show();
2020-02-09 23:02:19 +01:00
}).always(function() {
if(done) done(success);
});
}
function retry() {
2022-02-20 23:17:17 +01:00
let progressText = $("#progressText");
let wasHidden = progressText.hasClass("hidden");
2020-02-09 23:02:19 +01:00
$("#btnRetry").hide();
2022-02-20 23:17:17 +01:00
progressText.removeClass("hidden");
2020-02-09 23:02:19 +01:00
sendRequest({ }, function(success) {
2022-02-20 23:17:17 +01:00
if (wasHidden) {
$("#progressText").addClass("hidden");
}
if(!success) {
$("#btnRetry").show();
}
2020-02-09 23:02:19 +01:00
});
}
2022-02-20 23:17:17 +01:00
function waitForStatusChange() {
setTimeout(() => {
requestCurrentStep((step) => {
if (currentState === PENDING) {
if (step !== 2 || step == null) {
document.location.reload();
} else {
waitForStatusChange();
}
}
})
}, 2500);
}
2020-02-09 23:02:19 +01:00
$(document).ready(function() {
$("#btnSubmit").click(function() {
2023-03-05 15:30:06 +01:00
let params = { };
2021-03-31 13:59:02 +02:00
let submitButton = $("#btnSubmit");
let textBefore = submitButton.text();
submitButton.prop("disabled", true);
submitButton.html("Submitting… <i class=\"fas fa-spinner fa-spin\">");
2020-04-02 01:48:46 +02:00
$("#installForm .form-control").each(function() {
2021-03-31 13:59:02 +02:00
let type = $(this).attr("type") ?? $(this).prop("tagName").toLowerCase();
let name = $(this).attr("name");
2020-02-09 23:02:19 +01:00
if(type === "text") {
params[name] = $(this).val().trim();
} else if(type === "password" || type === "number") {
params[name] = $(this).val();
2020-04-02 01:48:46 +02:00
} else if(type === "select") {
params[name] = $(this).find(":selected").val();
2020-02-09 23:02:19 +01:00
}
}).promise().done(function() {
sendRequest(params, function(success) {
if(!success) {
2021-03-31 13:59:02 +02:00
submitButton.prop("disabled",false);
submitButton.text(textBefore);
2020-02-09 23:02:19 +01:00
} else {
2022-02-20 23:17:17 +01:00
setState(SUCCESSFUL);
2020-02-09 23:02:19 +01:00
}
});
});
});
$("#btnPrev").click(function() {
$("#btnPrev").prop("disabled", true);
sendRequest({ "prev": true }, function(success) {
if(!success) {
2023-03-05 15:30:06 +01:00
$("#btnPrev").prop("disabled", false);
2020-02-09 23:02:19 +01:00
} else {
window.location.reload();
}
});
});
$("#btnSkip").click(function() {
$("#btnSkip").prop("disabled", true);
sendRequest({ "skip": true }, function(success) {
if(!success) {
$("#btnSkip").prop("btnSkip",false);
} else {
window.location.reload();
}
});
});
2020-02-09 23:30:26 +01:00
$("#btnFinish").click(function() {
2020-04-02 21:19:06 +02:00
window.location = "/admin";
2020-02-09 23:30:26 +01:00
});
2020-02-09 23:02:19 +01:00
$("#btnRetry").click(function() {
retry();
});
2020-04-02 01:48:46 +02:00
// DATABASE PORT
2021-04-06 17:39:21 +02:00
let portField = $("#port");
let typeField = $("#type");
let prevPort = parseInt(portField.val());
let prevDbms = typeField.find("option:selected").val();
2020-04-02 01:48:46 +02:00
function updateDefaultPort() {
2021-03-31 13:59:02 +02:00
let defaultPorts = {
2020-04-02 01:48:46 +02:00
"mysql": 3306,
2021-03-31 13:59:02 +02:00
"postgres": 5432
2020-04-02 01:48:46 +02:00
};
2021-04-06 17:39:21 +02:00
let curDbms = typeField.find("option:selected").val();
2021-03-31 13:59:02 +02:00
if(defaultPorts[prevDbms] === prevPort) {
2021-04-06 17:39:21 +02:00
prevDbms = curDbms;
portField.val(prevPort = defaultPorts[curDbms]);
2020-04-02 01:48:46 +02:00
}
}
updateDefaultPort();
2021-04-06 17:39:21 +02:00
typeField.change(function() {
2020-04-02 01:48:46 +02:00
updateDefaultPort();
});
2022-02-20 23:17:17 +01:00
// INSTALL_DEPENDENCIES ?
if (getCurrentStep() === 2) {
sendRequest({}, () => {
waitForStatusChange();
});
}
2020-02-09 23:02:19 +01:00
});