blob: 4e004234b742358c90ac37b26de2ddc9b59335f6 [file] [log] [blame]
Matteo Scandolo219b1a72016-02-09 11:19:22 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
Matteo Scandolo9fe01af2016-02-09 16:01:49 -08005 .service('LogicTopologyHelper', function($window, $log, lodash, serviceTopologyConfig, NodeDrawer){
Matteo Scandoloaf286372016-02-09 14:46:14 -08006
7 var diagonal, nodes, links, i = 0, svgWidth, svgHeight, layout;
8
9 const baseData = {
10 name: 'Router',
11 type: 'router',
12 children: [
13 {
14 name: 'WAN',
15 type: 'network',
16 children: [
17 {
18 name: 'Rack',
19 type: 'rack',
20 children: [
21 {
22 name: 'LAN',
23 type: 'network',
24 children: [] //subscribers goes here
25 }
26 ]
27 }
28 ]
29 }
30 ]
31 };
32
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080033 /**
Matteo Scandolocc0db942016-02-11 17:37:08 -080034 * Calculate the horizontal position for each element.
35 * subsrcribers, devices and routers have the same fixed width 20
36 * network have a fixed width 104
37 * rack have a fixed width 105
38 * build and array of 6 elements representing the position of each element in the svg
39 * to equally space them
40 */
41
42 this.computeElementPosition = (svgWidth) => {
43
44 let xPos = [];
45
46 let totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, (el, val) => val + el, 0);
47
Matteo Scandolobec0a6c2016-02-11 17:58:18 -080048 let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2);
Matteo Scandolocc0db942016-02-11 17:37:08 -080049
50 let step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1);
51
52 lodash.forEach(serviceTopologyConfig.elWidths, (el, i) => {
53
54 // get half of the previous elements width
55 let previousElWidth = 0;
56 if(i !== 0){
Matteo Scandolobec0a6c2016-02-11 17:58:18 -080057 previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), (el, val) => val + el, 0);
Matteo Scandolocc0db942016-02-11 17:37:08 -080058 }
59
60 let elPos =
61 serviceTopologyConfig.widthMargin // right margin
62 + (step * i) // space between elements
63 + (el / 2) // this el width
64 + previousElWidth; // previous elements width
65
66 xPos.push(svgWidth - elPos);
67 })
68
69 return xPos
70 };
71
72 /**
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080073 * from a nested data structure,
74 * create nodes and links for a D3 Tree Layout
75 */
Matteo Scandoloaf286372016-02-09 14:46:14 -080076 const computeLayout = (data) => {
77 let nodes = layout.nodes(data);
78
79 // Normalize for fixed-depth.
Matteo Scandolocc0db942016-02-11 17:37:08 -080080 nodes.forEach((d) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -080081 // position the child node horizontally
Matteo Scandolocc0db942016-02-11 17:37:08 -080082 d.y = this.computeElementPosition(svgWidth)[d.depth];
Matteo Scandoloaf286372016-02-09 14:46:14 -080083 });
84
85 let links = layout.links(nodes);
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080086
Matteo Scandoloaf286372016-02-09 14:46:14 -080087 return [nodes, links];
88 };
89
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080090 /**
91 * Draw the containing group for any node or update the existing one
92 */
Matteo Scandoloaf286372016-02-09 14:46:14 -080093 const drawNodes = (svg, nodes) => {
94 // Update the nodes…
95 var node = svg.selectAll('g.node')
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080096 .data(nodes, d => {
97 if(!angular.isString(d.d3Id)){
98 d.d3Id = `tree-${++i}`;
99 }
100 return d.d3Id;
101 });
Matteo Scandoloaf286372016-02-09 14:46:14 -0800102
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800103 // Enter any new nodes
Matteo Scandoloaf286372016-02-09 14:46:14 -0800104 var nodeEnter = node.enter().append('g')
105 .attr({
106 class: d => `node ${d.type}`,
107 transform: `translate(${svgWidth / 2}, ${svgHeight / 2})`
108 });
109
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800110 NodeDrawer.addNetworks(nodeEnter.filter('.network'));
111 NodeDrawer.addRack(nodeEnter.filter('.rack'));
112 NodeDrawer.addPhisical(nodeEnter.filter('.router'));
113 NodeDrawer.addPhisical(nodeEnter.filter('.subscriber'));
114 NodeDrawer.addDevice(nodeEnter.filter('.device'));
Matteo Scandoloaf286372016-02-09 14:46:14 -0800115
116 // Transition nodes to their new position.
117 var nodeUpdate = node.transition()
118 .duration(serviceTopologyConfig.duration)
119 .attr({
120 'transform': d => `translate(${d.y},${d.x})`
121 });
122
123 // TODO handle node remove
124 };
125
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800126 /**
127 * Handle links in the tree layout
128 */
Matteo Scandoloaf286372016-02-09 14:46:14 -0800129 const drawLinks = (svg, links) => {
130
131 diagonal = d3.svg.diagonal()
132 .projection(d => [d.y, d.x]);
133
134 // Update the links…
135 var link = svg.selectAll('path.link')
136 .data(links, d => {
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800137 return d.target.d3Id
Matteo Scandoloaf286372016-02-09 14:46:14 -0800138 });
139
140 // Enter any new links at the parent's previous position.
141 link.enter().insert('path', 'g')
142 .attr('class', d => `link ${d.target.type}`)
143 .attr('d', function(d) {
144 var o = {x: svgHeight / 2, y: svgWidth / 2};
145 return diagonal({source: o, target: o});
146 });
147
148 // Transition links to their new position.
149 link.transition()
150 .duration(serviceTopologyConfig.duration)
151 .attr('d', diagonal);
152 };
153
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800154 /**
155 * Calculate the svg size and setup tree layout
156 */
Matteo Scandoloaf286372016-02-09 14:46:14 -0800157 this.drawTree = (svg) => {
158
159
160 svgWidth = svg.node().getBoundingClientRect().width;
161 svgHeight = svg.node().getBoundingClientRect().height;
162
163 const width = svgWidth - (serviceTopologyConfig.widthMargin * 2);
164 const height = svgHeight - (serviceTopologyConfig.heightMargin * 2);
165
166 layout = d3.layout.tree()
167 .size([height, width]);
168
169 // Compute the new tree layout.
170 [nodes, links] = computeLayout(baseData);
171
172 drawNodes(svg, nodes);
173 drawLinks(svg, links);
174
175 };
176
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800177 /**
178 * Add Subscribers to the tree
179 */
Matteo Scandoloaf286372016-02-09 14:46:14 -0800180 this.addSubscribers = (svg, subscribers) => {
181
Matteo Scandoloaf286372016-02-09 14:46:14 -0800182 subscribers.map((subscriber) => {
183 subscriber.children = subscriber.devices;
184 });
185
186 // add subscriber to data tree
187 baseData.children[0].children[0].children[0].children = subscribers;
188
Matteo Scandoloaf286372016-02-09 14:46:14 -0800189 [nodes, links] = computeLayout(baseData);
190
191 drawNodes(svg, nodes);
192 drawLinks(svg, links);
193 }
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800194 });
195
196}());