2016-12-07 12:10:47 +08:00
|
|
|
<template>
|
2016-12-08 18:33:04 +08:00
|
|
|
<ul :class="classes" :style="styles"><slot></slot></ul>
|
2016-12-07 12:10:47 +08:00
|
|
|
</template>
|
|
|
|
<script>
|
2016-12-07 20:45:21 +08:00
|
|
|
import { oneOf } from '../../utils/assist';
|
|
|
|
|
|
|
|
const prefixCls = 'ivu-menu';
|
|
|
|
|
2016-12-07 12:10:47 +08:00
|
|
|
export default {
|
2016-12-07 20:45:21 +08:00
|
|
|
props: {
|
|
|
|
mode: {
|
|
|
|
validator (value) {
|
|
|
|
return oneOf(value, ['horizontal', 'vertical']);
|
|
|
|
},
|
|
|
|
default: 'vertical'
|
|
|
|
},
|
|
|
|
theme: {
|
|
|
|
validator (value) {
|
|
|
|
return oneOf(value, ['light', 'dark', 'primary']);
|
|
|
|
},
|
|
|
|
default: 'light'
|
|
|
|
},
|
|
|
|
activeKey: {
|
|
|
|
type: [String, Number]
|
|
|
|
},
|
|
|
|
openKeys: {
|
2016-12-08 13:29:49 +08:00
|
|
|
type: Array,
|
|
|
|
default () {
|
|
|
|
return []
|
|
|
|
}
|
2016-12-07 20:45:21 +08:00
|
|
|
},
|
|
|
|
accordion: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2016-12-08 18:33:04 +08:00
|
|
|
},
|
|
|
|
width: {
|
|
|
|
type: String,
|
|
|
|
default: '240px'
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
classes () {
|
2016-12-08 15:18:40 +08:00
|
|
|
let theme = this.theme;
|
|
|
|
if (this.mode === 'vertical' && this.theme === 'primary') theme = 'light';
|
|
|
|
|
2016-12-07 20:45:21 +08:00
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
2016-12-08 15:18:40 +08:00
|
|
|
`${prefixCls}-${theme}`,
|
2016-12-07 20:45:21 +08:00
|
|
|
{
|
2016-12-08 15:18:40 +08:00
|
|
|
[`${prefixCls}-${this.mode}`]: this.mode
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
|
|
|
]
|
2016-12-08 18:33:04 +08:00
|
|
|
},
|
|
|
|
styles () {
|
|
|
|
let style = {};
|
|
|
|
|
|
|
|
if (this.mode === 'vertical') style.width = this.width;
|
|
|
|
|
|
|
|
return style;
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
updateActiveKey () {
|
|
|
|
this.$children.forEach((item, index) => {
|
2016-12-08 18:33:04 +08:00
|
|
|
if (!this.activeKey && index === 0) {
|
|
|
|
this.activeKey = -1;
|
|
|
|
}
|
2016-12-07 20:45:21 +08:00
|
|
|
|
|
|
|
if (item.$options.name === 'Submenu') {
|
|
|
|
item.active = false;
|
|
|
|
item.$children.forEach(subitem => {
|
|
|
|
if (subitem.$options.name === 'MenuGroup') {
|
|
|
|
subitem.$children.forEach(groupItem => {
|
|
|
|
if (groupItem.key === this.activeKey) {
|
|
|
|
groupItem.active = true;
|
|
|
|
groupItem.$parent.$parent.active = true;
|
|
|
|
} else {
|
|
|
|
groupItem.active = false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (subitem.key === this.activeKey) {
|
|
|
|
subitem.active = true;
|
|
|
|
subitem.$parent.active = true;
|
|
|
|
} else {
|
|
|
|
subitem.active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-12-08 11:55:38 +08:00
|
|
|
} else if (item.$options.name === 'MenuGroup') {
|
|
|
|
item.$children.forEach(groupItem => {
|
|
|
|
groupItem.active = groupItem.key === this.activeKey;
|
|
|
|
})
|
2016-12-07 20:45:21 +08:00
|
|
|
} else {
|
|
|
|
item.active = item.key === this.activeKey;
|
|
|
|
}
|
|
|
|
})
|
2016-12-08 13:29:49 +08:00
|
|
|
},
|
|
|
|
updateOpenKeys (key) {
|
|
|
|
const index = this.openKeys.indexOf(key);
|
|
|
|
if (index > -1) {
|
|
|
|
this.openKeys.splice(index, 1);
|
|
|
|
} else {
|
|
|
|
this.openKeys.push(key);
|
|
|
|
}
|
2016-12-08 18:33:04 +08:00
|
|
|
},
|
|
|
|
updateOpened () {
|
|
|
|
this.$children.forEach(item => {
|
|
|
|
if (item.$options.name === 'Submenu') {
|
|
|
|
if (this.openKeys.indexOf(item.key) > -1) item.opened = true;
|
|
|
|
}
|
|
|
|
})
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
compiled () {
|
|
|
|
this.updateActiveKey();
|
2016-12-08 18:33:04 +08:00
|
|
|
this.updateOpened();
|
2016-12-07 12:10:47 +08:00
|
|
|
},
|
2016-12-07 20:45:21 +08:00
|
|
|
events: {
|
|
|
|
'on-menu-item-select' (key) {
|
|
|
|
this.activeKey = key;
|
|
|
|
this.updateActiveKey();
|
|
|
|
this.$emit('on-select', key);
|
|
|
|
}
|
2016-12-08 13:29:49 +08:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
openKeys () {
|
|
|
|
this.$emit('on-open-change', this.openKeys);
|
|
|
|
}
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
2016-12-07 12:10:47 +08:00
|
|
|
}
|
|
|
|
</script>
|