/
handler.ts
368 lines (312 loc) 路 9.92 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import * as util from '../../util'
import { Graph } from '../../graph'
import { View } from '../../core/view'
import { Model } from '../../core/model'
import { State } from '../../core/state'
import { Shape, ImageShape } from '../../shape'
import { BaseHandler } from '../handler-base'
import { Rectangle, Point, Anchor } from '../../struct'
import { DomEvent, MouseEventEx, Disposable } from '../../common'
import { getAnchorOptions, createAnchorHighlightShape } from './option'
export class AnchorHandler extends BaseHandler {
inductionSize: number
currentState: State | null
currentPoint: Point | null
currentArea: Rectangle | null
currentAnchor: Anchor | null
protected knobs: Shape[] | null
protected points: Point[] | null
protected anchors: Anchor[] | null
protected highlight: Shape | null
private resetHandler: (() => void) | null
private containerEventInstalled = false
constructor(graph: Graph) {
super(graph)
this.inductionSize = graph.options.anchor.inductionSize
this.resetHandler = () => {
if (
this.currentState != null &&
this.graph.view.getState(this.currentState.cell) == null
) {
this.reset()
} else {
this.redraw()
}
}
this.graph.on(Graph.events.root, this.resetHandler)
this.graph.model.on(Model.events.change, this.resetHandler)
this.graph.view.on(View.events.scale, this.resetHandler)
this.graph.view.on(View.events.translate, this.resetHandler)
this.graph.view.on(View.events.scaleAndTranslate, this.resetHandler)
}
reset() {
this.destroyIcons()
this.destroyHighlight()
this.currentArea = null
this.currentPoint = null
this.currentState = null
this.currentAnchor = null
}
redraw() {
if (
this.currentState != null &&
this.anchors != null &&
this.knobs != null &&
this.points != null
) {
const state = this.graph.view.getState(this.currentState.cell)!
this.currentState = state
this.currentArea = state.bounds.clone()
for (let i = 0, ii = this.anchors.length; i < ii; i += 1) {
const anchor = this.anchors[i]
const point = this.graph.view.getConnectionPoint(state, anchor)!
this.redrawAnchor(state, anchor, point, this.knobs[i])
this.points[i] = point
this.currentArea.add(this.knobs[i].bounds)
}
}
}
protected redrawAnchor(
state: State,
anchor: Anchor,
point: Point,
icon?: Shape,
) {
const { image, cursor, className } = getAnchorOptions({
anchor,
point,
graph: this.graph,
cell: state.cell,
})
const bounds = new Rectangle(
Math.round(point.x - image.width / 2),
Math.round(point.y - image.height / 2),
image.width,
image.height,
)
if (icon == null) {
const img = new ImageShape(bounds, image.src)
img.dialect = 'svg'
img.preserveImageAspect = false
img.init(this.graph.view.getDecoratorPane())
util.toBack(img.elem)
icon = img // tslint:disable-line
const getState = () => this.currentState || state
MouseEventEx.redirectMouseEvents(icon.elem, this.graph, getState)
}
util.applyClassName(icon, this.graph.prefixCls, 'anchor', className)
icon.image = image.src
icon.bounds = bounds
icon.cursor = cursor
icon.redraw()
return icon
}
protected getTolerance(e: MouseEventEx) {
return this.graph.tolerance
}
protected isEventIgnored(e: MouseEventEx) {
return false
}
protected isStateIgnored(state: State, isSource: boolean) {
return false
}
/**
* Returns true if the current focused state should not be changed
* for the given event.
*
* This implementation returns true if shift is pressed.
*/
protected isKeepFocusEvent(e: MouseEventEx) {
return DomEvent.isShiftDown(e.getEvent())
}
protected getAnchors(state: State, isSource: boolean) {
if (
this.isEnabled() &&
state != null &&
!this.isStateIgnored(state, isSource) &&
this.graph.isCellConnectable(state.cell)
) {
const items = this.graph.getAnchors(state.cell, isSource)
if (items != null) {
return items.map(item => {
if (item instanceof Anchor) {
return item
}
return new Anchor(item)
})
}
}
return null
}
protected getCell(e: MouseEventEx, point: Point | null) {
let cell = e.getCell()
// Gets cell under actual point if different from event location
if (
cell == null &&
point != null &&
(e.getGraphX() !== point.x || e.getGraphY() !== point.y)
) {
cell = this.graph.getCellAt(point.x, point.y)
}
// Uses connectable parent node if one exists
if (cell != null && !this.graph.isCellConnectable(cell)) {
const parent = this.graph.getModel().getParent(cell)
if (
this.graph.model.isNode(parent) &&
this.graph.isCellConnectable(parent)
) {
cell = parent
}
}
return this.graph.isCellLocked(cell) ? null : cell
}
update(
e: MouseEventEx,
isSource: boolean,
existingEdge: boolean,
currentPoint: Point | null,
) {
if (this.isEnabled() && !this.isEventIgnored(e)) {
if (!this.containerEventInstalled && this.graph.container) {
DomEvent.addListener(
this.graph.container,
'mouseleave',
this.resetHandler!,
)
}
const graphX = e.getGraphX()
const graphY = e.getGraphY()
const tol = this.getTolerance(e)
const x = currentPoint != null ? currentPoint.x : graphX
const y = currentPoint != null ? currentPoint.y : graphY
const grid = new Rectangle(x - tol, y - tol, 2 * tol, 2 * tol)
const mouse = new Rectangle(graphX - tol, graphY - tol, 2 * tol, 2 * tol)
const state = this.graph.view.getState(this.getCell(e, currentPoint))
// Keeps focus icons visible while over node bounds and
// no other cell under mouse or shift is pressed
if (
!this.isKeepFocusEvent(e) &&
(this.currentState == null ||
this.currentArea == null ||
state != null ||
!this.graph.model.isNode(this.currentState.cell) ||
!this.currentArea.isIntersectWith(mouse)) &&
state !== this.currentState
) {
this.currentArea = null
this.currentState = null
this.focus(e, state!, isSource)
}
this.currentPoint = null
this.currentAnchor = null
// highlight hovering anchor
if (
this.knobs != null &&
this.points != null &&
this.anchors != null &&
(state == null || this.currentState === state)
) {
// console.log('highlight hovering anchor')
let bounds: Rectangle | null = null
let minDist: number | null = null
for (let i = 0, ii = this.knobs.length; i < ii; i += 1) {
const dx = graphX - this.knobs[i].bounds.getCenterX()
const dy = graphY - this.knobs[i].bounds.getCenterY()
const dis = dx * dx + dy * dy
// console.log(dx, dy, dis)
if (
(Math.sqrt(dis) < this.inductionSize ||
this.intersects(this.knobs[i], mouse) ||
(currentPoint != null && this.intersects(this.knobs[i], grid))) &&
(minDist == null || dis < minDist)
) {
this.currentPoint = this.points[i]
this.currentAnchor = this.anchors[i]
minDist = dis
bounds = this.knobs[i].bounds.clone()
if (this.highlight == null) {
this.highlight = this.createHighlightShape(state)
}
}
}
if (bounds != null && this.highlight != null) {
this.highlight.bounds = bounds
this.highlight.redraw()
}
}
if (this.currentAnchor == null) {
this.destroyHighlight()
}
} else {
this.currentState = null
this.currentPoint = null
this.currentAnchor = null
}
}
focus(e: MouseEventEx, state: State, isSource: boolean) {
this.anchors = this.getAnchors(state, isSource)
if (this.anchors != null) {
this.currentState = state
this.currentArea = state.bounds.clone()
this.destroyIcons()
this.knobs = []
this.points = []
for (let i = 0, ii = this.anchors.length; i < ii; i += 1) {
const c = this.anchors[i]
const p = this.graph.view.getConnectionPoint(state, c)!
const icon = this.redrawAnchor(state, c, p)
this.knobs.push(icon)
this.points.push(p)
this.currentArea.add(icon.bounds)
}
this.currentArea.grow(this.getTolerance(e))
} else {
this.destroyIcons()
this.destroyHighlight()
}
}
protected createHighlightShape(state: State | null) {
const s = (this.currentState || state)!
const shape = createAnchorHighlightShape({
graph: this.graph,
cell: s.cell,
})
shape.init(this.graph.view.getOverlayPane())
const getState = () => (this.currentState || state) as State
MouseEventEx.redirectMouseEvents(shape.elem, this.graph, getState)
return shape
}
protected intersects(icon: Shape, mouse: Rectangle) {
return icon.bounds.isIntersectWith(mouse)
}
protected destroyIcons() {
if (this.knobs != null) {
this.knobs.forEach(i => i.dispose())
this.knobs = null
this.points = null
}
}
protected destroyHighlight() {
if (this.highlight) {
this.highlight.dispose()
this.highlight = null
}
}
@Disposable.aop()
dispose() {
this.reset()
if (this.resetHandler != null) {
this.graph.off(null, this.resetHandler)
this.graph.model.off(null, this.resetHandler)
this.graph.view.off(null, this.resetHandler)
if (this.containerEventInstalled && this.graph.container) {
DomEvent.removeListener(
this.graph.container,
'mouseleave',
this.resetHandler,
)
}
this.resetHandler = null
}
}
}