iview/src/components/cascader/caspanel.vue

111 lines
3.7 KiB
Vue
Raw Normal View History

2016-11-15 19:17:54 +08:00
<template>
<ul v-if="data && data.length" :class="[prefixCls + '-menu']">
<Casitem
v-for="item in data"
:prefix-cls="prefixCls"
:data.sync="item"
:tmp-item="tmpItem"
@click.stop="handleClickItem(item)"
@mouseenter.stop="handleHoverItem(item)"></Casitem>
2016-11-15 23:23:16 +08:00
</ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data.sync="sublist" :disabled="disabled" :trigger="trigger" :change-on-select="changeOnSelect"></Caspanel>
2016-11-15 19:17:54 +08:00
</template>
<script>
import Casitem from './casitem.vue';
import { oneOf } from '../../utils/assist';
export default {
name: 'Caspanel',
components: { Casitem },
props: {
data: {
type: Array,
default () {
return []
}
},
sublist: {
type: Array,
default () {
return []
}
},
disabled: Boolean,
changeOnSelect: Boolean,
2016-11-15 23:23:16 +08:00
trigger: String,
2016-11-15 19:17:54 +08:00
prefixCls: String
},
data () {
return {
tmpItem: {},
result: []
}
},
methods: {
handleClickItem (item) {
2016-11-15 23:23:16 +08:00
if (this.trigger !== 'click' && item.children) return;
2016-11-15 19:17:54 +08:00
this.handleTriggerItem(item);
},
handleHoverItem (item) {
2016-11-15 23:23:16 +08:00
if (this.trigger !== 'hover' || !item.children) return;
2016-11-15 19:17:54 +08:00
this.handleTriggerItem(item);
},
2016-11-15 23:23:16 +08:00
handleTriggerItem (item, fromInit = false) {
2016-11-15 19:17:54 +08:00
if (item.disabled) return;
2016-11-15 23:23:16 +08:00
// return value back recursion
const backItem = this.getBaseItem(item);
this.tmpItem = backItem;
this.emitUpdate([backItem]);
2016-11-15 19:17:54 +08:00
if (item.children && item.children.length){
this.sublist = item.children;
2016-11-15 23:23:16 +08:00
this.$dispatch('on-result-change', false, this.changeOnSelect, fromInit);
2016-11-15 19:17:54 +08:00
} else {
this.sublist = [];
2016-11-15 23:23:16 +08:00
this.$dispatch('on-result-change', true, this.changeOnSelect, fromInit);
2016-11-15 19:17:54 +08:00
}
},
updateResult (item) {
this.result = [this.tmpItem].concat(item);
2016-11-15 23:23:16 +08:00
this.emitUpdate(this.result);
2016-11-15 19:17:54 +08:00
},
getBaseItem (item) {
let backItem = Object.assign({}, item);
if (backItem.children) {
delete backItem.children;
}
return backItem;
2016-11-15 23:23:16 +08:00
},
emitUpdate (result) {
if (this.$parent.$options.name === 'Caspanel') {
this.$parent.updateResult(result);
} else {
this.$parent.$parent.updateResult(result);
}
2016-11-15 19:17:54 +08:00
}
},
watch: {
data () {
this.sublist = [];
}
},
2016-11-15 23:23:16 +08:00
events: {
'on-find-selected' (val) {
let value = [...val];
for (let i = 0; i < value.length; i++) {
for (let j = 0; j < this.data.length; j++) {
if (value[i] === this.data[j].value) {
this.handleTriggerItem(this.data[j], true);
value.splice(0, 1);
this.$nextTick(() => {
this.$broadcast('on-find-selected', value);
});
return false;
}
}
}
}
2016-11-15 19:17:54 +08:00
}
}
</script>