feat(admin): 以固定双列替代桌面二级悬浮菜单
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
const assert = require('node:assert/strict')
|
||||
const path = require('node:path')
|
||||
const test = require('node:test')
|
||||
const { buildSync } = require('esbuild')
|
||||
const { createMemoryHistory, createRouter } = require('vue-router')
|
||||
|
||||
const admin = path.resolve(__dirname, '..')
|
||||
function loadModel(mode) {
|
||||
const { outputFiles } = buildSync({
|
||||
absWorkingDir: admin,
|
||||
entryPoints: ['src/layout/default/components/sidebar/dual-model.ts'],
|
||||
alias: { '@': path.join(admin, 'src') },
|
||||
external: ['vue-router', '@vue/shared', 'lodash'],
|
||||
bundle: true, write: false, platform: 'node', format: 'cjs',
|
||||
define: { 'import.meta.env.MODE': JSON.stringify(mode) }
|
||||
})
|
||||
const module = { exports: {} }
|
||||
new Function('require', 'module', 'exports', outputFiles[0].text)(require, module, module.exports)
|
||||
return module.exports
|
||||
}
|
||||
const { buildDualMenu, findDualSelection, firstDualLeaf } = loadModel('production')
|
||||
const page = (path, title, props = {}) => ({ path, meta: { type: 'C', title, ...props } })
|
||||
const group = (path, title, children) => ({ path, meta: { type: 'M', title }, children })
|
||||
|
||||
test('preserves server order and never promotes hidden, action or empty catalogue entries', () => {
|
||||
const input = [
|
||||
group('/empty', '仅隐页', [page('detail', '详情', { hidden: true })]),
|
||||
group('/also-empty', '空目录', []),
|
||||
{ ...group('/hidden', '隐目录', [page('jump', '不可见')]), meta: { hidden: true } },
|
||||
page('/action', '按钮', { type: 'A' }),
|
||||
{ ...page('/list', '列表', { perms: 'data/list' }), children: [page('edit', '编辑', { hidden: true })] },
|
||||
group('/ops', '业务', [page('https://example.com/go?from=admin', '外部', { query: '?q=1' }),
|
||||
group('reports', '报表', [page('day', '日报', { icon: 'el-icon-DataLine' })])])
|
||||
]
|
||||
const original = JSON.stringify(input)
|
||||
const roots = buildDualMenu(input)
|
||||
assert.deepEqual(roots.map(n => n.title), ['列表', '业务'])
|
||||
assert.equal(roots[0].children.length, 0)
|
||||
const [external, reports] = roots[1].children
|
||||
assert.equal(external.target, 'https://example.com/go?from=admin&q=1')
|
||||
assert.equal(external.external, true)
|
||||
assert.equal(firstDualLeaf(external), undefined)
|
||||
assert.equal(firstDualLeaf(roots[1]), reports.children[0])
|
||||
assert.equal(firstDualLeaf({ ...roots[1], children: [external] }), undefined)
|
||||
assert.equal(reports.children[0].path, '/ops/reports/day')
|
||||
assert.equal(reports.children[0].icon, 'el-icon-DataLine')
|
||||
assert.equal(JSON.stringify(input), original)
|
||||
})
|
||||
|
||||
test('preserves query defaults and index keys, including multiple index children', () => {
|
||||
const [root] = buildDualMenu([group('/stats', '统计', [
|
||||
page('', '默认'), page('', '分组', { query: '{"type":"a b","tab":"本 月"}' }),
|
||||
page('', '相同目标', { query: '{"type":"a b","tab":"本 月"}' })
|
||||
])])
|
||||
assert.deepEqual(root.children.map(n => n.path), ['/stats', '/stats', '/stats'])
|
||||
assert.equal(root.children[1].target, '/stats?type=a%20b&tab=%E6%9C%AC%20%E6%9C%88')
|
||||
assert.equal(new Set([root, ...root.children].map(n => n.key)).size, 4)
|
||||
})
|
||||
|
||||
test('restores module and exact query leaf from a URL, router back and hidden-detail activeMenu', async () => {
|
||||
const roots = buildDualMenu([group('/ops', '业务', [group('stats', '统计', [
|
||||
page('', '全部'), page('', 'A', { query: '?kind=a' }),
|
||||
page('', 'A月度', { query: '{"kind":"a","tab":"月 度"}' }), page('', 'B', { query: 'kind=b' })
|
||||
])]), page('/home', '主页')])
|
||||
const routes = [
|
||||
{ path: '/ops/stats', component: {} }, { path: '/home', component: {} },
|
||||
{ path: '/detail', component: {}, meta: { activeMenu: '/ops/stats?kind=b' } }
|
||||
]
|
||||
const router = createRouter({ history: createMemoryHistory(), routes })
|
||||
const selection = () => findDualSelection(roots, router.currentRoute.value)
|
||||
await router.push('/ops/stats/?tab=%E6%9C%88+%E5%BA%A6&kind=a')
|
||||
assert.equal(selection().leaf.title, 'A月度')
|
||||
assert.deepEqual(selection().ancestors.map(n => n.title), ['业务', '统计'])
|
||||
await router.push('/ops/stats?kind=b&page=2')
|
||||
assert.equal(selection().leaf.title, 'B')
|
||||
await router.push('/detail?id=42')
|
||||
assert.equal(selection().leaf.title, 'B')
|
||||
await router.push('/home')
|
||||
assert.equal(selection().root.title, '主页')
|
||||
assert.deepEqual(selection().ancestors, [])
|
||||
const returned = new Promise(resolve => {
|
||||
const stop = router.afterEach(() => { stop(); resolve() })
|
||||
})
|
||||
router.back()
|
||||
await returned
|
||||
assert.equal(selection().leaf.title, 'B')
|
||||
const refreshed = createRouter({ history: createMemoryHistory(), routes })
|
||||
await refreshed.push(router.currentRoute.value.fullPath)
|
||||
assert.equal(findDualSelection(roots, refreshed.currentRoute.value).leaf.title, 'B')
|
||||
await router.push('/ops/stats?kind=c')
|
||||
assert.equal(selection().leaf.title, '全部')
|
||||
assert.deepEqual(findDualSelection([], router.currentRoute.value), { ancestors: [] })
|
||||
})
|
||||
|
||||
test('honors read-only absolute /nav IDs without inventing an absolute-path override in native mode', () => {
|
||||
const records = [group('/nav/module', '一级', [group('/nav/group', '二级', [page('/nav/item', '页面')])])]
|
||||
const readonly = loadModel('live-readonly')
|
||||
const roots = readonly.buildDualMenu(records)
|
||||
assert.equal(readonly.firstDualLeaf(roots[0]).target, '/nav/item')
|
||||
const selected = readonly.findDualSelection(roots, { path: '/members/detail', meta: { activeMenu: '/nav/item' }, query: { id: '2' } })
|
||||
assert.equal(selected.root.title, '一级')
|
||||
assert.deepEqual(selected.ancestors.map(n => n.path), ['/nav/module', '/nav/group'])
|
||||
assert.equal(firstDualLeaf(buildDualMenu(records)[0]).path, '/nav/module/nav/group/nav/item')
|
||||
})
|
||||
Reference in New Issue
Block a user