This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/templates/MetaLab-2026/static/js/post-favorite.js
Victor_Jay 6c9a4414cb refactor: 全局移除浏览器 alert 弹窗,统一使用页面内 showToast 通知
- 替换全部 11 个文件中 36 处 alert() 为 showToast()
- API 错误/异常使用 showToast(msg, 'error'),3秒自动消失
- 表单校验提示使用 showToast(msg, 'warning'),3秒自动消失
- 成功确认使用 showToast(msg, 'success'),3秒自动消失
- 移除 post-energize.js 中与全局 showToast 冲突的本地实现
- 删除 posts.css 中已失去引用的 .energize-toast 样式
- 保留 showConfirm() 自定义确认弹窗(需用户确认的操作)
2026-06-03 01:25:12 +08:00

227 lines
8.7 KiB
JavaScript

// 收藏按钮(弹窗选择收藏夹)
(function() {
var favBtn = document.getElementById('favBtn');
var favCountEl = document.getElementById('favCount');
var container = document.getElementById('postFloatBar');
var overlay = document.getElementById('favOverlay');
var closeBtn = document.getElementById('favClose');
var folderListEl = document.getElementById('favFolderCheckList');
var newInput = document.getElementById('favNewInput');
var newBtn = document.getElementById('favNewConfirm');
if (!favBtn || !container) return;
var postId = container.dataset.postId;
var isFavorited = false;
var currentFolderId = null;
var loading = false;
function updateBtn(favorited) {
isFavorited = favorited;
if (favorited) {
favBtn.classList.add('active');
} else {
favBtn.classList.remove('active');
currentFolderId = null;
}
}
function loadStatus() {
return fetch('/api/posts/' + postId + '/folder-status')
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success && d.data && d.data.favorited) {
currentFolderId = d.data.folder_id;
updateBtn(true);
}
})
.catch(function() {});
}
function renderFolderList(folders) {
if (!folders || folders.length === 0) {
folderListEl.innerHTML = '<div class="fav-empty">暂无收藏夹,请在下方创建</div>';
return;
}
var html = '';
folders.forEach(function(f) {
var isActive = currentFolderId === f.id;
html += '<div class="fav-folder-option' + (isActive ? ' active' : '') + '" data-id="' + f.id + '">'
+ '<span class="fav-folder-opt-name">' + escapeHtml(f.name) + (f.is_default ? ' <small>(默认)</small>' : '') + '</span>'
+ (isActive ? '<span class="fav-active-tag">已收藏</span>' : '')
+ '<span class="fav-folder-opt-count">' + (f.item_count || 0) + ' 篇</span>'
+ '</div>';
});
folderListEl.innerHTML = html;
}
function showModal() {
if (!overlay) { handleNoModal(); return; }
loading = true;
overlay.style.display = 'flex';
folderListEl.innerHTML = '<div class="fav-loading">加载中...</div>';
api('GET', '/api/folders')
.then(function(d) {
var folders = (d.success && d.data && d.data.folders) ? d.data.folders : [];
renderFolderList(folders);
loading = false;
})
.catch(function() {
folderListEl.innerHTML = '<div class="fav-loading fav-error">加载失败,请重试</div>';
loading = false;
});
}
function hideModal() {
if (overlay) overlay.style.display = 'none';
loading = false;
}
function handleNoModal() {
if (!getCSRFToken()) {
window.location.href = '/auth/login?redirect=/posts/' + postId;
return;
}
favBtn.disabled = true;
api('POST', '/api/posts/' + postId + '/favorite')
.then(function(d) {
favBtn.disabled = false;
if (d.success) {
var f = d.data && d.data.favorited;
updateBtn(f);
if (favCountEl && d.data && typeof d.data.favorites_count === 'number') {
favCountEl.textContent = d.data.favorites_count;
} else if (favCountEl) {
var cur = parseInt(favCountEl.textContent) || 0;
favCountEl.textContent = f ? cur + 1 : Math.max(0, cur - 1);
}
}
})
.catch(function() { favBtn.disabled = false; });
}
function addToFolder(folderId) {
if (loading) return;
loading = true;
api('POST', '/api/folders/' + folderId + '/posts', { post_id: parseInt(postId) })
.then(function(d) {
loading = false;
if (d.success) {
currentFolderId = folderId;
updateBtn(true);
if (favCountEl) favCountEl.textContent = (parseInt(favCountEl.textContent) || 0) + 1;
hideModal();
} else if (d.message && d.message.indexOf('已在此收藏夹中') !== -1) {
// 后端返回已收藏,直接关闭弹窗(兜底)
currentFolderId = folderId;
updateBtn(true);
hideModal();
} else {
showToast(d.message || '操作失败', 'error');
}
})
.catch(function() { loading = false; });
}
function removeFromFolder(folderId) {
if (loading) return;
showConfirm('确认取消收藏', '确定移出该收藏夹吗?').then(function(ok) {
if (!ok) return;
loading = true;
api('DELETE', '/api/folders/' + folderId + '/posts/' + postId)
.then(function(d) {
loading = false;
if (d.success) {
currentFolderId = null;
updateBtn(false);
if (favCountEl) favCountEl.textContent = Math.max(0, (parseInt(favCountEl.textContent) || 0) - 1);
hideModal();
} else {
showToast(d.message || '操作失败', 'error');
}
})
.catch(function() { loading = false; });
});
}
function createAndAdd(name) {
if (loading) return;
loading = true;
newBtn.disabled = true;
newBtn.textContent = '创建中...';
api('POST', '/api/folders', { name: name, description: '', is_public: false })
.then(function(d) {
if (!d.success) {
loading = false;
newBtn.disabled = false;
newBtn.textContent = '创建并收藏';
showToast(d.message || '创建失败', 'error');
return;
}
var newId = d.data && d.data.id;
return api('POST', '/api/folders/' + newId + '/posts', { post_id: parseInt(postId) })
.then(function(d2) {
loading = false;
newBtn.disabled = false;
newBtn.textContent = '创建并收藏';
if (d2.success) {
currentFolderId = newId;
updateBtn(true);
if (favCountEl) favCountEl.textContent = (parseInt(favCountEl.textContent) || 0) + 1;
hideModal();
} else {
showToast(d2.message || '操作失败', 'error');
}
});
})
.catch(function() {
loading = false;
newBtn.disabled = false;
newBtn.textContent = '创建并收藏';
});
}
// 事件:点击收藏按钮 → 打开弹窗
favBtn.addEventListener('click', function() {
if (!getCSRFToken()) { window.location.href = '/auth/login?redirect=/posts/' + postId; return; }
showModal();
});
// 事件:关闭弹窗
if (closeBtn) closeBtn.addEventListener('click', hideModal);
if (overlay) overlay.addEventListener('click', function(e) {
if (e.target === overlay) hideModal();
});
// 事件:点击收藏夹选项
if (folderListEl) {
folderListEl.addEventListener('click', function(e) {
var option = e.target.closest('.fav-folder-option');
if (!option) return;
var folderId = parseInt(option.getAttribute('data-id'));
if (isNaN(folderId)) return;
if (option.classList.contains('active')) {
removeFromFolder(folderId);
} else {
addToFolder(folderId);
}
});
}
// 事件:新建收藏夹
if (newBtn && newInput) {
newBtn.addEventListener('click', function() {
var name = newInput.value.trim();
if (!name) { showToast('请输入收藏夹名称', 'warning'); return; }
if (name.length > 50) { showToast('收藏夹名称不能超过 50 个字符', 'warning'); return; }
createAndAdd(name);
});
newInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter') newBtn.click();
});
}
// 初始化
loadStatus();
})();