2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
|
|
|
<div class="el-tree-node"
|
2016-10-17 16:31:37 +00:00
|
|
|
@click.stop="handleClick"
|
2016-11-21 16:08:22 +00:00
|
|
|
v-show="node.visible"
|
|
|
|
:class="{ 'is-expanded': childNodeRendered && expanded, 'is-current': $tree.currentNode === _self, 'is-hidden': !node.visible }">
|
2016-10-17 16:31:37 +00:00
|
|
|
<div class="el-tree-node__content"
|
2016-11-16 07:35:46 +00:00
|
|
|
:style="{ 'padding-left': (node.level - 1) * 16 + 'px' }"
|
2016-10-17 16:31:37 +00:00
|
|
|
@click="handleExpandIconClick">
|
|
|
|
<span
|
|
|
|
class="el-tree-node__expand-icon"
|
|
|
|
:class="{ 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded }">
|
|
|
|
</span>
|
|
|
|
<el-checkbox
|
|
|
|
v-if="showCheckbox"
|
|
|
|
v-model="node.checked"
|
|
|
|
:indeterminate="node.indeterminate"
|
|
|
|
@change="handleCheckChange"
|
|
|
|
@click.native="handleUserClick">
|
|
|
|
</el-checkbox>
|
2016-09-04 02:27:09 +00:00
|
|
|
<span
|
|
|
|
v-if="node.loading"
|
2016-11-16 07:35:46 +00:00
|
|
|
class="el-tree-node__loading-icon el-icon-loading">
|
2016-09-04 02:27:09 +00:00
|
|
|
</span>
|
2016-10-17 16:31:37 +00:00
|
|
|
<node-content :node="node"></node-content>
|
2016-07-27 06:15:02 +00:00
|
|
|
</div>
|
2016-08-12 06:45:06 +00:00
|
|
|
<collapse-transition>
|
2016-10-17 16:31:37 +00:00
|
|
|
<div
|
|
|
|
class="el-tree-node__children"
|
2016-08-12 06:45:06 +00:00
|
|
|
v-show="expanded">
|
2016-10-17 16:31:37 +00:00
|
|
|
<el-tree-node
|
|
|
|
:render-content="renderContent"
|
|
|
|
v-for="child in node.childNodes"
|
2016-11-16 07:35:46 +00:00
|
|
|
:key="getNodeKey(child)"
|
2016-10-17 16:31:37 +00:00
|
|
|
:node="child">
|
|
|
|
</el-tree-node>
|
2016-08-12 06:45:06 +00:00
|
|
|
</div>
|
|
|
|
</collapse-transition>
|
2016-07-27 06:15:02 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2016-10-17 16:31:37 +00:00
|
|
|
<script type="text/jsx">
|
2016-07-27 06:15:02 +00:00
|
|
|
import CollapseTransition from './transition';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'el-tree-node',
|
|
|
|
|
|
|
|
props: {
|
|
|
|
node: {
|
|
|
|
default() {
|
|
|
|
return {};
|
|
|
|
}
|
2016-10-17 16:31:37 +00:00
|
|
|
},
|
|
|
|
props: {},
|
|
|
|
renderContent: Function
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2016-08-12 06:45:06 +00:00
|
|
|
components: {
|
2016-10-17 16:31:37 +00:00
|
|
|
CollapseTransition,
|
|
|
|
NodeContent: {
|
|
|
|
props: {
|
|
|
|
node: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render(h) {
|
|
|
|
const parent = this.$parent;
|
|
|
|
return (
|
|
|
|
parent.renderContent
|
|
|
|
? parent.renderContent.call(parent._renderProxy, h, { _self: parent.$parent.$vnode.context, node: this.node })
|
|
|
|
: <span class="el-tree-node__label">{ this.node.label }</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 06:45:06 +00:00
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
$tree: null,
|
|
|
|
expanded: false,
|
2016-10-17 16:31:37 +00:00
|
|
|
childNodeRendered: 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-11-16 07:35:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'node.expanded'(val) {
|
|
|
|
this.expanded = val;
|
|
|
|
if (val) {
|
|
|
|
this.childNodeRendered = true;
|
|
|
|
}
|
2016-10-05 15:06:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
methods: {
|
2016-11-16 07:35:46 +00:00
|
|
|
getNodeKey(node, index) {
|
|
|
|
const nodeKey = this.$tree.nodeKey;
|
|
|
|
if (nodeKey && node) {
|
|
|
|
return node.data[nodeKey];
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
|
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-10-17 16:31:37 +00:00
|
|
|
handleClick() {
|
|
|
|
this.$tree.currentNode = this;
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
handleExpandIconClick(event) {
|
|
|
|
let target = event.target;
|
|
|
|
if (target.tagName.toUpperCase() !== 'DIV' &&
|
2016-10-17 16:31:37 +00:00
|
|
|
target.parentNode.nodeName.toUpperCase() !== 'DIV' ||
|
|
|
|
target.nodeName.toUpperCase() === 'LABEL') return;
|
2016-07-27 06:15:02 +00:00
|
|
|
if (this.expanded) {
|
|
|
|
this.node.collapse();
|
|
|
|
} else {
|
2016-11-16 07:35:46 +00:00
|
|
|
this.node.expand();
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-10-17 16:31:37 +00:00
|
|
|
this.$tree.$emit('node-click', this.node.data, this.node, this);
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2016-09-08 03:37:30 +00:00
|
|
|
handleUserClick() {
|
|
|
|
if (this.node.indeterminate) {
|
2016-11-16 07:35:46 +00:00
|
|
|
this.node.setChecked(this.node.checked, !this.$tree.checkStrictly);
|
2016-09-08 03:37:30 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-12 15:21:47 +00:00
|
|
|
handleCheckChange(ev) {
|
2016-09-08 03:37:30 +00:00
|
|
|
if (!this.node.indeterminate) {
|
2016-11-16 07:35:46 +00:00
|
|
|
this.node.setChecked(ev.target.checked, !this.$tree.checkStrictly);
|
2016-09-08 03:37:30 +00:00
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
2016-11-16 07:35:46 +00:00
|
|
|
const parent = this.$parent;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
if (parent.$isTree) {
|
|
|
|
this.$tree = parent;
|
|
|
|
} else {
|
|
|
|
this.$tree = parent.$tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tree = this.$tree;
|
2016-10-17 16:31:37 +00:00
|
|
|
const props = this.props || {};
|
|
|
|
const childrenKey = props['children'] || 'children';
|
|
|
|
|
|
|
|
this.$watch(`node.data.${childrenKey}`, () => {
|
|
|
|
this.node.updateChildren();
|
|
|
|
});
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
if (!tree) {
|
|
|
|
console.warn('Can not find node\'s tree.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.showCheckbox = tree.showCheckbox;
|
2016-11-16 07:35:46 +00:00
|
|
|
|
|
|
|
if (this.node.expanded) {
|
|
|
|
this.expanded = true;
|
|
|
|
this.childNodeRendered = true;
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|