Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 5 | .service('LogicTopologyHelper', function($window, $log, lodash, serviceTopologyConfig, NodeDrawer){ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 6 | |
| 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 Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 33 | /** |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 34 | * 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 Scandolo | bec0a6c | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 48 | let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 49 | |
| 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 Scandolo | bec0a6c | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 57 | previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), (el, val) => val + el, 0); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 58 | } |
| 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 Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 73 | * from a nested data structure, |
| 74 | * create nodes and links for a D3 Tree Layout |
| 75 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 76 | const computeLayout = (data) => { |
| 77 | let nodes = layout.nodes(data); |
| 78 | |
| 79 | // Normalize for fixed-depth. |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 80 | nodes.forEach((d) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 81 | // position the child node horizontally |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 82 | d.y = this.computeElementPosition(svgWidth)[d.depth]; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 83 | }); |
| 84 | |
| 85 | let links = layout.links(nodes); |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 86 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 87 | return [nodes, links]; |
| 88 | }; |
| 89 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Draw the containing group for any node or update the existing one |
| 92 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 93 | const drawNodes = (svg, nodes) => { |
| 94 | // Update the nodes… |
| 95 | var node = svg.selectAll('g.node') |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 96 | .data(nodes, d => { |
| 97 | if(!angular.isString(d.d3Id)){ |
| 98 | d.d3Id = `tree-${++i}`; |
| 99 | } |
| 100 | return d.d3Id; |
| 101 | }); |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 102 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 103 | // Enter any new nodes |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 104 | var nodeEnter = node.enter().append('g') |
| 105 | .attr({ |
| 106 | class: d => `node ${d.type}`, |
| 107 | transform: `translate(${svgWidth / 2}, ${svgHeight / 2})` |
| 108 | }); |
| 109 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 110 | 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 Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 115 | |
| 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 Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Handle links in the tree layout |
| 128 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 129 | 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 Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 137 | return d.target.d3Id |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 138 | }); |
| 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 Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Calculate the svg size and setup tree layout |
| 156 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 157 | 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 Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 177 | /** |
| 178 | * Add Subscribers to the tree |
| 179 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 180 | this.addSubscribers = (svg, subscribers) => { |
| 181 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 182 | 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 Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 189 | [nodes, links] = computeLayout(baseData); |
| 190 | |
| 191 | drawNodes(svg, nodes); |
| 192 | drawLinks(svg, links); |
| 193 | } |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 194 | }); |
| 195 | |
| 196 | }()); |