-
Notifications
You must be signed in to change notification settings - Fork 286
/
DragAndDropPanel.js
333 lines (310 loc) · 10.1 KB
/
DragAndDropPanel.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
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
/****************************************************************************
** @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 {
DragDropEffects,
DragDropItem,
DragSource,
GraphComponent,
IEdge,
ILabel,
IListEnumerable,
IModelItem,
INode,
IPort,
IStripe,
LabelDropInputMode,
ListEnumerable,
NodeDropInputMode,
Point,
PortDropInputMode,
Rect,
SimpleNode,
StripeDropInputMode,
SvgExport,
VoidNodeStyle,
yfiles
} from 'yfiles'
/**
* @typedef {(T|object)} DragAndDropPanelItem
*/
/**
* A drag and drop panel component, from which users can drag the items to a {@link GraphComponent}.
*
* The component is mounted into the provided HTML element. It can be populated with nodes,
* edges, labels, and ports using {@link populatePanel}.
*/
export class DragAndDropPanel {
/**
* The desired maximum width of each item. This value is used to decide whether a
* visualization must be scaled down.
*/
maxItemWidth = 150
/**
* Whether the labels of the DnD node visual should be transferred to the created node or discarded.
*/
copyNodeLabels = true
/**
* Create a new DragAndDropPanel and mount it to the provided div element.
* @param {!HTMLElement} div The element that will display the palette items.
*/
constructor(div) {
this.div = div
}
/**
* Adds the provided items to this panel.
* @param {!Iterable.<DragAndDropPanelItem.<(INode|IEdge)>>} items
*/
populatePanel(items) {
// Convert the nodes into plain visualizations
const graphComponent = new GraphComponent()
for (const item of items) {
const modelItem = item instanceof INode || item instanceof IEdge ? item : item.modelItem
const visual =
modelItem instanceof INode
? this.createNodeVisual(modelItem, graphComponent)
: this.createEdgeVisual(modelItem, graphComponent)
this.addStartDragListeners(modelItem, visual)
this.div.appendChild(visual)
}
}
/**
* @param {!HTMLElement} element
* @param {!unknown} data
*/
beginDrag(element, data) {
const dragSource = this.startDrag(element, data)
// let the GraphComponent handle the preview rendering if possible
if (dragSource) {
dragSource.addQueryContinueDragListener((_, evt) => {
if (evt.dropTarget === null) {
createDragPreviewElement(element).classList.remove('hidden')
} else {
createDragPreviewElement(element).classList.add('hidden')
}
})
}
}
/**
* @param {!HTMLElement} element
* @param {!unknown} data
*/
startDrag(element, data) {
if (data instanceof INode) {
return NodeDropInputMode.startDrag(
element,
data,
DragDropEffects.ALL,
true,
createDragPreviewElement(element)
)
}
if (data instanceof IEdge) {
const dragSource = new DragSource(element)
void dragSource.startDrag(
new DragDropItem(IEdge.$class.name, data),
DragDropEffects.ALL,
true,
createDragPreviewElement(element)
)
return dragSource
}
if (data instanceof ILabel) {
return LabelDropInputMode.startDrag(
element,
data,
DragDropEffects.ALL,
true,
createDragPreviewElement(element)
)
}
if (data instanceof IPort) {
return PortDropInputMode.startDrag(
element,
data,
DragDropEffects.ALL,
true,
createDragPreviewElement(element)
)
}
if (data instanceof IStripe) {
return StripeDropInputMode.startDrag(
element,
data,
DragDropEffects.ALL,
true,
createDragPreviewElement(element)
)
}
}
/**
* Creates an element that contains the visualization of the given node.
* This method is used by populatePanel to create the visualization
* for each node provided by the factory.
* @param {!DragAndDropPanelItem.<INode>} original
* @param {!GraphComponent} graphComponent
* @returns {!HTMLDivElement}
*/
createNodeVisual(original, graphComponent) {
const graph = graphComponent.graph
graph.clear()
const originalNode = original instanceof INode ? original : original.modelItem
const node = graph.createNode(originalNode.layout, originalNode.style, originalNode.tag)
originalNode.labels.forEach((label) => {
graph.addLabel(
node,
label.text,
label.layoutParameter,
label.style,
label.preferredSize,
label.tag
)
})
originalNode.ports.forEach((port) => {
graph.addPort(node, port.locationParameter, port.style, port.tag)
})
return this.exportAndWrap(
graphComponent,
original instanceof INode ? undefined : original.tooltip
)
}
/**
* Creates an element that contains the visualization of the given edge.
* @param {!DragAndDropPanelItem.<IEdge>} original
* @param {!GraphComponent} graphComponent
* @returns {!HTMLDivElement}
*/
createEdgeVisual(original, graphComponent) {
const graph = graphComponent.graph
graph.clear()
const originalEdge = original instanceof IEdge ? original : original.modelItem
const n1 = graph.createNode(new Rect(0, 10, 0, 0), VoidNodeStyle.INSTANCE)
const n2 = graph.createNode(new Rect(50, 40, 0, 0), VoidNodeStyle.INSTANCE)
const edge = graph.createEdge(n1, n2, originalEdge.style)
graph.addBend(edge, new Point(25, 10))
graph.addBend(edge, new Point(25, 40))
return this.exportAndWrap(
graphComponent,
original instanceof IEdge ? undefined : original.tooltip
)
}
/**
* Exports and wraps the original visualization in an HTML element.
* @param {!GraphComponent} graphComponent
* @param {!string} [tooltip]
* @returns {!HTMLDivElement}
*/
exportAndWrap(graphComponent, tooltip) {
graphComponent.updateContentRect(10)
const exporter = new SvgExport({
worldBounds: graphComponent.contentRect
})
exporter.scale = exporter.calculateScaleForWidth(
Math.min(this.maxItemWidth, graphComponent.contentRect.width)
)
const visual = exporter.exportSvg(graphComponent)
// Firefox does not display the SVG correctly because of the clip - so we remove it.
visual.removeAttribute('clip-path')
const div = document.createElement('div')
div.setAttribute('class', 'demo-dnd-panel__item')
div.appendChild(visual)
if (tooltip) {
div.title = tooltip
}
return div
}
/**
* Adds mousedown and pointer down listeners to the given element that starts the drag operation.
* @param {!(INode|IEdge)} item
* @param {!HTMLElement} element
*/
addStartDragListeners(item, element) {
// the actual drag operation
const doDragOperation = () => {
// Ensure that the following code still works, even when the view-table module isn't loaded
const IStripe = yfiles.graph.IStripe
if (IStripe && item.tag instanceof IStripe) {
// If the dummy node has a stripe as its tag, we use the stripe directly
// This allows StripeDropInputMode to take over
this.beginDrag(element, item.tag)
} else if (item.tag instanceof ILabel || item.tag instanceof IPort) {
this.beginDrag(element, item.tag)
} else if (item instanceof IEdge) {
this.beginDrag(element, item)
} /*if (item instanceof INode)*/ else {
// Otherwise, we just use the node itself and let (hopefully) NodeDropInputMode take over
const simpleNode = new SimpleNode()
simpleNode.layout = item.layout
simpleNode.style = item.style.clone()
simpleNode.tag = item.tag
simpleNode.labels = this.copyNodeLabels ? item.labels : IListEnumerable.EMPTY
if (item.ports.size > 0) {
simpleNode.ports = new ListEnumerable(item.ports)
}
this.beginDrag(element, simpleNode)
}
}
element.addEventListener(
'mousedown',
(evt) => {
if (evt.button !== 0) {
return
}
doDragOperation()
evt.preventDefault()
},
false
)
const touchStartListener = (evt) => {
doDragOperation()
evt.preventDefault()
}
if (window.PointerEvent === undefined) {
element.addEventListener('touchstart', touchStartListener, { passive: false })
return
}
element.addEventListener(
'pointerdown',
(evt) => {
if (evt.pointerType === 'touch' || evt.pointerType === 'pen') {
touchStartListener(evt)
}
},
true
)
}
}
/**
* @param {!HTMLElement} templateElement
* @returns {!HTMLElement}
*/
function createDragPreviewElement(templateElement) {
const dragPreview = templateElement.cloneNode(true)
dragPreview.style.margin = '0'
return dragPreview
}