|
@@ -3,8 +3,9 @@
|
|
|
class="el-tree"
|
|
|
:class="{
|
|
|
'el-tree--highlight-current': highlightCurrent,
|
|
|
- dragging: !!store.dragSourceNode,
|
|
|
- 'drop-not-allow': !store.allowDrop
|
|
|
+ 'is-dragging': !!dragState.draggingNode,
|
|
|
+ 'is-drop-not-allow': !dragState.allowDrop,
|
|
|
+ 'is-drop-inner': dragState.dropType === 'inner'
|
|
|
}"
|
|
|
role="tree"
|
|
|
>
|
|
@@ -21,20 +22,20 @@
|
|
|
<span class="el-tree__empty-text">{{ emptyText }}</span>
|
|
|
</div>
|
|
|
<div
|
|
|
- v-if="!!dropAt"
|
|
|
- class="el-tree__drag-indicator"
|
|
|
- :style="{top: dragIndicatorOffset}"
|
|
|
- ref="drag-indicator">
|
|
|
+ v-show="dragState.showDropIndicator"
|
|
|
+ class="el-tree__drop-indicator"
|
|
|
+ ref="dropIndicator">
|
|
|
</div>
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
import TreeStore from './model/tree-store';
|
|
|
- import { getNodeKey } from './model/util';
|
|
|
+ import { getNodeKey, findNearestComponent } from './model/util';
|
|
|
import ElTreeNode from './tree-node.vue';
|
|
|
import {t} from 'element-ui/src/locale';
|
|
|
import emitter from 'element-ui/src/mixins/emitter';
|
|
|
+ import { addClass, removeClass } from 'element-ui/src/utils/dom';
|
|
|
|
|
|
export default {
|
|
|
name: 'ElTree',
|
|
@@ -51,7 +52,13 @@
|
|
|
root: null,
|
|
|
currentNode: null,
|
|
|
treeItems: null,
|
|
|
- checkboxItems: []
|
|
|
+ checkboxItems: [],
|
|
|
+ dragState: {
|
|
|
+ showDropIndicator: false,
|
|
|
+ draggingNode: null,
|
|
|
+ dropNode: null,
|
|
|
+ allowDrop: true
|
|
|
+ }
|
|
|
};
|
|
|
},
|
|
|
|
|
@@ -130,42 +137,9 @@
|
|
|
return this.data;
|
|
|
}
|
|
|
},
|
|
|
+
|
|
|
treeItemArray() {
|
|
|
return Array.prototype.slice.call(this.treeItems);
|
|
|
- },
|
|
|
- dragIndicatorOffset() {
|
|
|
- if (!this.dropAt) return;
|
|
|
-
|
|
|
- const dom = this.store.dragTargetDom;
|
|
|
- if (this.store.dragSourceNode.level !== this.store.dragTargetNode.level) {
|
|
|
- return (dom.offsetTop + dom.querySelector('.el-tree-node__content').scrollHeight) + 'px';
|
|
|
- } else {
|
|
|
- return (dom.offsetTop + dom.scrollHeight) + 'px';
|
|
|
- }
|
|
|
- },
|
|
|
- dropAt() {
|
|
|
- let target = this.store.dragTargetNode;
|
|
|
- let from = this.store.dragSourceNode;
|
|
|
- if (!target || !from) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- if (typeof this.allowDrop === 'function' && !this.allowDrop(from, target)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- if (target.level === from.level - 1) {
|
|
|
- return {
|
|
|
- parent: target,
|
|
|
- index: 0
|
|
|
- };
|
|
|
- }
|
|
|
- if (target.level === from.level) {
|
|
|
- return {
|
|
|
- parent: target.parent,
|
|
|
- index: target.parent.childNodes.indexOf(target) + 1
|
|
|
- };
|
|
|
- }
|
|
|
- return null;
|
|
|
}
|
|
|
},
|
|
|
|
|
@@ -174,13 +148,16 @@
|
|
|
this.store.defaultCheckedKeys = newVal;
|
|
|
this.store.setDefaultCheckedKey(newVal);
|
|
|
},
|
|
|
+
|
|
|
defaultExpandedKeys(newVal) {
|
|
|
this.store.defaultExpandedKeys = newVal;
|
|
|
this.store.setDefaultExpandedKeys(newVal);
|
|
|
},
|
|
|
+
|
|
|
data(newVal) {
|
|
|
this.store.setData(newVal);
|
|
|
},
|
|
|
+
|
|
|
checkboxItems(val) {
|
|
|
Array.prototype.forEach.call(val, (checkbox) => {
|
|
|
checkbox.setAttribute('tabindex', -1);
|
|
@@ -193,9 +170,11 @@
|
|
|
if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
|
|
|
this.store.filter(value);
|
|
|
},
|
|
|
+
|
|
|
getNodeKey(node) {
|
|
|
return getNodeKey(this.nodeKey, node.data);
|
|
|
},
|
|
|
+
|
|
|
getNodePath(data) {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in getNodePath');
|
|
|
const node = this.store.getNode(data);
|
|
@@ -208,70 +187,89 @@
|
|
|
}
|
|
|
return path.reverse();
|
|
|
},
|
|
|
+
|
|
|
getCheckedNodes(leafOnly) {
|
|
|
return this.store.getCheckedNodes(leafOnly);
|
|
|
},
|
|
|
+
|
|
|
getCheckedKeys(leafOnly) {
|
|
|
return this.store.getCheckedKeys(leafOnly);
|
|
|
},
|
|
|
+
|
|
|
getCurrentNode() {
|
|
|
const currentNode = this.store.getCurrentNode();
|
|
|
return currentNode ? currentNode.data : null;
|
|
|
},
|
|
|
+
|
|
|
getCurrentKey() {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in getCurrentKey');
|
|
|
const currentNode = this.getCurrentNode();
|
|
|
return currentNode ? currentNode[this.nodeKey] : null;
|
|
|
},
|
|
|
+
|
|
|
setCheckedNodes(nodes, leafOnly) {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
|
|
|
this.store.setCheckedNodes(nodes, leafOnly);
|
|
|
},
|
|
|
+
|
|
|
setCheckedKeys(keys, leafOnly) {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedKeys');
|
|
|
this.store.setCheckedKeys(keys, leafOnly);
|
|
|
},
|
|
|
+
|
|
|
setChecked(data, checked, deep) {
|
|
|
this.store.setChecked(data, checked, deep);
|
|
|
},
|
|
|
+
|
|
|
getHalfCheckedNodes() {
|
|
|
return this.store.getHalfCheckedNodes();
|
|
|
},
|
|
|
+
|
|
|
getHalfCheckedKeys() {
|
|
|
return this.store.getHalfCheckedKeys();
|
|
|
},
|
|
|
+
|
|
|
setCurrentNode(node) {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentNode');
|
|
|
this.store.setUserCurrentNode(node);
|
|
|
},
|
|
|
+
|
|
|
setCurrentKey(key) {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentKey');
|
|
|
this.store.setCurrentNodeKey(key);
|
|
|
},
|
|
|
+
|
|
|
getNode(data) {
|
|
|
return this.store.getNode(data);
|
|
|
},
|
|
|
+
|
|
|
remove(data) {
|
|
|
this.store.remove(data);
|
|
|
},
|
|
|
+
|
|
|
append(data, parentNode) {
|
|
|
this.store.append(data, parentNode);
|
|
|
},
|
|
|
+
|
|
|
insertBefore(data, refNode) {
|
|
|
this.store.insertBefore(data, refNode);
|
|
|
},
|
|
|
+
|
|
|
insertAfter(data, refNode) {
|
|
|
this.store.insertAfter(data, refNode);
|
|
|
},
|
|
|
+
|
|
|
handleNodeExpand(nodeData, node, instance) {
|
|
|
this.broadcast('ElTreeNode', 'tree-node-expand', node);
|
|
|
this.$emit('node-expand', nodeData, node, instance);
|
|
|
},
|
|
|
+
|
|
|
updateKeyChildren(key, data) {
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild');
|
|
|
this.store.updateChildren(key, data);
|
|
|
},
|
|
|
- initTabindex() {
|
|
|
+
|
|
|
+ initTabIndex() {
|
|
|
this.treeItems = this.$el.querySelectorAll('.is-focusable[role=treeitem]');
|
|
|
this.checkboxItems = this.$el.querySelectorAll('input[type=checkbox]');
|
|
|
const checkedItem = this.$el.querySelectorAll('.is-checked[role=treeitem]');
|
|
@@ -281,6 +279,7 @@
|
|
|
}
|
|
|
this.treeItems[0] && this.treeItems[0].setAttribute('tabindex', 0);
|
|
|
},
|
|
|
+
|
|
|
handelKeydown(ev) {
|
|
|
const currentItem = ev.target;
|
|
|
if (currentItem.className.indexOf('el-tree-node') === -1) return;
|
|
@@ -297,14 +296,12 @@
|
|
|
}
|
|
|
this.treeItemArray[nextIndex].focus(); // 选中
|
|
|
}
|
|
|
- const hasInput = currentItem.querySelector('[type="checkbox"]');
|
|
|
if ([37, 39].indexOf(keyCode) > -1) { // left、right 展开
|
|
|
currentItem.click(); // 选中
|
|
|
}
|
|
|
- if ([13, 32].indexOf(keyCode) > -1) { // space enter选中checkbox
|
|
|
- if (hasInput) {
|
|
|
- hasInput.click();
|
|
|
- }
|
|
|
+ const hasInput = currentItem.querySelector('[type="checkbox"]');
|
|
|
+ if ([13, 32].indexOf(keyCode) > -1 && hasInput) { // space enter选中checkbox
|
|
|
+ hasInput.click();
|
|
|
}
|
|
|
}
|
|
|
},
|
|
@@ -329,11 +326,143 @@
|
|
|
});
|
|
|
|
|
|
this.root = this.store.root;
|
|
|
+
|
|
|
+ let dragState = this.dragState;
|
|
|
+ this.$on('tree-node-drag-start', (event, treeNode) => {
|
|
|
+ if (typeof this.allowDrag === 'function' && !this.allowDrag(treeNode.node)) {
|
|
|
+ event.preventDefault();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ event.dataTransfer.effectAllowed = 'move';
|
|
|
+ event.dataTransfer.setData('text/plain', treeNode.node.label);
|
|
|
+ dragState.draggingNode = treeNode;
|
|
|
+ this.$emit('node-drag-start', event, treeNode.node);
|
|
|
+ });
|
|
|
+
|
|
|
+ this.$on('tree-node-drag-over', (event, treeNode) => {
|
|
|
+ const dropNode = findNearestComponent(event.target, 'ElTreeNode');
|
|
|
+ const oldDropNode = dragState.dropNode;
|
|
|
+ if (oldDropNode && oldDropNode !== dropNode) {
|
|
|
+ removeClass(oldDropNode.$el, 'is-drop-inner');
|
|
|
+ }
|
|
|
+ const draggingNode = dragState.draggingNode;
|
|
|
+ if (!draggingNode || !dropNode) return;
|
|
|
+
|
|
|
+ let allowDrop = true;
|
|
|
+ if (typeof this.allowDrop === 'function' && !this.allowDrop(draggingNode.node, dropNode.node)) {
|
|
|
+ allowDrop = false;
|
|
|
+ }
|
|
|
+ dragState.allowDrop = allowDrop;
|
|
|
+ event.dataTransfer.dropEffect = allowDrop ? 'move' : 'none';
|
|
|
+ if (allowDrop && oldDropNode !== dropNode) {
|
|
|
+ if (oldDropNode) {
|
|
|
+ this.$emit('node-drag-leave', draggingNode.node, oldDropNode.node, event);
|
|
|
+ }
|
|
|
+ this.$emit('node-drag-enter', draggingNode.node, dropNode.node, event);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (allowDrop) {
|
|
|
+ dragState.dropNode = dropNode;
|
|
|
+ }
|
|
|
+
|
|
|
+ let dropPrev = allowDrop;
|
|
|
+ let dropInner = allowDrop;
|
|
|
+ let dropNext = allowDrop;
|
|
|
+
|
|
|
+ if (dropNode.node.nextSibling === draggingNode.node) {
|
|
|
+ dropNext = false;
|
|
|
+ }
|
|
|
+ if (dropNode.node.previousSibling === draggingNode.node) {
|
|
|
+ dropPrev = false;
|
|
|
+ }
|
|
|
+ if (dropNode.node.contains(draggingNode.node, false)) {
|
|
|
+ dropInner = false;
|
|
|
+ }
|
|
|
+ if (draggingNode.node === dropNode.node || draggingNode.node.contains(dropNode.node)) {
|
|
|
+ dropPrev = false;
|
|
|
+ dropInner = false;
|
|
|
+ dropNext = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ const targetPosition = dropNode.$el.querySelector('.el-tree-node__expand-icon').getBoundingClientRect();
|
|
|
+ const treePosition = this.$el.getBoundingClientRect();
|
|
|
+
|
|
|
+ let dropType;
|
|
|
+ const prevPercent = dropPrev ? (dropInner ? 0.25 : (dropNext ? 0.5 : 1)) : -1;
|
|
|
+ const nextPercent = dropNext ? (dropInner ? 0.75 : (dropPrev ? 0.5 : 0)) : 1;
|
|
|
+
|
|
|
+ let indicatorTop = -9999;
|
|
|
+ const distance = event.clientY - targetPosition.top;
|
|
|
+ if (distance < targetPosition.height * prevPercent) {
|
|
|
+ dropType = 'before';
|
|
|
+ } else if (distance > targetPosition.height * nextPercent) {
|
|
|
+ dropType = 'after';
|
|
|
+ } else if (dropInner) {
|
|
|
+ dropType = 'inner';
|
|
|
+ } else {
|
|
|
+ dropType = 'none';
|
|
|
+ }
|
|
|
+
|
|
|
+ const dropIndicator = this.$refs.dropIndicator;
|
|
|
+ if (dropType === 'before') {
|
|
|
+ indicatorTop = targetPosition.top - treePosition.top;
|
|
|
+ } else if (dropType === 'after') {
|
|
|
+ indicatorTop = targetPosition.bottom - treePosition.top;
|
|
|
+ }
|
|
|
+ dropIndicator.style.top = indicatorTop + 'px';
|
|
|
+ dropIndicator.style.left = (targetPosition.right - treePosition.left) + 'px';
|
|
|
+
|
|
|
+ if (dropType === 'inner') {
|
|
|
+ addClass(dropNode.$el, 'is-drop-inner');
|
|
|
+ } else {
|
|
|
+ removeClass(dropNode.$el, 'is-drop-inner');
|
|
|
+ }
|
|
|
+
|
|
|
+ dragState.showDropIndicator = dropType === 'before' || dropType === 'after';
|
|
|
+ dragState.dropType = dropType;
|
|
|
+ this.$emit('node-drag-over', draggingNode.node, dropNode.node, event);
|
|
|
+ });
|
|
|
+
|
|
|
+ this.$on('tree-node-drag-end', (event) => {
|
|
|
+ const { draggingNode, dropType, dropNode } = dragState;
|
|
|
+ event.preventDefault();
|
|
|
+ event.dataTransfer.dropEffect = 'move';
|
|
|
+
|
|
|
+ if (draggingNode && dropNode) {
|
|
|
+ const data = draggingNode.node.data;
|
|
|
+ if (dropType === 'before') {
|
|
|
+ draggingNode.node.remove();
|
|
|
+ dropNode.node.parent.insertBefore({ data }, dropNode.node);
|
|
|
+ } else if (dropType === 'after') {
|
|
|
+ draggingNode.node.remove();
|
|
|
+ dropNode.node.parent.insertAfter({ data }, dropNode.node);
|
|
|
+ } else if (dropType === 'inner') {
|
|
|
+ dropNode.node.insertChild({ data });
|
|
|
+ draggingNode.node.remove();
|
|
|
+ }
|
|
|
+ removeClass(dropNode.$el, 'is-drop-inner');
|
|
|
+
|
|
|
+ this.$emit('node-drag-end', draggingNode.node, dropNode.node, dropType, event);
|
|
|
+ if (dropType !== 'none') {
|
|
|
+ this.$emit('node-drop', draggingNode.node, dropNode.node, dropType, event);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (draggingNode && !dropNode) {
|
|
|
+ this.$emit('node-drag-end', draggingNode.node, dropNode.node, dropType, event);
|
|
|
+ }
|
|
|
+
|
|
|
+ dragState.showDropIndicator = false;
|
|
|
+ dragState.draggingNode = null;
|
|
|
+ dragState.dropNode = null;
|
|
|
+ dragState.allowDrop = true;
|
|
|
+ });
|
|
|
},
|
|
|
+
|
|
|
mounted() {
|
|
|
- this.initTabindex();
|
|
|
+ this.initTabIndex();
|
|
|
this.$el.addEventListener('keydown', this.handelKeydown);
|
|
|
},
|
|
|
+
|
|
|
updated() {
|
|
|
this.treeItems = this.$el.querySelectorAll('[role=treeitem]');
|
|
|
this.checkboxItems = this.$el.querySelectorAll('input[type=checkbox]');
|