// /js/load-navbar.js import { getUser, clearAuthData } from './auth.js'; import { loadComponent } from './component-loader.js'; import { config } from './config.js'; // 역할 이름을 한글로 변환하는 맵 const ROLE_NAMES = { admin: '관리자', system: '시스템 관리자', leader: '그룹장', user: '작업자', support: '지원팀', default: '사용자', }; /** * 네비게이션 바 DOM을 사용자 정보와 역할에 맞게 수정하는 프로세서입니다. * @param {Document} doc - 파싱된 HTML 문서 객체 */ function processNavbarDom(doc) { const currentUser = getUser(); if (!currentUser) return; // 1. 역할 기반 메뉴 필터링 filterMenuByRole(doc, currentUser.role); // 2. 사용자 정보 채우기 populateUserInfo(doc, currentUser); } /** * 사용자 역할에 따라 메뉴 항목을 필터링합니다. * @param {Document} doc - 파싱된 HTML 문서 객체 * @param {string} userRole - 현재 사용자의 역할 */ function filterMenuByRole(doc, userRole) { const selectors = [ { role: 'admin', selector: '.admin-only' }, { role: 'system', selector: '.system-only' }, { role: 'leader', selector: '.leader-only' }, ]; selectors.forEach(({ role, selector }) => { if (userRole !== role && userRole !== 'system') { doc.querySelectorAll(selector).forEach(el => el.remove()); } }); } /** * 네비게이션 바에 사용자 정보를 채웁니다. * @param {Document} doc - 파싱된 HTML 문서 객체 * @param {object} user - 현재 사용자 객체 */ function populateUserInfo(doc, user) { const displayName = user.name || user.username; const roleName = ROLE_NAMES[user.role] || ROLE_NAMES.default; const elements = { 'userName': displayName, 'userRole': roleName, 'userInitial': displayName.charAt(0), }; for (const id in elements) { const el = doc.getElementById(id); if (el) el.textContent = elements[id]; } // 메인 대시보드 URL 설정 (그룹장 대시보드가 메인) const dashboardBtn = doc.getElementById('dashboardBtn'); if (dashboardBtn) { dashboardBtn.href = '/pages/dashboard/group-leader.html'; } } /** * 네비게이션 바와 관련된 모든 이벤트를 설정합니다. */ function setupNavbarEvents() { const logoutButton = document.getElementById('logoutBtn'); if (logoutButton) { logoutButton.addEventListener('click', () => { if (confirm('로그아웃 하시겠습니까?')) { clearAuthData(); window.location.href = config.paths.loginPage; } }); } } /** * 현재 시간을 업데이트하는 함수 */ function updateTime() { const timeElement = document.getElementById('timeValue'); if (timeElement) { const now = new Date(); timeElement.textContent = now.toLocaleTimeString('ko-KR', { hour12: false }); } } // 메인 로직: DOMContentLoaded 시 실행 document.addEventListener('DOMContentLoaded', async () => { if (getUser()) { // 1. 컴포넌트 로드 및 DOM 수정 await loadComponent('navbar', '#navbar-container', processNavbarDom); // 2. DOM에 삽입된 후에 이벤트 리스너 설정 setupNavbarEvents(); // 3. 실시간 시간 업데이트 시작 updateTime(); setInterval(updateTime, 1000); } });