2016-07-27 06:15:02 +00:00
|
|
|
<script>
|
2017-05-08 06:06:23 +00:00
|
|
|
import ElCollapseTransition from 'element-ui/src/transitions/collapse-transition';
|
2016-09-22 15:36:03 +00:00
|
|
|
import menuMixin from './menu-mixin';
|
2017-01-05 15:19:49 +00:00
|
|
|
import Emitter from 'element-ui/src/mixins/emitter';
|
2018-01-23 07:58:46 +00:00
|
|
|
import Popper from 'element-ui/src/utils/vue-popper';
|
|
|
|
|
|
|
|
const poperMixins = {
|
|
|
|
props: {
|
|
|
|
transformOrigin: {
|
|
|
|
type: [Boolean, String],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
offset: Popper.props.offset,
|
|
|
|
boundariesPadding: Popper.props.boundariesPadding,
|
|
|
|
popperOptions: Popper.props.popperOptions
|
|
|
|
},
|
|
|
|
data: Popper.data,
|
|
|
|
methods: Popper.methods,
|
|
|
|
beforeDestroy: Popper.beforeDestroy,
|
|
|
|
deactivated: Popper.deactivated
|
|
|
|
};
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-02-06 04:28:40 +00:00
|
|
|
export default {
|
2016-10-30 11:36:03 +00:00
|
|
|
name: 'ElSubmenu',
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-10-30 11:36:03 +00:00
|
|
|
componentName: 'ElSubmenu',
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2018-01-23 07:58:46 +00:00
|
|
|
mixins: [menuMixin, Emitter, poperMixins],
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-05-08 06:06:23 +00:00
|
|
|
components: { ElCollapseTransition },
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
props: {
|
2016-07-29 08:41:14 +00:00
|
|
|
index: {
|
2016-07-27 06:15:02 +00:00
|
|
|
type: String,
|
|
|
|
required: true
|
2018-01-04 09:11:51 +00:00
|
|
|
},
|
|
|
|
showTimeout: {
|
|
|
|
type: Number,
|
|
|
|
default: 300
|
|
|
|
},
|
|
|
|
hideTimeout: {
|
|
|
|
type: Number,
|
|
|
|
default: 300
|
2018-02-01 07:05:07 +00:00
|
|
|
},
|
2018-02-11 06:59:46 +00:00
|
|
|
popperClass: String,
|
|
|
|
disabled: Boolean
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2017-07-20 04:44:52 +00:00
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2018-01-23 07:58:46 +00:00
|
|
|
popperJS: null,
|
2016-09-05 03:30:05 +00:00
|
|
|
timeout: null,
|
2017-01-05 15:19:49 +00:00
|
|
|
items: {},
|
|
|
|
submenus: {}
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
},
|
2018-01-23 07:58:46 +00:00
|
|
|
watch: {
|
|
|
|
opened(val) {
|
|
|
|
if (this.isMenuPopup) {
|
|
|
|
this.$nextTick(_ => {
|
|
|
|
this.updatePopper();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
computed: {
|
2018-01-23 07:58:46 +00:00
|
|
|
// popper option
|
|
|
|
appendToBody() {
|
2018-03-08 11:19:10 +00:00
|
|
|
return this.isFirstLevel;
|
2018-01-23 07:58:46 +00:00
|
|
|
},
|
2017-07-20 04:44:52 +00:00
|
|
|
menuTransitionName() {
|
|
|
|
return this.rootMenu.collapse ? 'el-zoom-in-left' : 'el-zoom-in-top';
|
|
|
|
},
|
2016-09-22 15:36:03 +00:00
|
|
|
opened() {
|
2017-01-05 15:19:49 +00:00
|
|
|
return this.rootMenu.openedMenus.indexOf(this.index) > -1;
|
|
|
|
},
|
2017-09-16 12:50:19 +00:00
|
|
|
active() {
|
|
|
|
let isActive = false;
|
|
|
|
const submenus = this.submenus;
|
|
|
|
const items = this.items;
|
2017-01-05 15:19:49 +00:00
|
|
|
|
2017-09-16 12:50:19 +00:00
|
|
|
Object.keys(items).forEach(index => {
|
|
|
|
if (items[index].active) {
|
|
|
|
isActive = true;
|
|
|
|
}
|
|
|
|
});
|
2017-01-05 15:19:49 +00:00
|
|
|
|
2017-09-16 12:50:19 +00:00
|
|
|
Object.keys(submenus).forEach(index => {
|
|
|
|
if (submenus[index].active) {
|
|
|
|
isActive = true;
|
|
|
|
}
|
|
|
|
});
|
2017-01-05 15:19:49 +00:00
|
|
|
|
2017-09-16 12:50:19 +00:00
|
|
|
return isActive;
|
|
|
|
},
|
|
|
|
hoverBackground() {
|
|
|
|
return this.rootMenu.hoverBackground;
|
|
|
|
},
|
|
|
|
backgroundColor() {
|
|
|
|
return this.rootMenu.backgroundColor || '';
|
|
|
|
},
|
|
|
|
activeTextColor() {
|
|
|
|
return this.rootMenu.activeTextColor || '';
|
|
|
|
},
|
|
|
|
textColor() {
|
|
|
|
return this.rootMenu.textColor || '';
|
|
|
|
},
|
|
|
|
mode() {
|
|
|
|
return this.rootMenu.mode;
|
|
|
|
},
|
2018-01-23 07:58:46 +00:00
|
|
|
isMenuPopup() {
|
|
|
|
return this.rootMenu.isMenuPopup;
|
|
|
|
},
|
2017-09-16 12:50:19 +00:00
|
|
|
titleStyle() {
|
|
|
|
if (this.mode !== 'horizontal') {
|
|
|
|
return {
|
|
|
|
color: this.textColor
|
|
|
|
};
|
2017-01-05 15:19:49 +00:00
|
|
|
}
|
2017-09-16 12:50:19 +00:00
|
|
|
return {
|
|
|
|
borderBottomColor: this.active
|
|
|
|
? (this.rootMenu.activeTextColor ? this.activeTextColor : '')
|
|
|
|
: 'transparent',
|
|
|
|
color: this.active
|
|
|
|
? this.activeTextColor
|
|
|
|
: this.textColor
|
|
|
|
};
|
2018-03-08 11:19:10 +00:00
|
|
|
},
|
|
|
|
isFirstLevel() {
|
|
|
|
let isFirstLevel = true;
|
|
|
|
let parent = this.$parent;
|
|
|
|
while (parent && parent !== this.rootMenu) {
|
|
|
|
if (['ElSubmenu', 'ElMenuItemGroup'].indexOf(parent.$options.componentName) > -1) {
|
|
|
|
isFirstLevel = false;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
parent = parent.$parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isFirstLevel;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2018-01-23 07:58:46 +00:00
|
|
|
handleCollapseToggle(value) {
|
|
|
|
if (value) {
|
|
|
|
this.initPopper();
|
|
|
|
} else {
|
|
|
|
this.doDestroy();
|
|
|
|
}
|
|
|
|
},
|
2017-01-05 15:19:49 +00:00
|
|
|
addItem(item) {
|
|
|
|
this.$set(this.items, item.index, item);
|
|
|
|
},
|
|
|
|
removeItem(item) {
|
|
|
|
delete this.items[item.index];
|
|
|
|
},
|
|
|
|
addSubmenu(item) {
|
|
|
|
this.$set(this.submenus, item.index, item);
|
|
|
|
},
|
|
|
|
removeSubmenu(item) {
|
|
|
|
delete this.submenus[item.index];
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
handleClick() {
|
2018-02-11 06:59:46 +00:00
|
|
|
const { rootMenu, disabled } = this;
|
2017-07-20 04:44:52 +00:00
|
|
|
if (
|
|
|
|
(rootMenu.menuTrigger === 'hover' && rootMenu.mode === 'horizontal') ||
|
2018-02-11 06:59:46 +00:00
|
|
|
(rootMenu.collapse && rootMenu.mode === 'vertical') ||
|
|
|
|
disabled
|
2017-07-20 04:44:52 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-05 15:19:49 +00:00
|
|
|
this.dispatch('ElMenu', 'submenu-click', this);
|
2016-09-05 03:30:05 +00:00
|
|
|
},
|
|
|
|
handleMouseenter() {
|
2018-02-11 06:59:46 +00:00
|
|
|
const { rootMenu, disabled } = this;
|
2017-07-20 04:44:52 +00:00
|
|
|
if (
|
|
|
|
(rootMenu.menuTrigger === 'click' && rootMenu.mode === 'horizontal') ||
|
2018-02-11 06:59:46 +00:00
|
|
|
(!rootMenu.collapse && rootMenu.mode === 'vertical') ||
|
|
|
|
disabled
|
2017-07-20 04:44:52 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-26 03:51:41 +00:00
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.rootMenu.openMenu(this.index, this.indexPath);
|
2018-01-04 09:11:51 +00:00
|
|
|
}, this.showTimeout);
|
2016-09-05 03:30:05 +00:00
|
|
|
},
|
|
|
|
handleMouseleave() {
|
2017-07-20 04:44:52 +00:00
|
|
|
const {rootMenu} = this;
|
|
|
|
if (
|
|
|
|
(rootMenu.menuTrigger === 'click' && rootMenu.mode === 'horizontal') ||
|
|
|
|
(!rootMenu.collapse && rootMenu.mode === 'vertical')
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-26 03:51:41 +00:00
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = setTimeout(() => {
|
2017-08-28 09:57:27 +00:00
|
|
|
this.rootMenu.closeMenu(this.index);
|
2018-01-04 09:11:51 +00:00
|
|
|
}, this.hideTimeout);
|
2017-09-16 12:50:19 +00:00
|
|
|
},
|
|
|
|
handleTitleMouseenter() {
|
|
|
|
if (this.mode === 'horizontal' && !this.rootMenu.backgroundColor) return;
|
2017-11-09 07:12:16 +00:00
|
|
|
const title = this.$refs['submenu-title'];
|
|
|
|
title && (title.style.backgroundColor = this.rootMenu.hoverBackground);
|
2017-09-16 12:50:19 +00:00
|
|
|
},
|
|
|
|
handleTitleMouseleave() {
|
|
|
|
if (this.mode === 'horizontal' && !this.rootMenu.backgroundColor) return;
|
2017-11-09 07:12:16 +00:00
|
|
|
const title = this.$refs['submenu-title'];
|
|
|
|
title && (title.style.backgroundColor = this.rootMenu.backgroundColor || '');
|
2018-01-23 07:58:46 +00:00
|
|
|
},
|
|
|
|
updatePlacement() {
|
2018-03-08 11:19:10 +00:00
|
|
|
this.currentPlacement = this.mode === 'horizontal' && this.isFirstLevel
|
2018-02-09 03:05:37 +00:00
|
|
|
? 'bottom-start'
|
|
|
|
: 'right-start';
|
2018-01-23 07:58:46 +00:00
|
|
|
},
|
|
|
|
initPopper() {
|
|
|
|
this.referenceElm = this.$el;
|
|
|
|
this.popperElm = this.$refs.menu;
|
|
|
|
this.updatePlacement();
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
2016-09-22 15:36:03 +00:00
|
|
|
created() {
|
2017-01-05 15:19:49 +00:00
|
|
|
this.parentMenu.addSubmenu(this);
|
|
|
|
this.rootMenu.addSubmenu(this);
|
2018-01-23 07:58:46 +00:00
|
|
|
this.$on('toggle-collapse', this.handleCollapseToggle);
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.initPopper();
|
2017-01-05 15:19:49 +00:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.parentMenu.removeSubmenu(this);
|
|
|
|
this.rootMenu.removeSubmenu(this);
|
2018-01-23 07:58:46 +00:00
|
|
|
},
|
|
|
|
render(h) {
|
|
|
|
const {
|
|
|
|
active,
|
|
|
|
opened,
|
|
|
|
paddingStyle,
|
|
|
|
titleStyle,
|
|
|
|
backgroundColor,
|
|
|
|
rootMenu,
|
|
|
|
currentPlacement,
|
|
|
|
menuTransitionName,
|
2018-02-01 07:05:07 +00:00
|
|
|
mode,
|
2018-02-11 06:59:46 +00:00
|
|
|
disabled,
|
2018-02-09 03:05:37 +00:00
|
|
|
popperClass,
|
2018-02-11 06:59:46 +00:00
|
|
|
$slots,
|
2018-03-08 11:19:10 +00:00
|
|
|
isFirstLevel
|
2018-01-23 07:58:46 +00:00
|
|
|
} = this;
|
|
|
|
|
|
|
|
const popupMenu = (
|
|
|
|
<transition name={menuTransitionName}>
|
|
|
|
<div
|
|
|
|
ref="menu"
|
|
|
|
v-show={opened}
|
2018-02-01 07:05:07 +00:00
|
|
|
class={[`el-menu--${mode}`, popperClass]}
|
2018-01-23 07:58:46 +00:00
|
|
|
on-mouseenter={this.handleMouseenter}
|
|
|
|
on-mouseleave={this.handleMouseleave}
|
|
|
|
on-focus={this.handleMouseenter}>
|
|
|
|
<ul
|
|
|
|
role="menu"
|
|
|
|
class={['el-menu el-menu--popup', `el-menu--popup-${currentPlacement}`]}
|
|
|
|
style={{ backgroundColor: rootMenu.backgroundColor || '' }}>
|
|
|
|
{$slots.default}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
);
|
|
|
|
|
|
|
|
const inlineMenu = (
|
|
|
|
<el-collapse-transition>
|
|
|
|
<ul
|
|
|
|
role="menu"
|
|
|
|
class="el-menu el-menu--inline"
|
|
|
|
v-show={opened}
|
|
|
|
style={{ backgroundColor: rootMenu.backgroundColor || '' }}>
|
|
|
|
{$slots.default}
|
|
|
|
</ul>
|
|
|
|
</el-collapse-transition>
|
|
|
|
);
|
|
|
|
|
2018-02-09 03:05:37 +00:00
|
|
|
const submenuTitleIcon = (
|
2018-03-08 11:19:10 +00:00
|
|
|
rootMenu.mode === 'horizontal' && isFirstLevel ||
|
2018-02-09 03:05:37 +00:00
|
|
|
rootMenu.mode === 'vertical' && !rootMenu.collapse
|
|
|
|
) ? 'el-icon-arrow-down' : 'el-icon-arrow-right';
|
|
|
|
|
2018-01-23 07:58:46 +00:00
|
|
|
return (
|
|
|
|
<li
|
|
|
|
class={{
|
|
|
|
'el-submenu': true,
|
|
|
|
'is-active': active,
|
2018-02-11 06:59:46 +00:00
|
|
|
'is-opened': opened,
|
|
|
|
'is-disabled': disabled
|
2018-01-23 07:58:46 +00:00
|
|
|
}}
|
|
|
|
role="menuitem"
|
|
|
|
aria-haspopup="true"
|
|
|
|
aria-expanded={opened}
|
|
|
|
on-mouseenter={this.handleMouseenter}
|
|
|
|
on-mouseleave={this.handleMouseleave}
|
|
|
|
on-focus={this.handleMouseenter}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="el-submenu__title"
|
|
|
|
ref="submenu-title"
|
|
|
|
on-click={this.handleClick}
|
|
|
|
on-mouseenter={this.handleTitleMouseenter}
|
|
|
|
on-mouseleave={this.handleTitleMouseleave}
|
|
|
|
style={[paddingStyle, titleStyle, { backgroundColor }]}
|
|
|
|
>
|
|
|
|
{$slots.title}
|
2018-02-09 03:05:37 +00:00
|
|
|
<i class={[ 'el-submenu__icon-arrow', submenuTitleIcon ]}></i>
|
2018-01-23 07:58:46 +00:00
|
|
|
</div>
|
|
|
|
{this.isMenuPopup ? popupMenu : inlineMenu}
|
|
|
|
</li>
|
|
|
|
);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|