Files
sori.studio/lib/navigation-editor-tree.js
zenn ca1e17890b 메뉴 관리 개편, 추천 사이트·1뎁스 네비, 우측 Recommended 연동(v1.1.13)
상단 네비는 평면 테이블·드래그로 편집하고 한 단계 하위만 허용한다.
추천 사이트 탭·location recommended·공개 API와 우측 사이드 카드·파비콘 URL 유틸을 추가한다.
문서·배포 마이그레이션 안내·관리자 레이아웃·설정 화면 등 누적 변경을 반영한다.
2026-05-15 14:20:27 +09:00

68 lines
1.9 KiB
JavaScript

/**
* 관리자 UI용: 동일 location 항목을 부모 기준 트리 래퍼로 만든다(항목 객체는 원본 참조).
* @param {Array<Object>} flat - 전체 항목
* @param {'primary'|'footer'} location - 위치
* @returns {Array<{ item: Object, children: Array<{ item: Object, children: any[] }> }>}
*/
export const buildNavigationEditorTree = (flat, location) => {
const filtered = flat.filter((i) => i.location === location)
const nodeMap = new Map()
for (const item of filtered) {
nodeMap.set(String(item.id).trim(), { item, children: [] })
}
const roots = []
for (const item of filtered) {
const id = String(item.id).trim()
const wrap = nodeMap.get(id)
const p = item.parentId
if (p != null && String(p).trim() !== '') {
const pid = String(p).trim()
if (nodeMap.has(pid)) {
nodeMap.get(pid).children.push(wrap)
continue
}
}
roots.push(wrap)
}
/**
* @param {Array<{ item: Object, children: any[] }>} nodes - 노드
* @returns {void}
*/
const sortRec = (nodes) => {
nodes.sort((a, b) => (a.item.sortOrder || 0) - (b.item.sortOrder || 0))
for (const n of nodes) {
sortRec(n.children)
}
}
sortRec(roots)
return roots
}
/**
* 관리자 상단 네비 트리 래퍼를 표시·드래그용 평면 행 배열로 만든다.
* @param {Array<{ item: Object, children: any[] }>} wraps - `buildNavigationEditorTree` 결과
* @param {number} [depth] - 들여쓰기 단계(0=루트)
* @returns {Array<{ item: Object, depth: number }>}
*/
export const flattenNavigationEditorWrappers = (wraps, depth = 0) => {
const out = []
if (!Array.isArray(wraps)) {
return out
}
for (const w of wraps) {
if (!w?.item) {
continue
}
out.push({ item: w.item, depth })
if (w.children?.length) {
out.push(...flattenNavigationEditorWrappers(w.children, depth + 1))
}
}
return out
}