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

212 lines
5.4 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.store.currentNode === node,
'is-hidden': !node.visible
}">
<div class="el-tree-node__content"
2017-02-08 03:49:54 +00:00
:style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }">
<span
class="el-tree-node__expand-icon"
@click.stop="handleExpandIconClick"
:class="{ 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded }">
</span>
<el-checkbox
v-if="showCheckbox"
v-model="node.checked"
:indeterminate="node.indeterminate"
@change="handleCheckChange"
2017-01-05 09:07:21 +00:00
@click.native.stop="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>
<el-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)"
2017-01-14 08:02:52 +00:00
:node="child"
@node-expand="handleChildNodeExpand">
</el-tree-node>
2016-08-12 06:45:06 +00:00
</div>
</el-collapse-transition>
2016-07-27 06:15:02 +00:00
</div>
</template>
<script type="text/jsx">
2016-11-28 10:57:09 +00:00
import ElCheckbox from 'element-ui/packages/checkbox';
2017-01-14 08:02:52 +00:00
import emitter from 'element-ui/src/mixins/emitter';
2016-07-27 06:15:02 +00:00
export default {
name: 'ElTreeNode',
2016-07-27 06:15:02 +00:00
2017-01-14 08:02:52 +00:00
componentName: 'ElTreeNode',
mixins: [emitter],
2016-07-27 06:15:02 +00:00
props: {
node: {
default() {
return {};
}
},
props: {},
renderContent: Function
2016-07-27 06:15:02 +00:00
},
2016-08-12 06:45:06 +00:00
components: {
2016-11-25 09:17:05 +00:00
ElCheckbox,
NodeContent: {
props: {
node: {
required: true
}
},
render(h) {
const parent = this.$parent;
const node = this.node;
const data = node.data;
const store = node.store;
return (
parent.renderContent
? parent.renderContent.call(parent._renderProxy, h, { _self: parent.tree.$vnode.context, node, data, store })
: <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,
2016-07-27 06:15:02 +00:00
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);
2016-10-05 15:06:17 +00:00
}
this.oldChecked = checked;
this.indeterminate = indeterminate;
},
handleClick() {
const store = this.tree.store;
store.setCurrentNode(this.node);
this.tree.$emit('current-change', store.currentNode ? store.currentNode.data : null, store.currentNode);
this.tree.currentNode = this;
if (this.tree.expandOnClickNode) {
this.handleExpandIconClick();
}
this.tree.$emit('node-click', this.node.data, this.node, this);
},
handleExpandIconClick() {
if (this.node.isLeaf) return;
2016-07-27 06:15:02 +00:00
if (this.expanded) {
this.tree.$emit('node-collapse', this.node.data, this.node, this);
2016-07-27 06:15:02 +00:00
this.node.collapse();
} else {
this.node.expand();
this.$emit('node-expand', 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
}
2017-01-14 08:02:52 +00:00
},
handleChildNodeExpand(nodeData, node, instance) {
this.broadcast('ElTreeNode', 'tree-node-expand', node);
this.tree.$emit('node-expand', nodeData, node, instance);
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;
2016-07-27 06:15:02 +00:00
} else {
this.tree = parent.tree;
2016-07-27 06:15:02 +00:00
}
const tree = this.tree;
if (!tree) {
console.warn('Can not find node\'s tree.');
}
const props = tree.props || {};
const childrenKey = props['children'] || 'children';
this.$watch(`node.data.${childrenKey}`, () => {
this.node.updateChildren();
});
2016-07-27 06:15:02 +00:00
this.showCheckbox = tree.showCheckbox;
if (this.node.expanded) {
this.expanded = true;
this.childNodeRendered = true;
}
2017-01-14 08:02:52 +00:00
if(this.tree.accordion) {
this.$on('tree-node-expand', node => {
2017-01-14 08:02:52 +00:00
if(this.node !== node) {
this.node.collapse();
}
});
}
2016-07-27 06:15:02 +00:00
}
};
</script>