forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxConnectionHandler.js
More file actions
2204 lines (1949 loc) · 59.4 KB
/
mxConnectionHandler.js
File metadata and controls
2204 lines (1949 loc) · 59.4 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2006-2016, JGraph Ltd
* Copyright (c) 2006-2016, Gaudenz Alder
*/
/**
* Class: mxConnectionHandler
*
* Graph event handler that creates new connections. Uses <mxTerminalMarker>
* for finding and highlighting the source and target vertices and
* <factoryMethod> to create the edge instance. This handler is built-into
* <mxGraph.connectionHandler> and enabled using <mxGraph.setConnectable>.
*
* Example:
*
* (code)
* new mxConnectionHandler(graph, function(source, target, style)
* {
* edge = new mxCell('', new mxGeometry());
* edge.setEdge(true);
* edge.setStyle(style);
* edge.geometry.relative = true;
* return edge;
* });
* (end)
*
* Here is an alternative solution that just sets a specific user object for
* new edges by overriding <insertEdge>.
*
* (code)
* mxConnectionHandlerInsertEdge = mxConnectionHandler.prototype.insertEdge;
* mxConnectionHandler.prototype.insertEdge = function(parent, id, value, source, target, style)
* {
* value = 'Test';
*
* return mxConnectionHandlerInsertEdge.apply(this, arguments);
* };
* (end)
*
* Using images to trigger connections:
*
* This handler uses mxTerminalMarker to find the source and target cell for
* the new connection and creates a new edge using <connect>. The new edge is
* created using <createEdge> which in turn uses <factoryMethod> or creates a
* new default edge.
*
* The handler uses a "highlight-paradigm" for indicating if a cell is being
* used as a source or target terminal, as seen in other diagramming products.
* In order to allow both, moving and connecting cells at the same time,
* <mxConstants.DEFAULT_HOTSPOT> is used in the handler to determine the hotspot
* of a cell, that is, the region of the cell which is used to trigger a new
* connection. The constant is a value between 0 and 1 that specifies the
* amount of the width and height around the center to be used for the hotspot
* of a cell and its default value is 0.5. In addition,
* <mxConstants.MIN_HOTSPOT_SIZE> defines the minimum number of pixels for the
* width and height of the hotspot.
*
* This solution, while standards compliant, may be somewhat confusing because
* there is no visual indicator for the hotspot and the highlight is seen to
* switch on and off while the mouse is being moved in and out. Furthermore,
* this paradigm does not allow to create different connections depending on
* the highlighted hotspot as there is only one hotspot per cell and it
* normally does not allow cells to be moved and connected at the same time as
* there is no clear indication of the connectable area of the cell.
*
* To come across these issues, the handle has an additional <createIcons> hook
* with a default implementation that allows to create one icon to be used to
* trigger new connections. If this icon is specified, then new connections can
* only be created if the image is clicked while the cell is being highlighted.
* The <createIcons> hook may be overridden to create more than one
* <mxImageShape> for creating new connections, but the default implementation
* supports one image and is used as follows:
*
* In order to display the "connect image" whenever the mouse is over the cell,
* an DEFAULT_HOTSPOT of 1 should be used:
*
* (code)
* mxConstants.DEFAULT_HOTSPOT = 1;
* (end)
*
* In order to avoid confusion with the highlighting, the highlight color
* should not be used with a connect image:
*
* (code)
* mxConstants.HIGHLIGHT_COLOR = null;
* (end)
*
* To install the image, the connectImage field of the mxConnectionHandler must
* be assigned a new <mxImage> instance:
*
* (code)
* mxConnectionHandler.prototype.connectImage = new mxImage('images/green-dot.gif', 14, 14);
* (end)
*
* This will use the green-dot.gif with a width and height of 14 pixels as the
* image to trigger new connections. In createIcons the icon field of the
* handler will be set in order to remember the icon that has been clicked for
* creating the new connection. This field will be available under selectedIcon
* in the connect method, which may be overridden to take the icon that
* triggered the new connection into account. This is useful if more than one
* icon may be used to create a connection.
*
* Group: Events
*
* Event: mxEvent.START
*
* Fires when a new connection is being created by the user. The <code>state</code>
* property contains the state of the source cell.
*
* Event: mxEvent.CONNECT
*
* Fires between begin- and endUpdate in <connect>. The <code>cell</code>
* property contains the inserted edge, the <code>event</code> and <code>target</code>
* properties contain the respective arguments that were passed to <connect> (where
* target corresponds to the dropTarget argument). Finally, the <code>terminal</code>
* property corresponds to the target argument in <connect> or the clone of the source
* terminal if <createTarget> is enabled.
*
* Note that the target is the cell under the mouse where the mouse button was released.
* Depending on the logic in the handler, this doesn't necessarily have to be the target
* of the inserted edge. To print the source, target or any optional ports IDs that the
* edge is connected to, the following code can be used. To get more details about the
* actual connection point, <mxGraph.getConnectionConstraint> can be used. To resolve
* the port IDs, use <mxGraphModel.getCell>.
*
* (code)
* graph.connectionHandler.addListener(mxEvent.CONNECT, function(sender, evt)
* {
* var edge = evt.getProperty('cell');
* var source = graph.getModel().getTerminal(edge, true);
* var target = graph.getModel().getTerminal(edge, false);
*
* var style = graph.getCellStyle(edge);
* var sourcePortId = style[mxConstants.STYLE_SOURCE_PORT];
* var targetPortId = style[mxConstants.STYLE_TARGET_PORT];
*
* mxLog.show();
* mxLog.debug('connect', edge, source.id, target.id, sourcePortId, targetPortId);
* });
* (end)
*
* Event: mxEvent.RESET
*
* Fires when the <reset> method is invoked.
*
* Constructor: mxConnectionHandler
*
* Constructs an event handler that connects vertices using the specified
* factory method to create the new edges. Modify
* <mxConstants.ACTIVE_REGION> to setup the region on a cell which triggers
* the creation of a new connection or use connect icons as explained
* above.
*
* Parameters:
*
* graph - Reference to the enclosing <mxGraph>.
* factoryMethod - Optional function to create the edge. The function takes
* the source and target <mxCell> as the first and second argument and an
* optional cell style from the preview as the third argument. It returns
* the <mxCell> that represents the new edge.
*/
function mxConnectionHandler(graph, factoryMethod)
{
mxEventSource.call(this);
if (graph != null)
{
this.graph = graph;
this.factoryMethod = factoryMethod;
this.init();
// Handles escape keystrokes
this.escapeHandler = mxUtils.bind(this, function(sender, evt)
{
this.reset();
});
this.graph.addListener(mxEvent.ESCAPE, this.escapeHandler);
}
};
/**
* Extends mxEventSource.
*/
mxUtils.extend(mxConnectionHandler, mxEventSource);
/**
* Variable: graph
*
* Reference to the enclosing <mxGraph>.
*/
mxConnectionHandler.prototype.graph = null;
/**
* Variable: factoryMethod
*
* Function that is used for creating new edges. The function takes the
* source and target <mxCell> as the first and second argument and returns
* a new <mxCell> that represents the edge. This is used in <createEdge>.
*/
mxConnectionHandler.prototype.factoryMethod = true;
/**
* Variable: moveIconFront
*
* Specifies if icons should be displayed inside the graph container instead
* of the overlay pane. This is used for HTML labels on vertices which hide
* the connect icon. This has precendence over <moveIconBack> when set
* to true. Default is false.
*/
mxConnectionHandler.prototype.moveIconFront = false;
/**
* Variable: moveIconBack
*
* Specifies if icons should be moved to the back of the overlay pane. This can
* be set to true if the icons of the connection handler conflict with other
* handles, such as the vertex label move handle. Default is false.
*/
mxConnectionHandler.prototype.moveIconBack = false;
/**
* Variable: connectImage
*
* <mxImage> that is used to trigger the creation of a new connection. This
* is used in <createIcons>. Default is null.
*/
mxConnectionHandler.prototype.connectImage = null;
/**
* Variable: targetConnectImage
*
* Specifies if the connect icon should be centered on the target state
* while connections are being previewed. Default is false.
*/
mxConnectionHandler.prototype.targetConnectImage = false;
/**
* Variable: enabled
*
* Specifies if events are handled. Default is true.
*/
mxConnectionHandler.prototype.enabled = true;
/**
* Variable: select
*
* Specifies if new edges should be selected. Default is true.
*/
mxConnectionHandler.prototype.select = true;
/**
* Variable: createTarget
*
* Specifies if <createTargetVertex> should be called if no target was under the
* mouse for the new connection. Setting this to true means the connection
* will be drawn as valid if no target is under the mouse, and
* <createTargetVertex> will be called before the connection is created between
* the source cell and the newly created vertex in <createTargetVertex>, which
* can be overridden to create a new target. Default is false.
*/
mxConnectionHandler.prototype.createTarget = false;
/**
* Variable: marker
*
* Holds the <mxTerminalMarker> used for finding source and target cells.
*/
mxConnectionHandler.prototype.marker = null;
/**
* Variable: constraintHandler
*
* Holds the <mxConstraintHandler> used for drawing and highlighting
* constraints.
*/
mxConnectionHandler.prototype.constraintHandler = null;
/**
* Variable: error
*
* Holds the current validation error while connections are being created.
*/
mxConnectionHandler.prototype.error = null;
/**
* Variable: waypointsEnabled
*
* Specifies if single clicks should add waypoints on the new edge. Default is
* false.
*/
mxConnectionHandler.prototype.waypointsEnabled = false;
/**
* Variable: ignoreMouseDown
*
* Specifies if the connection handler should ignore the state of the mouse
* button when highlighting the source. Default is false, that is, the
* handler only highlights the source if no button is being pressed.
*/
mxConnectionHandler.prototype.ignoreMouseDown = false;
/**
* Variable: first
*
* Holds the <mxPoint> where the mouseDown took place while the handler is
* active.
*/
mxConnectionHandler.prototype.first = null;
/**
* Variable: connectIconOffset
*
* Holds the offset for connect icons during connection preview.
* Default is mxPoint(0, <mxConstants.TOOLTIP_VERTICAL_OFFSET>).
* Note that placing the icon under the mouse pointer with an
* offset of (0,0) will affect hit detection.
*/
mxConnectionHandler.prototype.connectIconOffset = new mxPoint(0, mxConstants.TOOLTIP_VERTICAL_OFFSET);
/**
* Variable: edgeState
*
* Optional <mxCellState> that represents the preview edge while the
* handler is active. This is created in <createEdgeState>.
*/
mxConnectionHandler.prototype.edgeState = null;
/**
* Variable: changeHandler
*
* Holds the change event listener for later removal.
*/
mxConnectionHandler.prototype.changeHandler = null;
/**
* Variable: drillHandler
*
* Holds the drill event listener for later removal.
*/
mxConnectionHandler.prototype.drillHandler = null;
/**
* Variable: mouseDownCounter
*
* Counts the number of mouseDown events since the start. The initial mouse
* down event counts as 1.
*/
mxConnectionHandler.prototype.mouseDownCounter = 0;
/**
* Variable: movePreviewAway
*
* Switch to enable moving the preview away from the mousepointer. This is required in browsers
* where the preview cannot be made transparent to events and if the built-in hit detection on
* the HTML elements in the page should be used. Default is the value of <mxClient.IS_VML>.
*/
mxConnectionHandler.prototype.movePreviewAway = mxClient.IS_VML;
/**
* Variable: outlineConnect
*
* Specifies if connections to the outline of a highlighted target should be
* enabled. This will allow to place the connection point along the outline of
* the highlighted target. Default is false.
*/
mxConnectionHandler.prototype.outlineConnect = false;
/**
* Variable: livePreview
*
* Specifies if the actual shape of the edge state should be used for the preview.
* Default is false. (Ignored if no edge state is created in <createEdgeState>.)
*/
mxConnectionHandler.prototype.livePreview = false;
/**
* Variable: cursor
*
* Specifies the cursor to be used while the handler is active. Default is null.
*/
mxConnectionHandler.prototype.cursor = null;
/**
* Variable: insertBeforeSource
*
* Specifies if new edges should be inserted before the source vertex in the
* cell hierarchy. Default is false for backwards compatibility.
*/
mxConnectionHandler.prototype.insertBeforeSource = false;
/**
* Function: isEnabled
*
* Returns true if events are handled. This implementation
* returns <enabled>.
*/
mxConnectionHandler.prototype.isEnabled = function()
{
return this.enabled;
};
/**
* Function: setEnabled
*
* Enables or disables event handling. This implementation
* updates <enabled>.
*
* Parameters:
*
* enabled - Boolean that specifies the new enabled state.
*/
mxConnectionHandler.prototype.setEnabled = function(enabled)
{
this.enabled = enabled;
};
/**
* Function: isInsertBefore
*
* Returns <insertBeforeSource> for non-loops and false for loops.
*
* Parameters:
*
* edge - <mxCell> that represents the edge to be inserted.
* source - <mxCell> that represents the source terminal.
* target - <mxCell> that represents the target terminal.
* evt - Mousedown event of the connect gesture.
* dropTarget - <mxCell> that represents the cell under the mouse when it was
* released.
*/
mxConnectionHandler.prototype.isInsertBefore = function(edge, source, target, evt, dropTarget)
{
return this.insertBeforeSource && source != target;
};
/**
* Function: isCreateTarget
*
* Returns <createTarget>.
*
* Parameters:
*
* evt - Current active native pointer event.
*/
mxConnectionHandler.prototype.isCreateTarget = function(evt)
{
return this.createTarget;
};
/**
* Function: setCreateTarget
*
* Sets <createTarget>.
*/
mxConnectionHandler.prototype.setCreateTarget = function(value)
{
this.createTarget = value;
};
/**
* Function: createShape
*
* Creates the preview shape for new connections.
*/
mxConnectionHandler.prototype.createShape = function()
{
// Creates the edge preview
var shape = (this.livePreview && this.edgeState != null) ?
this.graph.cellRenderer.createShape(this.edgeState) :
new mxPolyline([], mxConstants.INVALID_COLOR);
shape.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ?
mxConstants.DIALECT_VML : mxConstants.DIALECT_SVG;
shape.scale = this.graph.view.scale;
shape.pointerEvents = false;
shape.isDashed = true;
shape.init(this.graph.getView().getOverlayPane());
mxEvent.redirectMouseEvents(shape.node, this.graph, null);
return shape;
};
/**
* Function: init
*
* Initializes the shapes required for this connection handler. This should
* be invoked if <mxGraph.container> is assigned after the connection
* handler has been created.
*/
mxConnectionHandler.prototype.init = function()
{
this.graph.addMouseListener(this);
this.marker = this.createMarker();
this.constraintHandler = new mxConstraintHandler(this.graph);
// Redraws the icons if the graph changes
this.changeHandler = mxUtils.bind(this, function(sender)
{
if (this.iconState != null)
{
this.iconState = this.graph.getView().getState(this.iconState.cell);
}
if (this.iconState != null)
{
this.redrawIcons(this.icons, this.iconState);
this.constraintHandler.reset();
}
else if (this.previous != null && this.graph.view.getState(this.previous.cell) == null)
{
this.reset();
}
});
this.graph.getModel().addListener(mxEvent.CHANGE, this.changeHandler);
this.graph.getView().addListener(mxEvent.SCALE, this.changeHandler);
this.graph.getView().addListener(mxEvent.TRANSLATE, this.changeHandler);
this.graph.getView().addListener(mxEvent.SCALE_AND_TRANSLATE, this.changeHandler);
// Removes the icon if we step into/up or start editing
this.drillHandler = mxUtils.bind(this, function(sender)
{
this.reset();
});
this.graph.addListener(mxEvent.START_EDITING, this.drillHandler);
this.graph.getView().addListener(mxEvent.DOWN, this.drillHandler);
this.graph.getView().addListener(mxEvent.UP, this.drillHandler);
};
/**
* Function: isConnectableCell
*
* Returns true if the given cell is connectable. This is a hook to
* disable floating connections. This implementation returns true.
*/
mxConnectionHandler.prototype.isConnectableCell = function(cell)
{
return true;
};
/**
* Function: createMarker
*
* Creates and returns the <mxCellMarker> used in <marker>.
*/
mxConnectionHandler.prototype.createMarker = function()
{
var marker = new mxCellMarker(this.graph);
marker.hotspotEnabled = true;
// Overrides to return cell at location only if valid (so that
// there is no highlight for invalid cells)
marker.getCell = mxUtils.bind(this, function(me)
{
var cell = mxCellMarker.prototype.getCell.apply(marker, arguments);
this.error = null;
// Checks for cell at preview point (with grid)
if (cell == null && this.currentPoint != null)
{
cell = this.graph.getCellAt(this.currentPoint.x, this.currentPoint.y);
}
// Uses connectable parent vertex if one exists
if (cell != null && !this.graph.isCellConnectable(cell))
{
var parent = this.graph.getModel().getParent(cell);
if (this.graph.getModel().isVertex(parent) && this.graph.isCellConnectable(parent))
{
cell = parent;
}
}
if ((this.graph.isSwimlane(cell) && this.currentPoint != null &&
this.graph.hitsSwimlaneContent(cell, this.currentPoint.x, this.currentPoint.y)) ||
!this.isConnectableCell(cell))
{
cell = null;
}
if (cell != null)
{
if (this.isConnecting())
{
if (this.previous != null)
{
this.error = this.validateConnection(this.previous.cell, cell);
if (this.error != null && this.error.length == 0)
{
cell = null;
// Enables create target inside groups
if (this.isCreateTarget(me.getEvent()))
{
this.error = null;
}
}
}
}
else if (!this.isValidSource(cell, me))
{
cell = null;
}
}
else if (this.isConnecting() && !this.isCreateTarget(me.getEvent()) &&
!this.graph.allowDanglingEdges)
{
this.error = '';
}
return cell;
});
// Sets the highlight color according to validateConnection
marker.isValidState = mxUtils.bind(this, function(state)
{
if (this.isConnecting())
{
return this.error == null;
}
else
{
return mxCellMarker.prototype.isValidState.apply(marker, arguments);
}
});
// Overrides to use marker color only in highlight mode or for
// target selection
marker.getMarkerColor = mxUtils.bind(this, function(evt, state, isValid)
{
return (this.connectImage == null || this.isConnecting()) ?
mxCellMarker.prototype.getMarkerColor.apply(marker, arguments) :
null;
});
// Overrides to use hotspot only for source selection otherwise
// intersects always returns true when over a cell
marker.intersects = mxUtils.bind(this, function(state, evt)
{
if (this.connectImage != null || this.isConnecting())
{
return true;
}
return mxCellMarker.prototype.intersects.apply(marker, arguments);
});
return marker;
};
/**
* Function: start
*
* Starts a new connection for the given state and coordinates.
*/
mxConnectionHandler.prototype.start = function(state, x, y, edgeState)
{
this.previous = state;
this.first = new mxPoint(x, y);
this.edgeState = (edgeState != null) ? edgeState : this.createEdgeState(null);
// Marks the source state
this.marker.currentColor = this.marker.validColor;
this.marker.markedState = state;
this.marker.mark();
this.fireEvent(new mxEventObject(mxEvent.START, 'state', this.previous));
};
/**
* Function: isConnecting
*
* Returns true if the source terminal has been clicked and a new
* connection is currently being previewed.
*/
mxConnectionHandler.prototype.isConnecting = function()
{
return this.first != null && this.shape != null;
};
/**
* Function: isValidSource
*
* Returns <mxGraph.isValidSource> for the given source terminal.
*
* Parameters:
*
* cell - <mxCell> that represents the source terminal.
* me - <mxMouseEvent> that is associated with this call.
*/
mxConnectionHandler.prototype.isValidSource = function(cell, me)
{
return this.graph.isValidSource(cell);
};
/**
* Function: isValidTarget
*
* Returns true. The call to <mxGraph.isValidTarget> is implicit by calling
* <mxGraph.getEdgeValidationError> in <validateConnection>. This is an
* additional hook for disabling certain targets in this specific handler.
*
* Parameters:
*
* cell - <mxCell> that represents the target terminal.
*/
mxConnectionHandler.prototype.isValidTarget = function(cell)
{
return true;
};
/**
* Function: validateConnection
*
* Returns the error message or an empty string if the connection for the
* given source target pair is not valid. Otherwise it returns null. This
* implementation uses <mxGraph.getEdgeValidationError>.
*
* Parameters:
*
* source - <mxCell> that represents the source terminal.
* target - <mxCell> that represents the target terminal.
*/
mxConnectionHandler.prototype.validateConnection = function(source, target)
{
if (!this.isValidTarget(target))
{
return '';
}
return this.graph.getEdgeValidationError(null, source, target);
};
/**
* Function: getConnectImage
*
* Hook to return the <mxImage> used for the connection icon of the given
* <mxCellState>. This implementation returns <connectImage>.
*
* Parameters:
*
* state - <mxCellState> whose connect image should be returned.
*/
mxConnectionHandler.prototype.getConnectImage = function(state)
{
return this.connectImage;
};
/**
* Function: isMoveIconToFrontForState
*
* Returns true if the state has a HTML label in the graph's container, otherwise
* it returns <moveIconFront>.
*
* Parameters:
*
* state - <mxCellState> whose connect icons should be returned.
*/
mxConnectionHandler.prototype.isMoveIconToFrontForState = function(state)
{
if (state.text != null && state.text.node.parentNode == this.graph.container)
{
return true;
}
return this.moveIconFront;
};
/**
* Function: createIcons
*
* Creates the array <mxImageShapes> that represent the connect icons for
* the given <mxCellState>.
*
* Parameters:
*
* state - <mxCellState> whose connect icons should be returned.
*/
mxConnectionHandler.prototype.createIcons = function(state)
{
var image = this.getConnectImage(state);
if (image != null && state != null)
{
this.iconState = state;
var icons = [];
// Cannot use HTML for the connect icons because the icon receives all
// mouse move events in IE, must use VML and SVG instead even if the
// connect-icon appears behind the selection border and the selection
// border consumes the events before the icon gets a chance
var bounds = new mxRectangle(0, 0, image.width, image.height);
var icon = new mxImageShape(bounds, image.src, null, null, 0);
icon.preserveImageAspect = false;
if (this.isMoveIconToFrontForState(state))
{
icon.dialect = mxConstants.DIALECT_STRICTHTML;
icon.init(this.graph.container);
}
else
{
icon.dialect = (this.graph.dialect == mxConstants.DIALECT_SVG) ?
mxConstants.DIALECT_SVG : mxConstants.DIALECT_VML;
icon.init(this.graph.getView().getOverlayPane());
// Move the icon back in the overlay pane
if (this.moveIconBack && icon.node.previousSibling != null)
{
icon.node.parentNode.insertBefore(icon.node, icon.node.parentNode.firstChild);
}
}
icon.node.style.cursor = mxConstants.CURSOR_CONNECT;
// Events transparency
var getState = mxUtils.bind(this, function()
{
return (this.currentState != null) ? this.currentState : state;
});
// Updates the local icon before firing the mouse down event.
var mouseDown = mxUtils.bind(this, function(evt)
{
if (!mxEvent.isConsumed(evt))
{
this.icon = icon;
this.graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
new mxMouseEvent(evt, getState()));
}
});
mxEvent.redirectMouseEvents(icon.node, this.graph, getState, mouseDown);
icons.push(icon);
this.redrawIcons(icons, this.iconState);
return icons;
}
return null;
};
/**
* Function: redrawIcons
*
* Redraws the given array of <mxImageShapes>.
*
* Parameters:
*
* icons - Optional array of <mxImageShapes> to be redrawn.
*/
mxConnectionHandler.prototype.redrawIcons = function(icons, state)
{
if (icons != null && icons[0] != null && state != null)
{
var pos = this.getIconPosition(icons[0], state);
icons[0].bounds.x = pos.x;
icons[0].bounds.y = pos.y;
icons[0].redraw();
}
};
/**
* Function: redrawIcons
*
* Redraws the given array of <mxImageShapes>.
*
* Parameters:
*
* icons - Optional array of <mxImageShapes> to be redrawn.
*/
mxConnectionHandler.prototype.getIconPosition = function(icon, state)
{
var scale = this.graph.getView().scale;
var cx = state.getCenterX();
var cy = state.getCenterY();
if (this.graph.isSwimlane(state.cell))
{
var size = this.graph.getStartSize(state.cell);
cx = (size.width != 0) ? state.x + size.width * scale / 2 : cx;
cy = (size.height != 0) ? state.y + size.height * scale / 2 : cy;
var alpha = mxUtils.toRadians(mxUtils.getValue(state.style, mxConstants.STYLE_ROTATION) || 0);
if (alpha != 0)
{
var cos = Math.cos(alpha);
var sin = Math.sin(alpha);
var ct = new mxPoint(state.getCenterX(), state.getCenterY());
var pt = mxUtils.getRotatedPoint(new mxPoint(cx, cy), cos, sin, ct);
cx = pt.x;
cy = pt.y;
}
}
return new mxPoint(cx - icon.bounds.width / 2,
cy - icon.bounds.height / 2);
};
/**
* Function: destroyIcons
*
* Destroys the connect icons and resets the respective state.
*/
mxConnectionHandler.prototype.destroyIcons = function()
{
if (this.icons != null)
{
for (var i = 0; i < this.icons.length; i++)
{
this.icons[i].destroy();
}
this.icons = null;
this.icon = null;
this.selectedIcon = null;
this.iconState = null;
}
};
/**
* Function: isStartEvent
*
* Returns true if the given mouse down event should start this handler. The
* This implementation returns true if the event does not force marquee
* selection, and the currentConstraint and currentFocus of the
* <constraintHandler> are not null, or <previous> and <error> are not null and
* <icons> is null or <icons> and <icon> are not null.
*/
mxConnectionHandler.prototype.isStartEvent = function(me)
{
return ((this.constraintHandler.currentFocus != null && this.constraintHandler.currentConstraint != null) ||
(this.previous != null && this.error == null && (this.icons == null || (this.icons != null &&
this.icon != null))));
};
/**
* Function: mouseDown
*
* Handles the event by initiating a new connection.
*/
mxConnectionHandler.prototype.mouseDown = function(sender, me)
{
this.mouseDownCounter++;
if (this.isEnabled() && this.graph.isEnabled() && !me.isConsumed() &&
!this.isConnecting() && this.isStartEvent(me))
{
if (this.constraintHandler.currentConstraint != null &&
this.constraintHandler.currentFocus != null &&
this.constraintHandler.currentPoint != null)
{
this.sourceConstraint = this.constraintHandler.currentConstraint;
this.previous = this.constraintHandler.currentFocus;
this.first = this.constraintHandler.currentPoint.clone();
}
else
{
// Stores the location of the initial mousedown
this.first = new mxPoint(me.getGraphX(), me.getGraphY());
}
this.edgeState = this.createEdgeState(me);
this.mouseDownCounter = 1;
if (this.waypointsEnabled && this.shape == null)
{
this.waypoints = null;
this.shape = this.createShape();
if (this.edgeState != null)
{
this.shape.apply(this.edgeState);
}
}
// Stores the starting point in the geometry of the preview
if (this.previous == null && this.edgeState != null)
{
var pt = this.graph.getPointForEvent(me.getEvent());
this.edgeState.cell.geometry.setTerminalPoint(pt, true);
}
this.fireEvent(new mxEventObject(mxEvent.START, 'state', this.previous));
me.consume();
}
this.selectedIcon = this.icon;
this.icon = null;
};
/**
* Function: isImmediateConnectSource
*