Merge pull request #132 from baiyaaaaa/master

submenu auto expand based on default-active value
pull/144/head
杨奕 2016-09-22 21:40:07 -05:00 committed by GitHub
commit ba66b4d5b9
6 changed files with 121 additions and 128 deletions

View File

@ -1,6 +1,15 @@
## 更新日志 ## 更新日志
### 1.0.0-rc.5(待发布)
*2016-XX-XX*
- 修复 Table 头部不同步的问题 - 修复 Table 头部不同步的问题
- 修复 Menu 组件 default-active 绑定动态值无法更新的问题
- 新增特性 Menu 组件中若选中子菜单项现在会自动展开所有父级菜单
#### 非兼容性更新
- Menu 组件的 `unique-opend` 属性修正为 `unique-opened`
### 1.0.0-rc.4 ### 1.0.0-rc.4

View File

@ -30,12 +30,6 @@
<script> <script>
export default { export default {
data() {
return {
search: '',
search2: ''
};
},
methods: { methods: {
handleopen(key, keyPath) { handleopen(key, keyPath) {
console.log(key, keyPath); console.log(key, keyPath);
@ -159,7 +153,7 @@
| theme | 主题色 | string | light,dark | light | | theme | 主题色 | string | light,dark | light |
| default-active | 当前激活菜单的 index | string | — | — | | default-active | 当前激活菜单的 index | string | — | — |
| default-openeds | 当前打开的submenu的 key 数组 | Array | — | — | | default-openeds | 当前打开的submenu的 key 数组 | Array | — | — |
| unique-opend | 是否只保持一个子菜单的展开 | boolean | — | false | | unique-opened | 是否只保持一个子菜单的展开 | boolean | — | false |
| router | 是否使用 vue-router 的模式,启用该模式会在激活导航时以 index 作为 path 进行路由跳转 | boolean | — | false | | router | 是否使用 vue-router 的模式,启用该模式会在激活导航时以 index 作为 path 进行路由跳转 | boolean | — | false |
### Menu Events ### Menu Events

View File

@ -1,9 +1,12 @@
<script> <script>
import menuMixin from './menu-mixin';
module.exports = { module.exports = {
name: 'el-menu-item', name: 'el-menu-item',
componentName: 'menu-item', componentName: 'menu-item',
mixins: [menuMixin],
props: { props: {
index: { index: {
type: String, type: String,
@ -15,24 +18,6 @@
} }
}, },
computed: { computed: {
indexPath() {
var path = [this.index];
var parent = this.$parent;
while (parent.$options._componentTag !== 'el-menu') {
if (parent.index) {
path.unshift(parent.index);
}
parent = parent.$parent;
}
return path;
},
rootMenu() {
var parent = this.$parent;
while (parent.$options._componentTag !== 'el-menu') {
parent = parent.$parent;
}
return parent;
},
active() { active() {
return this.index === this.rootMenu.activeIndex; return this.index === this.rootMenu.activeIndex;
} }
@ -42,7 +27,8 @@
this.rootMenu.handleSelect(this.index, this.indexPath); this.rootMenu.handleSelect(this.index, this.indexPath);
} }
}, },
mounted() { created() {
this.rootMenu.menuItems[this.index] = this;
} }
}; };
</script> </script>

View File

@ -0,0 +1,22 @@
module.exports = {
computed: {
indexPath() {
var path = [this.index];
var parent = this.$parent;
while (parent.$options._componentTag !== 'el-menu') {
if (parent.index) {
path.unshift(parent.index);
}
parent = parent.$parent;
}
return path;
},
rootMenu() {
var parent = this.$parent;
while (parent.$options._componentTag !== 'el-menu') {
parent = parent.$parent;
}
return parent;
}
}
};

View File

@ -37,33 +37,67 @@
type: String, type: String,
default: 'light' default: 'light'
}, },
uniqueOpend: Boolean, uniqueOpened: Boolean,
router: Boolean router: Boolean
}, },
data() { data() {
return { return {
activeIndex: this.defaultActive, activeIndex: this.defaultActive,
openedMenus: this.defaultOpeneds.slice(0) openedMenus: this.defaultOpeneds.slice(0),
menuItems: {},
submenus: {}
}; };
}, },
watch: {
defaultActive(value) {
this.activeIndex = value;
let indexPath = this.menuItems[value].indexPath;
this.handleSelect(value, indexPath);
},
defaultOpeneds(value) {
this.openedMenus = value;
}
},
methods: { methods: {
handleMenuExpand(index, indexPath) { openMenu(index, indexPath) {
if (this.uniqueOpend) { let openedMenus = this.openedMenus;
this.broadcast('submenu', 'close-menu', indexPath); if (openedMenus.indexOf(index) !== -1) return;
this.openedMenus = this.openedMenus.filter((index) => { if (this.uniqueOpened) {
openedMenus = openedMenus.filter(index => {
return indexPath.indexOf(index) !== -1; return indexPath.indexOf(index) !== -1;
}); });
} }
this.$emit('open', index, indexPath); openedMenus.push(index);
}, },
handleMenuCollapse(index, indexPath) { closeMenu(index, indexPath) {
this.openedMenus.splice(this.openedMenus.indexOf(index), 1); this.openedMenus.splice(this.openedMenus.indexOf(index), 1);
},
handleSubmenuClick(index, indexPath) {
let isOpened = this.openedMenus.indexOf(index) !== -1;
if (isOpened) {
this.closeMenu(index, indexPath);
this.$emit('close', index, indexPath); this.$emit('close', index, indexPath);
} else {
this.openMenu(index, indexPath);
this.$emit('open', index, indexPath);
}
}, },
handleSelect(index, indexPath) { handleSelect(index, indexPath) {
this.activeIndex = index; this.activeIndex = index;
this.$emit('select', index, indexPath); this.$emit('select', index, indexPath);
this.broadcast('submenu', 'select', [index, indexPath]);
if (this.mode === 'horizontal') {
this.broadcast('submenu', 'item-select', [index, indexPath]);
this.openedMenus = [];
} else {
//
indexPath.forEach(index => {
let submenu = this.submenus[index];
submenu && this.openMenu(index, submenu.indexPath);
});
}
if (this.router) { if (this.router) {
this.$router.push(index); this.$router.push(index);
@ -71,9 +105,10 @@
} }
}, },
mounted() { mounted() {
this.broadcast('submenu', 'open-menu', this.openedMenus); let index = this.activeIndex;
this.$on('expand-menu', this.handleMenuExpand); let indexPath = this.menuItems[index].indexPath;
this.$on('collapse-menu', this.handleMenuCollapse);
this.handleSelect(index, indexPath);
} }
}; };
</script> </script>

