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

181 lines
4.5 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div class="el-tree-node"
@click.stop="handleClick"
v-show="node.visible"
:class="{ 'is-expanded': childNodeRendered && expanded, 'is-current': $tree.currentNode === _self, 'is-hidden': !node.visible }">
<div class="el-tree-node__content"
:style="{ 'padding-left': (node.level - 1) * 16 + 'px' }"
@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"
class="el-tree-node__loading-icon el-icon-loading">
2016-09-04 02:27:09 +00:00
</span>
<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>
<div
class="el-tree-node__children"
2016-08-12 06:45:06 +00:00
v-show="expanded">
<el-tree-node
:render-content="renderContent"
v-for="child in node.childNodes"
:key="getNodeKey(child)"
: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>
<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 {};
}
},
props: {},
renderContent: Function
2016-07-27 06:15:02 +00:00
},
2016-08-12 06:45:06 +00:00
components: {
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,
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);
},
'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: {
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;
},
handleClick() {
this.$tree.currentNode = this;
},
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() === 'LABEL') return;
2016-07-27 06:15:02 +00:00
if (this.expanded) {
this.node.collapse();
} else {
this.node.expand();
2016-07-27 06:15:02 +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) {
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) {
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() {
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;
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;
if (this.node.expanded) {
this.expanded = true;
this.childNodeRendered = true;
}
2016-07-27 06:15:02 +00:00
}
};
</script>