Merge pull request #2822 from lison16/menu
make menu support more than 2 levels
This commit is contained in:
commit
b4c3794697
8 changed files with 168 additions and 41 deletions
|
@ -1,32 +1,67 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<Menu mode="horizontal" :theme="theme1" active-name="1">
|
<Menu :theme="theme1" active-name="1" accordion @on-select="handleSelect" @on-open-change="handleOpen" :open-names="openArr">
|
||||||
<Menu-item name="1">
|
<Menu-item name="1">
|
||||||
<Icon type="ios-paper"></Icon>
|
<Icon type="ios-paper"></Icon>
|
||||||
内容管理
|
一级1
|
||||||
</Menu-item>
|
</Menu-item>
|
||||||
<Menu-item name="2">
|
<Menu-item name="2">
|
||||||
<Icon type="ios-people"></Icon>
|
<Icon type="ios-people"></Icon>
|
||||||
用户管理
|
一级2
|
||||||
</Menu-item>
|
</Menu-item>
|
||||||
<Submenu name="3">
|
<Submenu name="3">
|
||||||
<template slot="title">
|
<template slot="title">
|
||||||
<Icon type="stats-bars"></Icon>
|
<Icon type="ios-people"></Icon>
|
||||||
统计分析
|
一级3
|
||||||
</template>
|
</template>
|
||||||
<Menu-group title="使用">
|
<Menu-item name="3-1">二级1</Menu-item>
|
||||||
<Menu-item name="3-1">新增和启动</Menu-item>
|
<Menu-item name="3-2">二级2</Menu-item>
|
||||||
<Menu-item name="3-2">活跃分析</Menu-item>
|
<Submenu name="3-3">
|
||||||
<Menu-item name="3-3">时段分析</Menu-item>
|
<template slot="title">
|
||||||
</Menu-group>
|
<Icon type="stats-bars"></Icon>
|
||||||
<Menu-group title="留存">
|
二级3
|
||||||
<Menu-item name="3-4">用户留存</Menu-item>
|
</template>
|
||||||
<Menu-item name="3-5">流失用户</Menu-item>
|
<Menu-item name="3-3-1">三级1</Menu-item>
|
||||||
</Menu-group>
|
<Menu-item name="3-3-2">三级2</Menu-item>
|
||||||
|
<Submenu name="3-3-3">
|
||||||
|
<template slot="title">
|
||||||
|
<Icon type="stats-bars"></Icon>
|
||||||
|
三级3
|
||||||
|
</template>
|
||||||
|
<Menu-item name="3-3-3-1">四级1</Menu-item>
|
||||||
|
<Menu-item name="3-3-3-2">四级2</Menu-item>
|
||||||
|
<MenuGroup title="Menu-Group">
|
||||||
|
<MenuItem name="3-3-3-3-1">
|
||||||
|
<Icon type="document-text"></Icon>
|
||||||
|
Group-item1
|
||||||
|
</MenuItem>
|
||||||
|
<MenuItem name="3-3-3-3-2">
|
||||||
|
<Icon type="chatbubbles"></Icon>
|
||||||
|
Group-item2
|
||||||
|
</MenuItem>
|
||||||
|
</MenuGroup>
|
||||||
|
</Submenu>
|
||||||
|
<Submenu name="3-3-4">
|
||||||
|
<template slot="title">
|
||||||
|
<Icon type="stats-bars"></Icon>
|
||||||
|
三级4
|
||||||
|
</template>
|
||||||
|
<Menu-item name="3-3-4-1">四级1</Menu-item>
|
||||||
|
<Menu-item name="3-3-4-2">四级2</Menu-item>
|
||||||
|
</Submenu>
|
||||||
|
</Submenu>
|
||||||
|
<Submenu name="3-4">
|
||||||
|
<template slot="title">
|
||||||
|
<Icon type="stats-bars"></Icon>
|
||||||
|
二级4
|
||||||
|
</template>
|
||||||
|
<Menu-item name="3-4-1">三级1</Menu-item>
|
||||||
|
<Menu-item name="3-4-2">三级2</Menu-item>
|
||||||
|
</Submenu>
|
||||||
</Submenu>
|
</Submenu>
|
||||||
<Menu-item name="4">
|
<Menu-item name="4">
|
||||||
<Icon type="settings"></Icon>
|
<Icon type="settings"></Icon>
|
||||||
综合设置
|
一级4
|
||||||
</Menu-item>
|
</Menu-item>
|
||||||
</Menu>
|
</Menu>
|
||||||
<br>
|
<br>
|
||||||
|
@ -43,9 +78,20 @@
|
||||||
export default {
|
export default {
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
theme1: 'light',
|
theme1: 'dark',
|
||||||
value4: ''
|
value4: '',
|
||||||
|
openArr: ['3', '3-3', '3-3-3']
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
handleSelect (name) {
|
||||||
|
// console.log(name);
|
||||||
|
return name;
|
||||||
|
},
|
||||||
|
handleOpen (openArr) {
|
||||||
|
// console.log(openArr);
|
||||||
|
return openArr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
<template>
|
<template>
|
||||||
<li :class="[prefixCls + '-item-group']">
|
<li :class="[prefixCls + '-item-group']">
|
||||||
<div :class="[prefixCls + '-item-group-title']">{{ title }}</div>
|
<div :class="[prefixCls + '-item-group-title']" :style="groupStyle">{{ title }}</div>
|
||||||
<ul><slot></slot></ul>
|
<ul><slot></slot></ul>
|
||||||
</li>
|
</li>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
|
import { findComponentUpward, findComponentsUpward } from '../../utils/assist';
|
||||||
const prefixCls = 'ivu-menu';
|
const prefixCls = 'ivu-menu';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
@ -19,6 +20,19 @@
|
||||||
return {
|
return {
|
||||||
prefixCls: prefixCls
|
prefixCls: prefixCls
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
parentSubmenuNum () {
|
||||||
|
return findComponentsUpward(this, 'Submenu').length;
|
||||||
|
},
|
||||||
|
hasParentSubmenu () {
|
||||||
|
return findComponentUpward(this, 'Submenu');
|
||||||
|
},
|
||||||
|
groupStyle () {
|
||||||
|
return this.hasParentSubmenu ? {
|
||||||
|
paddingLeft: 43 + (this.parentSubmenuNum - 1) * 28 + 'px'
|
||||||
|
} : {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
<template>
|
<template>
|
||||||
<li :class="classes" @click.stop="handleClick"><slot></slot></li>
|
<li :class="classes" @click.stop="handleClick" :style="itemStyle"><slot></slot></li>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import Emitter from '../../mixins/emitter';
|
import Emitter from '../../mixins/emitter';
|
||||||
|
import { findComponentUpward } from '../../utils/assist';
|
||||||
const prefixCls = 'ivu-menu';
|
const prefixCls = 'ivu-menu';
|
||||||
|
import mixin from './mixin';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'MenuItem',
|
name: 'MenuItem',
|
||||||
mixins: [ Emitter ],
|
mixins: [ Emitter, mixin ],
|
||||||
props: {
|
props: {
|
||||||
name: {
|
name: {
|
||||||
type: [String, Number],
|
type: [String, Number],
|
||||||
|
@ -33,18 +35,18 @@
|
||||||
[`${prefixCls}-item-disabled`]: this.disabled
|
[`${prefixCls}-item-disabled`]: this.disabled
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
},
|
||||||
|
itemStyle () {
|
||||||
|
return this.hasParentSubmenu ? {
|
||||||
|
paddingLeft: 43 + (this.parentSubmenuNum - 1) * 24 + 'px'
|
||||||
|
} : {};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
handleClick () {
|
handleClick () {
|
||||||
if (this.disabled) return;
|
if (this.disabled) return;
|
||||||
|
|
||||||
let parent = this.$parent;
|
let parent = findComponentUpward(this, 'Submenu');
|
||||||
let name = parent.$options.name;
|
|
||||||
while (parent && (!name || name !== 'Submenu')) {
|
|
||||||
parent = parent.$parent;
|
|
||||||
if (parent) name = parent.$options.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parent) {
|
if (parent) {
|
||||||
this.dispatch('Submenu', 'on-menu-item-select', this.name);
|
this.dispatch('Submenu', 'on-menu-item-select', this.name);
|
||||||
|
@ -57,7 +59,7 @@
|
||||||
this.$on('on-update-active-name', (name) => {
|
this.$on('on-update-active-name', (name) => {
|
||||||
if (this.name === name) {
|
if (this.name === name) {
|
||||||
this.active = true;
|
this.active = true;
|
||||||
this.dispatch('Submenu', 'on-update-active-name', true);
|
this.dispatch('Submenu', 'on-update-active-name', name);
|
||||||
} else {
|
} else {
|
||||||
this.active = false;
|
this.active = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<ul :class="classes" :style="styles"><slot></slot></ul>
|
<ul :class="classes" :style="styles"><slot></slot></ul>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import { oneOf, findComponentsDownward } from '../../utils/assist';
|
import { oneOf, findComponentsDownward, findBrothersComponents } from '../../utils/assist';
|
||||||
import Emitter from '../../mixins/emitter';
|
import Emitter from '../../mixins/emitter';
|
||||||
|
|
||||||
const prefixCls = 'ivu-menu';
|
const prefixCls = 'ivu-menu';
|
||||||
|
@ -82,7 +82,14 @@
|
||||||
} else {
|
} else {
|
||||||
this.openNames.push(name);
|
this.openNames.push(name);
|
||||||
if (this.accordion) {
|
if (this.accordion) {
|
||||||
this.openNames.splice(0, this.openNames.length);
|
let currentSubmenu = {};
|
||||||
|
findComponentsDownward(this, 'Submenu').forEach(item => {
|
||||||
|
if (item.name === name) currentSubmenu = item;
|
||||||
|
});
|
||||||
|
findBrothersComponents(currentSubmenu, 'Submenu').forEach(item => {
|
||||||
|
let index = this.openNames.indexOf(item.name);
|
||||||
|
this.openNames.splice(index, index >= 0 ? 1 : 0);
|
||||||
|
});
|
||||||
this.openNames.push(name);
|
this.openNames.push(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/components/menu/mixin.js
Normal file
11
src/components/menu/mixin.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { findComponentUpward, findComponentsUpward } from '../../utils/assist';
|
||||||
|
export default {
|
||||||
|
computed: {
|
||||||
|
hasParentSubmenu () {
|
||||||
|
return findComponentUpward(this, 'Submenu');
|
||||||
|
},
|
||||||
|
parentSubmenuNum () {
|
||||||
|
return findComponentsUpward(this, 'Submenu').length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,6 +1,6 @@
|
||||||
<template>
|
<template>
|
||||||
<li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
|
<li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
|
||||||
<div :class="[prefixCls + '-submenu-title']" ref="reference" @click="handleClick">
|
<div :class="[prefixCls + '-submenu-title']" ref="reference" @click.stop="handleClick" :style="titleStyle">
|
||||||
<slot name="title"></slot>
|
<slot name="title"></slot>
|
||||||
<Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
|
<Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
|
||||||
</div>
|
</div>
|
||||||
|
@ -21,14 +21,15 @@
|
||||||
import Drop from '../select/dropdown.vue';
|
import Drop from '../select/dropdown.vue';
|
||||||
import Icon from '../icon/icon.vue';
|
import Icon from '../icon/icon.vue';
|
||||||
import CollapseTransition from '../base/collapse-transition';
|
import CollapseTransition from '../base/collapse-transition';
|
||||||
import { getStyle, findComponentUpward } from '../../utils/assist';
|
import { getStyle, findComponentUpward, findComponentsDownward } from '../../utils/assist';
|
||||||
import Emitter from '../../mixins/emitter';
|
import Emitter from '../../mixins/emitter';
|
||||||
|
import mixin from './mixin';
|
||||||
|
|
||||||
const prefixCls = 'ivu-menu';
|
const prefixCls = 'ivu-menu';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Submenu',
|
name: 'Submenu',
|
||||||
mixins: [ Emitter ],
|
mixins: [ Emitter, mixin ],
|
||||||
components: { Icon, Drop, CollapseTransition },
|
components: { Icon, Drop, CollapseTransition },
|
||||||
props: {
|
props: {
|
||||||
name: {
|
name: {
|
||||||
|
@ -54,9 +55,11 @@
|
||||||
return [
|
return [
|
||||||
`${prefixCls}-submenu`,
|
`${prefixCls}-submenu`,
|
||||||
{
|
{
|
||||||
[`${prefixCls}-item-active`]: this.active,
|
[`${prefixCls}-item-active`]: this.active && !this.hasParentSubmenu,
|
||||||
[`${prefixCls}-opened`]: this.opened,
|
[`${prefixCls}-opened`]: this.opened,
|
||||||
[`${prefixCls}-submenu-disabled`]: this.disabled
|
[`${prefixCls}-submenu-disabled`]: this.disabled,
|
||||||
|
[`${prefixCls}-submenu-has-parent-submenu`]: this.hasParentSubmenu,
|
||||||
|
[`${prefixCls}-child-item-active`]: this.active
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
@ -71,6 +74,11 @@
|
||||||
|
|
||||||
if (this.dropWidth) style.minWidth = `${this.dropWidth}px`;
|
if (this.dropWidth) style.minWidth = `${this.dropWidth}px`;
|
||||||
return style;
|
return style;
|
||||||
|
},
|
||||||
|
titleStyle () {
|
||||||
|
return this.hasParentSubmenu ? {
|
||||||
|
paddingLeft: 43 + (this.parentSubmenuNum - 1) * 24 + 'px'
|
||||||
|
} : {};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
@ -99,7 +107,7 @@
|
||||||
if (this.mode === 'horizontal') return;
|
if (this.mode === 'horizontal') return;
|
||||||
const opened = this.opened;
|
const opened = this.opened;
|
||||||
if (this.accordion) {
|
if (this.accordion) {
|
||||||
this.parent.$children.forEach(item => {
|
this.$parent.$children.forEach(item => {
|
||||||
if (item.$options.name === 'Submenu') item.opened = false;
|
if (item.$options.name === 'Submenu') item.opened = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -131,6 +139,10 @@
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
this.$on('on-update-active-name', (status) => {
|
this.$on('on-update-active-name', (status) => {
|
||||||
|
if (findComponentUpward(this, 'Submenu')) this.dispatch('Submenu', 'on-update-active-name', status);
|
||||||
|
if (findComponentsDownward(this, 'Submenu')) findComponentsDownward(this, 'Submenu').forEach(item => {
|
||||||
|
item.active = false;
|
||||||
|
});
|
||||||
this.active = status;
|
this.active = status;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,12 +160,17 @@
|
||||||
&-submenu-title-icon {
|
&-submenu-title-icon {
|
||||||
transition: transform @transition-time @ease-in-out;
|
transition: transform @transition-time @ease-in-out;
|
||||||
}
|
}
|
||||||
&-opened &-submenu-title-icon{
|
&-opened > * > &-submenu-title-icon{
|
||||||
transform: rotate(180deg);
|
transform: rotate(180deg);
|
||||||
}
|
}
|
||||||
|
|
||||||
&-vertical &-submenu &-item{
|
&-vertical &-submenu{
|
||||||
padding-left: 43px;
|
&-nested{
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
.@{menu-prefix-cls}-item{
|
||||||
|
padding-left: 43px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
&-vertical &-item-group{
|
&-vertical &-item-group{
|
||||||
&-title{
|
&-title{
|
||||||
|
@ -217,7 +222,10 @@
|
||||||
background: @primary-color !important;
|
background: @primary-color !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&-dark&-vertical &-item-active &-submenu-title{
|
// &-dark&-vertical &-item-active &-submenu-title{
|
||||||
|
// color: #fff;
|
||||||
|
// }
|
||||||
|
&-dark&-vertical &-child-item-active > &-submenu-title{
|
||||||
color: #fff;
|
color: #fff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,6 +234,12 @@
|
||||||
.@{menu-prefix-cls}-submenu-title{
|
.@{menu-prefix-cls}-submenu-title{
|
||||||
background: @menu-dark-title;
|
background: @menu-dark-title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.@{menu-prefix-cls}-submenu-has-parent-submenu{
|
||||||
|
.@{menu-prefix-cls}-submenu-title{
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.select-item(@menu-prefix-cls, @menu-dropdown-item-prefix-cls);
|
.select-item(@menu-prefix-cls, @menu-dropdown-item-prefix-cls);
|
||||||
|
|
|
@ -217,6 +217,27 @@ export function findComponentsDownward (context, componentName) {
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find components upward
|
||||||
|
export function findComponentsUpward (context, componentName) {
|
||||||
|
let parents = [];
|
||||||
|
if (context.$parent) {
|
||||||
|
if (context.$parent.$options.name === componentName) parents.push(context.$parent);
|
||||||
|
return parents.concat(findComponentsUpward(context.$parent, componentName));
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find brothers components
|
||||||
|
export function findBrothersComponents (context, componentName) {
|
||||||
|
let res = context.$parent.$children.filter(item => {
|
||||||
|
return item.$options.name === componentName;
|
||||||
|
});
|
||||||
|
let index = res.indexOf(context);
|
||||||
|
res.splice(index, 1);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
const trim = function(string) {
|
const trim = function(string) {
|
||||||
return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
|
return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
|
||||||
|
|
Loading…
Add table
Reference in a new issue