iview/src/components/cascader/caspanel.vue

164 lines
6 KiB
Vue
Raw Normal View History

2016-11-15 19:17:54 +08:00
<template>
2017-03-06 17:30:39 +08:00
<span>
<ul v-if="data && data.length" :class="[prefixCls + '-menu']">
<Casitem
v-for="item in data"
2017-07-14 15:13:09 +08:00
:key="getKey()"
2017-03-06 17:30:39 +08:00
:prefix-cls="prefixCls"
:data="item"
:tmp-item="tmpItem"
@click.native.stop="handleClickItem(item)"
@mouseenter.native.stop="handleHoverItem(item)"></Casitem>
</ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data="sublist" :disabled="disabled" :trigger="trigger" :change-on-select="changeOnSelect"></Caspanel>
</span>
2016-11-15 19:17:54 +08:00
</template>
<script>
import Casitem from './casitem.vue';
2017-03-06 17:30:39 +08:00
import Emitter from '../../mixins/emitter';
2017-08-08 17:19:03 +08:00
import { findComponentUpward, findComponentDownward } from '../../utils/assist';
2016-11-15 19:17:54 +08:00
2017-07-14 15:13:09 +08:00
let key = 1;
2016-11-15 19:17:54 +08:00
export default {
name: 'Caspanel',
2017-03-06 17:30:39 +08:00
mixins: [ Emitter ],
2016-11-15 19:17:54 +08:00
components: { Casitem },
props: {
data: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
2016-11-15 19:17:54 +08:00
}
},
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: {},
2017-03-06 17:30:39 +08:00
result: [],
sublist: []
2016-12-25 22:49:42 +08:00
};
2016-11-15 19:17:54 +08:00
},
2017-03-06 17:30:39 +08:00
watch: {
data () {
this.sublist = [];
}
},
2016-11-15 19:17:54 +08:00
methods: {
handleClickItem (item) {
2017-10-23 23:08:40 +08:00
if (this.trigger !== 'click' && item.children && item.children.length) return; // #1922
2017-05-19 10:08:19 +08:00
this.handleTriggerItem(item, false, true);
2016-11-15 19:17:54 +08:00
},
handleHoverItem (item) {
2017-10-23 23:08:40 +08:00
if (this.trigger !== 'hover' || !item.children || !item.children.length) return; // #1922
2017-05-19 10:08:19 +08:00
this.handleTriggerItem(item, false, true);
2016-11-15 19:17:54 +08:00
},
2017-05-19 10:08:19 +08:00
handleTriggerItem (item, fromInit = false, fromUser = false) {
2016-11-15 19:17:54 +08:00
if (item.disabled) return;
2017-05-09 15:46:08 +08:00
if (item.loading !== undefined && !item.children.length) {
const cascader = findComponentUpward(this, 'Cascader');
if (cascader && cascader.loadData) {
cascader.loadData(item, () => {
2017-05-19 10:08:19 +08:00
// todo
if (fromUser) {
cascader.isLoadedChildren = true;
}
2017-08-09 16:45:22 +08:00
if (item.children.length) {
this.handleTriggerItem(item);
}
2017-05-09 15:46:08 +08:00
});
return;
}
}
2017-04-22 14:00:10 +08:00
// return value back recursion // 向上递归,设置临时选中值(并非真实选中)
2016-11-15 23:23:16 +08:00
const backItem = this.getBaseItem(item);
if (backItem.label !== this.tmpItem.label || backItem.value !== this.tmpItem.value) {
this.tmpItem = backItem;
this.emitUpdate([backItem]);
}
2016-11-15 19:17:54 +08:00
if (item.children && item.children.length){
this.sublist = item.children;
2017-03-06 17:30:39 +08:00
this.dispatch('Cascader', 'on-result-change', {
lastValue: false,
changeOnSelect: this.changeOnSelect,
fromInit: fromInit
});
2017-08-08 17:19:03 +08:00
// #1553
if (this.changeOnSelect) {
const Caspanel = findComponentDownward(this, 'Caspanel');
if (Caspanel) {
Caspanel.$emit('on-clear', true);
}
}
2016-11-15 19:17:54 +08:00
} else {
this.sublist = [];
2017-03-06 17:30:39 +08:00
this.dispatch('Cascader', 'on-result-change', {
lastValue: true,
changeOnSelect: this.changeOnSelect,
fromInit: 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);
}
2017-07-14 15:13:09 +08:00
},
getKey () {
return key++;
2016-11-15 19:17:54 +08:00
}
},
2017-03-06 17:30:39 +08:00
mounted () {
this.$on('on-find-selected', (params) => {
const val = params.value;
2016-11-15 23:23:16 +08:00
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(() => {
2017-03-06 17:30:39 +08:00
this.broadcast('Caspanel', 'on-find-selected', {
value: value
});
2016-11-15 23:23:16 +08:00
});
return false;
}
}
}
2017-03-06 17:30:39 +08:00
});
2017-08-08 17:19:03 +08:00
// deep for #1553
this.$on('on-clear', (deep = false) => {
2016-11-16 10:07:03 +08:00
this.sublist = [];
this.tmpItem = {};
2017-08-08 17:19:03 +08:00
if (deep) {
const Caspanel = findComponentDownward(this, 'Caspanel');
if (Caspanel) {
Caspanel.$emit('on-clear', true);
}
}
2017-03-06 17:30:39 +08:00
});
2016-11-15 19:17:54 +08:00
}
2016-12-25 22:49:42 +08:00
};
</script>