- fix: 로그인 API에서 user.role_name 필드 올바르게 사용 (auth.service.js) - refactor: navbar 컴포넌트를 최신 dashboard-header 스타일로 전환 - refactor: 구버전 work-report-header 제거 (6개 페이지) - refactor: load-navbar.js를 최신 헤더 구조에 맞게 업데이트 - style: 파란색 그라데이션 헤더, 실시간 시계, 향상된 프로필 메뉴 - docs: 2026-01-20 개발 로그 추가 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
110 lines
3.0 KiB
JavaScript
110 lines
3.0 KiB
JavaScript
// /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];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 네비게이션 바와 관련된 모든 이벤트를 설정합니다.
|
|
*/
|
|
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);
|
|
}
|
|
}); |