iview/src/components/menu/menu.vue

133 lines
4.2 KiB
Vue
Raw Normal View History

<template>
<ul :class="classes" :style="styles"><slot></slot></ul>
</template>
<script>
2018-01-15 14:45:50 +08:00
import { oneOf, findComponentsDownward, findBrothersComponents } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
2016-12-07 20:45:21 +08:00
const prefixCls = 'ivu-menu';
export default {
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'
},
activeName: {
2016-12-07 20:45:21 +08:00
type: [String, Number]
},
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
},
width: {
type: String,
default: '240px'
2016-12-07 20:45:21 +08:00
}
},
data () {
return {
currentActiveName: this.activeName,
openedNames: []
};
},
2016-12-07 20:45:21 +08:00
computed: {
classes () {
let theme = this.theme;
if (this.mode === 'vertical' && this.theme === 'primary') theme = 'light';
2016-12-07 20:45:21 +08:00
return [
`${prefixCls}`,
`${prefixCls}-${theme}`,
2016-12-07 20:45:21 +08:00
{
[`${prefixCls}-${this.mode}`]: this.mode
2016-12-07 20:45:21 +08:00
}
2016-12-25 22:49:42 +08:00
];
},
styles () {
let style = {};
if (this.mode === 'vertical') style.width = this.width;
return style;
2016-12-07 20:45:21 +08:00
}
},
methods: {
updateActiveName () {
2017-04-20 10:43:40 +08:00
if (this.currentActiveName === undefined) {
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
},
updateOpenKeys (name) {
let names = [...this.openedNames];
const index = names.indexOf(name);
if (index >= 0) {
names.splice(index, 1);
2016-12-08 13:29:49 +08:00
} else {
2017-06-09 17:35:18 +08:00
if (this.accordion) {
let currentSubmenu = null;
2018-01-15 14:45:50 +08:00
findComponentsDownward(this, 'Submenu').forEach(item => {
if (item.name === name) currentSubmenu = item;
});
findBrothersComponents(currentSubmenu, 'Submenu').forEach(item => {
let i = names.indexOf(item.name);
if (i >= 0) names.splice(i, 1);
2018-01-15 14:45:50 +08:00
});
names.push(name);
2017-06-09 17:35:18 +08:00
}
2016-12-08 13:29:49 +08:00
}
this.openedNames = names;
this.$emit('on-open-change', this.openedNames);
},
updateOpened () {
2017-03-15 18:26:10 +08:00
const items = findComponentsDownward(this, 'Submenu');
if (items.length) {
items.forEach(item => {
if (this.openedNames.indexOf(item.name) > -1) item.opened = true;
else item.opened = false;
2017-03-15 18:26:10 +08:00
});
}
2016-12-07 20:45:21 +08:00
}
},
mounted () {
this.updateActiveName();
this.openedNames = [...this.openNames];
this.updateOpened();
this.$on('on-menu-item-select', (name) => {
this.currentActiveName = name;
this.$emit('on-select', name);
});
2016-12-08 13:29:49 +08:00
},
watch: {
openNames (names) {
this.openedNames = names;
},
activeName (val) {
this.currentActiveName = val;
2017-01-23 09:55:32 +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>