iview/src/components/menu/menu.vue

157 lines
5.4 KiB
Vue
Raw Normal View History

<template>
<ul :class="classes" :style="styles"><slot></slot></ul>
</template>
<script>
import { oneOf, findComponentsDownward, findComponentsUpward } 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 (this.accordion) findComponentsDownward(this, 'Submenu').forEach(item => {
item.opened = false;
});
if (index >= 0) {
let currentSubmenu = null;
findComponentsDownward(this, 'Submenu').forEach(item => {
if (item.name === name) {
currentSubmenu = item;
item.opened = false;
}
});
findComponentsUpward(currentSubmenu, 'Submenu').forEach(item => {
item.opened = true;
});
findComponentsDownward(currentSubmenu, 'Submenu').forEach(item => {
item.opened = false;
});
2016-12-08 13:29:49 +08:00
} else {
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;
item.opened = true;
}
2018-01-15 14:45:50 +08:00
});
findComponentsUpward(currentSubmenu, 'Submenu').forEach(item => {
item.opened = true;
});
} else {
findComponentsDownward(this, 'Submenu').forEach(item => {
if (item.name === name) item.opened = true;
2018-01-15 14:45:50 +08:00
});
2017-06-09 17:35:18 +08:00
}
2016-12-08 13:29:49 +08:00
}
let openedNames = findComponentsDownward(this, 'Submenu').filter(item => item.opened).map(item => item.name);
this.openedNames = [...openedNames];
this.$emit('on-open-change', 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
});
}
2018-08-28 10:42:57 +08:00
},
handleEmitSelectEvent (name) {
this.$emit('on-select', name);
2016-12-07 20:45:21 +08:00
}
},
mounted () {
this.openedNames = [...this.openNames];
this.updateOpened();
2019-04-10 10:43:18 +08:00
this.$nextTick(() => this.updateActiveName());
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>