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>
|
2017-03-15 18:26:10 +08:00
|
|
|
import { oneOf, findComponentsDownward } from '../../utils/assist';
|
2017-03-06 13:31:48 +08:00
|
|
|
import Emitter from '../../mixins/emitter';
|
2016-12-07 20:45:21 +08:00
|
|
|
|
|
|
|
const prefixCls = 'ivu-menu';
|
|
|
|
|
2016-12-07 12:10:47 +08:00
|
|
|
export default {
|
2017-03-06 13:31:48 +08:00
|
|
|
name: 'Menu',
|
|
|
|
mixins: [ Emitter ],
|
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'
|
|
|
|
},
|
2017-03-06 13:31:48 +08:00
|
|
|
activeName: {
|
2016-12-07 20:45:21 +08:00
|
|
|
type: [String, Number]
|
|
|
|
},
|
2017-03-06 13:31:48 +08:00
|
|
|
openNames: {
|
2016-12-08 13:29:49 +08:00
|
|
|
type: Array,
|
|
|
|
default () {
|
2016-12-25 22:49:42 +08:00
|
|
|
return [];
|
2016-12-08 13:29:49 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
},
|
2017-03-06 13:31:48 +08:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
currentActiveName: this.activeName
|
|
|
|
};
|
|
|
|
},
|
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-25 22:49:42 +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: {
|
2017-03-06 13:31:48 +08:00
|
|
|
updateActiveName () {
|
|
|
|
if (!this.currentActiveName) {
|
|
|
|
this.currentActiveName = -1;
|
|
|
|
}
|
|
|
|
this.broadcast('Submenu', 'on-update-active-name', false);
|
|
|
|
this.broadcast('MenuItem', 'on-update-active-name', this.currentActiveName);
|
2016-12-08 13:29:49 +08:00
|
|
|
},
|
2017-03-06 13:31:48 +08:00
|
|
|
updateOpenKeys (name) {
|
|
|
|
const index = this.openNames.indexOf(name);
|
2016-12-08 13:29:49 +08:00
|
|
|
if (index > -1) {
|
2017-03-06 13:31:48 +08:00
|
|
|
this.openNames.splice(index, 1);
|
2016-12-08 13:29:49 +08:00
|
|
|
} else {
|
2017-03-06 13:31:48 +08:00
|
|
|
this.openNames.push(name);
|
2016-12-08 13:29:49 +08:00
|
|
|
}
|
2016-12-08 18:33:04 +08:00
|
|
|
},
|
|
|
|
updateOpened () {
|
2017-03-15 18:26:10 +08:00
|
|
|
const items = findComponentsDownward(this, 'Submenu');
|
|
|
|
|
|
|
|
if (items.length) {
|
|
|
|
items.forEach(item => {
|
2017-03-06 13:31:48 +08:00
|
|
|
if (this.openNames.indexOf(item.name) > -1) item.opened = true;
|
2017-03-15 18:26:10 +08:00
|
|
|
});
|
|
|
|
}
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
|
|
|
},
|
2017-03-06 13:31:48 +08:00
|
|
|
mounted () {
|
|
|
|
this.updateActiveName();
|
2016-12-08 18:33:04 +08:00
|
|
|
this.updateOpened();
|
2017-03-06 13:31:48 +08:00
|
|
|
this.$on('on-menu-item-select', (name) => {
|
|
|
|
this.currentActiveName = name;
|
|
|
|
this.$emit('on-select', name);
|
|
|
|
});
|
2016-12-08 13:29:49 +08:00
|
|
|
},
|
|
|
|
watch: {
|
2017-03-06 13:31:48 +08:00
|
|
|
openNames () {
|
|
|
|
this.$emit('on-open-change', this.openNames);
|
|
|
|
},
|
|
|
|
activeName (val) {
|
|
|
|
this.currentActiveName = val;
|
2017-01-23 09:55:32 +08:00
|
|
|
},
|
2017-03-06 13:31:48 +08:00
|
|
|
currentActiveName () {
|
|
|
|
this.updateActiveName();
|
2016-12-08 13:29:49 +08:00
|
|
|
}
|
2016-12-07 20:45:21 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
|
|
|
</script>
|