메뉴 관리: 탭 분리·드래그 정렬·상단 트리·공개 접기 (v0.0.94)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
44
lib/navigation-editor-tree.js
Normal file
44
lib/navigation-editor-tree.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 관리자 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
|
||||
}
|
||||
Reference in New Issue
Block a user