Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 4 | angular.module('xos.diagnostic') |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 5 | .service('LogicTopologyHelper', function($window, $log, $rootScope, lodash, serviceTopologyConfig, NodeDrawer, ChartData){ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 6 | |
| 7 | var diagonal, nodes, links, i = 0, svgWidth, svgHeight, layout; |
| 8 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 9 | const baseData = ChartData.logicTopologyData; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 10 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 11 | /** |
Matteo Scandolo | cc0db94 | 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 | |
| 24 | let totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, (el, val) => val + el, 0); |
| 25 | |
Matteo Scandolo | bec0a6c | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 26 | let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 27 | |
| 28 | let step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1); |
| 29 | |
| 30 | lodash.forEach(serviceTopologyConfig.elWidths, (el, i) => { |
| 31 | |
| 32 | // get half of the previous elements width |
| 33 | let previousElWidth = 0; |
| 34 | if(i !== 0){ |
Matteo Scandolo | bec0a6c | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 35 | 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] | 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 | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 43 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 44 | xPos.push(svgWidth - elPos); |
| 45 | }) |
| 46 | |
| 47 | return xPos |
| 48 | }; |
| 49 | |
| 50 | /** |
Matteo Scandolo | 9fe01af | 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 | af28637 | 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 | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 58 | nodes.forEach((d) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 59 | // position the child node horizontally |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 60 | d.y = this.computeElementPosition(svgWidth)[d.depth]; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 61 | }); |
| 62 | |
| 63 | let links = layout.links(nodes); |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 64 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 65 | return [nodes, links]; |
| 66 | }; |
| 67 | |
Matteo Scandolo | 9fe01af | 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 | af28637 | 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 | 9fe01af | 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 | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 80 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 81 | // Enter any new nodes |
Matteo Scandolo | af28637 | 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 | fd46858 | 2016-02-16 16:03:43 -0800 | [diff] [blame] | 88 | // create Nodes |
Matteo Scandolo | 79de20a | 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 | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 94 | |
Matteo Scandolo | 388795a | 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 | fd46858 | 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 | af28637 | 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 | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 113 | var nodeExit = node.exit().remove(); |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 116 | /** |
| 117 | * Handle links in the tree layout |
| 118 | */ |
Matteo Scandolo | af28637 | 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 | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 127 | return d.target.d3Id |
Matteo Scandolo | af28637 | 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 | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 142 | |
| 143 | link.exit().remove(); |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Calculate the svg size and setup tree layout |
| 148 | */ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 149 | this.setupTree = (svg) => { |
Matteo Scandolo | af28637 | 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 | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 160 | }; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 161 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Update the tree layout |
| 164 | */ |
| 165 | |
| 166 | this.updateTree = (svg) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 167 | // Compute the new tree layout. |
| 168 | [nodes, links] = computeLayout(baseData); |
| 169 | |
Matteo Scandolo | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 170 | // console.log(baseData); |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 171 | drawNodes(svg, nodes); |
| 172 | drawLinks(svg, links); |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 173 | } |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 174 | |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 175 | }); |
| 176 | |
| 177 | }()); |