-
Notifications
You must be signed in to change notification settings - Fork 6
/
Graph.js
392 lines (365 loc) · 11 KB
/
Graph.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import {
mxClient,
mxUtils,
mxEvent,
mxGraph,
mxConstants,
mxPerimeter,
mxGraphHandler,
mxEdgeHandler,
mxEdgeStyle,
mxToolbar,
mxCell,
mxGeometry,
mxUndoManager,
mxPolyline,
mxCellState,
mxConnectionConstraint,
mxShape,
mxPoint,
mxConstraintHandler,
mxEllipse,
mxClipboard,
mxKeyHandler,
mxRubberband
} from '@/assets/mxgraph/4.0.0/mxgraph'
import './Shapes'
import rectangleImg from '@/assets/mxgraph/4.0.0/images/rectangle.png'
import ellipseImg from '@/assets/mxgraph/4.0.0/images/ellipse.png'
import rhombusImg from '@/assets/mxgraph/4.0.0/images/rhombus.png'
import triangleImg from '@/assets/mxgraph/4.0.0/images/triangle.gif'
import cylinderImg from '@/assets/mxgraph/4.0.0/images/arcedge.png'
import actorImg from '@/assets/mxgraph/4.0.0/images/actor.gif'
class Graph {
constructor(vm, containerEl, toolbarContainerEl) {
this.version = '1.0.0'
// graph 画板
this.container = containerEl
// 拖拽条,拖拽条也可以用
this.toolbarContainer = toolbarContainerEl
// mxGraph
this.mxGraph = null
// vm 实例
this.vm = vm
// 默认 vertex 样式
this.defaultStyle = {
width: 120,
height: 60
}
}
/* uuid */ uuid(len, radix) {
var chars = '0123456789abcdefghijklmnopqrstuvwxyz'.split('')
var uuid = []
radix = radix || chars.length
len = len || 32
for (var i = 0; i < len; i++) {
uuid[i] = chars[0 | (Math.random() * radix)]
}
return uuid.join('')
}
/* private*/ enableGuides() {
// Enables guides
mxGraphHandler.prototype.guidesEnabled = true
// Alt disables guides
mxGraphHandler.prototype.useGuidesForEvent = function(me) {
return !mxEvent.isAltDown(me.getEvent())
}
// Defines the guides to be red (default)
mxConstants.GUIDE_COLOR = '#FF0000'
// Defines the guides to be 1 pixel (default)
mxConstants.GUIDE_STROKEWIDTH = 1
// Enables snapping waypoints to terminals
mxEdgeHandler.prototype.snapToTerminals = true
}
/* private */ setCommonStyle() {
// Changes some default colors
// 选中 cell 后的连接点颜色
mxConstants.HANDLE_FILLCOLOR = '#99ccff'
mxConstants.HANDLE_STROKECOLOR = '#0088cf'
// edge 连接点颜色
mxConstants.CONNECT_HANDLE_FILLCOLOR = '#409EFF'
// 控制 vertex connect hover 边框颜色
mxConstants.DEFAULT_VALID_COLOR = '#409EFF'
// 选中 edge 后的颜色
mxConstants.EDGE_SELECTION_COLOR = '#00a8ff'
// 选中 vertex 后的边框颜色
mxConstants.VERTEX_SELECTION_COLOR = '#00a8ff'
mxConstraintHandler.prototype.highlightColor =
mxConstants.DEFAULT_VALID_COLOR
mxConstraintHandler.prototype.createHighlightShape = function() {
var hl = new mxEllipse(null, this.highlightColor, this.highlightColor, 0)
hl.opacity = mxConstants.HIGHLIGHT_OPACITY
return hl
}
}
/* private*/ setComponentsStyle() {
const graph = this.mxGraph
var vertexStyle = graph.getStylesheet().getDefaultVertexStyle()
vertexStyle[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_RECTANGLE
vertexStyle[mxConstants.STYLE_PERIMETER] = mxPerimeter.RectanglePerimeter
vertexStyle[mxConstants.STYLE_ROUNDED] = true
vertexStyle[mxConstants.STYLE_FILLCOLOR] = '#FFFFFF'
vertexStyle[mxConstants.STYLE_STROKECOLOR] = '#808080'
vertexStyle[mxConstants.STYLE_GRADIENTCOLOR] = '#FFFFFF'
vertexStyle[mxConstants.STYLE_FONTCOLOR] = '#5f5f5f'
vertexStyle[mxConstants.STYLE_ALIGN] = mxConstants.ALIGN_CENTER
vertexStyle[mxConstants.STYLE_VERTICAL_ALIGN] = mxConstants.ALIGN_MIDDLE
vertexStyle[mxConstants.STYLE_FONTSIZE] = '12'
vertexStyle[mxConstants.STYLE_FONTSTYLE] = 1
var edgeStyle = graph.getStylesheet().getDefaultEdgeStyle()
edgeStyle[mxConstants.STYLE_ROUNDED] = true
edgeStyle[mxConstants.STYLE_EDGE] = mxEdgeStyle.ElbowConnector
graph.alternateEdgeStyle = 'elbow=vertical'
}
/* private */ setKeyHandler() {
const graph = this.mxGraph
const vm = this.vm
const undoManager = new mxUndoManager()
const listener = function(sender, evt) {
undoManager.undoableEditHappened(evt.getProperty('edit'))
}
graph.getModel().addListener(mxEvent.UNDO, listener)
graph.getView().addListener(mxEvent.UNDO, listener)
const keyHandler = new mxKeyHandler(graph)
// 绑定删除键
keyHandler.bindKey(46, function(evt) {
if (graph.isEnabled()) {
vm.$confirm('确定删除吗')
.then(res => {
graph.removeCells()
})
.catch(cancel => console.log(cancel))
}
})
// 绑定Ctrl+A全选
keyHandler.bindControlKey(65, function(evt) {
if (graph.isEnabled()) {
graph.selectAll()
}
})
// 绑定Ctrl+C复制节点
keyHandler.bindControlKey(67, function(evt) {
if (graph.isEnabled()) {
var cells = graph.getSelectionCells()
if (cells.length > 0) {
cells = graph.cloneCells(cells)
var newCells = []
// 修改 cells 中的元素属性
cells.forEach(function(cell, i) {
cell.id = ''
cell.value = (cell.value || '') + '_1'
newCells.push(cell)
})
mxClipboard.setCells(newCells)
mxClipboard.paste(graph)
}
}
})
// 绑定Ctrl+S保存
keyHandler.bindControlKey(83, function(evt) {
if (graph.isEnabled()) {
// saveXml(graph)
}
})
// 绑定Ctrl+I新增
// keyHandler.bindControlKey(73, function(evt) {
// if (graph.isEnabled()) {
// graph.insertVertex(
// graphParent,
// null,
// '请输入文本',
// 10,
// 10,
// 100,
// 40,
// mxStyleList.rectangle
// )
// }
// })
// 绑定Ctrl+Z撤销
keyHandler.bindControlKey(90, function(evt) {
if (graph.isEnabled()) {
undoManager.undo()
}
})
// 绑定Ctrl+Y重做
keyHandler.bindControlKey(89, function(evt) {
if (graph.isEnabled()) {
undoManager.redo()
}
})
graph.isCellDeletable = function(cell) {
return true
}
}
/* private */ openVertex(vertex) {
const vm = this.vm
// vm.$refs.dialogVertex.dialogVertexVisible = true
vm.$set(vm.$refs.dialogVertex, 'dialogVertexVisible', true)
}
/* private */ openEdge(edge) {
const vm = this.vm
}
init() {
const _this = this
// Checks if the browser is supported
if (!mxClient.isBrowserSupported()) {
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false)
} else {
// Disables the built-in context menu
mxEvent.disableContextMenu(this.container)
this.enableGuides()
this.setCommonStyle()
// Creates the graph inside the given container
var graph = new mxGraph(this.container)
// Attention
this.mxGraph = graph
graph.setPanning(true)
graph.setTooltips(true)
graph.setConnectable(true)
graph.setCellsResizable(true)
// 设置节点 vertex and edge 双击事件
// 节点双击事件,双击打开详情面板
graph.addListener(mxEvent.CLICK, function(sender, evt) {
var cell = evt.getProperty('cell')
if (cell != null) {
if (cell.vertex) {
// console.log(cell.userData);
if (cell.userData && cell.userData.isGroup) {
// 组合双击不打开
} else {
console.log('I am vertex')
_this.openVertex(cell)
}
} else if (cell.edge) {
console.log('I am edge')
// openEdge(cell)
}
}
})
// 设置 vertex 和 edge 的样式
this.setComponentsStyle()
// 设置 Guides
graph.gridSize = 30
// Enables rubberband selection
new mxRubberband(graph)
this.setKeyHandler()
if (this.toolbarContainer) {
this.initToolbar()
}
}
}
initToolbar() {
const graph = this.mxGraph
const toolbar = new mxToolbar(this.toolbarContainer)
const _this = this
// 处理toolbar
toolbar.enabled = false
addVertex(
rectangleImg,
_this.defaultStyle.width,
_this.defaultStyle.height,
''
)
addVertex(
ellipseImg,
_this.defaultStyle.width,
_this.defaultStyle.height,
'shape=ellipse'
)
addVertex(
rhombusImg,
_this.defaultStyle.width,
_this.defaultStyle.height,
'shape=rhombus'
)
addVertex(
cylinderImg,
_this.defaultStyle.width,
_this.defaultStyle.height,
'shape=terminator;'
)
// addVertex(
// triangleImg,
// _this.defaultStyle.height,
// _this.defaultStyle.height,
// 'shape=triangle'
// )
// addVertex(
// cylinderImg,
// _this.defaultStyle.height,
// _this.defaultStyle.height,
// 'shape=cylinder'
// )
// addVertex(
// actorImg,
// _this.defaultStyle.height,
// _this.defaultStyle.height,
// 'shape=actor'
// )
// toolbar.addLine();
// toolbar 初始化完毕
// 添加工作条元素
function addVertex(icon, w, h, style) {
var vertex = new mxCell('请输入文本', new mxGeometry(0, 0, w, h), style)
vertex.setVertex(true)
addToolbarItem(graph, toolbar, vertex, icon)
}
// 绑定工作条元素事件
function addToolbarItem(graph, toolbar, prototype, image) {
// Function that is executed when the image is dropped on
// the graph. The cell argument points to the cell under
// the mousepointer if there is one.
var funct = function(graph, evt, cell) {
graph.stopEditing(false)
var pt = graph.getPointForEvent(evt)
var vertex = graph.getModel().cloneCell(prototype)
vertex.geometry.x = pt.x
vertex.geometry.y = pt.y
// 对新增的节点id给定uuid
var cells = graph.importCells([vertex], 0, 0, cell)
cells.forEach(function(item, i) {
item.id = _this.uuid()
})
graph.setSelectionCells(cells)
}
// Creates the image which is used as the drag icon (preview)
var img = toolbar.addMode(null, image, funct)
mxUtils.makeDraggable(img, graph, funct)
}
}
draw() {
const graph = this.mxGraph
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
var parent = graph.getDefaultParent()
// Adds cells to the model in a single step
graph.getModel().beginUpdate()
try {
var v1 = graph.insertVertex(
parent,
null,
'Hello,',
20,
20,
this.defaultStyle.width,
this.defaultStyle.height
)
var v2 = graph.insertVertex(
parent,
null,
'World!',
200,
150,
this.defaultStyle.width,
this.defaultStyle.height
)
var e1 = graph.insertEdge(parent, null, '', v1, v2)
} finally {
// Updates the display
graph.getModel().endUpdate()
}
}
}
export { Graph }