Skip to content

Commit 7d9d007

Browse files
committed
Merge branch 'unstable'
2 parents ce80cca + 3a58463 commit 7d9d007

File tree

2 files changed

+70
-38
lines changed

2 files changed

+70
-38
lines changed

src/alignment.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module.exports = function (cytoscape, cy, $) {
2-
1+
module.exports = function (cytoscape, cy, $, apiRegistered) {
2+
33
// Needed because parent nodes cannot be moved!
44
function moveTopDown(node, dx, dy) {
55
var nodes = node.union(node.descendants());
@@ -40,50 +40,58 @@ module.exports = function (cytoscape, cy, $) {
4040
}
4141

4242

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 ) {
4447

45-
var eles = getTopMostNodes(this.nodes(":visible"));
48+
cytoscape( "collection", "align", function (horizontal, vertical, alignTo) {
4649

47-
var modelNode = alignTo ? alignTo : eles[0];
50+
var eles = getTopMostNodes(this.nodes(":visible"));
4851

49-
eles = eles.not(modelNode);
52+
var modelNode = alignTo ? alignTo : eles[0];
5053

51-
horizontal = horizontal ? horizontal : "none";
52-
vertical = vertical ? vertical : "none";
54+
eles = eles.not(modelNode);
5355

56+
horizontal = horizontal ? horizontal : "none";
57+
vertical = vertical ? vertical : "none";
5458

55-
// 0 for center
56-
var xFactor = 0;
57-
var yFactor = 0;
5859

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;
6363

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;
6868

69+
if (horizontal == "top")
70+
yFactor = -1;
71+
else if (horizontal == "bottom")
72+
yFactor = 1;
6973

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());
7474

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());
7779

80+
if (vertical != "none")
81+
newPos.x = modelNode.position("x") + xFactor * (modelNode.outerWidth() - node.outerWidth()) / 2;
7882

79-
if (horizontal != "none")
80-
newPos.y = modelNode.position("y") + yFactor * (modelNode.outerHeight() - node.outerHeight()) / 2;
8183

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+
}
8494

85-
return this;
86-
});
8795

8896
if (cy.undoRedo) {
8997
function getNodePositions() {

src/index.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
if( !cytoscape ){ return; } // can't register if cytoscape unspecified
77

8-
var options = {
8+
// flag that indicates if extension api functions are registed to cytoscape
9+
// note that ideally these functions should not be directly registered to core from cytoscape.js
10+
// extensions
11+
var apiRegistered = false;
12+
13+
var defaults = {
914
// On/Off Modules
1015
/* From the following four snap options, at most one should be true at a given time */
1116
snapToGridOnRelease: true, // Snap to grid on release
@@ -56,7 +61,6 @@
5661
var _parentPadding = require("./parentPadding");
5762
var _alignment = require("./alignment");
5863
var debounce = require("./debounce");
59-
var snap, resize, snapToGridDuringDrag, drawGrid, eventsController, guidelines, parentPadding, alignment;
6064

6165
function getScratch(cy) {
6266
if (!cy.scratch("_gridGuide")) {
@@ -68,9 +72,20 @@
6872

6973
cytoscape( 'core', 'gridGuide', function(opts){
7074
var cy = this;
71-
$.extend(true, options, opts);
7275

73-
if (!getScratch(cy).initialized) {
76+
// access the scratch pad for cy
77+
var scratchPad = getScratch(cy);
78+
79+
// extend the already existing options for the instance or the default options
80+
var options = $.extend(true, {}, scratchPad.options || defaults, opts);
81+
82+
// reset the options for the instance
83+
scratchPad.options = options;
84+
85+
if (!scratchPad.initialized) {
86+
87+
var snap, resize, snapToGridDuringDrag, drawGrid, eventsController, guidelines, parentPadding, alignment;
88+
7489
snap = _snapOnRelease(cy, options.gridSpacing);
7590
resize = _resize(options.gridSpacing);
7691
snapToGridDuringDrag = _snapToGridDuringDrag(cy, snap);
@@ -80,12 +95,21 @@
8095

8196
eventsController = _eventsController(cy, snap, resize, snapToGridDuringDrag, drawGrid, guidelines, parentPadding, $, options);
8297

83-
alignment = _alignment(cytoscape, cy, $);
98+
alignment = _alignment(cytoscape, cy, $, apiRegistered);
99+
100+
// mark that api functions are registered to cytoscape
101+
apiRegistered = true;
84102

85103
eventsController.init(options);
86-
getScratch(cy).initialized = true;
87-
} else
104+
105+
// init params in scratchPad
106+
scratchPad.initialized = true;
107+
scratchPad.eventsController = eventsController;
108+
}
109+
else {
110+
var eventsController = scratchPad.eventsController;
88111
eventsController.syncWithOptions(options);
112+
}
89113

90114
return this; // chainability
91115
} ) ;

0 commit comments

Comments
 (0)