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

280 lines
7.6 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div
class="el-tree-node"
@click.stop="handleClick"
@contextmenu="($event) => this.handleContextMenu($event)"
v-show="node.visible"
:class="{
2017-08-03 03:54:42 +00:00
'is-expanded': expanded,
2019-04-23 02:52:02 +00:00
'is-current': node.isCurrent,
'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"
:draggable="tree.draggable"
@dragstart.stop="handleDragStart"
@dragover.stop="handleDragOver"
@dragend.stop="handleDragEnd"
@drop.stop="handleDrop"
ref="node"
>
<div class="el-tree-node__content"
2017-02-08 03:49:54 +00:00
:style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }">
<span
@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'
]"
>
</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
@change="handleCheckChange"
>
</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"
2017-12-24 10:59:09 +00:00
v-if="!renderAfterExpand || childNodeRendered"
v-show="expanded"
role="group"
:aria-expanded="expanded"
>
<el-tree-node
:render-content="renderContent"
v-for="child in node.childNodes"
2017-12-24 10:59:09 +00:00
:render-after-expand="renderAfterExpand"
:show-checkbox="showCheckbox"
: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">
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 {
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: {},
2017-12-24 10:59:09 +00:00
renderContent: Function,
renderAfterExpand: {
type: Boolean,
default: true
},
showCheckbox: {
type: Boolean,
default: false
2017-12-24 10:59:09 +00:00
}
2016-07-27 06:15:02 +00:00
},
2016-08-12 06:45:06 +00:00
components: {
ElCollapseTransition,
2016-11-25 09:17:05 +00:00
ElCheckbox,
NodeContent: {
props: {
node: {
required: true
}
},
render(h) {
const parent = this.$parent;
2018-02-06 08:45:27 +00:00
const tree = parent.tree;
const node = this.node;
2018-02-06 08:45:27 +00:00
const { data, store } = node;
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 })
: <span class="el-tree-node__label">{ 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
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) {
2017-08-03 03:54:42 +00:00
this.$nextTick(() => 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: {
2018-02-09 02:28:43 +00:00
getNodeKey(node) {
return getNodeKey(this.tree.nodeKey, node.data);
},
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();
}
if (this.tree.checkOnClickNode && !this.node.disabled) {
2018-05-10 10:01:49 +00:00
this.handleCheckChange(null, {
target: { checked: !this.node.checked }
});
}
this.tree.$emit('node-click', this.node.data, this.node, this);
},
handleContextMenu(event) {
if (this.tree._events['node-contextmenu'] && this.tree._events['node-contextmenu'].length > 0) {
event.stopPropagation();
event.preventDefault();
}
this.tree.$emit('node-contextmenu', event, 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
}
},
handleCheckChange(value, ev) {
2017-07-19 10:36:26 +00:00
this.node.setChecked(ev.target.checked, !this.tree.checkStrictly);
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
},
handleChildNodeExpand(nodeData, node, instance) {
this.broadcast('ElTreeNode', 'tree-node-expand', node);
this.tree.$emit('node-expand', nodeData, node, instance);
},
handleDragStart(event) {
if (!this.tree.draggable) return;
this.tree.$emit('tree-node-drag-start', event, this);
},
handleDragOver(event) {
if (!this.tree.draggable) return;
this.tree.$emit('tree-node-drag-over', event, this);
event.preventDefault();
},
handleDrop(event) {
event.preventDefault();
},
handleDragEnd(event) {
if (!this.tree.draggable) return;
this.tree.$emit('tree-node-drag-end', event, this);
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
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>