Update yangvizualiser: resizing, added new functionality like slider for
[dlux.git] / modules / common-sigmatopology-resources / src / main / resources / sigmatopology / sigma.directive.js
1 var modules = [
2   'common/sigmatopology/sigmatopology.module',
3   'sigma', 
4   'sigma-parsers-gexf', 
5   'sigma-forceAtlas2', 
6   'sigma-dragNodes',
7   'sigma-customShapes'
8 ];
9
10 define(modules, function(topologyModule) {
11   topologyModule.register.directive('sigmaTopology', function() {
12     // constants
13     return {
14       restrict: 'E',
15       scope: {
16           topologyData: '=topologyData',
17           topologyOptions: '=topologyOptions',
18           topologyCustfunc: '=topologyCustfunc',
19           panel: '=panel',
20           customShapes: '=customShapes',
21           dragNodes: '=dragNodes',
22           settings: '=settingsSigma',
23           settingsAtlas: '=settingsAtlas',
24           triggerResizeSigma: '=triggerResizeSigma'
25       },
26       templateUrl: 'src/common/sigmatopology/sigma.tpl.html',
27       link: function($scope, iElm, iAttrs, controller) {
28
29           var sigmaIstance = null,
30               getSlowDownNum = function(numOfNodes){
31                   // return 1/numOfNodes;
32                   x = 10;
33               switch(true){
34                 case (numOfNodes < 20):
35                   x = 15;
36                   break;
37                 case (numOfNodes < 50):
38                   x = 5;
39                   break;
40                 case (numOfNodes < 100):
41                   x = 2;
42                   break;
43                 case (numOfNodes < 250):
44                   x = 1;
45                   break;
46                 case (numOfNodes < 500):
47                   x = 0.8;
48                   break;
49                 case (numOfNodes < 1000):
50                   x = 0.4;
51                   break;
52                 case (numOfNodes < 2000):
53                   x = 0.3;
54                   break;
55                 // case (numOfNodes < 5000):
56                 //   x = 0.1;
57                 //   break;
58                 case (numOfNodes < 10000):
59                   x = 0.1;
60                   break;
61               }
62
63
64               return x;
65             },
66             Sigma = sigma,
67             defaulSettings = {
68               defaultLabelColor: '#fff',
69               doubleClickEnabled: false,
70               labelThreshold: 8
71             };
72
73           $scope.$watch('topologyData', function (ntdata) {
74
75             if(ntdata){
76
77               if ( sigmaIstance !== null ) {
78                 sigmaIstance.kill();
79               }
80               var timeToStopAtlas;
81
82               // Instantiate sigma:
83               sigma.renderers.def = sigma.renderers.canvas;
84
85               // console.info('sigma topology data', ntdata, $scope.topologyData);
86               sigmaIstance = new Sigma({
87                 graph: {
88                   nodes: $scope.topologyData.nodes ? $scope.topologyData.nodes : [],
89                   edges: $scope.topologyData.links
90                 },
91                 container: 'graph-container',
92                 settings: $scope.settings ? $scope.settings : defaulSettings
93               });
94
95               if ( $scope.settingsAtlas ) {
96                 $scope.settingsAtlas.slowDown = getSlowDownNum($scope.topologyData.nodes.length);
97               }
98
99               var defaultConfigAtlas = {
100                     adjustSizes: true,
101                     // scalingRatio: 10,
102                     gravity: 1,
103                     slowDown: getSlowDownNum($scope.topologyData.nodes.length)
104                   },
105                   configAtlas = $scope.settingsAtlas ? $scope.settingsAtlas : defaultConfigAtlas;
106
107               if ( $scope.customShapes ) {
108                 CustomShapes.init(sigmaIstance);
109                 sigmaIstance.refresh();
110               }
111
112               var dragListener = null;
113
114               if ( $scope.dragNodes ) {
115                 dragListener = Sigma.plugins.dragNodes(sigmaIstance, sigmaIstance.renderers[0]);
116               }
117
118               sigmaIstance.startForceAtlas2(configAtlas);
119
120               if ( $scope.topologyCustfunc && angular.isFunction($scope.topologyCustfunc) ) {
121                 $scope.topologyCustfunc(sigmaIstance, getSlowDownNum, dragListener);
122               }
123
124             }
125           });
126
127           $scope.$watch('triggerResizeSigma', function () {
128
129               var topoData = {
130                     nodes: [],
131                     links: []
132                   };
133
134               if ( sigmaIstance !== null ) {
135                 topoData.nodes = sigmaIstance.graph.nodes();
136                 topoData.links = sigmaIstance.graph.edges();
137                 sigmaIstance.kill();
138               }
139
140               // Instantiate sigma:
141               sigma.renderers.def = sigma.renderers.canvas;
142
143               sigmaIstance = new Sigma({
144                 graph: {
145                   nodes: topoData ? topoData.nodes : [],
146                   edges: topoData ? topoData.links : []
147                 },
148                 container: 'graph-container',
149                 settings: $scope.settings ? $scope.settings : defaulSettings
150               });
151
152               if ( $scope.customShapes ) {
153                 CustomShapes.init(sigmaIstance);
154                 sigmaIstance.refresh();
155               }
156
157               var dragListener = null;
158
159               if ( $scope.dragNodes ) {
160                 dragListener = Sigma.plugins.dragNodes(sigmaIstance, sigmaIstance.renderers[0]);
161               }
162
163               if ( $scope.topologyCustfunc && angular.isFunction($scope.topologyCustfunc) ) {
164                 var resize = true;
165                 $scope.topologyCustfunc(sigmaIstance, getSlowDownNum, dragListener, resize);
166               }
167
168           });
169
170       }
171     };
172   });
173 });