|
@@ -4,7 +4,7 @@ export interface TreeRaw {
|
|
|
ID: string;
|
|
ID: string;
|
|
|
parentID: string;
|
|
parentID: string;
|
|
|
seq: number;
|
|
seq: number;
|
|
|
- [propName: string]: any;
|
|
|
|
|
|
|
+ [props: string]: any;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export interface TreeNode extends TreeRaw {
|
|
export interface TreeNode extends TreeRaw {
|
|
@@ -308,7 +308,7 @@ export class Tree {
|
|
|
this.rawData.push(...nodes);
|
|
this.rawData.push(...nodes);
|
|
|
// 排序
|
|
// 排序
|
|
|
this.reSortData(nodes);
|
|
this.reSortData(nodes);
|
|
|
- return this.data;
|
|
|
|
|
|
|
+ return nodes;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 准备删除,返回所有需要删除的节点,包括嵌套节点
|
|
// 准备删除,返回所有需要删除的节点,包括嵌套节点
|
|
@@ -319,7 +319,7 @@ export class Tree {
|
|
|
/**
|
|
/**
|
|
|
* 删除节点
|
|
* 删除节点
|
|
|
* @param {TreeNode[]} treeNodes - 要删除的节点,不需要包含嵌套节点
|
|
* @param {TreeNode[]} treeNodes - 要删除的节点,不需要包含嵌套节点
|
|
|
- * @return {TreeNode[]} 返回删除节点后,树的data
|
|
|
|
|
|
|
+ * @return {TreeNode[]} 返回被删除的节点
|
|
|
*/
|
|
*/
|
|
|
delete(treeNodes: TreeNode[]): TreeNode[] {
|
|
delete(treeNodes: TreeNode[]): TreeNode[] {
|
|
|
const allDeletedNodes: TreeNode[] = [];
|
|
const allDeletedNodes: TreeNode[] = [];
|
|
@@ -351,7 +351,7 @@ export class Tree {
|
|
|
deleteNodes(treeNodes);
|
|
deleteNodes(treeNodes);
|
|
|
// 排序
|
|
// 排序
|
|
|
this.reSortData(allDeletedNodes);
|
|
this.reSortData(allDeletedNodes);
|
|
|
- return this.data;
|
|
|
|
|
|
|
+ return allDeletedNodes;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// IDList 返回所有需要删除的节点,包括嵌套节点
|
|
// IDList 返回所有需要删除的节点,包括嵌套节点
|
|
@@ -468,10 +468,12 @@ export class Tree {
|
|
|
}
|
|
}
|
|
|
// 最末节点的所有后兄弟节点,成为最末节点的子节点
|
|
// 最末节点的所有后兄弟节点,成为最末节点的子节点
|
|
|
const lastNodeNextBrothers = lastNode.getCtx().nextBrothers();
|
|
const lastNodeNextBrothers = lastNode.getCtx().nextBrothers();
|
|
|
|
|
+ const lastNodeLastChild = lastNode.getCtx().lastChild();
|
|
|
|
|
+ const lastNodeLastChildSeq = lastNodeLastChild ? lastNodeLastChild.seq : 0;
|
|
|
lastNodeNextBrothers.forEach((node, index) => {
|
|
lastNodeNextBrothers.forEach((node, index) => {
|
|
|
updateData.push({
|
|
updateData.push({
|
|
|
ID: node.ID,
|
|
ID: node.ID,
|
|
|
- update: { parentID: lastNode.ID, seq: index },
|
|
|
|
|
|
|
+ update: { parentID: lastNode.ID, seq: index + lastNodeLastChildSeq },
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
return updateData;
|
|
return updateData;
|
|
@@ -536,5 +538,68 @@ export class Tree {
|
|
|
...nodes
|
|
...nodes
|
|
|
);
|
|
);
|
|
|
this.updateValue(updateData);
|
|
this.updateValue(updateData);
|
|
|
|
|
+ this.reGenData();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 准备移动节点块(连续的兄弟节点),不维护seq连号
|
|
|
|
|
+ prepareMoveTo(
|
|
|
|
|
+ nodes: TreeNode[],
|
|
|
|
|
+ parent: TreeNode | None,
|
|
|
|
|
+ next: TreeNode | None
|
|
|
|
|
+ ): UpdateData[] {
|
|
|
|
|
+ const updateData: UpdateData[] = [];
|
|
|
|
|
+ let prev: TreeNode | None;
|
|
|
|
|
+ if (next) {
|
|
|
|
|
+ prev = next.getCtx().prev();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ prev = parent ? parent.getCtx().lastChild() : null;
|
|
|
|
|
+ }
|
|
|
|
|
+ const baseSeq = prev ? prev.seq + 1 : this.seqStartIndex;
|
|
|
|
|
+ updateData.push(
|
|
|
|
|
+ ...nodes.map((node, index) => ({
|
|
|
|
|
+ ID: node.ID,
|
|
|
|
|
+ update: {
|
|
|
|
|
+ parentID: (parent && parent.ID) || this.rootID,
|
|
|
|
|
+ seq: baseSeq + index,
|
|
|
|
|
+ },
|
|
|
|
|
+ }))
|
|
|
|
|
+ );
|
|
|
|
|
+ const curBaseSeq = baseSeq + nodes.length;
|
|
|
|
|
+ const nextBrothers = prev ? prev.getCtx().nextBrothers() : [];
|
|
|
|
|
+ updateData.push(
|
|
|
|
|
+ ...nextBrothers.map((node, index) => ({
|
|
|
|
|
+ ID: node.ID,
|
|
|
|
|
+ update: {
|
|
|
|
|
+ seq: curBaseSeq + index,
|
|
|
|
|
+ },
|
|
|
|
|
+ }))
|
|
|
|
|
+ );
|
|
|
|
|
+ return updateData;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ moveTo(
|
|
|
|
|
+ nodes: TreeNode[],
|
|
|
|
|
+ parent: TreeNode | None,
|
|
|
|
|
+ updateData: UpdateData[]
|
|
|
|
|
+ ): void {
|
|
|
|
|
+ const firstNode = nodes[0];
|
|
|
|
|
+ const newParentID = parent ? parent.ID : this.rootID;
|
|
|
|
|
+ const orgParentID = firstNode.parentID;
|
|
|
|
|
+ const orgBrothers = this.parentMap[orgParentID];
|
|
|
|
|
+ orgBrothers.splice(orgBrothers.indexOf(firstNode), nodes.length);
|
|
|
|
|
+ (this.parentMap[newParentID] || (this.parentMap[newParentID] = [])).push(
|
|
|
|
|
+ ...nodes
|
|
|
|
|
+ );
|
|
|
|
|
+ this.updateValue(updateData);
|
|
|
|
|
+ this.resortDataByID([orgParentID, newParentID]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 清除所有节点(不变更data引用)
|
|
|
|
|
+ clear(): void {
|
|
|
|
|
+ this.rawData.splice(0, this.rawData.length);
|
|
|
|
|
+ this.data.splice(0, this.data.length);
|
|
|
|
|
+ this.parentMap = {};
|
|
|
|
|
+ this.IDMap = {};
|
|
|
|
|
+ this.ctxMap = {};
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|