-
Notifications
You must be signed in to change notification settings - Fork 286
/
NodeTypePanel.js
274 lines (254 loc) · 8.2 KB
/
NodeTypePanel.js
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
/****************************************************************************
** @license
** This demo file is part of yFiles for HTML 2.6.
** Copyright (c) 2000-2024 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for HTML functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for HTML version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for HTML powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for HTML
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
import {
ExteriorLabelModel,
ExteriorLabelModelPosition,
GraphComponent,
ILabelModelParameter,
INode,
Point,
SimpleLabel,
Size
} from 'yfiles'
/**
* This class adds an HTML panel on top of the contents of the GraphComponent that can display arbitrary information
* about an {@link INode}. In order to not interfere with the positioning of the pop-up, HTML content should be added
* as ancestor of the {@link NodeTypePanel.div div element}, and use relative positioning. This implementation uses
* an {@link ILabelModelParameter} to determine the position of the pop-up.
*/
export default class NodeTypePanel {
div
dirty = false
_currentItems = null
/**
* Creates a new instance of {@link NodeTypePanel}.
* @param {!GraphComponent} graphComponent
* @param {!Array.<string>} typeColors
* @param {!Record.<string,object>} colorSets
*/
constructor(graphComponent, typeColors, colorSets) {
this.colorSets = colorSets
this.typeColors = typeColors
this.graphComponent = graphComponent
this.div = document.getElementById('node-type-panel')
// make the popup invisible
this.div.style.opacity = '0'
this.div.style.display = 'none'
this.registerListeners()
this.registerClickListeners()
}
/**
* Sets the {@link INode nodes} to display the type choice pop-up for.
* Setting this property to a value other than `null` shows the pop-up.
* Setting the property to `null` hides the pop-up.
* @type {?Array.<INode>}
*/
set currentItems(items) {
if (items && items.length > 0) {
if (!equals(items, this._currentItems)) {
this._currentItems = items
this.show()
}
} else {
this._currentItems = null
this.hide()
}
}
/**
* Returns all {@link INode}s to display the pop-up for.
* @type {?Array.<INode>}
*/
get currentItems() {
return this._currentItems
}
/**
* Registers listeners for viewport, node bounds and visual tree changes to the {@link GraphComponent}.
*/
registerListeners() {
// Adds listener for viewport changes
this.graphComponent.addViewportChangedListener(() => {
if (this.currentItems && this.currentItems.length > 0) {
this.dirty = true
}
})
// Adds listener for updates of the visual tree
this.graphComponent.addUpdatedVisualListener(() => {
if (this.currentItems && this.currentItems.length > 0 && this.dirty) {
this.dirty = false
this.updateLocation()
}
})
}
/**
* Registers click listeners for all buttons of this {@link NodeTypePanel}.
*/
registerClickListeners() {
for (const button of this.div.querySelectorAll('.node-type-button')) {
const classes = button.getAttribute('class')
const type = NodeTypePanel.findType(classes)
if (type > -1) {
button.style.backgroundColor = this.colorSets[this.typeColors[type]].fill
this.addClickListener(button, type)
}
}
}
/**
* @param {?string} cssClasses
* @returns {number}
*/
static findType(cssClasses) {
if (cssClasses != null && cssClasses.length > 0) {
for (const cssClass of cssClasses.split(' ')) {
if (cssClass.startsWith('type-')) {
return parseInt(cssClass.substring(5))
}
}
}
return -1
}
/**
* Registers a click listener to given element which will invoke the callback {@link nodeTypeChanged} and
* {@link typeChanged} in case the type of the current item changed.
* @param {?Element} element
* @param {number} type
*/
addClickListener(element, type) {
if (!element) {
return
}
element.addEventListener('click', () => {
if (this.currentItems) {
this.currentItems.forEach((item) => {
const oldType = item.tag && item.tag.type
if (oldType !== type) {
this.nodeTypeChanged(item, type, oldType)
}
})
this.typeChanged()
this.currentItems = null
}
})
}
/**
* Makes this pop-up visible.
*/
show() {
this.div.style.display = 'inline-block'
this.div.style.opacity = '1'
for (const btn of this.div.querySelectorAll('.node-type-button')) {
btn.classList.remove('current-type')
}
if (this.currentItems) {
this.currentItems.forEach((item) => {
this.div
.querySelector(`.type-${(item.tag && item.tag.type) || 0}`)
.classList.add('current-type')
})
}
this.updateLocation()
}
/**
* Hides this pop-up.
*/
hide() {
this.div.style.opacity = '0'
this.div.style.display = 'none'
}
/**
* Changes the location of this pop-up to the location calculated by the
* {@link NodeTypePanel.labelModelParameter}.
*/
updateLocation() {
if (!this.currentItems || this.currentItems.length === 0) {
return
}
const width = this.div.offsetWidth
const height = this.div.offsetHeight
const zoom = this.graphComponent.zoom
const labelModelParameter = new ExteriorLabelModel({ insets: [20, 0, 0, 0] }).createParameter(
ExteriorLabelModelPosition.NORTH
)
const dummyLabel = new SimpleLabel(this.currentItems[0], '', labelModelParameter)
if (labelModelParameter.supports(dummyLabel)) {
dummyLabel.preferredSize = new Size(width / zoom, height / zoom)
const { anchorX, anchorY } = labelModelParameter.model.getGeometry(
dummyLabel,
labelModelParameter
)
this.setLocation(anchorX, anchorY - height / zoom)
}
}
/**
* Sets the location of this pop-up to the given world coordinates.
* @param {number} x
* @param {number} y
*/
setLocation(x, y) {
// Calculate the view coordinates since we have to place the div in the regular HTML coordinate space
const viewPoint = this.graphComponent.toViewCoordinates(new Point(x, y))
this.div.style.left = `${viewPoint.x}px`
this.div.style.top = `${viewPoint.y}px`
}
/**
* Callback for when the type changed for a specific node
* @param {!INode} item
* @param {number} newType
* @param {number} oldType
*/
nodeTypeChanged(item, newType, oldType) {}
/**
* Callback for when the type changed for some or all nodes in the graph.
*/
typeChanged() {}
}
/**
* Checks the given arrays for equality.
* @param {?Array.<INode>} nodes1
* @param {?Array.<INode>} nodes2
* @returns {boolean}
*/
function equals(nodes1, nodes2) {
if (nodes1 === nodes2) {
return true
}
if (nodes1 == null || nodes2 == null) {
return false
}
if (nodes1.length !== nodes2.length) {
return false
}
nodes1.sort()
nodes2.sort()
for (let i = 0; i < nodes1.length; i++) {
if (nodes1[i] !== nodes2[i]) {
return false
}
}
return true
}