Skip to content

Commit

Permalink
feat: validateNode before added to graph
Browse files Browse the repository at this point in the history
re #182
  • Loading branch information
bubkoo committed Jul 7, 2020
1 parent 047dff7 commit 5e54d4d
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions packages/x6/src/addon/dnd/index.ts
Expand Up @@ -7,6 +7,7 @@ import { NodeView } from '../../view/node'
import { Graph } from '../../graph/graph'
import { EventArgs } from '../../graph/events'
import { Scroller } from '../scroller'
import { FunctionExt } from '../../util'

export class Dnd extends View {
public readonly options: Dnd.Options
Expand Down Expand Up @@ -242,21 +243,30 @@ export class Dnd extends View {
draggingNode.position(x, y, { silent: true })

const node = this.options.getDropNode(draggingNode)
if (this.drop(node, { x: e.clientX, y: e.clientY })) {
this.onDropped(draggingNode)
} else {
this.onDropInvalid()
}
const ret = this.drop(node, { x: e.clientX, y: e.clientY })
const callback = (valid: boolean) => {
if (valid) {
this.onDropped(draggingNode)
} else {
this.onDropInvalid()
}

if (this.targetGraph.options.embedding.enabled && draggingView) {
draggingView.setEventData(e, {
cell: node,
graph: this.targetGraph,
})
draggingView.finalizeEmbedding(draggingView.getEventData<any>(e))
if (this.targetGraph.options.embedding.enabled && draggingView) {
draggingView.setEventData(e, {
cell: node,
graph: this.targetGraph,
})
draggingView.finalizeEmbedding(draggingView.getEventData<any>(e))
}

this.targetModel.stopBatch('dnd')
}

this.targetModel.stopBatch('dnd')
if (typeof ret === 'boolean') {
callback(ret)
} else {
ret.then(callback)
}
}
}

Expand Down Expand Up @@ -351,8 +361,24 @@ export class Dnd extends View {
)

node.removeZIndex()
targetModel.addCell(node, { stencil: this.cid })
return true

const ret = this.options.validateNode
? this.options.validateNode.call(targetGraph, node)
: true

if (typeof ret === 'boolean') {
if (ret) {
targetModel.addCell(node, { stencil: this.cid })
}
return ret
}

return FunctionExt.toDeferredBoolean(ret).then((valid) => {
if (valid) {
targetModel.addCell(node, { stencil: this.cid })
}
return valid
})
}

return false
Expand Down Expand Up @@ -381,6 +407,7 @@ export namespace Dnd {
}
getDragNode: (node: Node) => Node
getDropNode: (node: Node) => Node
validateNode?: (this: Graph, node: Node) => any
}

export const defaults: Partial<Options> = {
Expand Down

0 comments on commit 5e54d4d

Please sign in to comment.