iview/src/components/menu/submenu.vue

135 lines
4.5 KiB
Vue
Raw Normal View History

<template>
2016-12-07 20:45:21 +08:00
<li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
<div :class="[prefixCls + '-submenu-title']" ref="reference" @click="handleClick">
2016-12-08 11:55:38 +08:00
<slot name="title"></slot>
<Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
2016-12-07 20:45:21 +08:00
</div>
2016-12-08 11:55:38 +08:00
<ul :class="[prefixCls]" v-if="mode === 'vertical'" v-show="opened"><slot></slot></ul>
<transition name="slide-up" v-else>
<Drop
v-show="opened"
placement="bottom"
ref="drop"
:style="dropStyle"><slot></slot></Drop>
</transition>
2016-12-07 20:45:21 +08:00
</li>
</template>
<script>
2016-12-07 20:45:21 +08:00
import Drop from '../select/dropdown.vue';
import Icon from '../icon/icon.vue';
2017-03-15 18:26:10 +08:00
import { getStyle, findComponentUpward } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
2017-01-17 12:04:33 +08:00
2016-12-07 20:45:21 +08:00
const prefixCls = 'ivu-menu';
export default {
2016-12-07 20:45:21 +08:00
name: 'Submenu',
mixins: [ Emitter ],
2016-12-07 20:45:21 +08:00
components: { Icon, Drop },
props: {
name: {
2016-12-07 20:45:21 +08:00
type: [String, Number],
required: true
2016-12-08 11:55:38 +08:00
},
disabled: {
type: Boolean,
default: false
2016-12-07 20:45:21 +08:00
}
},
data () {
2016-12-07 20:45:21 +08:00
return {
prefixCls: prefixCls,
active: false,
2017-01-17 12:04:33 +08:00
opened: false,
2017-03-15 18:26:10 +08:00
dropWidth: parseFloat(getStyle(this.$el, 'width')),
parent: findComponentUpward(this, 'Menu')
2016-12-25 22:49:42 +08:00
};
2016-12-07 20:45:21 +08:00
},
computed: {
classes () {
return [
`${prefixCls}-submenu`,
{
[`${prefixCls}-item-active`]: this.active,
2016-12-08 11:55:38 +08:00
[`${prefixCls}-opened`]: this.opened,
[`${prefixCls}-submenu-disabled`]: this.disabled
2016-12-07 20:45:21 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-12-07 20:45:21 +08:00
},
mode () {
2017-03-15 18:26:10 +08:00
return this.parent.mode;
2016-12-08 11:55:38 +08:00
},
accordion () {
2017-03-15 18:26:10 +08:00
return this.parent.accordion;
2017-01-17 12:04:33 +08:00
},
dropStyle () {
let style = {};
if (this.dropWidth) style.minWidth = `${this.dropWidth}px`;
return style;
2016-12-07 20:45:21 +08:00
}
},
methods: {
handleMouseenter () {
2016-12-08 11:55:38 +08:00
if (this.disabled) return;
2016-12-07 20:45:21 +08:00
if (this.mode === 'vertical') return;
2016-12-08 11:55:38 +08:00
2016-12-07 20:45:21 +08:00
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
2017-03-15 18:26:10 +08:00
this.parent.updateOpenKeys(this.name);
2016-12-07 20:45:21 +08:00
this.opened = true;
}, 250);
},
handleMouseleave () {
2016-12-08 11:55:38 +08:00
if (this.disabled) return;
2016-12-07 20:45:21 +08:00
if (this.mode === 'vertical') return;
2016-12-08 11:55:38 +08:00
2016-12-07 20:45:21 +08:00
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
2017-03-15 18:26:10 +08:00
this.parent.updateOpenKeys(this.name);
2016-12-07 20:45:21 +08:00
this.opened = false;
}, 150);
2016-12-08 11:55:38 +08:00
},
handleClick () {
if (this.disabled) return;
if (this.mode === 'horizontal') return;
const opened = this.opened;
if (this.accordion) {
2017-03-15 18:26:10 +08:00
this.parent.$children.forEach(item => {
2016-12-08 11:55:38 +08:00
if (item.$options.name === 'Submenu') item.opened = false;
});
}
this.opened = !opened;
2017-03-15 18:26:10 +08:00
this.parent.updateOpenKeys(this.name);
2016-12-07 20:45:21 +08:00
}
},
watch: {
mode (val) {
if (val === 'horizontal') {
this.$refs.drop.update();
}
},
opened (val) {
if (this.mode === 'vertical') return;
if (val) {
2017-01-17 12:04:33 +08:00
// set drop a width to fixed when menu has fixed position
this.dropWidth = parseFloat(getStyle(this.$el, 'width'));
2016-12-07 20:45:21 +08:00
this.$refs.drop.update();
} else {
this.$refs.drop.destroy();
}
}
},
mounted () {
this.$on('on-menu-item-select', (name) => {
if (this.mode === 'horizontal') this.opened = false;
this.dispatch('Menu', 'on-menu-item-select', name);
2016-12-07 20:45:21 +08:00
return true;
});
this.$on('on-update-active-name', (status) => {
this.active = status;
});
2016-12-07 20:45:21 +08:00
}
2016-12-25 22:49:42 +08:00
};
</script>