support Menu & Layout

support Menu & Layout
This commit is contained in:
梁灏 2017-03-06 13:31:48 +08:00
parent dcbfc23239
commit fd1582c5b5
10 changed files with 284 additions and 118 deletions

View file

@ -2,12 +2,14 @@
<li :class="classes" @click.stop="handleClick"><slot></slot></li>
</template>
<script>
import Emitter from '../../mixins/emitter';
const prefixCls = 'ivu-menu';
export default {
name: 'MenuItem',
mixins: [ Emitter ],
props: {
key: {
name: {
type: [String, Number],
required: true
},
@ -36,8 +38,30 @@
methods: {
handleClick () {
if (this.disabled) return;
this.$dispatch('on-menu-item-select', this.key);
let parent = this.$parent;
let name = parent.$options.name;
while (parent && (!name || name !== 'Submenu')) {
parent = parent.$parent;
if (parent) name = parent.$options.name;
}
if (parent) {
this.dispatch('Submenu', 'on-menu-item-select', this.name);
} else {
this.dispatch('Menu', 'on-menu-item-select', this.name);
}
}
},
mounted () {
this.$on('on-update-active-name', (name) => {
if (this.name === name) {
this.active = true;
this.dispatch('Submenu', 'on-update-active-name', true);
} else {
this.active = false;
}
});
}
};
</script>

View file

@ -3,10 +3,13 @@
</template>
<script>
import { oneOf } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
const prefixCls = 'ivu-menu';
export default {
name: 'Menu',
mixins: [ Emitter ],
props: {
mode: {
validator (value) {
@ -20,10 +23,10 @@
},
default: 'light'
},
activeKey: {
activeName: {
type: [String, Number]
},
openKeys: {
openNames: {
type: Array,
default () {
return [];
@ -38,6 +41,11 @@
default: '240px'
}
},
data () {
return {
currentActiveName: this.activeName
};
},
computed: {
classes () {
let theme = this.theme;
@ -60,74 +68,46 @@
}
},
methods: {
updateActiveKey () {
this.$children.forEach((item, index) => {
if (!this.activeKey && index === 0) {
this.activeKey = -1;
}
if (item.$options.name === 'Submenu') {
item.active = false;
item.$children.forEach(subitem => {
if (subitem.$options.name === 'MenuGroup') {
subitem.$children.forEach(groupItem => {
if (groupItem.key === this.activeKey) {
groupItem.active = true;
groupItem.$parent.$parent.active = true;
} else {
groupItem.active = false;
}
});
} else if (subitem.$options.name === 'MenuItem') {
if (subitem.key === this.activeKey) {
subitem.active = true;
subitem.$parent.active = true;
} else {
subitem.active = false;
}
}
});
} else if (item.$options.name === 'MenuGroup') {
item.$children.forEach(groupItem => {
groupItem.active = groupItem.key === this.activeKey;
});
} else if (item.$options.name === 'MenuItem') {
item.active = item.key === this.activeKey;
}
});
updateActiveName () {
if (!this.currentActiveName) {
this.currentActiveName = -1;
}
this.broadcast('Submenu', 'on-update-active-name', false);
this.broadcast('MenuItem', 'on-update-active-name', this.currentActiveName);
},
updateOpenKeys (key) {
const index = this.openKeys.indexOf(key);
updateOpenKeys (name) {
const index = this.openNames.indexOf(name);
if (index > -1) {
this.openKeys.splice(index, 1);
this.openNames.splice(index, 1);
} else {
this.openKeys.push(key);
this.openNames.push(name);
}
},
updateOpened () {
this.$children.forEach(item => {
if (item.$options.name === 'Submenu') {
if (this.openKeys.indexOf(item.key) > -1) item.opened = true;
if (this.openNames.indexOf(item.name) > -1) item.opened = true;
}
});
}
},
compiled () {
this.updateActiveKey();
mounted () {
this.updateActiveName();
this.updateOpened();
},
events: {
'on-menu-item-select' (key) {
this.activeKey = key;
this.$emit('on-select', key);
}
this.$on('on-menu-item-select', (name) => {
this.currentActiveName = name;
this.$emit('on-select', name);
});
},
watch: {
openKeys () {
this.$emit('on-open-change', this.openKeys);
openNames () {
this.$emit('on-open-change', this.openNames);
},
activeKey () {
this.updateActiveKey();
activeName (val) {
this.currentActiveName = val;
},
currentActiveName () {
this.updateActiveName();
}
}
};

View file

@ -1,31 +1,33 @@
<template>
<li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
<div :class="[prefixCls + '-submenu-title']" v-el:reference @click="handleClick">
<div :class="[prefixCls + '-submenu-title']" ref="reference" @click="handleClick">
<slot name="title"></slot>
<Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
</div>
<ul :class="[prefixCls]" v-if="mode === 'vertical'" v-show="opened"><slot></slot></ul>
<Drop
v-else
v-show="opened"
placement="bottom"
transition="slide-up"
v-ref:drop
:style="dropStyle"><slot></slot></Drop>
<transition name="slide-up" v-else>
<Drop
v-show="opened"
placement="bottom"
ref="drop"
:style="dropStyle"><slot></slot></Drop>
</transition>
</li>
</template>
<script>
import Drop from '../select/dropdown.vue';
import Icon from '../icon/icon.vue';
import { getStyle } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
const prefixCls = 'ivu-menu';
export default {
name: 'Submenu',
mixins: [ Emitter ],
components: { Icon, Drop },
props: {
key: {
name: {
type: [String, Number],
required: true
},
@ -54,9 +56,11 @@
];
},
mode () {
// todo while
return this.$parent.mode;
},
accordion () {
// todo while
return this.$parent.accordion;
},
dropStyle () {
@ -73,7 +77,8 @@
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.$parent.updateOpenKeys(this.key);
// todo while
this.$parent.updateOpenKeys(this.name);
this.opened = true;
}, 250);
},
@ -83,7 +88,8 @@
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.$parent.updateOpenKeys(this.key);
// todo while
this.$parent.updateOpenKeys(this.name);
this.opened = false;
}, 150);
},
@ -92,12 +98,14 @@
if (this.mode === 'horizontal') return;
const opened = this.opened;
if (this.accordion) {
// todo while
this.$parent.$children.forEach(item => {
if (item.$options.name === 'Submenu') item.opened = false;
});
}
this.opened = !opened;
this.$parent.updateOpenKeys(this.key);
// todo while
this.$parent.updateOpenKeys(this.name);
}
},
watch: {
@ -117,11 +125,15 @@
}
}
},
events: {
'on-menu-item-select' () {
mounted () {
this.$on('on-menu-item-select', (name) => {
if (this.mode === 'horizontal') this.opened = false;
this.dispatch('Menu', 'on-menu-item-select', name);
return true;
}
});
this.$on('on-update-active-name', (status) => {
this.active = status;
});
}
};
</script>