/
knob.ts
56 lines (48 loc) 路 1.58 KB
/
knob.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
import { Node } from '../model/node'
import { Knob } from '../addon/knob'
import { Base } from './base'
import { EventArgs } from './events'
export class KnobManager extends Base {
protected widgets: Map<Node, Knob[]> = new Map()
protected get isSelectionEnabled() {
return this.options.selecting.enabled === true
}
protected init() {
this.startListening()
}
protected startListening() {
this.graph.on('node:mouseup', this.onNodeMouseUp, this)
this.graph.on('node:selected', this.onNodeSelected, this)
this.graph.on('node:unselected', this.onNodeUnSelected, this)
}
protected stopListening() {
this.graph.off('node:mouseup', this.onNodeMouseUp, this)
this.graph.off('node:selected', this.onNodeSelected, this)
this.graph.off('node:unselected', this.onNodeUnSelected, this)
}
protected onNodeMouseUp({ node }: EventArgs['node:mouseup']) {
if (!this.isSelectionEnabled) {
const widgets = this.graph.hook.createKnob(node, { clearAll: true })
if (widgets) {
this.widgets.set(node, widgets)
}
}
}
protected onNodeSelected({ node }: EventArgs['node:selected']) {
if (this.isSelectionEnabled) {
const widgets = this.graph.hook.createKnob(node, { clearAll: false })
if (widgets) {
this.widgets.set(node, widgets)
}
}
}
protected onNodeUnSelected({ node }: EventArgs['node:unselected']) {
if (this.isSelectionEnabled) {
const widgets = this.widgets.get(node)
if (widgets) {
widgets.forEach((widget) => widget.dispose())
}
this.widgets.delete(node)
}
}
}