This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
forked from iVis-at-Bilkent/cytoscape.js-expand-collapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcytoscape-expand-collapse.js
1822 lines (1562 loc) · 154 KB
/
cytoscape-expand-collapse.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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.cytoscapeExpandCollapse = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
var boundingBoxUtilities = {
equalBoundingBoxes: function(bb1, bb2){
return bb1.x1 == bb2.x1 && bb1.x2 == bb2.x2 && bb1.y1 == bb2.y1 && bb1.y2 == bb2.y2;
},
getUnion: function(bb1, bb2){
var union = {
x1: Math.min(bb1.x1, bb2.x1),
x2: Math.max(bb1.x2, bb2.x2),
y1: Math.min(bb1.y1, bb2.y1),
y2: Math.max(bb1.y2, bb2.y2),
};
union.w = union.x2 - union.x1;
union.h = union.y2 - union.y1;
return union;
}
};
module.exports = boundingBoxUtilities;
},{}],2:[function(_dereq_,module,exports){
var debounce = _dereq_('./debounce');
module.exports = function (params, cy, api) {
var elementUtilities;
var fn = params;
var nodeWithRenderedCue, preventDrawing = false;
const getData = function(){
var scratch = cy.scratch('_cyExpandCollapse');
return scratch && scratch.cueUtilities;
};
const setData = function( data ){
var scratch = cy.scratch('_cyExpandCollapse');
if (scratch == null) {
scratch = {};
}
scratch.cueUtilities = data;
cy.scratch('_cyExpandCollapse', scratch);
};
var functions = {
init: function () {
var self = this;
var $canvas = document.createElement('canvas');
var $container = cy.container();
var ctx = $canvas.getContext( '2d' );
$container.append($canvas);
elementUtilities = _dereq_('./elementUtilities')(cy);
var offset = function(elt) {
var rect = elt.getBoundingClientRect();
return {
top: rect.top + document.documentElement.scrollTop,
left: rect.left + document.documentElement.scrollLeft
}
}
var _sizeCanvas = debounce(function () {
$canvas.height = cy.height();
$canvas.width = cy.width();
$canvas.style.position = 'absolute';
$canvas.style.top = 0;
$canvas.style.left = 0;
$canvas.style.zIndex = options().zIndex;
setTimeout(function () {
var canvasBb = offset($canvas);
var containerBb = offset($container);
$canvas.style.top = -(canvasBb.top - containerBb.top);
$canvas.style.left = -(canvasBb.left - containerBb.left);
// refresh the cues on canvas resize
if(cy){
clearDraws(true);
}
}, 0);
}, 250);
function sizeCanvas() {
_sizeCanvas();
}
sizeCanvas();
var data = {};
// if there are events field in data unbind them here
// to prevent binding the same event multiple times
// if (!data.hasEventFields) {
// functions['unbind'].apply( $container );
// }
window.addEventListener('resize', data.eWindowResize = function () {
sizeCanvas();
});
function options() {
return cy.scratch('_cyExpandCollapse').options;
}
function clearDraws() {
var w = cy.width();
var h = cy.height();
ctx.clearRect(0, 0, w, h);
}
function drawExpandCollapseCue(node) {
var children = node.children();
var collapsedChildren = node._private.data.collapsedChildren;
var hasChildren = children != null && children.length > 0;
// If this is a simple node with no collapsed children return directly
if (!hasChildren && collapsedChildren == null) {
return;
}
var isCollapsed = node.hasClass('cy-expand-collapse-collapsed-node');
//Draw expand-collapse rectangles
var rectSize = options().expandCollapseCueSize;
var lineSize = options().expandCollapseCueLineSize;
var diff;
var expandcollapseStartX;
var expandcollapseStartY;
var expandcollapseEndX;
var expandcollapseEndY;
var expandcollapseRectSize;
var expandcollapseCenterX;
var expandcollapseCenterY;
var cueCenter;
if (options().expandCollapseCuePosition === 'top-left') {
var offset = 1;
var size = cy.zoom() < 1 ? rectSize / (2*cy.zoom()) : rectSize / 2;
var x = node.position('x') - node.width() / 2 - parseFloat(node.css('padding-left'))
+ parseFloat(node.css('border-width')) + size + offset;
var y = node.position('y') - node.height() / 2 - parseFloat(node.css('padding-top'))
+ parseFloat(node.css('border-width')) + size + offset;
cueCenter = {
x : x,
y : y
};
} else {
var option = options().expandCollapseCuePosition;
cueCenter = typeof option === 'function' ? option.call(this, node) : option;
}
var expandcollapseCenter = elementUtilities.convertToRenderedPosition(cueCenter);
// convert to rendered sizes
rectSize = Math.max(rectSize, rectSize * cy.zoom());
lineSize = Math.max(lineSize, lineSize * cy.zoom());
diff = (rectSize - lineSize) / 2;
expandcollapseCenterX = expandcollapseCenter.x;
expandcollapseCenterY = expandcollapseCenter.y;
expandcollapseStartX = expandcollapseCenterX - rectSize / 2;
expandcollapseStartY = expandcollapseCenterY - rectSize / 2;
expandcollapseEndX = expandcollapseStartX + rectSize;
expandcollapseEndY = expandcollapseStartY + rectSize;
expandcollapseRectSize = rectSize;
// Draw expand/collapse cue if specified use an image else render it in the default way
if (isCollapsed && options().expandCueImage) {
var img=new Image();
img.src = options().expandCueImage;
ctx.drawImage(img, expandcollapseStartX, expandcollapseStartY, rectSize, rectSize);
}
else if (!isCollapsed && options().collapseCueImage) {
var img=new Image();
img.src = options().collapseCueImage;
ctx.drawImage(img, expandcollapseStartX, expandcollapseStartY, rectSize, rectSize);
}
else {
var oldFillStyle = ctx.fillStyle;
var oldWidth = ctx.lineWidth;
var oldStrokeStyle = ctx.strokeStyle;
ctx.fillStyle = "black";
ctx.strokeStyle = "black";
ctx.ellipse(expandcollapseCenterX, expandcollapseCenterY, rectSize / 2, rectSize / 2, 0, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.lineWidth = Math.max(2.6, 2.6 * cy.zoom());
ctx.moveTo(expandcollapseStartX + diff, expandcollapseStartY + rectSize / 2);
ctx.lineTo(expandcollapseStartX + lineSize + diff, expandcollapseStartY + rectSize / 2);
if (isCollapsed) {
ctx.moveTo(expandcollapseStartX + rectSize / 2, expandcollapseStartY + diff);
ctx.lineTo(expandcollapseStartX + rectSize / 2, expandcollapseStartY + lineSize + diff);
}
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = oldStrokeStyle;
ctx.fillStyle = oldFillStyle;
ctx.lineWidth = oldWidth;
}
node._private.data.expandcollapseRenderedStartX = expandcollapseStartX;
node._private.data.expandcollapseRenderedStartY = expandcollapseStartY;
node._private.data.expandcollapseRenderedCueSize = expandcollapseRectSize;
nodeWithRenderedCue = node;
}
{
cy.on('expandcollapse.clearvisualcue', function() {
if ( nodeWithRenderedCue ) {
clearDraws();
}
});
cy.bind('zoom pan', data.eZoom = function () {
if ( nodeWithRenderedCue ) {
clearDraws();
}
});
// check if mouse is inside given node
var isInsideCompound = function(node, e){
if (node){
var currMousePos = e.position || e.cyPosition;
var topLeft = {
x: (node.position("x") - node.width() / 2 - parseFloat(node.css('padding-left'))),
y: (node.position("y") - node.height() / 2 - parseFloat(node.css('padding-top')))};
var bottomRight = {
x: (node.position("x") + node.width() / 2 + parseFloat(node.css('padding-right'))),
y: (node.position("y") + node.height() / 2+ parseFloat(node.css('padding-bottom')))};
if (currMousePos.x >= topLeft.x && currMousePos.y >= topLeft.y &&
currMousePos.x <= bottomRight.x && currMousePos.y <= bottomRight.y){
return true;
}
}
return false;
};
cy.on('mousemove', 'node', data.eMouseMove= function(e){
if(!isInsideCompound(nodeWithRenderedCue, e)){
clearDraws()
}
else if(nodeWithRenderedCue && !preventDrawing){
drawExpandCollapseCue(nodeWithRenderedCue);
}
});
cy.on('mouseover', 'node', data.eMouseOver = function (e) {
var node = this;
// clear draws if any
if (api.isCollapsible(node) || api.isExpandable(node)){
if ( nodeWithRenderedCue && nodeWithRenderedCue.id() != node.id() ) {
clearDraws();
}
drawExpandCollapseCue(node);
}
});
var oldMousePos = null, currMousePos = null;
cy.on('mousedown', data.eMouseDown = function(e){
oldMousePos = e.renderedPosition || e.cyRenderedPosition
});
cy.on('mouseup', data.eMouseUp = function(e){
currMousePos = e.renderedPosition || e.cyRenderedPosition
});
cy.on('grab', 'node', data.eGrab = function (e) {
preventDrawing = true;
});
cy.on('free', 'node', data.eFree = function (e) {
preventDrawing = false;
});
cy.on('position', 'node', data.ePosition = function () {
if (nodeWithRenderedCue)
clearDraws();
});
cy.on('remove', 'node', data.eRemove = function () {
clearDraws();
nodeWithRenderedCue = null;
});
var ur;
cy.on('select', 'node', data.eSelect = function(){
if (this.length > cy.nodes(":selected").length)
this.unselect();
});
cy.on('tap', data.eTap = function (event) {
var node = nodeWithRenderedCue;
var opts = options();
if (node){
var expandcollapseRenderedStartX = node._private.data.expandcollapseRenderedStartX;
var expandcollapseRenderedStartY = node._private.data.expandcollapseRenderedStartY;
var expandcollapseRenderedRectSize = node._private.data.expandcollapseRenderedCueSize;
var expandcollapseRenderedEndX = expandcollapseRenderedStartX + expandcollapseRenderedRectSize;
var expandcollapseRenderedEndY = expandcollapseRenderedStartY + expandcollapseRenderedRectSize;
var cyRenderedPos = event.renderedPosition || event.cyRenderedPosition;
var cyRenderedPosX = cyRenderedPos.x;
var cyRenderedPosY = cyRenderedPos.y;
var factor = (opts.expandCollapseCueSensitivity - 1) / 2;
if ( (Math.abs(oldMousePos.x - currMousePos.x) < 5 && Math.abs(oldMousePos.y - currMousePos.y) < 5)
&& cyRenderedPosX >= expandcollapseRenderedStartX - expandcollapseRenderedRectSize * factor
&& cyRenderedPosX <= expandcollapseRenderedEndX + expandcollapseRenderedRectSize * factor
&& cyRenderedPosY >= expandcollapseRenderedStartY - expandcollapseRenderedRectSize * factor
&& cyRenderedPosY <= expandcollapseRenderedEndY + expandcollapseRenderedRectSize * factor) {
if(opts.undoable && !ur)
ur = cy.undoRedo({
defaultActions: false
});
if(api.isCollapsible(node))
if (opts.undoable){
ur.do("collapse", {
nodes: node,
options: opts
});
}
else
api.collapse(node, opts);
else if(api.isExpandable(node))
if (opts.undoable)
ur.do("expand", {
nodes: node,
options: opts
});
else
api.expand(node, opts);
}
}
});
}
// write options to data
data.hasEventFields = true;
setData( data );
},
unbind: function () {
// var $container = this;
var data = getData();
if (!data.hasEventFields) {
console.log( 'events to unbind does not exist' );
return;
}
cy.trigger('expandcollapse.clearvisualcue');
cy.off('mouseover', 'node', data.eMouseOver)
.off('mousemove', 'node', data.eMouseMove)
.off('mousedown', 'node', data.eMouseDown)
.off('mouseup', 'node', data.eMouseUp)
.off('free', 'node', data.eFree)
.off('grab', 'node', data.eGrab)
.off('position', 'node', data.ePosition)
.off('remove', 'node', data.eRemove)
.off('tap', 'node', data.eTap)
.off('add', 'node', data.eAdd)
.off('select', 'node', data.eSelect)
.off('free', 'node', data.eFree)
.off('zoom pan', data.eZoom);
window.removeEventListener('resize', data.eWindowResize);
},
rebind: function () {
var data = getData();
if (!data.hasEventFields) {
console.log( 'events to rebind does not exist' );
return;
}
cy.on('mouseover', 'node', data.eMouseOver)
.on('mousemove', 'node', data.eMouseMove)
.on('mousedown', 'node', data.eMouseDown)
.on('mouseup', 'node', data.eMouseUp)
.on('free', 'node', data.eFree)
.on('grab', 'node', data.eGrab)
.on('position', 'node', data.ePosition)
.on('remove', 'node', data.eRemove)
.on('tap', 'node', data.eTap)
.on('add', 'node', data.eAdd)
.on('select', 'node', data.eSelect)
.on('free', 'node', data.eFree)
.on('zoom pan', data.eZoom);
window.addEventListener('resize', data.eWindowResize);
}
};
if (functions[fn]) {
return functions[fn].apply(cy.container(), Array.prototype.slice.call(arguments, 1));
} else if (typeof fn == 'object' || !fn) {
return functions.init.apply(cy.container(), arguments);
} else {
throw new Error('No such function `' + fn + '` for cytoscape.js-expand-collapse');
}
return this;
};
},{"./debounce":3,"./elementUtilities":4}],3:[function(_dereq_,module,exports){
var debounce = (function () {
/**
* lodash 3.1.1 (Custom Build) <https://lodash.com/>
* Build: `lodash modern modularize exports="npm" -o ./`
* Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
* Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <https://lodash.com/license>
*/
/** Used as the `TypeError` message for "Functions" methods. */
var FUNC_ERROR_TEXT = 'Expected a function';
/* Native method references for those with the same name as other `lodash` methods. */
var nativeMax = Math.max,
nativeNow = Date.now;
/**
* Gets the number of milliseconds that have elapsed since the Unix epoch
* (1 January 1970 00:00:00 UTC).
*
* @static
* @memberOf _
* @category Date
* @example
*
* _.defer(function(stamp) {
* console.log(_.now() - stamp);
* }, _.now());
* // => logs the number of milliseconds it took for the deferred function to be invoked
*/
var now = nativeNow || function () {
return new Date().getTime();
};
/**
* Creates a debounced function that delays invoking `func` until after `wait`
* milliseconds have elapsed since the last time the debounced function was
* invoked. The debounced function comes with a `cancel` method to cancel
* delayed invocations. Provide an options object to indicate that `func`
* should be invoked on the leading and/or trailing edge of the `wait` timeout.
* Subsequent calls to the debounced function return the result of the last
* `func` invocation.
*
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
* on the trailing edge of the timeout only if the the debounced function is
* invoked more than once during the `wait` timeout.
*
* See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)
* for details over the differences between `_.debounce` and `_.throttle`.
*
* @static
* @memberOf _
* @category Function
* @param {Function} func The function to debounce.
* @param {number} [wait=0] The number of milliseconds to delay.
* @param {Object} [options] The options object.
* @param {boolean} [options.leading=false] Specify invoking on the leading
* edge of the timeout.
* @param {number} [options.maxWait] The maximum time `func` is allowed to be
* delayed before it's invoked.
* @param {boolean} [options.trailing=true] Specify invoking on the trailing
* edge of the timeout.
* @returns {Function} Returns the new debounced function.
* @example
*
* // avoid costly calculations while the window size is in flux
* jQuery(window).on('resize', _.debounce(calculateLayout, 150));
*
* // invoke `sendMail` when the click event is fired, debouncing subsequent calls
* jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
* 'leading': true,
* 'trailing': false
* }));
*
* // ensure `batchLog` is invoked once after 1 second of debounced calls
* var source = new EventSource('/stream');
* jQuery(source).on('message', _.debounce(batchLog, 250, {
* 'maxWait': 1000
* }));
*
* // cancel a debounced call
* var todoChanges = _.debounce(batchLog, 1000);
* Object.observe(models.todo, todoChanges);
*
* Object.observe(models, function(changes) {
* if (_.find(changes, { 'user': 'todo', 'type': 'delete'})) {
* todoChanges.cancel();
* }
* }, ['delete']);
*
* // ...at some point `models.todo` is changed
* models.todo.completed = true;
*
* // ...before 1 second has passed `models.todo` is deleted
* // which cancels the debounced `todoChanges` call
* delete models.todo;
*/
function debounce(func, wait, options) {
var args,
maxTimeoutId,
result,
stamp,
thisArg,
timeoutId,
trailingCall,
lastCalled = 0,
maxWait = false,
trailing = true;
if (typeof func != 'function') {
throw new TypeError(FUNC_ERROR_TEXT);
}
wait = wait < 0 ? 0 : (+wait || 0);
if (options === true) {
var leading = true;
trailing = false;
} else if (isObject(options)) {
leading = !!options.leading;
maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait);
trailing = 'trailing' in options ? !!options.trailing : trailing;
}
function cancel() {
if (timeoutId) {
clearTimeout(timeoutId);
}
if (maxTimeoutId) {
clearTimeout(maxTimeoutId);
}
lastCalled = 0;
maxTimeoutId = timeoutId = trailingCall = undefined;
}
function complete(isCalled, id) {
if (id) {
clearTimeout(id);
}
maxTimeoutId = timeoutId = trailingCall = undefined;
if (isCalled) {
lastCalled = now();
result = func.apply(thisArg, args);
if (!timeoutId && !maxTimeoutId) {
args = thisArg = undefined;
}
}
}
function delayed() {
var remaining = wait - (now() - stamp);
if (remaining <= 0 || remaining > wait) {
complete(trailingCall, maxTimeoutId);
} else {
timeoutId = setTimeout(delayed, remaining);
}
}
function maxDelayed() {
complete(trailing, timeoutId);
}
function debounced() {
args = arguments;
stamp = now();
thisArg = this;
trailingCall = trailing && (timeoutId || !leading);
if (maxWait === false) {
var leadingCall = leading && !timeoutId;
} else {
if (!maxTimeoutId && !leading) {
lastCalled = stamp;
}
var remaining = maxWait - (stamp - lastCalled),
isCalled = remaining <= 0 || remaining > maxWait;
if (isCalled) {
if (maxTimeoutId) {
maxTimeoutId = clearTimeout(maxTimeoutId);
}
lastCalled = stamp;
result = func.apply(thisArg, args);
}
else if (!maxTimeoutId) {
maxTimeoutId = setTimeout(maxDelayed, remaining);
}
}
if (isCalled && timeoutId) {
timeoutId = clearTimeout(timeoutId);
}
else if (!timeoutId && wait !== maxWait) {
timeoutId = setTimeout(delayed, wait);
}
if (leadingCall) {
isCalled = true;
result = func.apply(thisArg, args);
}
if (isCalled && !timeoutId && !maxTimeoutId) {
args = thisArg = undefined;
}
return result;
}
debounced.cancel = cancel;
return debounced;
}
/**
* Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an object, else `false`.
* @example
*
* _.isObject({});
* // => true
*
* _.isObject([1, 2, 3]);
* // => true
*
* _.isObject(1);
* // => false
*/
function isObject(value) {
// Avoid a V8 JIT bug in Chrome 19-20.
// See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
var type = typeof value;
return !!value && (type == 'object' || type == 'function');
}
return debounce;
})();
module.exports = debounce;
},{}],4:[function(_dereq_,module,exports){
function elementUtilities(cy) {
return {
moveNodes: function (positionDiff, nodes, notCalcTopMostNodes) {
var topMostNodes = notCalcTopMostNodes ? nodes : this.getTopMostNodes(nodes);
topMostNodes.positions(function(ele, i){
return {
x: topMostNodes[i].position("x") + positionDiff.x,
y: topMostNodes[i].position("y") + positionDiff.y
};
});
for (var i = 0; i < topMostNodes.length; i++) {
var node = topMostNodes[i];
var children = node.children();
this.moveNodes(positionDiff, children, true);
}
},
getTopMostNodes: function (nodes) {//*//
var nodesMap = {};
for (var i = 0; i < nodes.length; i++) {
nodesMap[nodes[i].id()] = true;
}
var roots = nodes.filter(function (ele, i) {
if(typeof ele === "number") {
ele = i;
}
var parent = ele.parent()[0];
while (parent != null) {
if (nodesMap[parent.id()]) {
return false;
}
parent = parent.parent()[0];
}
return true;
});
return roots;
},
rearrange: function (layoutBy) {
if (typeof layoutBy === "function") {
layoutBy();
} else if (layoutBy != null) {
var layout = cy.layout(layoutBy);
if (layout && layout.run) {
layout.run();
}
}
},
convertToRenderedPosition: function (modelPosition) {
var pan = cy.pan();
var zoom = cy.zoom();
var x = modelPosition.x * zoom + pan.x;
var y = modelPosition.y * zoom + pan.y;
return {
x: x,
y: y
};
}
};
}
module.exports = elementUtilities;
},{}],5:[function(_dereq_,module,exports){
var boundingBoxUtilities = _dereq_('./boundingBoxUtilities');
// Expand collapse utilities
function expandCollapseUtilities(cy) {
var elementUtilities = _dereq_('./elementUtilities')(cy);
return {
//the number of nodes moving animatedly after expand operation
animatedlyMovingNodeCount: 0,
/*
* A funtion basicly expanding a node, it is to be called when a node is expanded anyway.
* Single parameter indicates if the node is expanded alone and if it is truthy then layoutBy parameter is considered to
* perform layout after expand.
*/
expandNodeBaseFunction: function (node, single, layoutBy) {
if (!node._private.data.collapsedChildren){
return;
}
//check how the position of the node is changed
var positionDiff = {
x: node._private.position.x - node._private.data['position-before-collapse'].x,
y: node._private.position.y - node._private.data['position-before-collapse'].y
};
node.removeData("infoLabel");
node.removeClass('cy-expand-collapse-collapsed-node');
node.trigger("expandcollapse.beforeexpand");
var restoredNodes = node._private.data.collapsedChildren;
restoredNodes.restore();
var parentData = cy.scratch('_cyExpandCollapse').parentData;
for(var i = 0; i < restoredNodes.length; i++){
delete parentData[restoredNodes[i].id()];
}
cy.scratch('_cyExpandCollapse').parentData = parentData;
this.repairEdges(node);
node._private.data.collapsedChildren = null;
elementUtilities.moveNodes(positionDiff, node.children());
node.removeData('position-before-collapse');
node.trigger("position"); // position not triggered by default when nodes are moved
node.trigger("expandcollapse.afterexpand");
// If expand is called just for one node then call end operation to perform layout
if (single) {
this.endOperation(layoutBy);
}
},
/*
* A helper function to collapse given nodes in a simple way (Without performing layout afterward)
* It collapses all root nodes bottom up.
*/
simpleCollapseGivenNodes: function (nodes) {//*//
nodes.data("collapse", true);
var roots = elementUtilities.getTopMostNodes(nodes);
for (var i = 0; i < roots.length; i++) {
var root = roots[i];
// Collapse the nodes in bottom up order
this.collapseBottomUp(root);
}
return nodes;
},
/*
* A helper function to expand given nodes in a simple way (Without performing layout afterward)
* It expands all top most nodes top down.
*/
simpleExpandGivenNodes: function (nodes, applyFishEyeViewToEachNode) {
nodes.data("expand", true); // Mark that the nodes are still to be expanded
var roots = elementUtilities.getTopMostNodes(nodes);
for (var i = 0; i < roots.length; i++) {
var root = roots[i];
this.expandTopDown(root, applyFishEyeViewToEachNode); // For each root node expand top down
}
return nodes;
},
/*
* Expands all nodes by expanding all top most nodes top down with their descendants.
*/
simpleExpandAllNodes: function (nodes, applyFishEyeViewToEachNode) {
if (nodes === undefined) {
nodes = cy.nodes();
}
var orphans;
orphans = elementUtilities.getTopMostNodes(nodes);
var expandStack = [];
for (var i = 0; i < orphans.length; i++) {
var root = orphans[i];
this.expandAllTopDown(root, expandStack, applyFishEyeViewToEachNode);
}
return expandStack;
},
/*
* The operation to be performed after expand/collapse. It rearrange nodes by layoutBy parameter.
*/
endOperation: function (layoutBy) {
var self = this;
cy.ready(function () {
setTimeout(function() {
elementUtilities.rearrange(layoutBy);
}, 0);
});
},
/*
* Calls simple expandAllNodes. Then performs end operation.
*/
expandAllNodes: function (nodes, options) {//*//
var expandedStack = this.simpleExpandAllNodes(nodes, options.fisheye);
this.endOperation(options.layoutBy);
/*
* return the nodes to undo the operation
*/
return expandedStack;
},
/*
* Expands the root and its collapsed descendents in top down order.
*/
expandAllTopDown: function (root, expandStack, applyFishEyeViewToEachNode) {
if (root._private.data.collapsedChildren != null) {
expandStack.push(root);
this.expandNode(root, applyFishEyeViewToEachNode);
}
var children = root.children();
for (var i = 0; i < children.length; i++) {
var node = children[i];
this.expandAllTopDown(node, expandStack, applyFishEyeViewToEachNode);
}
},
//Expand the given nodes perform end operation after expandation
expandGivenNodes: function (nodes, options) {
// If there is just one node to expand we need to animate for fisheye view, but if there are more then one node we do not
if (nodes.length === 1) {
var node = nodes[0];
if (node._private.data.collapsedChildren != null) {
// Expand the given node the third parameter indicates that the node is simple which ensures that fisheye parameter will be considered
this.expandNode(node, options.fisheye, true, options.animate, options.layoutBy, options.animationDuration);
}
}
else {
// First expand given nodes and then perform layout according to the layoutBy parameter
this.simpleExpandGivenNodes(nodes, options.fisheye);
this.endOperation(options.layoutBy);
}
/*
* return the nodes to undo the operation
*/
return nodes;
},
//collapse the given nodes then perform end operation
collapseGivenNodes: function (nodes, options) {
/*
* In collapse operation there is no fisheye view to be applied so there is no animation to be destroyed here. We can do this
* in a batch.
*/
cy.startBatch();
this.simpleCollapseGivenNodes(nodes/*, options*/);
cy.endBatch();
nodes.trigger("position"); // position not triggered by default when collapseNode is called
this.endOperation(options.layoutBy);
// Update the style
cy.style().update();
/*
* return the nodes to undo the operation
*/
return nodes;
},
//collapse the nodes in bottom up order starting from the root
collapseBottomUp: function (root) {
var children = root.children();
for (var i = 0; i < children.length; i++) {
var node = children[i];
this.collapseBottomUp(node);
}
//If the root is a compound node to be collapsed then collapse it
if (root.data("collapse") && root.children().length > 0) {
this.collapseNode(root);
root.removeData("collapse");
}
},
//expand the nodes in top down order starting from the root
expandTopDown: function (root, applyFishEyeViewToEachNode) {
if (root.data("expand") && root._private.data.collapsedChildren != null) {
// Expand the root and unmark its expand data to specify that it is no more to be expanded
this.expandNode(root, applyFishEyeViewToEachNode);
root.removeData("expand");
}
// Make a recursive call for children of root
var children = root.children();
for (var i = 0; i < children.length; i++) {
var node = children[i];
this.expandTopDown(node);
}
},
// Converst the rendered position to model position according to global pan and zoom values
convertToModelPosition: function (renderedPosition) {
var pan = cy.pan();
var zoom = cy.zoom();
var x = (renderedPosition.x - pan.x) / zoom;
var y = (renderedPosition.y - pan.y) / zoom;
return {
x: x,
y: y
};
},
/*
* This method expands the given node. It considers applyFishEyeView, animate and layoutBy parameters.
* It also considers single parameter which indicates if this node is expanded alone. If this parameter is truthy along with
* applyFishEyeView parameter then the state of view port is to be changed to have extra space on the screen (if needed) before appliying the
* fisheye view.
*/
expandNode: function (node, applyFishEyeView, single, animate, layoutBy, animationDuration) {
var self = this;
var commonExpandOperation = function (node, applyFishEyeView, single, animate, layoutBy, animationDuration) {
if (applyFishEyeView) {
node._private.data['width-before-fisheye'] = node._private.data['size-before-collapse'].w;
node._private.data['height-before-fisheye'] = node._private.data['size-before-collapse'].h;
// Fisheye view expand the node.
// The first paramter indicates the node to apply fisheye view, the third parameter indicates the node
// to be expanded after fisheye view is applied.
self.fishEyeViewExpandGivenNode(node, single, node, animate, layoutBy, animationDuration);
}
// If one of these parameters is truthy it means that expandNodeBaseFunction is already to be called.
// However if none of them is truthy we need to call it here.
if (!single || !applyFishEyeView || !animate) {
self.expandNodeBaseFunction(node, single, layoutBy);
}
};
if (node._private.data.collapsedChildren != null) {
this.storeWidthHeight(node);
var animating = false; // Variable to check if there is a current animation, if there is commonExpandOperation will be called after animation
// If the node is the only node to expand and fisheye view should be applied, then change the state of viewport
// to create more space on screen (If needed)
if (applyFishEyeView && single) {
var topLeftPosition = this.convertToModelPosition({x: 0, y: 0});
var bottomRightPosition = this.convertToModelPosition({x: cy.width(), y: cy.height()});
var padding = 80;
var bb = {
x1: topLeftPosition.x,
x2: bottomRightPosition.x,
y1: topLeftPosition.y,
y2: bottomRightPosition.y
};
var nodeBB = {
x1: node._private.position.x - node._private.data['size-before-collapse'].w / 2 - padding,
x2: node._private.position.x + node._private.data['size-before-collapse'].w / 2 + padding,
y1: node._private.position.y - node._private.data['size-before-collapse'].h / 2 - padding,
y2: node._private.position.y + node._private.data['size-before-collapse'].h / 2 + padding
};
var unionBB = boundingBoxUtilities.getUnion(nodeBB, bb);
// If these bboxes are not equal then we need to change the viewport state (by pan and zoom)