168 lines
6.0 KiB
HTML
168 lines
6.0 KiB
HTML
{% extends "base.html" %}
|
|
{% block title %}话题 - 论坛{% endblock %}
|
|
{% block content %}
|
|
<div class="page-title" id="topic-header">
|
|
<h1 id="topic-title"></h1>
|
|
<p id="topic-meta"></p>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="prose" id="topic-content"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<span>全部回复 (<span id="reply-count">0</span>)</span>
|
|
</div>
|
|
<div class="card-body" style="padding:12px 20px;" id="replies-container"></div>
|
|
<div id="replyPagination" style="display:none;margin-top:12px;"></div>
|
|
</div>
|
|
|
|
{% if current_user %}
|
|
<div class="card">
|
|
<div class="card-header"><span>发表回复</span></div>
|
|
<div class="card-body">
|
|
<p style="font-size:13px;color:var(--text-light);margin-bottom:12px;">支持 Markdown 语法</p>
|
|
<form id="reply-form">
|
|
<div class="form-group">
|
|
<textarea name="content" class="form-control" placeholder="写下你的回复...(支持 Markdown 语法)" required></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">发表回复</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
{% else %}
|
|
<div class="card">
|
|
<div class="card-body" style="text-align:center;">
|
|
<p style="color:var(--text-light);">请<a href="/login/">登录</a>后发表回复</p>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
|
|
<a href="/forum/" class="btn">← 返回论坛</a>
|
|
|
|
<script>
|
|
function renderReplies(data) {
|
|
var container = document.getElementById('replies-container');
|
|
if (data.replies.length === 0) {
|
|
container.innerHTML = '<div class="empty-state"><p>暂无回复</p></div>';
|
|
document.getElementById('replyPagination').style.display = 'none';
|
|
return;
|
|
}
|
|
container.innerHTML = data.replies.map(function(r, i) {
|
|
var num = (data.page - 1) * 20 + i + 1;
|
|
return '<div class="reply-item">' +
|
|
'<div class="meta"><strong>' + r.author_name + '</strong> <span>#' + num + '</span> <span>' + r.created_at + '</span></div>' +
|
|
'<div class="content">' + r.content_html + '</div></div>';
|
|
}).join('');
|
|
|
|
var pg = document.getElementById('replyPagination');
|
|
if (data.total_pages <= 1) { pg.style.display = 'none'; return; }
|
|
pg.style.display = 'block';
|
|
var html = '<div class="pagination">';
|
|
if (data.has_prev) html += '<a href="javascript:void(0)" onclick="loadRepliesPage(' + data.prev_page + ')">« 上一页</a>';
|
|
else html += '<span class="disabled">« 上一页</span>';
|
|
for (var p = 1; p <= data.total_pages; p++) {
|
|
if (p === data.page) html += '<span class="active">' + p + '</span>';
|
|
else html += '<a href="javascript:void(0)" onclick="loadRepliesPage(' + p + ')">' + p + '</a>';
|
|
}
|
|
if (data.has_next) html += '<a href="javascript:void(0)" onclick="loadRepliesPage(' + data.next_page + ')">下一页 »</a>';
|
|
else html += '<span class="disabled">下一页 »</span>';
|
|
html += '</div>';
|
|
pg.innerHTML = html;
|
|
}
|
|
|
|
function loadRepliesPage(page) {
|
|
var id = window.location.pathname.match(/\/forum\/(\d+)\//)[1];
|
|
fetch('/api/forum/' + id + '/replies/?page=' + page)
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) {
|
|
if (!data.success) return;
|
|
renderReplies(data);
|
|
});
|
|
}
|
|
|
|
(async function() {
|
|
var id = window.location.pathname.match(/\/forum\/(\d+)\//)[1];
|
|
var res = await fetch('/api/forum/' + id + '/');
|
|
var data = await res.json();
|
|
if (!data.success) return;
|
|
|
|
var t = data.topic;
|
|
document.getElementById('topic-title').textContent = t.title;
|
|
document.getElementById('topic-meta').innerHTML =
|
|
'<span class="badge badge-info">' + t.category + '</span>' +
|
|
' 由 ' + t.author_name + ' 发布于 ' + t.created_at +
|
|
' · ' + t.view_count + ' 次浏览 · ' + t.reply_count + ' 条回复' +
|
|
' · <a href="javascript:void(0)" id="fav-btn" class="' + (t.is_favorited ? 'favorited' : '') + '" onclick="toggleFav(' + id + ', \'forum\')">' + (t.is_favorited ? '⭐ 已收藏' : '☆ 收藏') + '</a>';
|
|
document.getElementById('topic-content').innerHTML = t.content_html;
|
|
|
|
var rc = document.getElementById('reply-count');
|
|
rc.textContent = data.reply_total;
|
|
|
|
renderReplies({
|
|
replies: data.replies,
|
|
page: data.reply_page,
|
|
total_pages: data.reply_total_pages,
|
|
has_prev: data.reply_has_prev,
|
|
has_next: data.reply_has_next,
|
|
prev_page: data.reply_prev_page,
|
|
next_page: data.reply_next_page,
|
|
total: data.reply_total,
|
|
});
|
|
|
|
var form = document.getElementById('reply-form');
|
|
if (form) {
|
|
form.addEventListener('submit', async function(e) {
|
|
e.preventDefault();
|
|
var fd = new FormData(form);
|
|
fd.append('csrf_token', CSRF_TOKEN);
|
|
var r = await fetch('/forum/' + id + '/reply/', { method: 'POST', body: fd });
|
|
if (r.redirected) { window.location.reload(); return; }
|
|
var j = await r.json();
|
|
if (j.success) window.location.reload();
|
|
});
|
|
}
|
|
})();
|
|
</script>
|
|
<script>
|
|
function toggleFav(id, type) {
|
|
var btn = document.getElementById('fav-btn');
|
|
if (btn.classList.contains('favorited')) {
|
|
// 需要先查 favorite_id
|
|
fetch('/api/favorites/check/?type=' + type + '&id=' + id)
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) {
|
|
if (data.is_favorited && data.favorite_id) {
|
|
var fd = new FormData();
|
|
fd.append('csrf_token', CSRF_TOKEN);
|
|
fetch('/api/favorites/' + data.favorite_id + '/remove/', { method: 'POST', body: fd })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(d) {
|
|
if (d.success) {
|
|
btn.classList.remove('favorited');
|
|
btn.innerHTML = '☆ 收藏';
|
|
}
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
var fd = new FormData();
|
|
fd.append('csrf_token', CSRF_TOKEN);
|
|
fd.append('target_type', type);
|
|
fd.append('target_id', id);
|
|
fetch('/api/favorites/add/', { method: 'POST', body: fd })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) {
|
|
if (data.success) {
|
|
btn.classList.add('favorited');
|
|
btn.innerHTML = '⭐ 已收藏';
|
|
}
|
|
});
|
|
}
|
|
}
|
|
</script>
|
|
{% endblock %}
|