274 lines
11 KiB
HTML
274 lines
11 KiB
HTML
{% extends "admin/base.html" %}
|
|
{% block title %}{{ verbose_name }} - LiteAdmin{% endblock %}
|
|
{% block breadcrumb %}<a href="/admin/">控制台</a> / <span>{{ verbose_name }}</span>{% endblock %}
|
|
{% block content %}
|
|
<div class="page-header">
|
|
<h2>{{ verbose_name }}</h2>
|
|
<div>
|
|
{% if can_add %}
|
|
<a href="/admin/{{ model_name }}/add/" class="btn btn-primary">+ 新增</a>
|
|
{% endif %}
|
|
{% if show_tag_manager %}
|
|
<button class="btn" onclick="showTagManager('{{ model_name }}')">🏷️ 标签管理</button>
|
|
{% endif %}
|
|
{% if can_import %}
|
|
<a href="/admin/user/import/" class="btn">📥 批量导入</a>
|
|
{% endif %}
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
{% if search_fields %}
|
|
<div class="card-body" style="padding-bottom: 0;">
|
|
<div class="search-box" style="display:flex;gap:8px;">
|
|
<input type="text" id="searchInput" class="form-control" placeholder="搜索..." value="{{ search_query or '' }}">
|
|
<button class="btn btn-primary" onclick="doSearch()">搜索</button>
|
|
{% if search_query %}<a href="/admin/{{ model_name }}/" class="btn">清除</a>{% endif %}
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
<div class="card-body">
|
|
<div id="listTableWrap">
|
|
<div class="empty-state"><div class="icon">📄</div><p>加载中...</p></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 标签管理弹窗 -->
|
|
<div id="tagManagerModal" style="display:none; position:fixed; top:0; left:0; right:0; bottom:0; background:rgba(0,0,0,0.5); z-index:999; align-items:center; justify-content:center;">
|
|
<div style="background:#fff; border-radius:8px; width:500px; max-width:90%; max-height:80vh; padding:24px; overflow-y:auto;">
|
|
<h3 id="tagManagerTitle" style="margin-bottom:16px;">标签管理</h3>
|
|
<div id="tagList"></div>
|
|
<div style="margin-top:16px; padding-top:16px; border-top:1px solid var(--border);">
|
|
<h4 style="margin-bottom:8px;">新增标签</h4>
|
|
<div style="display:flex; gap:8px;">
|
|
<input type="text" id="newTagName" class="form-control" placeholder="标签名称" style="flex:1;">
|
|
<button class="btn btn-primary btn-sm" onclick="addTag()">添加</button>
|
|
</div>
|
|
</div>
|
|
<div style="display:flex; gap:8px; justify-content:flex-end; margin-top:16px;">
|
|
<button type="button" class="btn" onclick="document.getElementById('tagManagerModal').style.display='none'">关闭</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
var MODEL_NAME = '{{ model_name }}';
|
|
var SEARCH_FIELDS = {% if search_fields %}true{% else %}false{% endif %};
|
|
var CURRENT_PAGE = 1;
|
|
var SEARCH_QUERY = '{{ search_query or '' }}';
|
|
|
|
function doSearch() {
|
|
SEARCH_QUERY = document.getElementById('searchInput').value.trim();
|
|
CURRENT_PAGE = 1;
|
|
loadList();
|
|
}
|
|
|
|
function loadList() {
|
|
var url = '/api/admin/' + MODEL_NAME + '/?page=' + CURRENT_PAGE;
|
|
if (SEARCH_QUERY) url += '&q=' + encodeURIComponent(SEARCH_QUERY);
|
|
|
|
var wrap = document.getElementById('listTableWrap');
|
|
wrap.innerHTML = '<div class="empty-state"><div class="icon">📄</div><p>加载中...</p></div>';
|
|
|
|
fetch(url)
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(res) {
|
|
if (res.error) {
|
|
wrap.innerHTML = '<div class="empty-state"><p style="color:var(--danger);">' + res.error + '</p></div>';
|
|
return;
|
|
}
|
|
renderTable(res, wrap);
|
|
});
|
|
}
|
|
|
|
function renderTable(res, wrap) {
|
|
if (!res.items || res.items.length === 0) {
|
|
wrap.innerHTML = '<div class="empty-state"><div class="icon">📄</div><p>暂无数据</p></div>';
|
|
return;
|
|
}
|
|
|
|
var keys = Object.keys(res.items[0]);
|
|
var html = '<div class="table-wrap"><table><thead><tr>';
|
|
for (var i = 0; i < keys.length; i++) {
|
|
if (keys[i] === 'id') continue;
|
|
html += '<th>' + keys[i] + '</th>';
|
|
}
|
|
html += '<th style="width:120px;">操作</th>';
|
|
html += '</tr></thead><tbody>';
|
|
|
|
res.items.forEach(function(item) {
|
|
html += '<tr>';
|
|
var isFirst = true;
|
|
for (var key in item) {
|
|
if (key === 'id') continue;
|
|
html += '<td>';
|
|
if (isFirst) {
|
|
html += '<a href="/admin/' + res.model_name + '/' + item.id + '/">' + (item[key] || '-') + '</a>';
|
|
isFirst = false;
|
|
} else {
|
|
html += item[key] || '-';
|
|
}
|
|
html += '</td>';
|
|
}
|
|
html += '<td style="white-space:nowrap;">';
|
|
html += '<a href="/admin/' + res.model_name + '/' + item.id + '/" class="btn btn-sm">查看</a>';
|
|
if (res.can_edit) {
|
|
html += '<a href="/admin/' + res.model_name + '/' + item.id + '/edit/" class="btn btn-sm">编辑</a>';
|
|
}
|
|
html += '</td>';
|
|
html += '</tr>';
|
|
});
|
|
html += '</tbody></table></div>';
|
|
|
|
if (res.total_pages > 1) {
|
|
html += '<div class="pagination">';
|
|
if (res.has_prev) {
|
|
html += '<a href="javascript:void(0)" onclick="goPage(' + res.prev_page + ')">« 上一页</a>';
|
|
} else {
|
|
html += '<span class="disabled">« 上一页</span>';
|
|
}
|
|
for (var p = 1; p <= res.total_pages; p++) {
|
|
if (p === res.page) {
|
|
html += '<span class="active">' + p + '</span>';
|
|
} else if (p <= 3 || p > res.total_pages - 2 || (p >= res.page - 2 && p <= res.page + 2)) {
|
|
html += '<a href="javascript:void(0)" onclick="goPage(' + p + ')">' + p + '</a>';
|
|
} else if (p === 4 || p === res.total_pages - 2) {
|
|
html += '<span>...</span>';
|
|
}
|
|
}
|
|
if (res.has_next) {
|
|
html += '<a href="javascript:void(0)" onclick="goPage(' + res.next_page + ')">下一页 »</a>';
|
|
} else {
|
|
html += '<span class="disabled">下一页 »</span>';
|
|
}
|
|
html += '</div>';
|
|
html += '<div style="text-align:center;margin-top:8px;font-size:13px;color:var(--text-light);">共 ' + res.total + ' 条,第 ' + res.page + '/' + res.total_pages + ' 页</div>';
|
|
}
|
|
|
|
wrap.innerHTML = html;
|
|
}
|
|
|
|
function goPage(page) {
|
|
CURRENT_PAGE = page;
|
|
loadList();
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
loadList();
|
|
});
|
|
|
|
document.addEventListener('keydown', function(e) {
|
|
if (e.key === 'Enter' && document.activeElement === document.getElementById('searchInput')) {
|
|
doSearch();
|
|
}
|
|
});
|
|
|
|
var currentTagModel = '';
|
|
|
|
function showTagManager(modelName) {
|
|
currentTagModel = modelName;
|
|
var titleMap = {'forumtopic': '论坛分类标签', 'knowledgearticle': '知识库分类标签'};
|
|
document.getElementById('tagManagerTitle').textContent = titleMap[modelName] || '标签管理';
|
|
document.getElementById('tagManagerModal').style.display = 'flex';
|
|
loadTags();
|
|
}
|
|
|
|
function loadTags() {
|
|
var list = document.getElementById('tagList');
|
|
list.innerHTML = '<div style="text-align:center;padding:20px;color:var(--text-light);">加载中...</div>';
|
|
|
|
fetch('/admin/' + currentTagModel + '/tags/')
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(res) {
|
|
if (res.error) {
|
|
list.innerHTML = '<div style="text-align:center;padding:20px;color:var(--danger);">' + res.error + '</div>';
|
|
return;
|
|
}
|
|
if (res.tags.length === 0) {
|
|
list.innerHTML = '<div class="empty-state">暂无标签</div>';
|
|
return;
|
|
}
|
|
var html = '<table style="width:100%;border-collapse:collapse;">';
|
|
html += '<thead><tr style="background:#f8fafc;border-bottom:2px solid var(--border);">';
|
|
html += '<th style="padding:8px 12px;text-align:left;font-size:13px;">标签名称</th>';
|
|
html += '<th style="padding:8px 12px;text-align:center;font-size:13px;width:120px;">操作</th>';
|
|
html += '</tr></thead><tbody>';
|
|
res.tags.forEach(function(tag) {
|
|
html += '<tr id="tag-row-' + tag.id + '" style="border-bottom:1px solid var(--border);">';
|
|
html += '<td style="padding:8px 12px;">';
|
|
html += '<span id="tag-name-' + tag.id + '">' + tag.name + '</span>';
|
|
html += '<div id="tag-name-edit-' + tag.id + '" style="display:none;"><input type="text" class="form-control" value="' + tag.name + '" style="font-size:13px;padding:4px 8px;"></div>';
|
|
html += '</td>';
|
|
html += '<td style="padding:8px 12px;text-align:center;white-space:nowrap;">';
|
|
html += '<button class="btn btn-sm" id="tag-edit-btn-' + tag.id + '" onclick="editTag(' + tag.id + ')">编辑</button>';
|
|
html += '<button class="btn btn-sm btn-primary" id="tag-save-btn-' + tag.id + '" onclick="saveTag(' + tag.id + ')" style="display:none;">保存</button>';
|
|
html += '<button class="btn btn-sm" id="tag-cancel-btn-' + tag.id + '" onclick="cancelEditTag(' + tag.id + ')" style="display:none;">取消</button>';
|
|
html += '<button class="btn btn-sm btn-danger" onclick="deleteTag(' + tag.id + ')">删除</button>';
|
|
html += '</td>';
|
|
html += '</tr>';
|
|
});
|
|
html += '</tbody></table>';
|
|
list.innerHTML = html;
|
|
});
|
|
}
|
|
|
|
function editTag(id) {
|
|
document.getElementById('tag-name-' + id).style.display = 'none';
|
|
document.getElementById('tag-name-edit-' + id).style.display = 'block';
|
|
document.getElementById('tag-edit-btn-' + id).style.display = 'none';
|
|
document.getElementById('tag-save-btn-' + id).style.display = 'inline-block';
|
|
document.getElementById('tag-cancel-btn-' + id).style.display = 'inline-block';
|
|
}
|
|
|
|
function cancelEditTag(id) {
|
|
document.getElementById('tag-name-' + id).style.display = 'block';
|
|
document.getElementById('tag-name-edit-' + id).style.display = 'none';
|
|
document.getElementById('tag-edit-btn-' + id).style.display = 'inline-block';
|
|
document.getElementById('tag-save-btn-' + id).style.display = 'none';
|
|
document.getElementById('tag-cancel-btn-' + id).style.display = 'none';
|
|
}
|
|
|
|
function saveTag(id) {
|
|
var name = document.querySelector('#tag-name-edit-' + id + ' input').value.trim();
|
|
if (!name) { alert('标签名称不能为空'); return; }
|
|
|
|
var data = new FormData();
|
|
data.append('name', name);
|
|
|
|
fetch('/admin/' + currentTagModel + '/tags/' + id + '/edit/', { method: 'POST', body: data })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(res) {
|
|
if (res.error) { alert(res.error); return; }
|
|
document.getElementById('tag-name-' + id).textContent = name;
|
|
cancelEditTag(id);
|
|
});
|
|
}
|
|
|
|
function deleteTag(id) {
|
|
if (!confirm('确定要删除这个标签吗?')) return;
|
|
fetch('/admin/' + currentTagModel + '/tags/' + id + '/delete/', { method: 'POST' })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(res) {
|
|
if (res.error) { alert(res.error); return; }
|
|
loadTags();
|
|
});
|
|
}
|
|
|
|
function addTag() {
|
|
var name = document.getElementById('newTagName').value.trim();
|
|
if (!name) { alert('标签名称不能为空'); return; }
|
|
|
|
var data = new FormData();
|
|
data.append('name', name);
|
|
|
|
fetch('/admin/' + currentTagModel + '/tags/add/', { method: 'POST', body: data })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(res) {
|
|
if (res.error) { alert(res.error); return; }
|
|
document.getElementById('newTagName').value = '';
|
|
loadTags();
|
|
});
|
|
}
|
|
</script>
|
|
{% endblock %} |