第一次提交
This commit is contained in:
70
static/js/custom.js
Normal file
70
static/js/custom.js
Normal file
@@ -0,0 +1,70 @@
|
||||
// 自定义JavaScript函数
|
||||
|
||||
// 设置前端域名全局变量
|
||||
if (typeof FRONTEND_DOMAIN !== 'undefined') {
|
||||
window.FRONTEND_DOMAIN = FRONTEND_DOMAIN;
|
||||
}
|
||||
|
||||
// 显示加载动画
|
||||
function showLoading() {
|
||||
const loadingElement = document.getElementById('loading');
|
||||
if (loadingElement) {
|
||||
loadingElement.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏加载动画
|
||||
function hideLoading() {
|
||||
const loadingElement = document.getElementById('loading');
|
||||
if (loadingElement) {
|
||||
loadingElement.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
function formatDate(dateString) {
|
||||
if (!dateString) return '-';
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN');
|
||||
}
|
||||
|
||||
// 格式化文件大小
|
||||
function formatFileSize(bytes) {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
// 显示通知 - 统一的消息弹窗函数
|
||||
function showNotification(message, type = 'info') {
|
||||
// 创建通知元素
|
||||
const alertDiv = document.createElement('div');
|
||||
const alertType = type === 'error' ? 'danger' : type;
|
||||
alertDiv.className = `alert alert-${alertType} alert-dismissible fade show`;
|
||||
alertDiv.innerHTML = `
|
||||
${message}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
`;
|
||||
alertDiv.style.marginBottom = '10px';
|
||||
|
||||
// 插入到通知容器中
|
||||
const container = document.getElementById('notification-container') || document.querySelector('main');
|
||||
if (container) {
|
||||
container.insertBefore(alertDiv, container.firstChild);
|
||||
|
||||
// 自动隐藏
|
||||
setTimeout(() => {
|
||||
if (alertDiv.parentNode) {
|
||||
alertDiv.remove();
|
||||
}
|
||||
}, 5000);
|
||||
} else {
|
||||
// 如果找不到容器,使用console输出作为备选方案
|
||||
console.log(`[${type.toUpperCase()}] ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// API请求函数 - 添加认证支持 - REMOVED DUPLICATE
|
||||
// This function is now defined in base.html to avoid duplication
|
||||
134
static/js/pagination.js
Normal file
134
static/js/pagination.js
Normal file
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* 公共分页工具函数
|
||||
*/
|
||||
|
||||
// 渲染分页导航
|
||||
function renderPagination(pagination, pageChangeCallback) {
|
||||
const paginationEl = document.getElementById('pagination');
|
||||
if (!paginationEl) return;
|
||||
|
||||
// 验证分页数据
|
||||
if (!pagination || typeof pagination !== 'object' ||
|
||||
!Number.isFinite(pagination.total_pages) || pagination.total_pages <= 1) {
|
||||
paginationEl.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = '';
|
||||
|
||||
// 上一页
|
||||
if (pagination.has_prev) {
|
||||
html += `<li class="page-item">
|
||||
<a class="page-link" href="#" data-page="${pagination.current_page - 1}">上一页</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
// 页码
|
||||
const startPage = Math.max(1, pagination.current_page - 2);
|
||||
const endPage = Math.min(pagination.total_pages, pagination.current_page + 2);
|
||||
|
||||
if (startPage > 1) {
|
||||
html += `<li class="page-item"><a class="page-link" href="#" data-page="1">1</a></li>`;
|
||||
if (startPage > 2) {
|
||||
html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = startPage; i <= endPage; i++) {
|
||||
html += `<li class="page-item ${i === pagination.current_page ? 'active' : ''}">
|
||||
<a class="page-link" href="#" data-page="${i}">${i}</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
if (endPage < pagination.total_pages) {
|
||||
if (endPage < pagination.total_pages - 1) {
|
||||
html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
|
||||
}
|
||||
html += `<li class="page-item"><a class="page-link" href="#" data-page="${pagination.total_pages}">${pagination.total_pages}</a></li>`;
|
||||
}
|
||||
|
||||
// 下一页
|
||||
if (pagination.has_next) {
|
||||
html += `<li class="page-item">
|
||||
<a class="page-link" href="#" data-page="${pagination.current_page + 1}">下一页</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
paginationEl.innerHTML = html;
|
||||
|
||||
// 绑定分页点击事件
|
||||
paginationEl.querySelectorAll('.page-link').forEach(link => {
|
||||
link.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
const page = parseInt(this.dataset.page);
|
||||
if (page && page !== pagination.current_page) {
|
||||
if (typeof pageChangeCallback === 'function') {
|
||||
pageChangeCallback(page);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 获取URL参数
|
||||
function getUrlParameter(name) {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
return urlParams.get(name);
|
||||
}
|
||||
|
||||
// 设置URL参数
|
||||
function setUrlParameter(param, value) {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
if (value === null || value === '') {
|
||||
urlParams.delete(param);
|
||||
} else {
|
||||
urlParams.set(param, value);
|
||||
}
|
||||
window.history.replaceState({}, '', `${window.location.pathname}?${urlParams.toString()}`);
|
||||
}
|
||||
|
||||
// 批量更新复选框状态
|
||||
function setupBatchSelection(selectAllId, itemCheckboxClass, selectedCountId, batchButtons) {
|
||||
const selectAll = document.getElementById(selectAllId);
|
||||
if (!selectAll) return;
|
||||
|
||||
// 全选/取消全选
|
||||
selectAll.addEventListener('change', function() {
|
||||
const checkboxes = document.querySelectorAll(itemCheckboxClass);
|
||||
checkboxes.forEach(checkbox => {
|
||||
checkbox.checked = this.checked;
|
||||
});
|
||||
updateBatchButtons(selectedCountId, batchButtons);
|
||||
});
|
||||
|
||||
// 单个复选框事件
|
||||
document.querySelectorAll(itemCheckboxClass).forEach(checkbox => {
|
||||
checkbox.addEventListener('change', () => {
|
||||
updateBatchButtons(selectedCountId, batchButtons);
|
||||
});
|
||||
});
|
||||
|
||||
// 初始状态
|
||||
updateBatchButtons(selectedCountId, batchButtons);
|
||||
}
|
||||
|
||||
// 更新批量操作按钮状态
|
||||
function updateBatchButtons(selectedCountId, batchButtons) {
|
||||
const selectedCount = document.querySelectorAll('.license-checkbox:checked, .product-checkbox:checked, .device-checkbox:checked').length;
|
||||
|
||||
if (selectedCountId) {
|
||||
const countElement = document.getElementById(selectedCountId);
|
||||
if (countElement) {
|
||||
countElement.textContent = `已选择 ${selectedCount} 项`;
|
||||
}
|
||||
}
|
||||
|
||||
if (batchButtons) {
|
||||
batchButtons.forEach(buttonId => {
|
||||
const button = document.getElementById(buttonId);
|
||||
if (button) {
|
||||
button.style.display = selectedCount > 0 ? 'inline-block' : 'none';
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user