Files
Linux-book/03.数据库/03.数据库开发项目代码示例/01.笔记网站/js/common.js
T
2026-08-12 14:20:36 +08:00

62 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ==================== Shared Utilities ==================== */
/* URL parameter helper */
function getUrlParam(name) {
const url = new URL(window.location.href);
return url.searchParams.get(name);
}
/* Toast notification */
function showToast(msg, type) {
type = type || 'info';
var icons = { success: '✅', error: '❌', info: '️' };
var container = document.getElementById('toastContainer');
if (!container) { container = document.createElement('div'); container.id = 'toastContainer'; container.className = 'toast-container'; document.body.appendChild(container); }
var t = document.createElement('div');
t.className = 'toast toast-' + type;
t.innerHTML = '<span>' + (icons[type] || '') + '</span> ' + msg;
container.appendChild(t);
setTimeout(function () { t.remove(); }, 3000);
}
/* Render shared navbar */
function renderNavbar() {
var nav = document.getElementById('navbarActions');
if (!nav) return;
nav.innerHTML =
'<a href="index.html" class="btn btn-ghost">🏠 首页</a>' +
'<a href="admin.html" class="btn btn-outline btn-sm">⚙️ 后台管理</a>' +
'<a href="register.html" class="btn btn-ghost btn-sm">✍️ 注册</a>' +
'<a href="login.html" class="btn btn-primary btn-sm">🔑 登录</a>';
}
/* Category badge helper */
function categoryBadge(cat) {
if (!cat) return '';
return '<span class="note-category" style="background:' + cat.color + '15;color:' + cat.color + '">' + cat.icon + ' ' + cat.name + '</span>';
}
/* Empty state helper */
function emptyState(icon, text) {
return '<div class="empty-state"><div class="empty-icon">' + icon + '</div><p>' + text + '</p></div>';
}
/* Modal helpers */
function renderModal(title, content) {
var div = document.createElement('div');
div.className = 'modal-overlay';
div.onclick = function (e) { if (e.target === div) closeModal(); };
div.innerHTML = '<div class="form-modal" onclick="event.stopPropagation()"><h3>' + title + '</h3>' + content + '</div>';
document.body.appendChild(div);
}
function closeModal() {
var o = document.querySelector('.modal-overlay');
if (o) o.remove();
}
/* ==================== Bootstrap on every page ==================== */
document.addEventListener('DOMContentLoaded', function () {
renderNavbar();
});