2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
2017-11-09 03:19:52 +00:00
|
|
|
<div
|
|
|
|
class="el-tree-node"
|
2016-10-17 16:31:37 +00:00
|
|
|
@click.stop="handleClick"
|
2018-02-06 03:42:01 +00:00
|
|
|
@contextmenu="($event) => this.handleContextMenu($event)"
|
2016-11-21 16:08:22 +00:00
|
|
|
v-show="node.visible"
|
2016-12-17 06:09:05 +00:00
|
|
|
:class="{
|
2017-08-03 03:54:42 +00:00
|
|
|
'is-expanded': expanded,
|
2016-12-17 06:09:05 +00:00
|
|
|
'is-current': tree.store.currentNode === node,
|
2017-11-09 03:19:52 +00:00
|
|
|
'is-hidden': !node.visible,
|
|
|
|
'is-focusable': !node.disabled,
|
|
|
|
'is-checked': !node.disabled && node.checked
|
|
|
|
}"
|
|
|
|
role="treeitem"
|
|
|
|
tabindex="-1"
|
|
|
|
:aria-expanded="expanded"
|
|
|
|
:aria-disabled="node.disabled"
|
|
|
|
:aria-checked="node.checked"
|
2018-03-20 05:42:04 +00:00
|
|
|
:draggable="tree.draggable"
|
|
|
|
@dragstart.stop="handleDragStart"
|
|
|
|
@dragover.stop="handleDragOver"
|
|
|
|
@dragend.stop="handleDragEnd"
|
|
|
|
@drop.stop="handleDrop"
|
|
|
|
ref="node"
|
2017-11-09 03:19:52 +00:00
|
|
|
>
|
2016-10-17 16:31:37 +00:00
|
|
|
<div class="el-tree-node__content"
|
2017-02-08 03:49:54 +00:00
|
|
|
:style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }">
|
2016-10-17 16:31:37 +00:00
|
|
|
<span
|
2016-12-19 07:38:51 +00:00
|
|
|
@click.stop="handleExpandIconClick"
|
2018-11-07 11:06:36 +00:00
|
|
|
:class="[
|
|
|
|
{ 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded },
|
|
|
|
'el-tree-node__expand-icon',
|
|
|
|
tree.iconClass ? tree.iconClass : 'el-icon-caret-right'
|
|
|
|
]"
|
|
|
|
>
|
2016-10-17 16:31:37 +00:00
|
|
|
</span>
|
|
|
|
<el-checkbox
|
|
|
|
v-if="showCheckbox"
|
|
|
|
v-model="node.checked"
|
|
|
|
:indeterminate="node.indeterminate"
|
2017-07-19 10:36:26 +00:00
|
|
|
:disabled="!!node.disabled"
|
2017-07-24 06:36:05 +00:00
|
|
|
@click.native.stop
|
2017-11-09 03:19:52 +00:00
|
|
|
@change="handleCheckChange"
|
|
|
|
>
|
2016-10-17 16:31:37 +00:00
|
|
|
</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>
|
2017-03-23 09:57:14 +00:00
|
|
|
<el-collapse-transition>
|
2016-10-17 16:31:37 +00:00
|
|
|
<div
|
|
|
|
class="el-tree-node__children"
|
2017-12-24 10:59:09 +00:00
|
|
|
v-if="!renderAfterExpand || childNodeRendered"
|
2017-11-09 03:19:52 +00:00
|
|
|
v-show="expanded"
|
|
|
|
role="group"
|
|
|
|
:aria-expanded="expanded"
|
|
|
|
>
|
2016-10-17 16:31:37 +00:00
|
|
|
<el-tree-node
|
|
|
|
:render-content="renderContent"
|
|
|
|
v-for="child in node.childNodes"
|
2017-12-24 10:59:09 +00:00
|
|
|
:render-after-expand="renderAfterExpand"
|
2016-11-16 07:35:46 +00:00
|
|
|
:key="getNodeKey(child)"
|
2017-01-14 08:02:52 +00:00
|
|
|
:node="child"
|
2017-01-24 02:37:44 +00:00
|
|
|
@node-expand="handleChildNodeExpand">
|
2016-10-17 16:31:37 +00:00
|
|
|
</el-tree-node>
|
2016-08-12 06:45:06 +00:00
|
|
|
</div>
|
2017-03-23 09:57:14 +00:00
|
|
|
</el-collapse-transition>
|
2016-07-27 06:15:02 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2016-10-17 16:31:37 +00:00
|
|
|
<script type="text/jsx">
|
2017-05-08 06:06:23 +00:00
|
|
|
import ElCollapseTransition from 'element-ui/src/transitions/collapse-transition';
|
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';
|
2018-02-09 02:28:43 +00:00
|
|
|
import { getNodeKey } from './model/util';
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
export default {
|
2016-12-31 15:33:51 +00:00
|
|
|
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 {};
|
|
|
|
}
|
2016-10-17 16:31:37 +00:00
|
|
|
},
|
|
|
|
props: {},
|
2017-12-24 10:59:09 +00:00
|
|
|
renderContent: Function,
|
|
|
|
renderAfterExpand: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2016-08-12 06:45:06 +00:00
|
|
|
components: {
|
2017-05-08 06:06:23 +00:00
|
|
|
ElCollapseTransition,
|
2016-11-25 09:17:05 +00:00
|
|
|
ElCheckbox,
|
2016-10-17 16:31:37 +00:00
|
|
|
NodeContent: {
|
|
|
|
props: {
|
|
|
|
node: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render(h) {
|
|
|
|
const parent = this.$parent;
|
2018-02-06 08:45:27 +00:00
|
|
|
const tree = parent.tree;
|
2016-11-23 02:46:05 +00:00
|
|
|
const node = this.node;
|
2018-02-06 08:45:27 +00:00
|
|
|
const { data, store } = node;
|
2016-10-17 16:31:37 +00:00
|
|
|
return (
|
|
|
|
parent.renderContent
|
2018-02-06 08:45:27 +00:00
|
|
|
? parent.renderContent.call(parent._renderProxy, h, { _self: tree.$vnode.context, node, data, store })
|
|
|
|
: tree.$scopedSlots.default
|
|
|
|
? tree.$scopedSlots.default({ node, data })
|
2018-03-28 03:46:48 +00:00
|
|
|
: <span class="el-tree-node__label">{ node.label }</span>
|
2016-10-17 16:31:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 06:45:06 +00:00
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2016-11-23 02:46:05 +00:00
|
|
|
tree: null,
|
2016-07-27 06:15:02 +00:00
|
|
|
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) {
|
2017-08-03 03:54:42 +00:00
|
|
|
this.$nextTick(() => this.expanded = val);
|
2016-11-16 07:35:46 +00:00
|
|
|
if (val) {
|
|
|
|
this.childNodeRendered = true;
|
|
|
|
}
|
2016-10-05 15:06:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
methods: {
|
2018-02-09 02:28:43 +00:00
|
|
|
getNodeKey(node) {
|
|
|
|
return getNodeKey(this.tree.nodeKey, node.data);
|
2016-11-16 07:35:46 +00:00
|
|
|
},
|
|
|
|
|
2016-10-05 15:06:17 +00:00
|
|
|
handleSelectChange(checked, indeterminate) {
|
|
|
|
if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
|
2016-11-23 02:46:05 +00:00
|
|
|
this.tree.$emit('check-change', this.node.data, checked, indeterminate);
|
2016-10-05 15:06:17 +00:00
|
|
|
}
|
|
|
|
this.oldChecked = checked;
|
|
|
|
this.indeterminate = indeterminate;
|
|
|
|
},
|
|
|
|
|
2016-10-17 16:31:37 +00:00
|
|
|
handleClick() {
|
2016-12-17 06:09:05 +00:00
|
|
|
const store = this.tree.store;
|
|
|
|
store.setCurrentNode(this.node);
|
|
|
|
this.tree.$emit('current-change', store.currentNode ? store.currentNode.data : null, store.currentNode);
|
2016-12-19 07:38:51 +00:00
|
|
|
this.tree.currentNode = this;
|
|
|
|
if (this.tree.expandOnClickNode) {
|
2016-12-23 06:39:20 +00:00
|
|
|
this.handleExpandIconClick();
|
2016-12-19 07:38:51 +00:00
|
|
|
}
|
2018-07-03 10:27:50 +00:00
|
|
|
if (this.tree.checkOnClickNode && !this.node.disabled) {
|
2018-05-10 10:01:49 +00:00
|
|
|
this.handleCheckChange(null, {
|
|
|
|
target: { checked: !this.node.checked }
|
|
|
|
});
|
|
|
|
}
|
2016-12-19 07:38:51 +00:00
|
|
|
this.tree.$emit('node-click', this.node.data, this.node, this);
|
2016-10-17 16:31:37 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 03:42:01 +00:00
|
|
|
handleContextMenu(event) {
|
2018-03-14 07:16:52 +00:00
|
|
|
if (this.tree._events['node-contextmenu'] && this.tree._events['node-contextmenu'].length > 0) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2018-02-06 03:42:01 +00:00
|
|
|
this.tree.$emit('node-contextmenu', event, this.node.data, this.node, this);
|
|
|
|
},
|
|
|
|
|
2016-12-23 06:39:20 +00:00
|
|
|
handleExpandIconClick() {
|
2017-01-24 08:16:43 +00:00
|
|
|
if (this.node.isLeaf) return;
|
2016-07-27 06:15:02 +00:00
|
|
|
if (this.expanded) {
|
2017-01-24 02:37:44 +00:00
|
|
|
this.tree.$emit('node-collapse', this.node.data, this.node, this);
|
2016-07-27 06:15:02 +00:00
|
|
|
this.node.collapse();
|
|
|
|
} else {
|
2016-11-16 07:35:46 +00:00
|
|
|
this.node.expand();
|
2017-01-16 08:34:32 +00:00
|
|
|
this.$emit('node-expand', this.node.data, this.node, this);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-12 15:11:11 +00:00
|
|
|
handleCheckChange(value, ev) {
|
2017-07-19 10:36:26 +00:00
|
|
|
this.node.setChecked(ev.target.checked, !this.tree.checkStrictly);
|
2018-02-09 02:31:11 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
const store = this.tree.store;
|
|
|
|
this.tree.$emit('check', this.node.data, {
|
|
|
|
checkedNodes: store.getCheckedNodes(),
|
|
|
|
checkedKeys: store.getCheckedKeys(),
|
|
|
|
halfCheckedNodes: store.getHalfCheckedNodes(),
|
|
|
|
halfCheckedKeys: store.getHalfCheckedKeys(),
|
|
|
|
});
|
|
|
|
});
|
2017-01-14 08:02:52 +00:00
|
|
|
},
|
|
|
|
|
2017-01-24 02:37:44 +00:00
|
|
|
handleChildNodeExpand(nodeData, node, instance) {
|
2017-01-16 08:34:32 +00:00
|
|
|
this.broadcast('ElTreeNode', 'tree-node-expand', node);
|
2017-01-24 02:37:44 +00:00
|
|
|
this.tree.$emit('node-expand', nodeData, node, instance);
|
2018-03-20 05:42:04 +00:00
|
|
|
},
|
|
|
|
|
2018-03-28 03:46:48 +00:00
|
|
|
handleDragStart(event) {
|
2018-05-10 03:56:49 +00:00
|
|
|
if (!this.tree.draggable) return;
|
2018-03-28 03:46:48 +00:00
|
|
|
this.tree.$emit('tree-node-drag-start', event, this);
|
2018-03-20 05:42:04 +00:00
|
|
|
},
|
|
|
|
|
2018-03-28 03:46:48 +00:00
|
|
|
handleDragOver(event) {
|
2018-05-10 03:56:49 +00:00
|
|
|
if (!this.tree.draggable) return;
|
2018-03-28 03:46:48 +00:00
|
|
|
this.tree.$emit('tree-node-drag-over', event, this);
|
|
|
|
event.preventDefault();
|
2018-03-20 05:42:04 +00:00
|
|
|
},
|
|
|
|
|
2018-03-28 03:46:48 +00:00
|
|
|
handleDrop(event) {
|
|
|
|
event.preventDefault();
|
2018-03-20 05:42:04 +00:00
|
|
|
},
|
|
|
|
|
2018-03-28 03:46:48 +00:00
|
|
|
handleDragEnd(event) {
|
2018-05-10 03:56:49 +00:00
|
|
|
if (!this.tree.draggable) return;
|
2018-03-28 03:46:48 +00:00
|
|
|
this.tree.$emit('tree-node-drag-end', event, this);
|
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
|
|
|
|
2016-11-23 02:46:05 +00:00
|
|
|
if (parent.isTree) {
|
|
|
|
this.tree = parent;
|
2016-07-27 06:15:02 +00:00
|
|
|
} else {
|
2016-11-23 02:46:05 +00:00
|
|
|
this.tree = parent.tree;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 02:46:05 +00:00
|
|
|
const tree = this.tree;
|
2016-12-02 10:00:41 +00:00
|
|
|
if (!tree) {
|
|
|
|
console.warn('Can not find node\'s tree.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const props = tree.props || {};
|
2016-10-17 16:31:37 +00:00
|
|
|
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;
|
2016-11-16 07:35:46 +00:00
|
|
|
|
|
|
|
if (this.node.expanded) {
|
|
|
|
this.expanded = true;
|
|
|
|
this.childNodeRendered = true;
|
|
|
|
}
|
2017-01-14 08:02:52 +00:00
|
|
|
|
|
|
|
if(this.tree.accordion) {
|
2017-01-16 08:34:32 +00:00
|
|
|
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>
|