element/packages/tree/src/tree-node.vue

123 lines
3.2 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div class="el-tree-node"
:class="{ expanded: childrenRendered && expanded }">
<div class="el-tree-node__content" :style="{ 'padding-left': node.level * 16 + 'px' }"
@click="handleExpandIconClick">
<span class="el-tree-node__expand-icon"
:class="{ 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded }"
></span>
2016-09-08 03:37:30 +00:00
<el-checkbox v-if="showCheckbox" :indeterminate="node.indeterminate" v-model="node.checked" @change="handleCheckChange" @click.native="handleUserClick"></el-checkbox>
2016-09-04 02:27:09 +00:00
<span
v-if="node.loading"
class="el-tree-node__icon el-icon-loading"
>
2016-09-04 02:27:09 +00:00
</span>
<span class="el-tree-node__label" v-html="node.label"></span>
2016-07-27 06:15:02 +00:00
</div>
2016-08-12 06:45:06 +00:00
<collapse-transition>
<div class="el-tree-node__children"
v-show="expanded">
<el-tree-node v-for="child in node.children" :node="child"></el-tree-node>
</div>
</collapse-transition>
2016-07-27 06:15:02 +00:00
</div>
</template>
<script type="text/ecmascript-6">
import CollapseTransition from './transition';
export default {
name: 'el-tree-node',
props: {
node: {
default() {
return {};
}
}
},
2016-08-12 06:45:06 +00:00
components: {
CollapseTransition
},
2016-07-27 06:15:02 +00:00
data() {
return {
$tree: null,
expanded: false,
childrenRendered: false,
2016-10-05 15:06:17 +00:00
showCheckbox: false,
oldChecked: null,
oldIndeterminate: null
2016-07-27 06:15:02 +00:00
};
},
2016-10-05 15:06:17 +00:00
watch: {
'node.indeterminate'(val) {
this.handleSelectChange(this.node.checked, val);
},
'node.checked'(val) {
this.handleSelectChange(val, this.node.indeterminate);
}
},
2016-07-27 06:15:02 +00:00
methods: {
2016-10-05 15:06:17 +00:00
handleSelectChange(checked, indeterminate) {
if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
this.$tree.$emit('check-change', this.node.data, checked, indeterminate);
}
this.oldChecked = checked;
this.indeterminate = indeterminate;
},
2016-07-27 06:15:02 +00:00
handleExpandIconClick(event) {
let target = event.target;
if (target.tagName.toUpperCase() !== 'DIV' &&
target.parentNode.nodeName.toUpperCase() !== 'DIV' ||
target.nodeName.toUpperCase() === 'LABLE') return;
if (this.expanded) {
this.node.collapse();
this.expanded = false;
} else {
this.node.expand(() => {
this.expanded = true;
this.childrenRendered = true;
});
}
2016-10-05 15:06:17 +00:00
this.$tree.$emit('node-click', this.node.data);
2016-07-27 06:15:02 +00:00
},
2016-09-08 03:37:30 +00:00
handleUserClick() {
if (this.node.indeterminate) {
this.node.setChecked(this.node.checked, true);
}
},
2016-10-12 15:21:47 +00:00
handleCheckChange(ev) {
2016-09-08 03:37:30 +00:00
if (!this.node.indeterminate) {
2016-10-12 15:21:47 +00:00
this.node.setChecked(ev.target.checked, true);
2016-09-08 03:37:30 +00:00
}
2016-07-27 06:15:02 +00:00
}
},
created() {
var parent = this.$parent;
if (parent.$isTree) {
this.$tree = parent;
} else {
this.$tree = parent.$tree;
}
const tree = this.$tree;
if (!tree) {
console.warn('Can not find node\'s tree.');
}
this.showCheckbox = tree.showCheckbox;
}
};
</script>