View File

@ -1,12 +1,33 @@
<template>
<li
:class="{ 'el-submenu': true, 'is-active': active, 'is-opened': opened}"
@mouseenter="handleMouseenter"
@mouseleave="handleMouseleave"
>
<div class="el-submenu__title" @click="handleClick">
<slot name="title"></slot>
<i :class="{
'el-submenu__icon-arrow': true,
'el-icon-arrow-down': rootMenu.mode === 'vertical',
'el-icon-caret-bottom': rootMenu.mode === 'horizontal'
}">
</i>
</div>
<transition :name="rootMenu.mode === 'horizontal' ? 'md-fade-bottom' : ''">
<ul class="el-menu" v-show="opened"><slot></slot></ul>
</transition>
</li>
</template>
<script> <script>
import emitter from 'main/mixins/emitter'; import menuMixin from './menu-mixin';
module.exports = { module.exports = {
name: 'el-submenu', name: 'el-submenu',
componentName: 'submenu', componentName: 'submenu',
mixins: [emitter], mixins: [menuMixin],
props: { props: {
index: { index: {
@ -16,117 +37,43 @@
}, },
data() { data() {
return { return {
opened: false,
timeout: null, timeout: null,
active: false active: false
}; };
}, },
computed: { computed: {
indexPath() { opened() {
var path = [this.index]; return this.rootMenu.openedMenus.indexOf(this.index) !== -1;
var parent = this.$parent;
while (parent.$options._componentTag !== 'el-menu') {
if (parent.index) {
path.unshift(parent.index);
}
parent = parent.$parent;
}
return path;
},
rootMenu() {
var parent = this.$parent;
while (parent.$options._componentTag !== 'el-menu') {
parent = parent.$parent;
}
return parent;
},
mode() {
return this.rootMenu.mode;
} }
}, },
methods: { methods: {
handleClick() { handleClick() {
if (this.mode === 'vertical') { this.rootMenu.handleSubmenuClick(this.index, this.indexPath);
if (!this.opened) {
this.dispatch('menu', 'expand-menu', [this.index, this.indexPath]);
this.opened = true;
} else {
this.dispatch('menu', 'collapse-menu', [this.index, this.indexPath]);
this.opened = false;
}
}
}, },
handleMouseenter() { handleMouseenter() {
if (this.mode === 'horizontal') { if (this.rootMenu.mode === 'horizontal') {
clearTimeout(this.timeout); clearTimeout(this.timeout);
this.timeout = setTimeout(() => { this.timeout = setTimeout(() => {
this.dispatch('menu', 'expand-menu', [this.index, this.indexPath]); this.rootMenu.openMenu(this.index, this.indexPath);
this.opened = true;
}, 300); }, 300);
} }
}, },
handleMouseleave() { handleMouseleave() {
if (this.mode === 'horizontal') { if (this.rootMenu.mode === 'horizontal') {
clearTimeout(this.timeout); clearTimeout(this.timeout);
this.timeout = setTimeout(() => { this.timeout = setTimeout(() => {
this.dispatch('menu', 'collapse-menu', [this.index, this.indexPath]); this.rootMenu.closeMenu(this.index, this.indexPath);
this.opened = false;
}, 300); }, 300);
} }
} }
}, },
mounted() { created() {
this.$on('close-menu', (openedIndexs) => { this.rootMenu.submenus[this.index] = this;
if (openedIndexs && openedIndexs.indexOf(this.index) === -1) {
this.opened = false;
}
});
this.$on('open-menu', (IndexsArray) => {
if (IndexsArray && IndexsArray.indexOf(this.index) !== -1) {
this.opened = true;
}
});
this.$on('select', (index, indexPath) => {
if (this.mode === 'horizontal') {
this.active = indexPath.indexOf(this.index) !== -1;
this.opened = false;
}
});
}, },
render(h) { mounted() {
var submenu; this.$on('item-select', (index, indexPath) => {
this.active = indexPath.indexOf(this.index) !== -1;
if (this.mode === 'horizontal') { });
submenu = (
<transition name="md-fade-bottom">
{this.opened ? <ul class="el-menu">{this.$slots.default}</ul> : null }
</transition>
);
} else {
submenu = (
this.opened ? <ul class="el-menu">{this.$slots.default}</ul> : null
);
}
return (
<li
class={{ 'el-submenu': true, 'is-active': this.active, 'is-opened': this.opened}}
on-mouseenter={this.handleMouseenter}
on-mouseleave={this.handleMouseleave}
>
<div class="el-submenu__title" on-click={this.handleClick}>
{this.$slots.title}
<i class={{
'el-submenu__icon-arrow': true,
'el-icon-arrow-down': this.mode === 'vertical',
'el-icon-caret-bottom': this.mode === 'horizontal'
}}>
</i>
</div>
{submenu}
</li>
);
} }
}; };
</script> </script>