|
1 | | -module.exports = function (cytoscape, cy, $) { |
2 | | - |
| 1 | +module.exports = function (cytoscape, cy, $, apiRegistered) { |
| 2 | + |
3 | 3 | // Needed because parent nodes cannot be moved! |
4 | 4 | function moveTopDown(node, dx, dy) { |
5 | 5 | var nodes = node.union(node.descendants()); |
@@ -40,50 +40,58 @@ module.exports = function (cytoscape, cy, $) { |
40 | 40 | } |
41 | 41 |
|
42 | 42 |
|
43 | | - cytoscape( "collection", "align", function (horizontal, vertical, alignTo) { |
| 43 | + // If extension api functions are not registed to cytoscape yet register them here. |
| 44 | + // Note that ideally these functions should not be directly registered to core from cytoscape.js |
| 45 | + // extensions |
| 46 | + if ( !apiRegistered ) { |
44 | 47 |
|
45 | | - var eles = getTopMostNodes(this.nodes(":visible")); |
| 48 | + cytoscape( "collection", "align", function (horizontal, vertical, alignTo) { |
46 | 49 |
|
47 | | - var modelNode = alignTo ? alignTo : eles[0]; |
| 50 | + var eles = getTopMostNodes(this.nodes(":visible")); |
48 | 51 |
|
49 | | - eles = eles.not(modelNode); |
| 52 | + var modelNode = alignTo ? alignTo : eles[0]; |
50 | 53 |
|
51 | | - horizontal = horizontal ? horizontal : "none"; |
52 | | - vertical = vertical ? vertical : "none"; |
| 54 | + eles = eles.not(modelNode); |
53 | 55 |
|
| 56 | + horizontal = horizontal ? horizontal : "none"; |
| 57 | + vertical = vertical ? vertical : "none"; |
54 | 58 |
|
55 | | - // 0 for center |
56 | | - var xFactor = 0; |
57 | | - var yFactor = 0; |
58 | 59 |
|
59 | | - if (vertical == "left") |
60 | | - xFactor = -1; |
61 | | - else if (vertical == "right") |
62 | | - xFactor = 1; |
| 60 | + // 0 for center |
| 61 | + var xFactor = 0; |
| 62 | + var yFactor = 0; |
63 | 63 |
|
64 | | - if (horizontal == "top") |
65 | | - yFactor = -1; |
66 | | - else if (horizontal == "bottom") |
67 | | - yFactor = 1; |
| 64 | + if (vertical == "left") |
| 65 | + xFactor = -1; |
| 66 | + else if (vertical == "right") |
| 67 | + xFactor = 1; |
68 | 68 |
|
| 69 | + if (horizontal == "top") |
| 70 | + yFactor = -1; |
| 71 | + else if (horizontal == "bottom") |
| 72 | + yFactor = 1; |
69 | 73 |
|
70 | | - for (var i = 0; i < eles.length; i++) { |
71 | | - var node = eles[i]; |
72 | | - var oldPos = $.extend({}, node.position()); |
73 | | - var newPos = $.extend({}, node.position()); |
74 | 74 |
|
75 | | - if (vertical != "none") |
76 | | - newPos.x = modelNode.position("x") + xFactor * (modelNode.outerWidth() - node.outerWidth()) / 2; |
| 75 | + for (var i = 0; i < eles.length; i++) { |
| 76 | + var node = eles[i]; |
| 77 | + var oldPos = $.extend({}, node.position()); |
| 78 | + var newPos = $.extend({}, node.position()); |
77 | 79 |
|
| 80 | + if (vertical != "none") |
| 81 | + newPos.x = modelNode.position("x") + xFactor * (modelNode.outerWidth() - node.outerWidth()) / 2; |
78 | 82 |
|
79 | | - if (horizontal != "none") |
80 | | - newPos.y = modelNode.position("y") + yFactor * (modelNode.outerHeight() - node.outerHeight()) / 2; |
81 | 83 |
|
82 | | - moveTopDown(node, newPos.x - oldPos.x, newPos.y - oldPos.y); |
83 | | - } |
| 84 | + if (horizontal != "none") |
| 85 | + newPos.y = modelNode.position("y") + yFactor * (modelNode.outerHeight() - node.outerHeight()) / 2; |
| 86 | + |
| 87 | + moveTopDown(node, newPos.x - oldPos.x, newPos.y - oldPos.y); |
| 88 | + } |
| 89 | + |
| 90 | + return this; |
| 91 | + }); |
| 92 | + |
| 93 | + } |
84 | 94 |
|
85 | | - return this; |
86 | | - }); |
87 | 95 |
|
88 | 96 | if (cy.undoRedo) { |
89 | 97 | function getNodePositions() { |
|
0 commit comments