iview/src/components/tree/node.vue

145 lines
4.9 KiB
Vue
Raw Normal View History

2017-03-24 20:50:13 +08:00
<template>
2017-06-02 14:30:03 +08:00
<collapse-transition>
<ul :class="classes">
2017-03-24 20:50:13 +08:00
<li>
<span :class="arrowClasses" @click="handleExpand">
2017-10-24 18:14:22 +08:00
<Icon v-if="showArrow" type="arrow-right-b"></Icon>
<Icon v-if="showLoading" type="load-c" class="ivu-load-loop"></Icon>
2017-03-24 20:50:13 +08:00
</span>
<Checkbox
2017-06-02 14:30:03 +08:00
v-if="showCheckbox"
:value="data.checked"
:indeterminate="data.indeterminate"
2017-06-02 14:30:03 +08:00
:disabled="data.disabled || data.disableCheckbox"
@click.native.prevent="handleCheck"></Checkbox>
<Render v-if="data.render" :render="data.render"></Render>
<span v-else :class="titleClasses" v-html="data.title" @click="handleSelect"></span>
2017-03-24 20:50:13 +08:00
<Tree-node
v-if="data.expand"
2017-10-24 14:08:52 +02:00
v-for="(item, i) in data.children"
:key="i"
2017-06-02 14:30:03 +08:00
:data="item"
:multiple="multiple"
:show-checkbox="showCheckbox">
2017-03-24 20:50:13 +08:00
</Tree-node>
</li>
</ul>
2017-06-02 14:30:03 +08:00
</collapse-transition>
2017-03-24 20:50:13 +08:00
</template>
<script>
import Checkbox from '../checkbox/checkbox.vue';
2017-04-07 11:29:31 +08:00
import Icon from '../icon/icon.vue';
import Render from '../base/render';
2017-06-02 14:30:03 +08:00
import CollapseTransition from '../base/collapse-transition';
2017-03-24 20:50:13 +08:00
import Emitter from '../../mixins/emitter';
2017-10-24 18:14:22 +08:00
import { findComponentUpward } from '../../utils/assist';
2017-03-24 20:50:13 +08:00
const prefixCls = 'ivu-tree';
export default {
name: 'TreeNode',
mixins: [ Emitter ],
components: { Checkbox, Icon, CollapseTransition, Render },
2017-03-24 20:50:13 +08:00
props: {
data: {
type: Object,
default () {
return {};
}
},
multiple: {
type: Boolean,
default: false
},
showCheckbox: {
type: Boolean,
default: false
}
},
data () {
return {
prefixCls: prefixCls
2017-03-24 20:50:13 +08:00
};
2017-10-24 14:08:52 +02:00
},
watch: {
2017-03-24 20:50:13 +08:00
},
computed: {
classes () {
return [
`${prefixCls}-children`
2017-03-24 21:06:49 +08:00
];
2017-03-24 20:50:13 +08:00
},
selectedCls () {
return [
{
[`${prefixCls}-node-selected`]: this.data.selected
}
];
},
arrowClasses () {
return [
`${prefixCls}-arrow`,
{
[`${prefixCls}-arrow-disabled`]: this.data.disabled,
2017-10-24 18:14:22 +08:00
[`${prefixCls}-arrow-open`]: this.data.expand
2017-03-24 20:50:13 +08:00
}
];
},
titleClasses () {
return [
`${prefixCls}-title`,
{
[`${prefixCls}-title-selected`]: this.data.selected
}
];
2017-10-24 18:14:22 +08:00
},
showArrow () {
return (this.data.children && this.data.children.length) || ('loading' in this.data && !this.data.loading);
},
showLoading () {
return 'loading' in this.data && this.data.loading;
2017-03-24 20:50:13 +08:00
}
},
methods: {
handleExpand () {
2017-10-24 18:14:22 +08:00
const item = this.data;
if (item.disabled) return;
// async loading
2017-10-24 14:08:52 +02:00
if (item.children.length === 0) {
2017-10-24 18:14:22 +08:00
const tree = findComponentUpward(this, 'Tree');
if (tree && tree.loadData) {
2017-10-24 14:08:52 +02:00
this.$set(this.data, 'loading', true);
tree.loadData(item, children => {
this.$set(this.data, 'loading', false);
if (children.length) {
this.$set(this.data, 'children', children);
this.$nextTick(() => this.handleExpand());
2017-10-24 18:14:22 +08:00
}
});
return;
}
}
if (item.children && item.children.length) {
this.$set(this.data, 'expand', !this.data.expand);
this.dispatch('Tree', 'toggle-expand', this.data);
}
2017-03-24 20:50:13 +08:00
},
handleSelect () {
if (this.data.disabled) return;
this.dispatch('Tree', 'on-selected', this.data.nodeKey);
2017-03-24 20:50:13 +08:00
},
handleCheck () {
if (this.data.disabled) return;
const changes = {
checked: !this.data.checked && !this.data.indeterminate,
nodeKey: this.data.nodeKey
};
this.dispatch('Tree', 'on-check', changes);
2017-03-24 20:50:13 +08:00
}
}
};
</script>