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