refactor: posts/show.html 内联 JS 拆分为独立文件
- 拆分 926 行内联 JS 为 5 个独立模块文件 - post-view.js: Vditor 渲染 + 提交审核按钮 (~50 行) - post-energize.js: 赋能按钮 + 弹窗 (~76 行) - post-reactions.js: 赞/踩系统 (~71 行) - post-favorite.js: 收藏按钮 + 收藏夹弹窗 (~218 行) - post-comments.js: 评论系统 (~456 行) - show.html 从 1080 行缩减至 159 行
This commit is contained in:
76
templates/MetaLab-2026/static/js/post-energize.js
Normal file
76
templates/MetaLab-2026/static/js/post-energize.js
Normal file
@ -0,0 +1,76 @@
|
||||
// 赋能按钮 + 弹窗
|
||||
(function() {
|
||||
var btn = document.getElementById('energizeBtn');
|
||||
var overlay = document.getElementById('energizeOverlay');
|
||||
var closeBtn = document.getElementById('energizeClose');
|
||||
var capHint = document.getElementById('energizeCapHint');
|
||||
var lightBtn = document.getElementById('energizeLight');
|
||||
var heavyBtn = document.getElementById('energizeHeavy');
|
||||
if (!btn || !overlay) return;
|
||||
|
||||
var postId = btn.dataset.id;
|
||||
|
||||
function showOverlay() {
|
||||
overlay.style.display = 'flex';
|
||||
}
|
||||
function hideOverlay() {
|
||||
overlay.style.display = 'none';
|
||||
capHint.style.display = 'none';
|
||||
lightBtn.disabled = false;
|
||||
heavyBtn.disabled = false;
|
||||
}
|
||||
|
||||
btn.addEventListener('click', function() {
|
||||
fetch('/api/energy/info')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(d) {
|
||||
if (d.success && d.data && d.data.daily_exp_cap_reached) {
|
||||
capHint.style.display = 'block';
|
||||
}
|
||||
showOverlay();
|
||||
})
|
||||
.catch(function() { showOverlay(); });
|
||||
});
|
||||
|
||||
closeBtn.addEventListener('click', hideOverlay);
|
||||
overlay.addEventListener('click', function(e) {
|
||||
if (e.target === overlay) hideOverlay();
|
||||
});
|
||||
|
||||
function doEnergize(amount) {
|
||||
lightBtn.disabled = true;
|
||||
heavyBtn.disabled = true;
|
||||
|
||||
fetch('/api/energy/energize', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': getCSRFToken()
|
||||
},
|
||||
body: JSON.stringify({ post_id: parseInt(postId), amount: amount })
|
||||
})
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(d) {
|
||||
if (d.success) {
|
||||
alert(d.data.message || '赋能成功!');
|
||||
hideOverlay();
|
||||
} else {
|
||||
alert(d.message || '赋能失败');
|
||||
lightBtn.disabled = false;
|
||||
heavyBtn.disabled = false;
|
||||
}
|
||||
})
|
||||
.catch(function() {
|
||||
alert('请求失败');
|
||||
lightBtn.disabled = false;
|
||||
heavyBtn.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
lightBtn.addEventListener('click', function() {
|
||||
doEnergize(parseInt(lightBtn.dataset.amount));
|
||||
});
|
||||
heavyBtn.addEventListener('click', function() {
|
||||
doEnergize(parseInt(heavyBtn.dataset.amount));
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user