/** * 테마 분석 페이지 * - 테마 목록 조회 (ka90001) * - 테마 구성종목 조회 (ka90002) */ (function () { let currentDate = '1'; let currentSort = '3'; // 3=등락률순, 1=기간수익률순 let selectedCode = null; let selectedName = null; let allThemes = []; let clientSortCol = null; // 헤더 클릭 정렬 컬럼 (null=서버 순서 유지) let clientSortDesc = true; const listEl = document.getElementById('themeList'); const countEl = document.getElementById('themeCount'); const searchEl = document.getElementById('themeSearch'); const emptyEl = document.getElementById('themeDetailEmpty'); const contentEl = document.getElementById('themeDetailContent'); const loadingEl = document.getElementById('themeDetailLoading'); const nameEl = document.getElementById('detailThemeName'); const fluRtEl = document.getElementById('detailFluRt'); const periodRtEl = document.getElementById('detailPeriodRt'); const stockListEl = document.getElementById('detailStockList'); // ── 포맷 유틸 ──────────────────────────────────────────────── function fmtRate(f) { if (f == null) return '-'; const sign = f >= 0 ? '+' : ''; return sign + f.toFixed(2) + '%'; } function fmtNum(n) { if (n == null) return '-'; return Math.abs(n).toLocaleString('ko-KR'); } function rateClass(f) { if (f > 0) return 'text-red-500'; if (f < 0) return 'text-blue-500'; return 'text-gray-500'; } function rateBg(f) { if (f > 0) return 'bg-red-50 text-red-500'; if (f < 0) return 'bg-blue-50 text-blue-500'; return 'bg-gray-100 text-gray-500'; } // ── 테마 목록 행 렌더 ──────────────────────────────────────── function makeRow(t, rank) { const fluCls = rateClass(t.fluRt); const periodCls = t.periodRt >= 0 ? 'text-purple-600' : 'text-blue-500'; const isSelected = t.code === selectedCode; return `
${s.name}
${s.code}
${fmtNum(s.curPrc)}원
${sign}${fmtRate(s.fluRt)}구성종목을 불러오지 못했습니다.
'; console.error('테마 구성종목 조회 실패:', e); } } // ── 날짜 탭 이벤트 ─────────────────────────────────────────── document.querySelectorAll('.date-tab').forEach(btn => { btn.addEventListener('click', () => { currentDate = btn.dataset.date; document.querySelectorAll('.date-tab').forEach(b => { b.className = b.dataset.date === currentDate ? 'date-tab px-3 py-1.5 bg-blue-500 text-white text-xs font-medium' : 'date-tab px-3 py-1.5 bg-white text-gray-600 hover:bg-gray-50 border-l border-gray-200 text-xs font-medium'; }); loadThemes(); }); }); // ── 정렬 탭 이벤트 (서버 사이드) ───────────────────────────── document.querySelectorAll('.sort-tab').forEach(btn => { btn.addEventListener('click', () => { currentSort = btn.dataset.sort; // 헤더 클라이언트 정렬 초기화 (서버 순서 우선) clientSortCol = null; document.querySelectorAll('.sort-tab').forEach(b => { b.className = b.dataset.sort === currentSort ? 'sort-tab px-3 py-1.5 bg-blue-500 text-white text-xs font-medium' : 'sort-tab px-3 py-1.5 bg-white text-gray-600 hover:bg-gray-50 border-l border-gray-200 text-xs font-medium'; }); updateThemeColHeaders(); loadThemes(); }); }); // ── 헤더 컬럼 클릭 정렬 ────────────────────────────────────── document.querySelectorAll('.theme-col-sort').forEach(el => { el.addEventListener('click', () => { const col = el.dataset.col; if (clientSortCol === col) { clientSortDesc = !clientSortDesc; } else { clientSortCol = col; clientSortDesc = true; } updateThemeColHeaders(); renderList(allThemes); }); }); // ── 검색 필터 이벤트 ───────────────────────────────────────── searchEl.addEventListener('input', () => renderList(allThemes)); // ── 초기 로드 ──────────────────────────────────────────────── loadThemes(); })();