refactor: Externalize all template JavaScript to asset files
This commit is contained in:
138
core/scripts/webpanel/assets/js/base.js
Normal file
138
core/scripts/webpanel/assets/js/base.js
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
$(function () {
|
||||||
|
const darkModeToggle = $("#darkModeToggle");
|
||||||
|
const darkModeIcon = $("#darkModeIcon");
|
||||||
|
const isDarkMode = localStorage.getItem("darkMode") === "enabled";
|
||||||
|
|
||||||
|
setDarkMode(isDarkMode);
|
||||||
|
updateIcon(isDarkMode);
|
||||||
|
|
||||||
|
darkModeToggle.on("click", function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const enabled = $("body").hasClass("dark-mode");
|
||||||
|
localStorage.setItem("darkMode", enabled ? "disabled" : "enabled");
|
||||||
|
setDarkMode(!enabled);
|
||||||
|
updateIcon(!enabled);
|
||||||
|
});
|
||||||
|
|
||||||
|
function setDarkMode(enabled) {
|
||||||
|
$("body").toggleClass("dark-mode", enabled);
|
||||||
|
|
||||||
|
if (enabled) {
|
||||||
|
$(".main-header").addClass("navbar-dark").removeClass("navbar-light navbar-white");
|
||||||
|
$(".card").addClass("bg-dark");
|
||||||
|
} else {
|
||||||
|
$(".main-header").addClass("navbar-white navbar-light").removeClass("navbar-dark");
|
||||||
|
$(".card").removeClass("bg-dark");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateIcon(enabled) {
|
||||||
|
darkModeIcon.removeClass("fa-moon fa-sun")
|
||||||
|
.addClass(enabled ? "fa-sun" : "fa-moon");
|
||||||
|
}
|
||||||
|
|
||||||
|
const versionUrl = $('body').data('version-url');
|
||||||
|
$.ajax({
|
||||||
|
url: versionUrl,
|
||||||
|
type: 'GET',
|
||||||
|
success: function (response) {
|
||||||
|
$('#panel-version').text(`Version: ${response.current_version || 'N/A'}`);
|
||||||
|
},
|
||||||
|
error: function (error) {
|
||||||
|
console.error("Error fetching version:", error);
|
||||||
|
$('#panel-version').text('Version: Error');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function shouldCheckForUpdates() {
|
||||||
|
const lastCheck = localStorage.getItem('lastUpdateCheck');
|
||||||
|
const updateDismissed = localStorage.getItem('updateDismissed');
|
||||||
|
const now = Date.now();
|
||||||
|
const checkInterval = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
if (!lastCheck) return true;
|
||||||
|
if (updateDismissed && now - parseInt(updateDismissed) < 2 * 60 * 60 * 1000) return false;
|
||||||
|
|
||||||
|
return now - parseInt(lastCheck) > checkInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
function showUpdateBar(version, changelog) {
|
||||||
|
$('#updateMessage').text(`Version ${version} is now available`);
|
||||||
|
|
||||||
|
const converter = new showdown.Converter();
|
||||||
|
const htmlChangelog = changelog ? converter.makeHtml(changelog) : '<p>No changelog available.</p>';
|
||||||
|
$('#changelogText').html(htmlChangelog);
|
||||||
|
|
||||||
|
$('#updateBar').slideDown(300);
|
||||||
|
|
||||||
|
$('#viewRelease').off('click').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
window.open('https://github.com/ReturnFI/Blitz/releases/latest', '_blank');
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#showChangelog').off('click').on('click', function() {
|
||||||
|
const $content = $('#changelogContent');
|
||||||
|
const $icon = $(this).find('i');
|
||||||
|
|
||||||
|
if ($content.is(':visible')) {
|
||||||
|
$content.slideUp(250);
|
||||||
|
$icon.removeClass('fa-chevron-up').addClass('fa-chevron-down');
|
||||||
|
$(this).css('opacity', '0.8');
|
||||||
|
} else {
|
||||||
|
$content.slideDown(250);
|
||||||
|
$icon.removeClass('fa-chevron-down').addClass('fa-chevron-up');
|
||||||
|
$(this).css('opacity', '1');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('.dropdown-toggle').dropdown();
|
||||||
|
|
||||||
|
$('#remindLater').off('click').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
$('#updateBar').slideUp(350);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#skipVersion').off('click').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
localStorage.setItem('dismissedVersion', version);
|
||||||
|
localStorage.setItem('updateDismissed', Date.now().toString());
|
||||||
|
$('#updateBar').slideUp(350);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#closeUpdateBar').off('click').on('click', function() {
|
||||||
|
$('#updateBar').slideUp(350);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkForUpdates() {
|
||||||
|
if (!shouldCheckForUpdates()) return;
|
||||||
|
|
||||||
|
const checkVersionUrl = $('body').data('check-version-url');
|
||||||
|
$.ajax({
|
||||||
|
url: checkVersionUrl,
|
||||||
|
type: 'GET',
|
||||||
|
timeout: 10000,
|
||||||
|
success: function (response) {
|
||||||
|
localStorage.setItem('lastUpdateCheck', Date.now().toString());
|
||||||
|
|
||||||
|
if (response.is_latest) {
|
||||||
|
localStorage.removeItem('updateDismissed');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dismissedVersion = localStorage.getItem('dismissedVersion');
|
||||||
|
if (dismissedVersion === response.latest_version) return;
|
||||||
|
|
||||||
|
showUpdateBar(response.latest_version, response.changelog);
|
||||||
|
},
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
if (status !== 'timeout') {
|
||||||
|
console.warn("Update check failed:", error);
|
||||||
|
}
|
||||||
|
localStorage.setItem('lastUpdateCheck', Date.now().toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(checkForUpdates, 2000);
|
||||||
|
});
|
||||||
95
core/scripts/webpanel/assets/js/config.js
Normal file
95
core/scripts/webpanel/assets/js/config.js
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
const mainContent = document.querySelector('.content-wrapper > div');
|
||||||
|
const GET_FILE_URL = mainContent.dataset.getFileUrl;
|
||||||
|
const SET_FILE_URL = mainContent.dataset.setFileUrl;
|
||||||
|
|
||||||
|
const saveButton = document.getElementById("save-button");
|
||||||
|
const restoreButton = document.getElementById("restore-button");
|
||||||
|
const container = document.getElementById("jsoneditor");
|
||||||
|
|
||||||
|
const editor = new JSONEditor(container, {
|
||||||
|
mode: "code",
|
||||||
|
onChange: validateJson
|
||||||
|
});
|
||||||
|
|
||||||
|
function validateJson() {
|
||||||
|
try {
|
||||||
|
editor.get();
|
||||||
|
updateSaveButton(true);
|
||||||
|
hideErrorMessage();
|
||||||
|
} catch (error) {
|
||||||
|
updateSaveButton(false);
|
||||||
|
showErrorMessage("Invalid JSON! Please correct the errors.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateSaveButton(isValid) {
|
||||||
|
saveButton.disabled = !isValid;
|
||||||
|
saveButton.style.cursor = isValid ? "pointer" : "not-allowed";
|
||||||
|
saveButton.style.setProperty('background-color', isValid ? "#28a745" : "#ccc", 'important');
|
||||||
|
saveButton.style.setProperty('color', isValid ? "#fff" : "#666", 'important');
|
||||||
|
}
|
||||||
|
|
||||||
|
function showErrorMessage(message) {
|
||||||
|
Swal.fire({
|
||||||
|
title: "Error",
|
||||||
|
text: message,
|
||||||
|
icon: "error",
|
||||||
|
showConfirmButton: false,
|
||||||
|
timer: 5000,
|
||||||
|
position: 'top-right',
|
||||||
|
toast: true,
|
||||||
|
showClass: { popup: 'animate__animated animate__fadeInDown' },
|
||||||
|
hideClass: { popup: 'animate__animated animate__fadeOutUp' }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function hideErrorMessage() {
|
||||||
|
Swal.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveJson() {
|
||||||
|
Swal.fire({
|
||||||
|
title: 'Are you sure?',
|
||||||
|
text: 'Do you want to save the changes?',
|
||||||
|
icon: 'warning',
|
||||||
|
showCancelButton: true,
|
||||||
|
confirmButtonText: 'Yes, save it!',
|
||||||
|
cancelButtonText: 'Cancel',
|
||||||
|
reverseButtons: true
|
||||||
|
}).then((result) => {
|
||||||
|
if (result.isConfirmed) {
|
||||||
|
fetch(SET_FILE_URL, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(editor.get())
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
Swal.fire('Saved!', 'Your changes have been saved.', 'success');
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
Swal.fire('Error!', 'There was an error saving your data.', 'error');
|
||||||
|
console.error("Error saving JSON:", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreJson() {
|
||||||
|
fetch(GET_FILE_URL)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(json => {
|
||||||
|
editor.set(json);
|
||||||
|
Swal.fire('Success!', 'Your JSON has been loaded.', 'success');
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
Swal.fire('Error!', 'There was an error loading your JSON.', 'error');
|
||||||
|
console.error("Error loading JSON:", error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
saveButton.addEventListener('click', saveJson);
|
||||||
|
restoreButton.addEventListener('click', restoreJson);
|
||||||
|
|
||||||
|
restoreJson();
|
||||||
|
});
|
||||||
73
core/scripts/webpanel/assets/js/index.js
Normal file
73
core/scripts/webpanel/assets/js/index.js
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
function updateServerInfo() {
|
||||||
|
const serverStatusUrl = document.querySelector('.content').dataset.serverStatusUrl;
|
||||||
|
fetch(serverStatusUrl)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
document.getElementById('cpu-usage').textContent = data.cpu_usage;
|
||||||
|
document.getElementById('ram-usage').textContent = `${data.ram_usage} / ${data.total_ram}`;
|
||||||
|
document.getElementById('online-users').textContent = data.online_users;
|
||||||
|
document.getElementById('uptime').textContent = data.uptime;
|
||||||
|
|
||||||
|
document.getElementById('server-ipv4').textContent = `IPv4: ${data.server_ipv4 || 'N/A'}`;
|
||||||
|
document.getElementById('server-ipv6').textContent = `IPv6: ${data.server_ipv6 || 'N/A'}`;
|
||||||
|
|
||||||
|
document.getElementById('download-speed').textContent = `🔽 Download: ${data.download_speed}`;
|
||||||
|
document.getElementById('upload-speed').textContent = `🔼 Upload: ${data.upload_speed}`;
|
||||||
|
document.getElementById('tcp-connections').textContent = `TCP: ${data.tcp_connections}`;
|
||||||
|
document.getElementById('udp-connections').textContent = `UDP: ${data.udp_connections}`;
|
||||||
|
|
||||||
|
document.getElementById('reboot-uploaded-traffic').textContent = data.reboot_uploaded_traffic;
|
||||||
|
document.getElementById('reboot-downloaded-traffic').textContent = data.reboot_downloaded_traffic;
|
||||||
|
document.getElementById('reboot-total-traffic').textContent = data.reboot_total_traffic;
|
||||||
|
|
||||||
|
document.getElementById('user-uploaded-traffic').textContent = data.user_uploaded_traffic;
|
||||||
|
document.getElementById('user-downloaded-traffic').textContent = data.user_downloaded_traffic;
|
||||||
|
document.getElementById('user-total-traffic').textContent = data.user_total_traffic;
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error fetching server info:', error));
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateServiceStatuses() {
|
||||||
|
const servicesStatusUrl = document.querySelector('.content').dataset.servicesStatusUrl;
|
||||||
|
fetch(servicesStatusUrl)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
updateServiceBox('hysteria2', data.hysteria_server);
|
||||||
|
updateServiceBox('telegrambot', data.hysteria_telegram_bot);
|
||||||
|
updateServiceBox('iplimit', data.hysteria_iplimit);
|
||||||
|
updateServiceBox('normalsub', data.hysteria_normal_sub);
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error fetching service statuses:', error));
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateServiceBox(serviceName, status) {
|
||||||
|
const statusElement = document.getElementById(serviceName + '-status');
|
||||||
|
const statusBox = document.getElementById(serviceName + '-status-box');
|
||||||
|
|
||||||
|
if (status === true) {
|
||||||
|
statusElement.textContent = 'Active';
|
||||||
|
statusBox.classList.remove('bg-danger');
|
||||||
|
statusBox.classList.add('bg-success');
|
||||||
|
} else {
|
||||||
|
statusElement.textContent = 'Inactive';
|
||||||
|
statusBox.classList.remove('bg-success');
|
||||||
|
statusBox.classList.add('bg-danger');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
updateServerInfo();
|
||||||
|
updateServiceStatuses();
|
||||||
|
setInterval(updateServerInfo, 2000);
|
||||||
|
setInterval(updateServiceStatuses, 10000);
|
||||||
|
|
||||||
|
const toggleIpBtn = document.getElementById('toggle-ip-visibility');
|
||||||
|
const ipAddressesDiv = document.getElementById('ip-addresses');
|
||||||
|
toggleIpBtn.addEventListener('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const isBlurred = ipAddressesDiv.style.filter === 'blur(5px)';
|
||||||
|
ipAddressesDiv.style.filter = isBlurred ? 'none' : 'blur(5px)';
|
||||||
|
toggleIpBtn.querySelector('i').classList.toggle('fa-eye');
|
||||||
|
toggleIpBtn.querySelector('i').classList.toggle('fa-eye-slash');
|
||||||
|
});
|
||||||
|
});
|
||||||
1193
core/scripts/webpanel/assets/js/settings.js
Normal file
1193
core/scripts/webpanel/assets/js/settings.js
Normal file
File diff suppressed because it is too large
Load Diff
73
core/scripts/webpanel/assets/js/users.js
Normal file
73
core/scripts/webpanel/assets/js/users.js
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
function updateServerInfo() {
|
||||||
|
const serverStatusUrl = document.querySelector('.content').dataset.serverStatusUrl;
|
||||||
|
fetch(serverStatusUrl)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
document.getElementById('cpu-usage').textContent = data.cpu_usage;
|
||||||
|
document.getElementById('ram-usage').textContent = `${data.ram_usage} / ${data.total_ram}`;
|
||||||
|
document.getElementById('online-users').textContent = data.online_users;
|
||||||
|
document.getElementById('uptime').textContent = data.uptime;
|
||||||
|
|
||||||
|
document.getElementById('server-ipv4').textContent = `IPv4: ${data.server_ipv4 || 'N/A'}`;
|
||||||
|
document.getElementById('server-ipv6').textContent = `IPv6: ${data.server_ipv6 || 'N/A'}`;
|
||||||
|
|
||||||
|
document.getElementById('download-speed').textContent = `🔽 Download: ${data.download_speed}`;
|
||||||
|
document.getElementById('upload-speed').textContent = `🔼 Upload: ${data.upload_speed}`;
|
||||||
|
document.getElementById('tcp-connections').textContent = `TCP: ${data.tcp_connections}`;
|
||||||
|
document.getElementById('udp-connections').textContent = `UDP: ${data.udp_connections}`;
|
||||||
|
|
||||||
|
document.getElementById('reboot-uploaded-traffic').textContent = data.reboot_uploaded_traffic;
|
||||||
|
document.getElementById('reboot-downloaded-traffic').textContent = data.reboot_downloaded_traffic;
|
||||||
|
document.getElementById('reboot-total-traffic').textContent = data.reboot_total_traffic;
|
||||||
|
|
||||||
|
document.getElementById('user-uploaded-traffic').textContent = data.user_uploaded_traffic;
|
||||||
|
document.getElementById('user-downloaded-traffic').textContent = data.user_downloaded_traffic;
|
||||||
|
document.getElementById('user-total-traffic').textContent = data.user_total_traffic;
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error fetching server info:', error));
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateServiceStatuses() {
|
||||||
|
const servicesStatusUrl = document.querySelector('.content').dataset.servicesStatusUrl;
|
||||||
|
fetch(servicesStatusUrl)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
updateServiceBox('hysteria2', data.hysteria_server);
|
||||||
|
updateServiceBox('telegrambot', data.hysteria_telegram_bot);
|
||||||
|
updateServiceBox('iplimit', data.hysteria_iplimit);
|
||||||
|
updateServiceBox('normalsub', data.hysteria_normal_sub);
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error fetching service statuses:', error));
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateServiceBox(serviceName, status) {
|
||||||
|
const statusElement = document.getElementById(serviceName + '-status');
|
||||||
|
const statusBox = document.getElementById(serviceName + '-status-box');
|
||||||
|
|
||||||
|
if (status === true) {
|
||||||
|
statusElement.textContent = 'Active';
|
||||||
|
statusBox.classList.remove('bg-danger');
|
||||||
|
statusBox.classList.add('bg-success');
|
||||||
|
} else {
|
||||||
|
statusElement.textContent = 'Inactive';
|
||||||
|
statusBox.classList.remove('bg-success');
|
||||||
|
statusBox.classList.add('bg-danger');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
updateServerInfo();
|
||||||
|
updateServiceStatuses();
|
||||||
|
setInterval(updateServerInfo, 2000);
|
||||||
|
setInterval(updateServiceStatuses, 10000);
|
||||||
|
|
||||||
|
const toggleIpBtn = document.getElementById('toggle-ip-visibility');
|
||||||
|
const ipAddressesDiv = document.getElementById('ip-addresses');
|
||||||
|
toggleIpBtn.addEventListener('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const isBlurred = ipAddressesDiv.style.filter === 'blur(5px)';
|
||||||
|
ipAddressesDiv.style.filter = isBlurred ? 'none' : 'blur(5px)';
|
||||||
|
toggleIpBtn.querySelector('i').classList.toggle('fa-eye');
|
||||||
|
toggleIpBtn.querySelector('i').classList.toggle('fa-eye-slash');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user