Initial commit: finish basic WebUI interface
This commit is contained in:
140
src/utils/functions.js
Normal file
140
src/utils/functions.js
Normal file
@@ -0,0 +1,140 @@
|
||||
import { postLog } from '../../logger.js';
|
||||
|
||||
function showNotification(message, type = 'info') {
|
||||
const icons = {
|
||||
success: '✓',
|
||||
error: '✕',
|
||||
info: 'ℹ',
|
||||
warning: '⚠'
|
||||
};
|
||||
|
||||
const colors = {
|
||||
success: '#52c41a',
|
||||
error: '#ff4d4f',
|
||||
info: '#1890ff',
|
||||
warning: '#faad14'
|
||||
};
|
||||
|
||||
const notification = document.createElement('div');
|
||||
notification.style.cssText = `
|
||||
position: fixed;
|
||||
top: -60px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: ${colors[type] || colors.info};
|
||||
color: white;
|
||||
padding: 12px 24px;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
z-index: 10000;
|
||||
transition: top 0.3s ease-out;
|
||||
cursor: pointer;
|
||||
`;
|
||||
|
||||
notification.innerHTML = `<span style="font-size: 18px;">${icons[type] || icons.info}</span><span>${message}</span>`;
|
||||
|
||||
document.body.appendChild(notification);
|
||||
|
||||
setTimeout(() => {
|
||||
notification.style.top = '20px';
|
||||
}, 10);
|
||||
|
||||
setTimeout(() => {
|
||||
notification.style.top = '-60px';
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(notification);
|
||||
}, 300);
|
||||
}, 3000);
|
||||
|
||||
notification.addEventListener('click', () => {
|
||||
notification.style.top = '-60px';
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(notification);
|
||||
}, 300);
|
||||
});
|
||||
|
||||
postLog.info(`Notification: ${message} (${type})`);
|
||||
}
|
||||
|
||||
function setCookie(name, value, days = 7) {
|
||||
const expires = new Date();
|
||||
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
|
||||
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
|
||||
postLog.debug(`Cookie set: ${name}`);
|
||||
}
|
||||
|
||||
function getCookie(name) {
|
||||
const nameEQ = name + '=';
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
let cookie = cookies[i];
|
||||
while (cookie.charAt(0) === ' ') {
|
||||
cookie = cookie.substring(1, cookie.length);
|
||||
}
|
||||
if (cookie.indexOf(nameEQ) === 0) {
|
||||
const value = cookie.substring(nameEQ.length, cookie.length);
|
||||
postLog.debug(`Cookie get: ${name}`);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function deleteCookie(name) {
|
||||
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
|
||||
postLog.debug(`Cookie deleted: ${name}`);
|
||||
}
|
||||
|
||||
function formatDate(timestamp) {
|
||||
const date = new Date(timestamp);
|
||||
return date.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
});
|
||||
}
|
||||
|
||||
function formatBytes(bytes) {
|
||||
if (bytes === 0) return '0 B';
|
||||
const k = 1024;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
function highlightText(text, keyword) {
|
||||
if (!keyword) return text;
|
||||
const regex = new RegExp(`(${keyword})`, 'gi');
|
||||
return text.replace(regex, '<mark style="background-color: #fffb8f; padding: 0 2px;">$1</mark>');
|
||||
}
|
||||
|
||||
function debounce(func, wait) {
|
||||
let timeout;
|
||||
return function executedFunction(...args) {
|
||||
const later = () => {
|
||||
clearTimeout(timeout);
|
||||
func(...args);
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
}
|
||||
|
||||
export {
|
||||
showNotification,
|
||||
setCookie,
|
||||
getCookie,
|
||||
deleteCookie,
|
||||
formatDate,
|
||||
formatBytes,
|
||||
highlightText,
|
||||
debounce
|
||||
};
|
||||
Reference in New Issue
Block a user