Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 5 | .service('LogicTopologyHelper', function($window, $log, lodash, serviceTopologyConfig, NodeDrawer){ |
Matteo Scandolo | 11dc8c4 | 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', |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 20 | computeNodes: [], |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 21 | children: [ |
| 22 | { |
| 23 | name: 'LAN', |
| 24 | type: 'network', |
| 25 | children: [] //subscribers goes here |
| 26 | } |
| 27 | ] |
| 28 | } |
| 29 | ] |
| 30 | } |
| 31 | ] |
| 32 | }; |
| 33 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 34 | /** |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 35 | * Calculate the horizontal position for each element. |
| 36 | * subsrcribers, devices and routers have the same fixed width 20 |
| 37 | * network have a fixed width 104 |
| 38 | * rack have a fixed width 105 |
| 39 | * build and array of 6 elements representing the position of each element in the svg |
| 40 | * to equally space them |
| 41 | */ |
| 42 | |
| 43 | this.computeElementPosition = (svgWidth) => { |
| 44 | |
| 45 | let xPos = []; |
| 46 | |
| 47 | let totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, (el, val) => val + el, 0); |
| 48 | |
Matteo Scandolo | fc532ed | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 49 | let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2); |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 50 | |
| 51 | let step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1); |
| 52 | |
| 53 | lodash.forEach(serviceTopologyConfig.elWidths, (el, i) => { |
| 54 | |
| 55 | // get half of the previous elements width |
| 56 | let previousElWidth = 0; |
| 57 | if(i !== 0){ |
Matteo Scandolo | fc532ed | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 58 | previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), (el, val) => val + el, 0); |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | let elPos = |
| 62 | serviceTopologyConfig.widthMargin // right margin |
| 63 | + (step * i) // space between elements |
| 64 | + (el / 2) // this el width |
| 65 | + previousElWidth; // previous elements width |
| 66 | |
| 67 | xPos.push(svgWidth - elPos); |
| 68 | }) |
| 69 | |
| 70 | return xPos |
| 71 | }; |
| 72 | |
| 73 | /** |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 74 | * from a nested data structure, |
| 75 | * create nodes and links for a D3 Tree Layout |
| 76 | */ |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 77 | const computeLayout = (data) => { |
| 78 | let nodes = layout.nodes(data); |
| 79 | |
| 80 | // Normalize for fixed-depth. |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 81 | nodes.forEach((d) => { |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 82 | // position the child node horizontally |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 83 | d.y = this.computeElementPosition(svgWidth)[d.depth]; |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 84 | }); |
| 85 | |
| 86 | let links = layout.links(nodes); |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 87 | |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 88 | return [nodes, links]; |
| 89 | }; |
| 90 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Draw the containing group for any node or update the existing one |
| 93 | */ |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 94 | const drawNodes = (svg, nodes) => { |
| 95 | // Update the nodes… |
| 96 | var node = svg.selectAll('g.node') |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 97 | .data(nodes, d => { |
| 98 | if(!angular.isString(d.d3Id)){ |
| 99 | d.d3Id = `tree-${++i}`; |
| 100 | } |
| 101 | return d.d3Id; |
| 102 | }); |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 103 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 104 | // Enter any new nodes |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 105 | var nodeEnter = node.enter().append('g') |
| 106 | .attr({ |
| 107 | class: d => `node ${d.type}`, |
| 108 | transform: `translate(${svgWidth / 2}, ${svgHeight / 2})` |
| 109 | }); |
| 110 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 111 | NodeDrawer.addNetworks(nodeEnter.filter('.network')); |
| 112 | NodeDrawer.addRack(nodeEnter.filter('.rack')); |
| 113 | NodeDrawer.addPhisical(nodeEnter.filter('.router')); |
| 114 | NodeDrawer.addPhisical(nodeEnter.filter('.subscriber')); |
| 115 | NodeDrawer.addDevice(nodeEnter.filter('.device')); |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 116 | |
| 117 | // Transition nodes to their new position. |
| 118 | var nodeUpdate = node.transition() |
| 119 | .duration(serviceTopologyConfig.duration) |
| 120 | .attr({ |
| 121 | 'transform': d => `translate(${d.y},${d.x})` |
| 122 | }); |
| 123 | |
| 124 | // TODO handle node remove |
| 125 | }; |
| 126 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Handle links in the tree layout |
| 129 | */ |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 130 | const drawLinks = (svg, links) => { |
| 131 | |
| 132 | diagonal = d3.svg.diagonal() |
| 133 | .projection(d => [d.y, d.x]); |
| 134 | |
| 135 | // Update the links… |
| 136 | var link = svg.selectAll('path.link') |
| 137 | .data(links, d => { |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 138 | return d.target.d3Id |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 139 | }); |
| 140 | |
| 141 | // Enter any new links at the parent's previous position. |
| 142 | link.enter().insert('path', 'g') |
| 143 | .attr('class', d => `link ${d.target.type}`) |
| 144 | .attr('d', function(d) { |
| 145 | var o = {x: svgHeight / 2, y: svgWidth / 2}; |
| 146 | return diagonal({source: o, target: o}); |
| 147 | }); |
| 148 | |
| 149 | // Transition links to their new position. |
| 150 | link.transition() |
| 151 | .duration(serviceTopologyConfig.duration) |
| 152 | .attr('d', diagonal); |
| 153 | }; |
| 154 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Calculate the svg size and setup tree layout |
| 157 | */ |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 158 | this.setupTree = (svg) => { |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 159 | |
| 160 | |
| 161 | svgWidth = svg.node().getBoundingClientRect().width; |
| 162 | svgHeight = svg.node().getBoundingClientRect().height; |
| 163 | |
| 164 | const width = svgWidth - (serviceTopologyConfig.widthMargin * 2); |
| 165 | const height = svgHeight - (serviceTopologyConfig.heightMargin * 2); |
| 166 | |
| 167 | layout = d3.layout.tree() |
| 168 | .size([height, width]); |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 169 | }; |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 170 | |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 171 | /** |
| 172 | * Update the tree layout |
| 173 | */ |
| 174 | |
| 175 | this.updateTree = (svg) => { |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 176 | // Compute the new tree layout. |
| 177 | [nodes, links] = computeLayout(baseData); |
| 178 | |
| 179 | drawNodes(svg, nodes); |
| 180 | drawLinks(svg, links); |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 181 | } |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 182 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 183 | /** |
| 184 | * Add Subscribers to the tree |
| 185 | */ |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 186 | this.addSubscribers = (subscribers) => { |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 187 | |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 188 | subscribers.map((subscriber) => { |
| 189 | subscriber.children = subscriber.devices; |
| 190 | }); |
| 191 | |
| 192 | // add subscriber to data tree |
| 193 | baseData.children[0].children[0].children[0].children = subscribers; |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 194 | |
| 195 | return baseData; |
| 196 | }; |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 197 | |
Matteo Scandolo | 7fd4d04 | 2016-02-16 14:44:51 -0800 | [diff] [blame^] | 198 | /** |
| 199 | * Add compute nodes to the rack element |
| 200 | */ |
| 201 | |
| 202 | this.addComputeNodes = (computeNodes) => { |
| 203 | baseData.children[0].children[0].computeNodes = computeNodes; |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 204 | } |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 205 | }); |
| 206 | |
| 207 | }()); |