import type { Menu, MenuInner } from '../../abc'; export function fixItem(item: MenuInner): void { item._aclResult = true; if (!item.link) item.link = ''; if (!item.externalLink) item.externalLink = ''; // badge if (item.badge) { if (item.badgeDot !== true) { item.badgeDot = false; } if (!item.badgeStatus) { item.badgeStatus = 'error'; } } if (!Array.isArray(item.children)) { item.children = []; } // icon if (typeof item.icon === 'string') { let type = 'class'; let value = item.icon; // compatible `anticon anticon-user` if (item.icon.indexOf(`anticon-`) !== -1) { type = 'icon'; value = value.split('-').slice(1).join('-'); } else if (/^https?:\/\//.test(item.icon)) { type = 'img'; } item.icon = { type, value } as any; } if (item.icon !== null) { item.icon = { theme: 'outline', spin: false, ...(item.icon as any) }; } // group item.group = item.group !== false; // hidden item._hidden = typeof item.hide === 'undefined' ? false : item.hide; // disabled item.disabled = typeof item.disabled === 'undefined' ? false : item.disabled; // acl item._aclResult = true; } export function visit(data: Menu[], callback: (item: Menu, parentMenum: Menu | null, depth?: number) => void): Menu[] { const inFn = (list: Menu[], parentMenu: Menu | null, depth: number): void => { for (const item of list) { callback(item, parentMenu, depth); if (item.children && item.children.length > 0) { inFn(item.children, item, depth + 1); } else { item.children = []; } } }; inFn(data, null, 0); return data; } export function loadShortcut(data: Menu[], shortcuts: MenuInner[]): Menu[] { if (shortcuts.length === 0 || data.length === 0) { return data; } const ls = data[0].children as MenuInner[]; let pos = ls.findIndex(w => w.shortcutRoot === true); if (pos === -1) { pos = ls.findIndex(w => w.link!.includes('dashboard')); pos = (pos !== -1 ? pos : -1) + 1; const shortcutMenu = { text: '快捷菜单', i18n: 'shortcut', icon: 'icon-rocket', children: [] } as MenuInner; data[0].children!.splice(pos, 0, shortcutMenu); } let _data = data[0].children![pos]; _data = Object.assign(_data, { shortcutRoot: true, _id: -1, _parent: null, _depth: 1 } as MenuInner); _data.children = shortcuts.map(i => { i._depth = 2; i._parent = _data; return i; }); return data; } export function resume( data: Menu[], callback?: (item: Menu, parentMenum: Menu | null, depth?: number) => void ): Menu[] { let i = 1; const shortcuts: Menu[] = []; visit(data, (item: MenuInner, parent, depth) => { item._id = i; i += 1; item._parent = parent; item._depth = depth; fixItem(item); if (parent && item.shortcut === true && parent.shortcutRoot !== true) { shortcuts.push(item); } if (callback) callback(item, parent, depth); }); loadShortcut(data, shortcuts); return data; } export function mapYzSideToNav(menus: Menu[]): void { menus.forEach(menu => { menu.badgeDot = menu.badge_dot || null; menu.badgeStatus = menu.badge_status || null; menu.shortcutRoot = menu.shortcut_root || null; menu.reuse = true; if (menu.children) { mapYzSideToNav(menu.children); } }); }