iview/src/components/tree/tree.vue

331 lines
13 KiB
Vue
Raw Normal View History

<template>
2017-02-07 11:34:00 +08:00
<ul :class="classes">
<li v-for="(item, index) in data" :class="itemCls(item)">
<span :class="arrowCls(item)" @click="setExpand(item.disabled, index)">
2017-02-07 13:34:16 +08:00
<Icon type="arrow-right-b"></Icon>
2017-02-07 11:34:00 +08:00
</span>
2017-02-07 13:34:16 +08:00
<Checkbox
2017-02-07 15:01:46 +08:00
v-if="showCheckbox"
:value="item.checked && item.childrenCheckedStatus == 2"
2017-02-07 13:34:16 +08:00
:disabled="item.disabled || item.disableCheckbox"
:indeterminate="item.checked && item.childrenCheckedStatus == 1"
2017-03-03 10:35:38 +08:00
@click.native.prevent="setCheck(item.disabled||item.disableCheckbox, index)"></Checkbox>
<a :class="titleCls(item)" @click="setSelect(item.disabled, index)">
2017-02-07 11:34:00 +08:00
<span :class="[prefixCls + '-title']" v-html="item.title"></span>
</a>
<transition name="slide-up">
<Tree
v-if="!item.isLeaf"
v-show="item.expand"
:class="expandCls(item)"
:data="item.children"
:name="name+'.'+index"
:multiple="multiple"
:show-checkbox="showCheckbox"></Tree>
</transition>
2017-02-07 11:34:00 +08:00
</li>
</ul>
</template>
<script>
2017-02-07 13:34:16 +08:00
import Icon from '../icon/icon.vue';
import Checkbox from '../checkbox/checkbox.vue';
2017-02-07 11:34:00 +08:00
import { t } from '../../locale';
2017-03-03 11:00:11 +08:00
import Emitter from '../../mixins/emitter';
2017-02-07 11:34:00 +08:00
const prefixCls = 'ivu-tree';
export default {
name: 'Tree',
2017-02-07 13:34:16 +08:00
components: { Icon, Checkbox },
2017-03-03 11:00:11 +08:00
mixins: [ Emitter ],
2017-02-07 11:34:00 +08:00
props: {
data: {
2017-02-07 11:34:00 +08:00
type: Array,
default () {
return [];
}
},
name: {
2017-02-07 11:34:00 +08:00
type: String,
default: '0'
},
multiple: {
type: Boolean,
default: false
},
showCheckbox: {
type: Boolean,
default: false
},
emptyText: {
type: String,
default () {
return t('i.tree.emptyText');
}
}
},
data () {
2017-02-07 11:34:00 +08:00
return {
prefixCls: prefixCls
2017-02-07 11:34:00 +08:00
};
},
2017-02-07 11:34:00 +08:00
computed: {
classes () {
if (this.name === '0') {
2017-02-07 11:34:00 +08:00
return this.prefixCls;
} else {
return `${this.prefixCls}-child-tree`;
}
}
},
watch: {
data () {
if (this.name === '0') {
2017-02-07 11:34:00 +08:00
this.setKey();
this.preHandle();
}
}
},
methods: {
itemCls (item) {
return [
{
[`${prefixCls}-item-disabled`]: item.disabled
}
];
},
arrowCls (item) {
return [
`${this.prefixCls}-switcher`,
{
[`${this.prefixCls}-switcher-disabled`]: item.disabled,
[`${this.prefixCls}-noline_close`]: !item.expand && !item.isLeaf,
[`${this.prefixCls}-noline_open`]: item.expand && !item.isLeaf,
[`${this.prefixCls}-switcher-noop`]: item.isLeaf
}
];
},
titleCls (item) {
return [
{
[`${this.prefixCls}-node-selected`]: item.selected
}
];
},
expandCls (item) {
return [
{
[`${this.prefixCls}-child-tree-open`]: item.expand
}
];
},
setKey () {
for (let i = 0; i < this.data.length; i++) {
this.data[i].name = `${this.name}.${i}`;
2017-02-07 11:34:00 +08:00
}
},
2017-02-07 13:34:16 +08:00
preHandle () {
for (let [i, item] of this.data.entries()) {
2017-02-07 15:29:53 +08:00
if (!item.children || !item.children.length) {
// this.$set(`data[${i}].isLeaf`, true);
// this.$set(`data[${i}].childrenCheckedStatus`, 2);
this.$set(this.data[i], 'isLeaf', true);
this.$set(this.data[i], 'childrenCheckedStatus', 2);
2017-02-07 11:34:00 +08:00
continue;
}
if (item.checked && !item.childrenCheckedStatus) {
// this.$set(`data[${i}].childrenCheckedStatus`, 2);
this.$set(this.data[i], 'childrenCheckedStatus', 2);
// this.$broadcast('parentChecked', true, `${this.name}.${i}`);
this.broadcast('Tree', 'parentChecked', {
status: true,
name: `${this.name}.${i}`
});
2017-02-07 11:34:00 +08:00
} else {
2017-02-07 15:29:53 +08:00
let status = this.getChildrenCheckedStatus(item.children);
// this.$set(`data[${i}].childrenCheckedStatus`, status);
this.$set(this.data[i], 'childrenCheckedStatus', status);
// if (status !== 0) this.$set(`data[${i}].checked`, true);
if (status !== 0) this.$set(this.data[i], 'checked', true);
2017-02-07 11:34:00 +08:00
}
}
},
2017-02-07 13:34:16 +08:00
setExpand (disabled, index) {
2017-02-07 11:34:00 +08:00
if (!disabled) {
// this.$set(`data[${index}].expand`, !this.data[index].expand);
this.$set(this.data[index], 'expand', !this.data[index].expand);
2017-02-07 11:34:00 +08:00
}
},
2017-02-07 13:34:16 +08:00
setSelect (disabled, index) {
2017-02-07 11:34:00 +08:00
if (!disabled) {
const selected = !this.data[index].selected;
if (this.multiple || !selected) {
// this.$set(`data[${index}].selected`, selected);
this.$set(this.data[index], 'selected', selected);
2017-02-07 11:34:00 +08:00
} else {
for (let i = 0; i < this.data.length; i++) {
if (i == index) {
// this.$set(`data[${i}].selected`, true);
this.$set(this.data[i], 'selected', true);
2017-02-07 11:34:00 +08:00
} else {
// this.$set(`data[${i}].selected`, false);
this.$set(this.data[i], 'selected', false);
2017-02-07 11:34:00 +08:00
}
}
}
// this.$dispatch('nodeSelected', this, selected);
this.dispatch('Tree', 'nodeSelected', {
ori: this,
selected: selected
2017-03-03 10:35:38 +08:00
});
2017-02-07 11:34:00 +08:00
}
},
2017-02-07 13:34:16 +08:00
setCheck (disabled, index) {
2017-02-07 11:34:00 +08:00
if (disabled) return;
const checked = !this.data[index].checked;
// this.$set(`data[${index}].checked`, checked);
this.$set(this.data[index], 'checked', checked);
// this.$set(`data[${index}].childrenCheckedStatus`, checked ? 2 : 0);
this.$set(this.data[index], 'childrenCheckedStatus', checked ? 2 : 0);
// this.$dispatch('childChecked', this, this.name);
this.dispatch('Tree', 'childChecked', {
ori: this,
name: this.name
});
// this.$broadcast('parentChecked', checked, `${this.name}.${index}`);
this.broadcast('Tree', 'parentChecked', {
status: checked,
name: `${this.name}.${index}`
});
2017-02-07 11:34:00 +08:00
},
2017-02-07 13:34:16 +08:00
getNodes (data, opt) {
2017-02-07 11:34:00 +08:00
data = data || this.data;
let res = [];
for (let node of data) {
let tmp = true;
for (let [key, value] of Object.entries(opt)) {
if (node[key] != value) {
tmp = false;
break;
}
}
if (tmp) {
res.push(node);
}
2017-02-07 15:29:53 +08:00
if (node.children && node.children.length) {
res = res.concat(this.getNodes(node.children, opt));
2017-02-07 11:34:00 +08:00
}
}
return res;
},
2017-02-07 13:34:16 +08:00
getSelectedNodes () {
2017-02-07 11:34:00 +08:00
return this.getNodes(this.data, {selected: true});
},
2017-02-07 13:34:16 +08:00
getCheckedNodes () {
2017-02-07 11:34:00 +08:00
return this.getNodes(this.data, {checked: true, childrenCheckedStatus: 2});
},
2017-02-07 13:34:16 +08:00
getChildrenCheckedStatus (children) {
2017-02-07 11:34:00 +08:00
let checkNum = 0, child_childrenAllChecked = true;
for (let child of children) {
if (child.checked) {
checkNum++;
}
if (child.childrenCheckedStatus !== 2) {
child_childrenAllChecked = false;
}
}
// select all
if (checkNum == children.length) {
return child_childrenAllChecked ? 2 : 1;
// select some
} else if (checkNum > 0) {
return 1;
} else {
return 0;
}
}
},
mounted () {
2017-02-07 11:34:00 +08:00
this.setKey();
this.preHandle();
// this.$on('nodeSelected', (ori, selected) => {
this.$on('nodeSelected', (params) => {
const ori = params.ori;
const selected = params.selected;
if (this.name !== '0') return true;
2017-02-07 11:34:00 +08:00
if (!this.multiple && selected) {
if (this !== ori) {
for (let i = 0; i < this.data.length; i++) {
// this.$set(`data[${i}].selected`, false);
this.$set(this.data[i], 'selected', false);
2017-02-07 11:34:00 +08:00
}
}
// this.$broadcast('cancelSelected', ori);
this.broadcast('Tree', 'cancelSelected', ori);
2017-02-07 11:34:00 +08:00
}
2017-02-07 15:29:53 +08:00
this.$nextTick(() => {
this.$emit('on-select-change', this.getSelectedNodes());
});
2017-02-07 11:34:00 +08:00
});
this.$on('cancelSelected', ori => {
// this.$broadcast('cancelSelected', ori);
this.broadcast('Tree', 'cancelSelected', ori);
2017-02-07 11:34:00 +08:00
if (this !== ori) {
for (let i = 0; i < this.data.length; i++) {
// this.$set(`data[${i}].selected`, false);
this.$set(this.data[i], 'selected', false);
2017-02-07 11:34:00 +08:00
}
}
});
// this.$on('parentChecked', (status, name) => {
this.$on('parentChecked', (params) => {
const status = params.status;
const name = params.name;
if (this.name == name || this.name.startsWith(name + '.')) {
2017-02-07 11:34:00 +08:00
for (let i = 0; i < this.data.length; i++) {
// this.$set(`data[${i}].checked`, status);
this.$set(this.data[i], 'checked', status);
// this.$set(`data[${i}].childrenCheckedStatus`, status ? 2 : 0);
this.$set(this.data[i], 'childrenCheckedStatus', status ? 2 : 0);
2017-02-07 11:34:00 +08:00
}
// this.$broadcast('parentChecked', status, name);
this.broadcast('Tree', 'parentChecked', {
status: status,
name: name
});
2017-02-07 11:34:00 +08:00
}
});
// this.$on('childChecked', (ori, name) => {
this.$on('childChecked', (params) => {
const ori = params.ori;
const name = params.name;
if (this.name === '0') {
2017-02-07 11:34:00 +08:00
this.$nextTick(() => {
2017-02-07 15:29:53 +08:00
this.$emit('on-check-change', this.getCheckedNodes());
2017-02-07 11:34:00 +08:00
});
}
if (this === ori) return;
for (let [i,item] of this.data.entries()) {
if (this.name + '.' + i == name) {
2017-02-07 15:29:53 +08:00
let temp = this.getChildrenCheckedStatus(item.children);
2017-02-07 11:34:00 +08:00
if (temp != item.childrenCheckedStatus) {
// this.$set(`data[${i}].checked`, !!temp);
this.$set(this.data[i], 'checked', !!temp);
// this.$set(`data[${i}].childrenCheckedStatus`, temp);
this.$set(this.data[i], 'childrenCheckedStatus', temp);
// if (this.name !== '0') this.$dispatch('childChecked', this, this.name);
if (this.name !== '0') this.dispatch('Tree', 'childChecked', {
ori: this,
name: this.name
});
2017-02-07 11:34:00 +08:00
}
}
}
});
}
};
</script>