rancher/ui 菜单部分源码解析

本文深入解析了一款应用程序中导航菜单的实现逻辑,包括菜单数据结构、处理函数及如何根据用户权限动态更新菜单项。通过分析源代码,揭示了菜单项的条件显示、路由配置和资源权限管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于菜单是在登录授权后的主页面一直存在,所有在 app/authenticated 模块下找

打开app/authenticated/template.hbs

app/authenticated/template.hbs 源码地址

有一个page-header组件在顶部,这个组件就是顶部菜单的内容

 

 

app/components/page-header/template.hbs 源码地址

 

所有逻辑都写在 app/components/page-header/component.js

 

重要的变量解释

pageScope  表明当前页面是什么作用于下,分为 global (全局), cluster(集群), project(项目) 一级菜单与二级菜单都会根据此变量改变。

 

updateNavTree 函数是用于处理菜单逻辑的函数

 

根据getTree() 找到  navigation-tree.js 菜单数据的相关处理方法 

 

全局搜索 bulkAdd 方法 找到 /app/instance-initializers/nav.js  此文件定义了所有的菜单数据。 

子菜单示例数据

{
	scope:          'global',
	id:             'global-catalogs',
	localizedLabel: 'nav.admin.catalogs',
	route:          'global-admin.catalog',
	resource:       ['catalog'],
	resourceScope:  'global',
}

 

主菜单示例数据

{
    scope:          'project',
    id:             'infra',
    localizedLabel: 'nav.infra.tab',
    ctx:            [getProjectId],
    submenu:        []
}

 

菜单数据的部分注释。摘自源码,可能不是最新的。 

/* Tree item options
  {
    id: 'str' (identifier to allow removal... should be unique)
    localizedLabel: 'i18n key', (or function that returns one)
    label: 'Displayed unlocalized label', (or function that returns string)
    icon: 'icon icon-something',
    condition: function() {
      // return true if this item should be displayed
      // condition can depend on anything page-header/component.js shouldUpdateNavTree() depends on
    }
    target: '_blank', (for url only)
    route: 'target.route.path', // as in link-to
    ctx: ['values', 'asContextToRoute', orFunctionThatReturnsValue, anotherFunction]
    qp: {a: 'hello', b: 'world'],
    moreCurrentWhen: ['additional.routes','for.current-when'],

    submenu: [
      // Another tree item (only one level of submenu supported, no arbitrary depth nesting)
      {...},
      {...}
    ]
  },
*/

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拿我格子衫来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值