@@ -996,8 +996,8 @@ function createRBTree(compare) {
996996 return new RedBlackTree ( compare || defaultCompare , null )
997997}
998998} , { } ] , 2 :[ function ( require , module , exports ) {
999- module . exports = function ( cytoscape , cy , $ ) {
1000-
999+ module . exports = function ( cytoscape , cy , $ , apiRegistered ) {
1000+
10011001 // Needed because parent nodes cannot be moved!
10021002 function moveTopDown ( node , dx , dy ) {
10031003 var nodes = node . union ( node . descendants ( ) ) ;
@@ -1038,50 +1038,58 @@ module.exports = function (cytoscape, cy, $) {
10381038 }
10391039
10401040
1041- cytoscape ( "collection" , "align" , function ( horizontal , vertical , alignTo ) {
1041+ // If extension api functions are not registed to cytoscape yet register them here.
1042+ // Note that ideally these functions should not be directly registered to core from cytoscape.js
1043+ // extensions
1044+ if ( ! apiRegistered ) {
10421045
1043- var eles = getTopMostNodes ( this . nodes ( ":visible" ) ) ;
1046+ cytoscape ( "collection" , "align" , function ( horizontal , vertical , alignTo ) {
10441047
1045- var modelNode = alignTo ? alignTo : eles [ 0 ] ;
1048+ var eles = getTopMostNodes ( this . nodes ( ":visible" ) ) ;
10461049
1047- eles = eles . not ( modelNode ) ;
1050+ var modelNode = alignTo ? alignTo : eles [ 0 ] ;
10481051
1049- horizontal = horizontal ? horizontal : "none" ;
1050- vertical = vertical ? vertical : "none" ;
1052+ eles = eles . not ( modelNode ) ;
10511053
1054+ horizontal = horizontal ? horizontal : "none" ;
1055+ vertical = vertical ? vertical : "none" ;
10521056
1053- // 0 for center
1054- var xFactor = 0 ;
1055- var yFactor = 0 ;
10561057
1057- if ( vertical == "left" )
1058- xFactor = - 1 ;
1059- else if ( vertical == "right" )
1060- xFactor = 1 ;
1058+ // 0 for center
1059+ var xFactor = 0 ;
1060+ var yFactor = 0 ;
10611061
1062- if ( horizontal == "top " )
1063- yFactor = - 1 ;
1064- else if ( horizontal == "bottom " )
1065- yFactor = 1 ;
1062+ if ( vertical == "left " )
1063+ xFactor = - 1 ;
1064+ else if ( vertical == "right " )
1065+ xFactor = 1 ;
10661066
1067+ if ( horizontal == "top" )
1068+ yFactor = - 1 ;
1069+ else if ( horizontal == "bottom" )
1070+ yFactor = 1 ;
10671071
1068- for ( var i = 0 ; i < eles . length ; i ++ ) {
1069- var node = eles [ i ] ;
1070- var oldPos = $ . extend ( { } , node . position ( ) ) ;
1071- var newPos = $ . extend ( { } , node . position ( ) ) ;
10721072
1073- if ( vertical != "none" )
1074- newPos . x = modelNode . position ( "x" ) + xFactor * ( modelNode . outerWidth ( ) - node . outerWidth ( ) ) / 2 ;
1073+ for ( var i = 0 ; i < eles . length ; i ++ ) {
1074+ var node = eles [ i ] ;
1075+ var oldPos = $ . extend ( { } , node . position ( ) ) ;
1076+ var newPos = $ . extend ( { } , node . position ( ) ) ;
10751077
1078+ if ( vertical != "none" )
1079+ newPos . x = modelNode . position ( "x" ) + xFactor * ( modelNode . outerWidth ( ) - node . outerWidth ( ) ) / 2 ;
10761080
1077- if ( horizontal != "none" )
1078- newPos . y = modelNode . position ( "y" ) + yFactor * ( modelNode . outerHeight ( ) - node . outerHeight ( ) ) / 2 ;
10791081
1080- moveTopDown ( node , newPos . x - oldPos . x , newPos . y - oldPos . y ) ;
1081- }
1082+ if ( horizontal != "none" )
1083+ newPos . y = modelNode . position ( "y" ) + yFactor * ( modelNode . outerHeight ( ) - node . outerHeight ( ) ) / 2 ;
1084+
1085+ moveTopDown ( node , newPos . x - oldPos . x , newPos . y - oldPos . y ) ;
1086+ }
1087+
1088+ return this ;
1089+ } ) ;
1090+
1091+ }
10821092
1083- return this ;
1084- } ) ;
10851093
10861094 if ( cy . undoRedo ) {
10871095 function getNodePositions ( ) {
@@ -2734,7 +2742,12 @@ module.exports = function (opts, cy, $, debounce) {
27342742
27352743 if ( ! cytoscape ) { return ; } // can't register if cytoscape unspecified
27362744
2737- var options = {
2745+ // flag that indicates if extension api functions are registed to cytoscape
2746+ // note that ideally these functions should not be directly registered to core from cytoscape.js
2747+ // extensions
2748+ var apiRegistered = false ;
2749+
2750+ var defaults = {
27382751 // On/Off Modules
27392752 /* From the following four snap options, at most one should be true at a given time */
27402753 snapToGridOnRelease : true , // Snap to grid on release
@@ -2785,7 +2798,6 @@ module.exports = function (opts, cy, $, debounce) {
27852798 var _parentPadding = require ( "./parentPadding" ) ;
27862799 var _alignment = require ( "./alignment" ) ;
27872800 var debounce = require ( "./debounce" ) ;
2788- var snap , resize , snapToGridDuringDrag , drawGrid , eventsController , guidelines , parentPadding , alignment ;
27892801
27902802 function getScratch ( cy ) {
27912803 if ( ! cy . scratch ( "_gridGuide" ) ) {
@@ -2797,9 +2809,20 @@ module.exports = function (opts, cy, $, debounce) {
27972809
27982810 cytoscape ( 'core' , 'gridGuide' , function ( opts ) {
27992811 var cy = this ;
2800- $ . extend ( true , options , opts ) ;
28012812
2802- if ( ! getScratch ( cy ) . initialized ) {
2813+ // access the scratch pad for cy
2814+ var scratchPad = getScratch ( cy ) ;
2815+
2816+ // extend the already existing options for the instance or the default options
2817+ var options = $ . extend ( true , { } , scratchPad . options || defaults , opts ) ;
2818+
2819+ // reset the options for the instance
2820+ scratchPad . options = options ;
2821+
2822+ if ( ! scratchPad . initialized ) {
2823+
2824+ var snap , resize , snapToGridDuringDrag , drawGrid , eventsController , guidelines , parentPadding , alignment ;
2825+
28032826 snap = _snapOnRelease ( cy , options . gridSpacing ) ;
28042827 resize = _resize ( options . gridSpacing ) ;
28052828 snapToGridDuringDrag = _snapToGridDuringDrag ( cy , snap ) ;
@@ -2809,12 +2832,21 @@ module.exports = function (opts, cy, $, debounce) {
28092832
28102833 eventsController = _eventsController ( cy , snap , resize , snapToGridDuringDrag , drawGrid , guidelines , parentPadding , $ , options ) ;
28112834
2812- alignment = _alignment ( cytoscape , cy , $ ) ;
2835+ alignment = _alignment ( cytoscape , cy , $ , apiRegistered ) ;
2836+
2837+ // mark that api functions are registered to cytoscape
2838+ apiRegistered = true ;
28132839
28142840 eventsController . init ( options ) ;
2815- getScratch ( cy ) . initialized = true ;
2816- } else
2841+
2842+ // init params in scratchPad
2843+ scratchPad . initialized = true ;
2844+ scratchPad . eventsController = eventsController ;
2845+ }
2846+ else {
2847+ var eventsController = scratchPad . eventsController ;
28172848 eventsController . syncWithOptions ( options ) ;
2849+ }
28182850
28192851 return this ; // chainability
28202852 } ) ;
0 commit comments