Tree: update drag and drop logic (#10372)

This commit is contained in:
FuryBean
2018-03-28 11:46:48 +08:00
committed by 杨奕
parent 438348c33b
commit 4fe58a3d96
9 changed files with 505 additions and 161 deletions

View File

@@ -168,12 +168,58 @@ export default class Node {
return getPropertyFromData(this, 'disabled');
}
get nextSibling() {
const parent = this.parent;
if (parent) {
const index = parent.childNodes.indexOf(this);
if (index > -1) {
return parent.childNodes[index + 1];
}
}
return null;
}
get previousSibling() {
const parent = this.parent;
if (parent) {
const index = parent.childNodes.indexOf(this);
if (index > -1) {
return index > 0 ? parent.childNodes[index - 1] : null;
}
}
return null;
}
contains(target, deep = true) {
const walk = function(parent) {
const children = parent.childNodes || [];
let result = false;
for (let i = 0, j = children.length; i < j; i++) {
const child = children[i];
if (child === target || (deep && walk(child))) {
result = true;
break;
}
}
return result;
};
return walk(this);
}
remove() {
const parent = this.parent;
if (parent) {
parent.removeChild(this);
}
}
insertChild(child, index, batch) {
if (!child) throw new Error('insertChild error: child is required.');
if (!(child instanceof Node)) {
if (!batch) {
const children = this.getChildren() || [];
const children = this.getChildren(true);
if (children.indexOf(child.data) === -1) {
if (typeof index === 'undefined' || index < 0) {
children.push(child.data);
@@ -357,7 +403,7 @@ export default class Node {
}
}
getChildren() { // this is data
getChildren(forceInit = false) { // this is data
if (this.level === 0) return this.data;
const data = this.data;
if (!data) return null;
@@ -372,6 +418,10 @@ export default class Node {
data[children] = null;
}
if (forceInit && !data[children]) {
data[children] = [];
}
return data[children];
}

View File

@@ -6,11 +6,6 @@ export default class TreeStore {
this.currentNode = null;
this.currentNodeKey = null;
this.dragSourceNode = null;
this.dragTargetNode = null;
this.dragTargetDom = null;
this.allowDrop = true;
for (let option in options) {
if (options.hasOwnProperty(option)) {
this[option] = options[option];

View File

@@ -14,3 +14,14 @@ export const getNodeKey = function(key, data) {
if (!key) return data[NODE_KEY];
return data[key];
};
export const findNearestComponent = (element, componentName) => {
let target = element;
while (target && target.tagName !== 'BODY') {
if (target.__vue__ && target.__vue__.$options.name === componentName) {
return target.__vue__;
}
target = target.parentNode;
}
return null;
};