Commit 55d26ac5 by musnow

update indexOf rewrite to includes

parent 0b6fbe66
...@@ -26,7 +26,7 @@ export default { ...@@ -26,7 +26,7 @@ export default {
default: false default: false
} }
}, },
data () { data() {
return { return {
openKeys: [], openKeys: [],
selectedKeys: [], selectedKeys: [],
...@@ -34,17 +34,17 @@ export default { ...@@ -34,17 +34,17 @@ export default {
} }
}, },
computed: { computed: {
rootSubmenuKeys: (vm) => { rootSubmenuKeys: vm => {
const keys = [] const keys = []
vm.menu.forEach(item => keys.push(item.path)) vm.menu.forEach(item => keys.push(item.path))
return keys return keys
} }
}, },
created () { created() {
this.updateMenu() this.updateMenu()
}, },
watch: { watch: {
collapsed (val) { collapsed(val) {
if (val) { if (val) {
this.cachedOpenKeys = this.openKeys this.cachedOpenKeys = this.openKeys
this.openKeys = [] this.openKeys = []
...@@ -52,94 +52,79 @@ export default { ...@@ -52,94 +52,79 @@ export default {
this.openKeys = this.cachedOpenKeys this.openKeys = this.cachedOpenKeys
} }
}, },
'$route': function () { $route: function() {
this.updateMenu() this.updateMenu()
} }
}, },
methods: { methods: {
renderIcon: function (h, icon) { renderIcon: function(h, icon) {
return icon === 'none' || icon === undefined ? null return icon === 'none' || icon === undefined ? null : h(Icon, { props: { type: icon !== undefined ? icon : '' } })
: h(Icon, { props: { type: icon !== undefined ? icon : '' } })
}, },
renderMenuItem: function (h, menu, pIndex, index) { renderMenuItem: function(h, menu, pIndex, index) {
return h(Item, { key: menu.path ? menu.path : 'item_' + pIndex + '_' + index }, return h(Item, { key: menu.path ? menu.path : 'item_' + pIndex + '_' + index }, [
[ h('router-link', { attrs: { to: { name: menu.name } } }, [
h( this.renderIcon(h, menu.meta.icon),
'router-link', h('span', [menu.meta.title])
{ attrs: { to: { name: menu.name } } }, ])
[ ])
this.renderIcon(h, menu.meta.icon),
h('span', [ menu.meta.title ])
]
)
]
)
}, },
renderSubMenu: function (h, menu, pIndex, index) { renderSubMenu: function(h, menu, pIndex, index) {
const this2_ = this const this2_ = this
const subItem = [ h('span', const subItem = [h('span', { slot: 'title' }, [this.renderIcon(h, menu.meta.icon), h('span', [menu.meta.title])])]
{ slot: 'title' },
[
this.renderIcon(h, menu.meta.icon),
h('span', [ menu.meta.title ])
]
) ]
const itemArr = [] const itemArr = []
const pIndex_ = pIndex + '_' + index const pIndex_ = pIndex + '_' + index
if (!menu.alwaysShow) { if (!menu.alwaysShow) {
menu.children.forEach(function (item, i) { menu.children.forEach(function(item, i) {
itemArr.push(this2_.renderItem(h, item, pIndex_, i)) itemArr.push(this2_.renderItem(h, item, pIndex_, i))
}) })
} }
return h( return h(SubMenu, { key: menu.path ? menu.path : 'submenu_' + pIndex + '_' + index }, subItem.concat(itemArr))
SubMenu,
{ key: menu.path ? menu.path : 'submenu_' + pIndex + '_' + index },
subItem.concat(itemArr)
)
}, },
renderItem: function (h, menu, pIndex, index) { renderItem: function(h, menu, pIndex, index) {
if (!menu.hidden) { if (!menu.hidden) {
return menu.children && !menu.alwaysShow ? this.renderSubMenu(h, menu, pIndex, index) : this.renderMenuItem(h, menu, pIndex, index) return menu.children && !menu.alwaysShow
? this.renderSubMenu(h, menu, pIndex, index)
: this.renderMenuItem(h, menu, pIndex, index)
} }
}, },
renderMenu: function (h, menuTree) { renderMenu: function(h, menuTree) {
const this2_ = this const this2_ = this
const menuArr = [] const menuArr = []
menuTree.forEach(function (menu, i) { menuTree.forEach(function(menu, i) {
if (!menu.hidden) { if (!menu.hidden) {
menuArr.push(this2_.renderItem(h, menu, '0', i)) menuArr.push(this2_.renderItem(h, menu, '0', i))
} }
}) })
return menuArr return menuArr
}, },
onOpenChange (openKeys) { onOpenChange(openKeys) {
const latestOpenKey = openKeys.find(key => this.openKeys.indexOf(key) === -1) const latestOpenKey = openKeys.find(key => !this.openKeys.includes(key))
if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) { if (!this.rootSubmenuKeys.includes(latestOpenKey)) {
this.openKeys = openKeys this.openKeys = openKeys
} else { } else {
this.openKeys = latestOpenKey ? [ latestOpenKey ] : [] this.openKeys = latestOpenKey ? [latestOpenKey] : []
} }
}, },
updateMenu () { updateMenu() {
const routes = this.$route.matched.concat() const routes = this.$route.matched.concat()
if (routes.length >= 4 && this.$route.meta.hidden) { if (routes.length >= 4 && this.$route.meta.hidden) {
routes.pop() routes.pop()
this.selectedKeys = [ routes[2].path ] this.selectedKeys = [routes[2].path]
} else { } else {
this.selectedKeys = [ routes.pop().path ] this.selectedKeys = [routes.pop().path]
} }
const openKeys = [] const openKeys = []
if (this.mode === 'inline') { if (this.mode === 'inline') {
routes.forEach((item) => { routes.forEach(item => {
openKeys.push(item.path) openKeys.push(item.path)
}) })
} }
this.collapsed ? this.cachedOpenKeys = openKeys : this.openKeys = openKeys this.collapsed ? (this.cachedOpenKeys = openKeys) : (this.openKeys = openKeys)
} }
}, },
render (h) { render(h) {
return h( return h(
Menu, Menu,
{ {
...@@ -151,12 +136,13 @@ export default { ...@@ -151,12 +136,13 @@ export default {
}, },
on: { on: {
openChange: this.onOpenChange, openChange: this.onOpenChange,
select: (obj) => { select: obj => {
this.selectedKeys = obj.selectedKeys this.selectedKeys = obj.selectedKeys
this.$emit('select', obj) this.$emit('select', obj)
} }
} }
}, this.renderMenu(h, this.menu) },
this.renderMenu(h, this.menu)
) )
} }
} }
\ No newline at end of file
...@@ -9,10 +9,10 @@ import { asyncRouterMap, constantRouterMap } from '@/config/router.config' ...@@ -9,10 +9,10 @@ import { asyncRouterMap, constantRouterMap } from '@/config/router.config'
*/ */
function hasPermission(permission, route) { function hasPermission(permission, route) {
if (route.meta && route.meta.permission) { if (route.meta && route.meta.permission) {
let flag = -1 let flag = false
for (let i = 0, len = permission.length; i < len; i++) { for (let i = 0, len = permission.length; i < len; i++) {
flag = route.meta.permission.indexOf(permission[i]) flag = route.meta.permission.includes(permission[i])
if (flag >= 0) { if (flag) {
return true return true
} }
} }
...@@ -31,7 +31,7 @@ function hasPermission(permission, route) { ...@@ -31,7 +31,7 @@ function hasPermission(permission, route) {
// eslint-disable-next-line // eslint-disable-next-line
function hasRole(roles, route) { function hasRole(roles, route) {
if (route.meta && route.meta.roles) { if (route.meta && route.meta.roles) {
return route.meta.roles.indexOf(roles.id) return route.meta.roles.includes(roles.id)
} else { } else {
return true return true
} }
...@@ -73,4 +73,4 @@ const permission = { ...@@ -73,4 +73,4 @@ const permission = {
} }
} }
export default permission export default permission
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment