iview/src/components/menu/menu.vue

117 lines
3.4 KiB
Vue
Raw Normal View History

<template>
<ul :class="classes" :style="styles"><slot></slot></ul>
</template>
<script>
2017-03-15 18:26:10 +08:00
import { oneOf, findComponentsDownward } 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
};
},
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 () {
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
},
updateOpenKeys (name) {
const index = this.openNames.indexOf(name);
2016-12-08 13:29:49 +08:00
if (index > -1) {
this.openNames.splice(index, 1);
2016-12-08 13:29:49 +08:00
} else {
this.openNames.push(name);
2016-12-08 13:29:49 +08:00
}
},
updateOpened () {
2017-03-15 18:26:10 +08:00
const items = findComponentsDownward(this, 'Submenu');
if (items.length) {
items.forEach(item => {
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
}
},
mounted () {
this.updateActiveName();
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 () {
this.$emit('on-open-change', this.openNames);
},
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>