You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.6 KiB
88 lines
2.6 KiB
document.addEventListener("DOMContentLoaded", function () {
|
|
document.querySelectorAll(".run-btn").forEach((button) => {
|
|
button.addEventListener("click", function () {
|
|
let command = this.getAttribute("data-command");
|
|
|
|
// Hiển thị hộp thoại xác nhận
|
|
if (confirm(`Bạn có chắc chắn restart ?`)) {
|
|
runCommand(this, command);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
function runCommand(button, commandStr) {
|
|
const originalText = button.innerHTML;
|
|
button.innerHTML = '<span class="spinner-border spinner-border-sm"></span> Running...';
|
|
button.disabled = true;
|
|
|
|
const outputContainer = document.getElementById("commandOutput");
|
|
outputContainer.innerHTML = ""; // Xóa output cũ
|
|
|
|
// Chuyển đổi chuỗi JSON thành mảng lệnh
|
|
let command;
|
|
try {
|
|
command = JSON.parse(commandStr);
|
|
} catch (error) {
|
|
showToast("Invalid command format", "danger");
|
|
button.innerHTML = originalText;
|
|
button.disabled = false;
|
|
return;
|
|
}
|
|
|
|
// Kiểm tra nếu command không phải là mảng
|
|
if (!Array.isArray(command)) {
|
|
showToast("Command must be an array", "danger");
|
|
button.innerHTML = originalText;
|
|
button.disabled = false;
|
|
return;
|
|
}
|
|
|
|
// Gửi danh sách command dưới dạng URL parameter
|
|
const url = new URL("/run-command", window.location.origin);
|
|
command.forEach(cmd => url.searchParams.append("command[]", cmd));
|
|
|
|
const eventSource = new EventSource(url);
|
|
|
|
eventSource.onmessage = function (event) {
|
|
if (event.data === "SUCCESS") {
|
|
showToast("Success", "success");
|
|
eventSource.close();
|
|
button.innerHTML = originalText;
|
|
button.disabled = false;
|
|
} else if (event.data === "ERROR") {
|
|
showToast("Error", "danger");
|
|
eventSource.close();
|
|
button.innerHTML = originalText;
|
|
button.disabled = false;
|
|
} else {
|
|
outputContainer.innerHTML += event.data + "\n";
|
|
outputContainer.scrollTop = outputContainer.scrollHeight;
|
|
}
|
|
};
|
|
|
|
eventSource.onerror = function () {
|
|
showToast("Error", "danger");
|
|
eventSource.close();
|
|
button.innerHTML = originalText;
|
|
button.disabled = false;
|
|
};
|
|
}
|
|
|
|
|
|
// Hiển thị Toast thông báo
|
|
function showToast(message, type) {
|
|
const toastContainer = document.getElementById("toastContainer");
|
|
const toast = document.createElement("div");
|
|
|
|
toast.className = `toast align-items-center text-bg-${type} border-0 show`;
|
|
toast.role = "alert";
|
|
toast.innerHTML = `<div class="toast-body text-center fw-bold">${message}</div>`;
|
|
|
|
toastContainer.appendChild(toast);
|
|
|
|
setTimeout(() => {
|
|
toast.remove();
|
|
}, 5000);
|
|
}
|
|
|