web-base/js/install.js

164 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-02-09 23:02:19 +01:00
const NOT_STARTED = 0;
const PENDING = 1;
const SUCCESFULL = 2;
const ERROR = 3;
function setState(state) {
2021-03-31 13:59:02 +02:00
let li = $("#currentStep");
let icon, color, text;
2020-02-09 23:02:19 +01:00
switch (state) {
case PENDING:
icon = 'fas fa-spin fa-spinner';
text = "Loading…";
color = "muted";
break;
case SUCCESFULL:
icon = 'fas fa-check-circle';
text = "Successfull";
color = "success";
break;
case ERROR:
icon = 'fas fa-times-circle';
text = "Failed";
color = "danger";
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;
}
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");
statusBox.html("An error occurred during intallation: " + data.msg);
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");
statusBox.html("An error occurred during intallation. Try <a href=\"/index.php\">restarting the process</a>.");
statusBox.show();
2020-02-09 23:02:19 +01:00
}).always(function() {
if(done) done(success);
});
}
function retry() {
$("#btnRetry").hide();
$("#progressText").show();
sendRequest({ }, function(success) {
$("#progressText").hide();
if(!success) $("#btnRetry").show();
});
}
$(document).ready(function() {
$("#btnSubmit").click(function() {
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 {
setState(SUCCESFULL);
}
});
});
});
$("#btnPrev").click(function() {
$("#btnPrev").prop("disabled", true);
sendRequest({ "prev": true }, function(success) {
if(!success) {
$("#btnPrev").prop("disabled",false);
} 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();
});
2020-02-09 23:02:19 +01:00
});