Skip to content

Commit

Permalink
fix: tree detachment action
Browse files Browse the repository at this point in the history
  • Loading branch information
Lydanne committed Sep 24, 2020
1 parent fe10044 commit b784802
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
25 changes: 25 additions & 0 deletions packages/tree/entity/Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TreeNode } from './TreeNode'

export function createAction(tree) {
// this.root changed update this.raw
const appendChild = function (node) {
if (TreeNode.isType(node)) {
tree.appendRawNode(this, node.data.raw) // 这里的this表示当前的TreeNode
return [node]
}
tree.appendRawNode(this, node)
return [tree.rawNodeToTreeNode(node)]
}
const removeChild = function (index) {
tree.removeChildRawNode(this, index) // 这里的this表示当前的TreeNode
}
const insertChild = function (index, node) {
if (TreeNode.isType(node)) {
tree.insertRawNode(this, index, node.data.raw) // 这里的this表示当前的TreeNode
return [index, node]
}
tree.insertRawNode(this, index, node)
return [index, tree.rawNodeToTreeNode(node)]
}
return { appendChild, removeChild, insertChild }
}
31 changes: 2 additions & 29 deletions packages/tree/entity/Tree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TreeNode } from './TreeNode'
import { nodeMap, nodeEach, transitionObjectKey } from '../libs/util'
import { createAction } from './Action'

export class Tree {
/**
Expand All @@ -10,7 +11,7 @@ export class Tree {
constructor(list, defaultNodeKey = {}, defaultNodeValue = {}) {
this.isUpdateRaw = true
this.raw = list
this.injectAction = this.createAction() // The core method is injected with interceptor functions, the insert RowNode is automatically converted to TreeNode
this.injectAction = createAction(this) // The core method is injected with interceptor functions, the insert RowNode is automatically converted to TreeNode
this.root = new TreeNode(
Date.now(),
'root',
Expand Down Expand Up @@ -79,34 +80,6 @@ export class Tree {
this.isUpdateRaw = true
}

createAction() {
// this.root changed update this.raw
const that = this
const appendChild = function (node) {
if (TreeNode.isType(node)) {
that.appendRawNode(this, node.data.raw)
return [node]
}
that.appendRawNode(this, node)
return [that.rawNodeToTreeNode(node)]
}

const removeChild = function (index) {
that.removeChildRawNode(this, index)
}

const insertChild = function (index, node) {
if (TreeNode.isType(node)) {
that.insertRawNode(this, index, node.data.raw)
return [index, node]
}
that.insertRawNode(this, index, node)
return [index, that.rawNodeToTreeNode(node)]
}

return { appendChild, removeChild, insertChild }
}

rawNodeToTreeNode(rawNode) {
const { childNodes } = this.defaultNodeKey
return nodeMap(
Expand Down
8 changes: 7 additions & 1 deletion packages/tree/entity/TreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class TreeNode {
label,
childNodes = [],
{
/* 默认值 */
parent = null,
isAsync = false,
isVisable = true,
Expand All @@ -27,7 +28,12 @@ export class TreeNode {
data = {},
asyncLoadFn = () => null
} = {},
{ insertChild = null, appendChild = null, removeChild = null } = {}
{
/* 拦截函数 */
insertChild = null,
appendChild = null,
removeChild = null
} = {}
) {
this.id = id || label
this.label = label
Expand Down

0 comments on commit b784802

Please sign in to comment.