This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathGraph.js
11388 lines (9715 loc) · 299 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
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-2012, JGraph Ltd
*/
// Workaround for allowing target="_blank" in HTML sanitizer
// see https://code.google.com/p/google-caja/issues/detail?can=2&q=&colspec=ID%20Type%20Status%20Priority%20Owner%20Summary&groupby=&sort=&id=1296
if (typeof html4 !== 'undefined')
{
html4.ATTRIBS["a::target"] = 0;
html4.ATTRIBS["source::src"] = 0;
html4.ATTRIBS["video::src"] = 0;
// Would be nice for tooltips but probably a security risk...
//html4.ATTRIBS["video::autoplay"] = 0;
//html4.ATTRIBS["video::autobuffer"] = 0;
}
// Workaround for handling named HTML entities in mxUtils.parseXml
// LATER: How to configure DOMParser to just ignore all entities?
(function()
{
var entities = [
['nbsp', '160'],
['shy', '173']
];
var parseXml = mxUtils.parseXml;
mxUtils.parseXml = function(text)
{
for (var i = 0; i < entities.length; i++)
{
text = text.replace(new RegExp(
'&' + entities[i][0] + ';', 'g'),
'&#' + entities[i][1] + ';');
}
return parseXml(text);
};
})();
// Shim for missing toISOString in older versions of IE
// See https://stackoverflow.com/questions/12907862
if (!Date.prototype.toISOString)
{
(function()
{
function pad(number)
{
var r = String(number);
if (r.length === 1)
{
r = '0' + r;
}
return r;
};
Date.prototype.toISOString = function()
{
return this.getUTCFullYear()
+ '-' + pad( this.getUTCMonth() + 1 )
+ '-' + pad( this.getUTCDate() )
+ 'T' + pad( this.getUTCHours() )
+ ':' + pad( this.getUTCMinutes() )
+ ':' + pad( this.getUTCSeconds() )
+ '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
+ 'Z';
};
}());
}
// Shim for Date.now()
if (!Date.now)
{
Date.now = function()
{
return new Date().getTime();
};
}
// Changes default colors
/**
* Measurements Units
*/
mxConstants.POINTS = 1;
mxConstants.MILLIMETERS = 2;
mxConstants.INCHES = 3;
/**
* This ratio is with page scale 1
*/
mxConstants.PIXELS_PER_MM = 3.937;
mxConstants.PIXELS_PER_INCH = 100;
mxConstants.SHADOW_OPACITY = 0.25;
mxConstants.SHADOWCOLOR = '#000000';
mxConstants.VML_SHADOWCOLOR = '#d0d0d0';
mxGraph.prototype.pageBreakColor = '#c0c0c0';
mxGraph.prototype.pageScale = 1;
// Letter page format is default in US, Canada and Mexico
(function()
{
try
{
if (navigator != null && navigator.language != null)
{
var lang = navigator.language.toLowerCase();
mxGraph.prototype.pageFormat = (lang === 'en-us' || lang === 'en-ca' || lang === 'es-mx') ?
mxConstants.PAGE_FORMAT_LETTER_PORTRAIT : mxConstants.PAGE_FORMAT_A4_PORTRAIT;
}
}
catch (e)
{
// ignore
}
})();
// Matches label positions of mxGraph 1.x
mxText.prototype.baseSpacingTop = 5;
mxText.prototype.baseSpacingBottom = 1;
// Keeps edges between relative child cells inside parent
mxGraphModel.prototype.ignoreRelativeEdgeParent = false;
// Defines grid properties
mxGraphView.prototype.gridImage = (mxClient.IS_SVG) ? 'data:image/gif;base64,R0lGODlhCgAKAJEAAAAAAP///8zMzP///yH5BAEAAAMALAAAAAAKAAoAAAIJ1I6py+0Po2wFADs=' :
IMAGE_PATH + '/grid.gif';
mxGraphView.prototype.gridSteps = 4;
mxGraphView.prototype.minGridSize = 4;
// UrlParams is null in embed mode
mxGraphView.prototype.defaultGridColor = '#d0d0d0';
mxGraphView.prototype.gridColor = mxGraphView.prototype.defaultGridColor;
//Units
mxGraphView.prototype.unit = mxConstants.POINTS;
mxGraphView.prototype.setUnit = function(unit)
{
if (this.unit != unit)
{
this.unit = unit;
this.fireEvent(new mxEventObject('unitChanged', 'unit', unit));
}
};
// Alternative text for unsupported foreignObjects
mxSvgCanvas2D.prototype.foAltText = '[Not supported by viewer]';
// Hook for custom constraints
mxShape.prototype.getConstraints = function(style, w, h)
{
return null;
};
/**
* Constructs a new graph instance. Note that the constructor does not take a
* container because the graph instance is needed for creating the UI, which
* in turn will create the container for the graph. Hence, the container is
* assigned later in EditorUi.
*/
/**
* Defines graph class.
*/
Graph = function(container, model, renderHint, stylesheet, themes, standalone)
{
mxGraph.call(this, container, model, renderHint, stylesheet);
this.themes = themes || this.defaultThemes;
this.currentEdgeStyle = mxUtils.clone(this.defaultEdgeStyle);
this.currentVertexStyle = mxUtils.clone(this.defaultVertexStyle);
this.standalone = (standalone != null) ? standalone : false;
// Sets the base domain URL and domain path URL for relative links.
var b = this.baseUrl;
var p = b.indexOf('//');
this.domainUrl = '';
this.domainPathUrl = '';
if (p > 0)
{
var d = b.indexOf('/', p + 2);
if (d > 0)
{
this.domainUrl = b.substring(0, d);
}
d = b.lastIndexOf('/');
if (d > 0)
{
this.domainPathUrl = b.substring(0, d + 1);
}
}
// Adds support for HTML labels via style. Note: Currently, only the Java
// backend supports HTML labels but CSS support is limited to the following:
// http://docs.oracle.com/javase/6/docs/api/index.html?javax/swing/text/html/CSS.html
// TODO: Wrap should not affect isHtmlLabel output (should be handled later)
this.isHtmlLabel = function(cell)
{
var style = this.getCurrentCellStyle(cell);
return (style != null) ? (style['html'] == '1' || style[mxConstants.STYLE_WHITE_SPACE] == 'wrap') : false;
};
// Implements a listener for hover and click handling on edges
if (this.edgeMode)
{
var start = {
point: null,
event: null,
state: null,
handle: null,
selected: false
};
// Uses this event to process mouseDown to check the selection state before it is changed
this.addListener(mxEvent.FIRE_MOUSE_EVENT, mxUtils.bind(this, function(sender, evt)
{
if (evt.getProperty('eventName') == 'mouseDown' && this.isEnabled())
{
var me = evt.getProperty('event');
var state = me.getState();
if (!mxEvent.isAltDown(me.getEvent()) && state != null)
{
// Checks if state was removed in call to stopEditing above
if (this.model.isEdge(state.cell))
{
start.point = new mxPoint(me.getGraphX(), me.getGraphY());
start.selected = this.isCellSelected(state.cell);
start.state = state;
start.event = me;
if (state.text != null && state.text.boundingBox != null &&
mxUtils.contains(state.text.boundingBox, me.getGraphX(), me.getGraphY()))
{
start.handle = mxEvent.LABEL_HANDLE;
}
else
{
var handler = this.selectionCellsHandler.getHandler(state.cell);
if (handler != null && handler.bends != null && handler.bends.length > 0)
{
start.handle = handler.getHandleForEvent(me);
}
}
}
else if (!this.panningHandler.isActive())
{
var handler = this.selectionCellsHandler.getHandler(state.cell);
// Cell handles have precedence over row and col resize
if (handler == null || handler.getHandleForEvent(me) == null)
{
var box = new mxRectangle(me.getGraphX(), me.getGraphY());
box.grow(mxEvent.isTouchEvent(me.getEvent()) ?
mxShape.prototype.svgStrokeTolerance - 1 :
mxShape.prototype.svgStrokeTolerance / 2);
if (this.isTableCell(state.cell))
{
var row = this.model.getParent(state.cell);
var table = this.model.getParent(row);
if ((mxUtils.intersects(box, new mxRectangle(state.x, state.y - 1, state.width, 1)) &&
this.model.getChildAt(table, 0) != row) || mxUtils.intersects(box, new mxRectangle(
state.x, state.y + state.height - 1, state.width, 1)) ||
(mxUtils.intersects(box, new mxRectangle(state.x - 1, state.y, 1, state.height)) &&
this.model.getChildAt(row, 0) != state.cell) || mxUtils.intersects(box, new mxRectangle(
state.x + state.width - 1, state.y, 1, state.height)))
{
var wasSelected = this.selectionCellsHandler.isHandled(table);
this.selectCellForEvent(table, me.getEvent());
handler = this.selectionCellsHandler.getHandler(table);
if (handler != null)
{
var handle = handler.getHandleForEvent(me);
if (handle != null)
{
handler.start(me.getGraphX(), me.getGraphY(), handle);
handler.blockDelayedSelection = !wasSelected;
me.consume();
}
}
}
}
// Hover for swimlane start sizes inside tables
var current = state;
while (!me.isConsumed() && current != null && (this.isTableCell(current.cell) ||
this.isTableRow(current.cell) || this.isTable(current.cell)))
{
if (this.isSwimlane(current.cell))
{
var offset = this.getActualStartSize(current.cell);
if (((offset.x > 0 || offset.width > 0) && mxUtils.intersects(box, new mxRectangle(
current.x + offset.x - offset.width - 1 + ((offset.x == 0) ? current.width : 0),
current.y, 1, current.height))) || ((offset.y > 0 || offset.height > 0) &&
mxUtils.intersects(box, new mxRectangle(current.x, current.y + offset.y -
offset.height - 1 + ((offset.y == 0) ? current.height : 0), current.width, 1))))
{
this.selectCellForEvent(current.cell, me.getEvent());
handler = this.selectionCellsHandler.getHandler(current.cell);
if (handler != null)
{
// Swimlane start size handle is last custom handle
var handle = mxEvent.CUSTOM_HANDLE - handler.customHandles.length + 1;
handler.start(me.getGraphX(), me.getGraphY(), handle);
me.consume();
}
}
}
current = this.view.getState(this.model.getParent(current.cell));
}
}
}
}
}
}));
var mouseDown = null;
this.addMouseListener(
{
mouseDown: function(sender, me) {},
mouseMove: mxUtils.bind(this, function(sender, me)
{
// Checks if any other handler is active
var handlerMap = this.selectionCellsHandler.handlers.map;
for (var key in handlerMap)
{
if (handlerMap[key].index != null)
{
return;
}
}
if (this.isEnabled() && !this.panningHandler.isActive() && !mxEvent.isAltDown(me.getEvent()))
{
var tol = this.tolerance;
if (start.point != null && start.state != null && start.event != null)
{
var state = start.state;
if (Math.abs(start.point.x - me.getGraphX()) > tol ||
Math.abs(start.point.y - me.getGraphY()) > tol)
{
// Lazy selection for edges inside groups
if (!this.isCellSelected(state.cell))
{
this.selectCellForEvent(state.cell, me.getEvent());
}
var handler = this.selectionCellsHandler.getHandler(state.cell);
if (handler != null && handler.bends != null && handler.bends.length > 0)
{
var handle = handler.getHandleForEvent(start.event);
var edgeStyle = this.view.getEdgeStyle(state);
var entity = edgeStyle == mxEdgeStyle.EntityRelation;
// Handles special case where label was clicked on unselected edge in which
// case the label will be moved regardless of the handle that is returned
if (!start.selected && start.handle == mxEvent.LABEL_HANDLE)
{
handle = start.handle;
}
if (!entity || handle == 0 || handle == handler.bends.length - 1 || handle == mxEvent.LABEL_HANDLE)
{
// Source or target handle or connected for direct handle access or orthogonal line
// with just two points where the central handle is moved regardless of mouse position
if (handle == mxEvent.LABEL_HANDLE || handle == 0 || state.visibleSourceState != null ||
handle == handler.bends.length - 1 || state.visibleTargetState != null)
{
if (!entity && handle != mxEvent.LABEL_HANDLE)
{
var pts = state.absolutePoints;
// Default case where handles are at corner points handles
// drag of corner as drag of existing point
if (pts != null && ((edgeStyle == null && handle == null) ||
edgeStyle == mxEdgeStyle.OrthConnector))
{
// Does not use handles if they were not initially visible
handle = start.handle;
if (handle == null)
{
var box = new mxRectangle(start.point.x, start.point.y);
box.grow(mxEdgeHandler.prototype.handleImage.width / 2);
if (mxUtils.contains(box, pts[0].x, pts[0].y))
{
// Moves source terminal handle
handle = 0;
}
else if (mxUtils.contains(box, pts[pts.length - 1].x, pts[pts.length - 1].y))
{
// Moves target terminal handle
handle = handler.bends.length - 1;
}
else
{
// Checks if edge has no bends
var nobends = edgeStyle != null && (pts.length == 2 || (pts.length == 3 &&
((Math.round(pts[0].x - pts[1].x) == 0 && Math.round(pts[1].x - pts[2].x) == 0) ||
(Math.round(pts[0].y - pts[1].y) == 0 && Math.round(pts[1].y - pts[2].y) == 0))));
if (nobends)
{
// Moves central handle for straight orthogonal edges
handle = 2;
}
else
{
// Finds and moves vertical or horizontal segment
handle = mxUtils.findNearestSegment(state, start.point.x, start.point.y);
// Converts segment to virtual handle index
if (edgeStyle == null)
{
handle = mxEvent.VIRTUAL_HANDLE - handle;
}
// Maps segment to handle
else
{
handle += 1;
}
}
}
}
}
// Creates a new waypoint and starts moving it
if (handle == null)
{
handle = mxEvent.VIRTUAL_HANDLE;
}
}
handler.start(me.getGraphX(), me.getGraphX(), handle);
start.state = null;
start.event = null;
start.point = null;
start.handle = null;
start.selected = false;
me.consume();
// Removes preview rectangle in graph handler
this.graphHandler.reset();
}
}
else if (entity && (state.visibleSourceState != null || state.visibleTargetState != null))
{
// Disables moves on entity to make it consistent
this.graphHandler.reset();
me.consume();
}
}
}
}
else
{
// Updates cursor for unselected edges under the mouse
var state = me.getState();
if (state != null)
{
var cursor = null;
// Checks if state was removed in call to stopEditing above
if (this.model.isEdge(state.cell))
{
var box = new mxRectangle(me.getGraphX(), me.getGraphY());
box.grow(mxEdgeHandler.prototype.handleImage.width / 2);
var pts = state.absolutePoints;
if (pts != null)
{
if (state.text != null && state.text.boundingBox != null &&
mxUtils.contains(state.text.boundingBox, me.getGraphX(), me.getGraphY()))
{
cursor = 'move';
}
else if (mxUtils.contains(box, pts[0].x, pts[0].y) ||
mxUtils.contains(box, pts[pts.length - 1].x, pts[pts.length - 1].y))
{
cursor = 'pointer';
}
else if (state.visibleSourceState != null || state.visibleTargetState != null)
{
// Moving is not allowed for entity relation but still indicate hover state
var tmp = this.view.getEdgeStyle(state);
cursor = 'crosshair';
if (tmp != mxEdgeStyle.EntityRelation && this.isOrthogonal(state))
{
var idx = mxUtils.findNearestSegment(state, me.getGraphX(), me.getGraphY());
if (idx < pts.length - 1 && idx >= 0)
{
cursor = (Math.round(pts[idx].x - pts[idx + 1].x) == 0) ?
'col-resize' : 'row-resize';
}
}
}
}
}
else
{
var box = new mxRectangle(me.getGraphX(), me.getGraphY());
box.grow(mxShape.prototype.svgStrokeTolerance / 2);
if (this.isTableCell(state.cell))
{
var row = this.model.getParent(state.cell);
var table = this.model.getParent(row);
if ((mxUtils.intersects(box, new mxRectangle(state.x - 1, state.y, 1, state.height)) &&
this.model.getChildAt(row, 0) != state.cell) || mxUtils.intersects(box,
new mxRectangle(state.x + state.width - 1, state.y, 1, state.height)))
{
cursor ='col-resize';
}
else if ((mxUtils.intersects(box, new mxRectangle(state.x, state.y - 1, state.width, 1)) &&
this.model.getChildAt(table, 0) != row) || mxUtils.intersects(box,
new mxRectangle(state.x, state.y + state.height - 1, state.width, 1)))
{
cursor ='row-resize';
}
}
// Hover for swimlane start sizes inside tables
var current = state;
while (cursor == null && current != null && (this.isTableCell(current.cell) ||
this.isTableRow(current.cell) || this.isTable(current.cell)))
{
if (this.isSwimlane(current.cell))
{
var offset = this.getActualStartSize(current.cell);
if ((offset.x > 0 || offset.width > 0) && mxUtils.intersects(box, new mxRectangle(
current.x + offset.x - offset.width - 1 + ((offset.x == 0) ? current.width : 0),
current.y, 1, current.height)))
{
cursor ='col-resize';
}
else if ((offset.y > 0 || offset.height > 0) && mxUtils.intersects(box, new mxRectangle(
current.x, current.y + offset.y - offset.height - 1 + ((offset.y == 0) ? current.height : 0),
current.width, 1)))
{
cursor ='row-resize';
}
}
current = this.view.getState(this.model.getParent(current.cell));
}
}
if (cursor != null)
{
state.setCursor(cursor);
}
}
}
}
}),
mouseUp: mxUtils.bind(this, function(sender, me)
{
start.state = null;
start.event = null;
start.point = null;
start.handle = null;
})
});
}
// HTML entities are displayed as plain text in wrapped plain text labels
this.cellRenderer.getLabelValue = function(state)
{
var result = mxCellRenderer.prototype.getLabelValue.apply(this, arguments);
if (state.view.graph.isHtmlLabel(state.cell))
{
if (state.style['html'] != 1)
{
result = mxUtils.htmlEntities(result, false);
}
else
{
result = state.view.graph.sanitizeHtml(result);
}
}
return result;
};
// All code below not available and not needed in embed mode
if (typeof mxVertexHandler !== 'undefined')
{
this.setConnectable(true);
this.setDropEnabled(true);
this.setPanning(true);
this.setTooltips(true);
this.setAllowLoops(true);
this.allowAutoPanning = true;
this.resetEdgesOnConnect = false;
this.constrainChildren = false;
this.constrainRelativeChildren = true;
// Do not scroll after moving cells
this.graphHandler.scrollOnMove = false;
this.graphHandler.scaleGrid = true;
// Disables cloning of connection sources by default
this.connectionHandler.setCreateTarget(false);
this.connectionHandler.insertBeforeSource = true;
// Disables built-in connection starts
this.connectionHandler.isValidSource = function(cell, me)
{
return false;
};
// Sets the style to be used when an elbow edge is double clicked
this.alternateEdgeStyle = 'vertical';
if (stylesheet == null)
{
this.loadStylesheet();
}
// Adds page centers to the guides for moving cells
var graphHandlerGetGuideStates = this.graphHandler.getGuideStates;
this.graphHandler.getGuideStates = function()
{
var result = graphHandlerGetGuideStates.apply(this, arguments);
// Create virtual cell state for page centers
if (this.graph.pageVisible)
{
var guides = [];
var pf = this.graph.pageFormat;
var ps = this.graph.pageScale;
var pw = pf.width * ps;
var ph = pf.height * ps;
var t = this.graph.view.translate;
var s = this.graph.view.scale;
var layout = this.graph.getPageLayout();
for (var i = 0; i < layout.width; i++)
{
guides.push(new mxRectangle(((layout.x + i) * pw + t.x) * s,
(layout.y * ph + t.y) * s, pw * s, ph * s));
}
for (var j = 1; j < layout.height; j++)
{
guides.push(new mxRectangle((layout.x * pw + t.x) * s,
((layout.y + j) * ph + t.y) * s, pw * s, ph * s));
}
// Page center guides have precedence over normal guides
result = guides.concat(result);
}
return result;
};
// Overrides zIndex for dragElement
mxDragSource.prototype.dragElementZIndex = mxPopupMenu.prototype.zIndex;
// Overrides color for virtual guides for page centers
mxGuide.prototype.getGuideColor = function(state, horizontal)
{
return (state.cell == null) ? '#ffa500' /* orange */ : mxConstants.GUIDE_COLOR;
};
// Changes color of move preview for black backgrounds
this.graphHandler.createPreviewShape = function(bounds)
{
this.previewColor = (this.graph.background == '#000000') ? '#ffffff' : mxGraphHandler.prototype.previewColor;
return mxGraphHandler.prototype.createPreviewShape.apply(this, arguments);
};
// Handles parts of cells by checking if part=1 is in the style and returning the parent
// if the parent is not already in the list of cells. container style is used to disable
// step into swimlanes and dropTarget style is used to disable acting as a drop target.
// LATER: Handle recursive parts
var graphHandlerGetCells = this.graphHandler.getCells;
this.graphHandler.getCells = function(initialCell)
{
var cells = graphHandlerGetCells.apply(this, arguments);
var lookup = new mxDictionary();
var newCells = [];
for (var i = 0; i < cells.length; i++)
{
// Moves composite parent with exception of selected table rows
var cell = (!this.graph.isTableRow(cells[i]) || !this.graph.isCellSelected(cells[i])) ?
this.graph.getCompositeParent(cells[i]) : cells[i];
if (cell != null && !lookup.get(cell))
{
lookup.put(cell, true);
newCells.push(cell);
}
}
return newCells;
};
// Handles parts of cells for drag and drop
var graphHandlerStart = this.graphHandler.start;
this.graphHandler.start = function(cell, x, y, cells)
{
cell = this.graph.getCompositeParent(cell);
graphHandlerStart.apply(this, arguments);
};
// Handles parts of cells when cloning the source for new connections
this.connectionHandler.createTargetVertex = function(evt, source)
{
source = this.graph.getCompositeParent(source);
return mxConnectionHandler.prototype.createTargetVertex.apply(this, arguments);
};
var rubberband = new mxRubberband(this);
this.getRubberband = function()
{
return rubberband;
};
// Timer-based activation of outline connect in connection handler
var startTime = new Date().getTime();
var timeOnTarget = 0;
var connectionHandlerMouseMove = this.connectionHandler.mouseMove;
this.connectionHandler.mouseMove = function()
{
var prev = this.currentState;
connectionHandlerMouseMove.apply(this, arguments);
if (prev != this.currentState)
{
startTime = new Date().getTime();
timeOnTarget = 0;
}
else
{
timeOnTarget = new Date().getTime() - startTime;
}
};
// Activates outline connect after 1500ms with touch event or if alt is pressed inside the shape
// outlineConnect=0 is a custom style that means do not connect to strokes inside the shape,
// or in other words, connect to the shape's perimeter if the highlight is under the mouse
// (the name is because the highlight, including all strokes, is called outline in the code)
var connectionHandleIsOutlineConnectEvent = this.connectionHandler.isOutlineConnectEvent;
this.connectionHandler.isOutlineConnectEvent = function(me)
{
return (this.currentState != null && me.getState() == this.currentState && timeOnTarget > 2000) ||
((this.currentState == null || mxUtils.getValue(this.currentState.style, 'outlineConnect', '1') != '0') &&
connectionHandleIsOutlineConnectEvent.apply(this, arguments));
};
// Adds shift+click to toggle selection state
var isToggleEvent = this.isToggleEvent;
this.isToggleEvent = function(evt)
{
return isToggleEvent.apply(this, arguments) || (!mxClient.IS_CHROMEOS && mxEvent.isShiftDown(evt));
};
// Workaround for Firefox where first mouse down is received
// after tap and hold if scrollbars are visible, which means
// start rubberband immediately if no cell is under mouse.
var isForceRubberBandEvent = rubberband.isForceRubberbandEvent;
rubberband.isForceRubberbandEvent = function(me)
{
return (isForceRubberBandEvent.apply(this, arguments) && !mxEvent.isShiftDown(me.getEvent()) &&
!mxEvent.isControlDown(me.getEvent())) || (mxClient.IS_CHROMEOS && mxEvent.isShiftDown(me.getEvent())) ||
(mxUtils.hasScrollbars(this.graph.container) && mxClient.IS_FF &&
mxClient.IS_WIN && me.getState() == null && mxEvent.isTouchEvent(me.getEvent()));
};
// Shows hand cursor while panning
var prevCursor = null;
this.panningHandler.addListener(mxEvent.PAN_START, mxUtils.bind(this, function()
{
if (this.isEnabled())
{
prevCursor = this.container.style.cursor;
this.container.style.cursor = 'move';
}
}));
this.panningHandler.addListener(mxEvent.PAN_END, mxUtils.bind(this, function()
{
if (this.isEnabled())
{
this.container.style.cursor = prevCursor;
}
}));
this.popupMenuHandler.autoExpand = true;
this.popupMenuHandler.isSelectOnPopup = function(me)
{
return mxEvent.isMouseEvent(me.getEvent());
};
// Handles links if graph is read-only or cell is locked
var click = this.click;
this.click = function(me)
{
var locked = me.state == null && me.sourceState != null &&
this.isCellLocked(me.sourceState.cell);
if ((!this.isEnabled() || locked) && !me.isConsumed())
{
var cell = (locked) ? me.sourceState.cell : me.getCell();
if (cell != null)
{
var link = this.getClickableLinkForCell(cell);
if (link != null)
{
if (this.isCustomLink(link))
{
this.customLinkClicked(link);
}
else
{
this.openLink(link);
}
}
}
if (this.isEnabled() && locked)
{
this.clearSelection();
}
}
else
{
return click.apply(this, arguments);
}
};
// Redirects tooltips for locked cells
this.tooltipHandler.getStateForEvent = function(me)
{
return me.sourceState;
};
// Redirects cursor for locked cells
var getCursorForMouseEvent = this.getCursorForMouseEvent;
this.getCursorForMouseEvent = function(me)
{
var locked = me.state == null && me.sourceState != null && this.isCellLocked(me.sourceState.cell);
return this.getCursorForCell((locked) ? me.sourceState.cell : me.getCell());
};
// Shows pointer cursor for clickable cells with links
// ie. if the graph is disabled and cells cannot be selected
var getCursorForCell = this.getCursorForCell;
this.getCursorForCell = function(cell)
{
if (!this.isEnabled() || this.isCellLocked(cell))
{
var link = this.getClickableLinkForCell(cell);
if (link != null)
{
return 'pointer';
}
else if (this.isCellLocked(cell))
{
return 'default';
}
}
return getCursorForCell.apply(this, arguments);
};
// Changes rubberband selection to be recursive
this.selectRegion = function(rect, evt)
{
var cells = this.getAllCells(rect.x, rect.y, rect.width, rect.height);
this.selectCellsForEvent(cells, evt);
return cells;
};
// Recursive implementation for rubberband selection
this.getAllCells = function(x, y, width, height, parent, result)
{
result = (result != null) ? result : [];
if (width > 0 || height > 0)
{
var model = this.getModel();
var right = x + width;
var bottom = y + height;
if (parent == null)
{
parent = this.getCurrentRoot();
if (parent == null)
{
parent = model.getRoot();
}
}
if (parent != null)
{
var childCount = model.getChildCount(parent);
for (var i = 0; i < childCount; i++)
{
var cell = model.getChildAt(parent, i);
var state = this.view.getState(cell);
if (state != null && this.isCellVisible(cell) && mxUtils.getValue(state.style, 'locked', '0') != '1')
{
var deg = mxUtils.getValue(state.style, mxConstants.STYLE_ROTATION) || 0;
var box = state;
if (deg != 0)
{
box = mxUtils.getBoundingBox(box, deg);
}
if ((model.isEdge(cell) || model.isVertex(cell)) &&
box.x >= x && box.y + box.height <= bottom &&
box.y >= y && box.x + box.width <= right)
{
result.push(cell);
}
this.getAllCells(x, y, width, height, cell, result);
}
}
}
}
return result;
};
// Never removes cells from parents that are being moved
var graphHandlerShouldRemoveCellsFromParent = this.graphHandler.shouldRemoveCellsFromParent;
this.graphHandler.shouldRemoveCellsFromParent = function(parent, cells, evt)
{
if (this.graph.isCellSelected(parent))
{
return false;
}
return graphHandlerShouldRemoveCellsFromParent.apply(this, arguments);
};
// Unlocks all cells
this.isCellLocked = function(cell)
{
var pState = this.view.getState(cell);
while (pState != null)
{
if (mxUtils.getValue(pState.style, 'locked', '0') == '1')
{
return true;