Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
| 5 | .factory('d3', function($window){ |
| 6 | return $window.d3; |
| 7 | }) |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 8 | .service('TreeLayout', function($window, $log, lodash, ServiceRelation, serviceTopologyConfig){ |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 9 | |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 10 | const drawLegend = (svg) => { |
| 11 | const legendContainer = svg.append('g') |
| 12 | .attr({ |
| 13 | class: 'legend' |
| 14 | }); |
| 15 | |
| 16 | legendContainer.append('rect') |
| 17 | .attr({ |
| 18 | transform: d => `translate(10, 80)`, |
| 19 | width: 100, |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 20 | height: 100 |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 21 | }); |
| 22 | |
| 23 | // service |
| 24 | const service = legendContainer.append('g') |
| 25 | .attr({ |
| 26 | class: 'node service' |
| 27 | }); |
| 28 | |
| 29 | service.append('circle') |
| 30 | .attr({ |
| 31 | r: serviceTopologyConfig.circle.radius, |
| 32 | transform: d => `translate(30, 100)` |
| 33 | }); |
| 34 | |
| 35 | service.append('text') |
| 36 | .attr({ |
| 37 | transform: d => `translate(45, 100)`, |
| 38 | dy: '.35em' |
| 39 | }) |
| 40 | .text('Service') |
| 41 | .style('fill-opacity', 1); |
| 42 | |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 43 | // slice |
| 44 | const slice = legendContainer.append('g') |
| 45 | .attr({ |
| 46 | class: 'node slice' |
| 47 | }); |
| 48 | |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 49 | slice.append('rect') |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 50 | .attr({ |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 51 | width: 20, |
| 52 | height: 20, |
| 53 | x: -10, |
| 54 | y: -10, |
| 55 | transform: d => `translate(30, 130)` |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 56 | }); |
| 57 | |
| 58 | slice.append('text') |
| 59 | .attr({ |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 60 | transform: d => `translate(45, 130)`, |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 61 | dy: '.35em' |
| 62 | }) |
| 63 | .text('Slices') |
| 64 | .style('fill-opacity', 1); |
| 65 | |
| 66 | // instance |
| 67 | const instance = legendContainer.append('g') |
| 68 | .attr({ |
| 69 | class: 'node instance' |
| 70 | }); |
| 71 | |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 72 | instance.append('rect') |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 73 | .attr({ |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 74 | width: 20, |
| 75 | height: 20, |
| 76 | x: -10, |
| 77 | y: -10, |
| 78 | transform: d => `translate(30, 160)` |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 79 | }); |
| 80 | |
| 81 | instance.append('text') |
| 82 | .attr({ |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 83 | transform: d => `translate(45, 160)`, |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 84 | dy: '.35em' |
| 85 | }) |
| 86 | .text('Instances') |
| 87 | .style('fill-opacity', 1); |
| 88 | }; |
| 89 | |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 90 | var _svg, _layout, _source; |
| 91 | |
| 92 | var i = 0; |
| 93 | |
| 94 | // given a canvas, a layout and a data source, draw a tree layout |
| 95 | const updateTree = (svg, layout, source) => { |
| 96 | |
| 97 | //cache data |
| 98 | _svg = svg; |
| 99 | _layout = layout; |
| 100 | _source = source; |
| 101 | |
| 102 | const maxDepth = ServiceRelation.depthOf(source); |
| 103 | |
| 104 | const diagonal = d3.svg.diagonal() |
| 105 | .projection(d => [d.y, d.x]); |
| 106 | |
| 107 | // Compute the new tree layout. |
| 108 | var nodes = layout.nodes(source).reverse(), |
| 109 | links = layout.links(nodes); |
| 110 | |
| 111 | // Normalize for fixed-depth. |
| 112 | nodes.forEach(function(d) { |
| 113 | // position the child node horizontally |
Matteo Scandolo | 39f4947 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 114 | const step = (($window.innerWidth - (serviceTopologyConfig.widthMargin * 2)) / maxDepth); |
| 115 | d.y = d.depth * step; |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 116 | }); |
| 117 | |
| 118 | // Update the nodes… |
| 119 | var node = svg.selectAll('g.node') |
| 120 | .data(nodes, function(d) { return d.id || (d.id = ++i); }); |
| 121 | |
| 122 | // Enter any new nodes at the parent's previous position. |
| 123 | var nodeEnter = node.enter().append('g') |
| 124 | .attr({ |
| 125 | class: d => `node ${d.type}`, |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 126 | transform: `translate(${source.y0}, ${source.x0})` |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 127 | }); |
Matteo Scandolo | 6398251 | 2016-01-25 17:46:37 -0800 | [diff] [blame] | 128 | |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 129 | const subscriberNodes = nodeEnter.filter('.subscriber'); |
| 130 | const internetNodes = nodeEnter.filter('.internet'); |
| 131 | const serviceNodes = nodeEnter.filter('.service'); |
| 132 | const instanceNodes = nodeEnter.filter('.instance'); |
| 133 | const sliceNodes = nodeEnter.filter('.slice'); |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 134 | |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 135 | subscriberNodes.append('rect') |
| 136 | .attr(serviceTopologyConfig.square); |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 137 | |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 138 | internetNodes.append('rect') |
| 139 | .attr(serviceTopologyConfig.square); |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 140 | |
| 141 | serviceNodes.append('circle') |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 142 | .attr('r', 1e-6) |
| 143 | .style('fill', d => d._children ? 'lightsteelblue' : '#fff') |
| 144 | .on('click', serviceClick); |
| 145 | |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 146 | sliceNodes.append('rect') |
| 147 | .attr({ |
| 148 | width: 20, |
| 149 | height: 20, |
| 150 | x: -10, |
| 151 | y: -10 |
| 152 | }); |
| 153 | |
| 154 | instanceNodes.append('rect') |
| 155 | .attr({ |
| 156 | width: 20, |
| 157 | height: 20, |
| 158 | x: -10, |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 159 | y: -10, |
| 160 | class: d => d.active ?'' : 'active' |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 161 | }); |
| 162 | |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 163 | nodeEnter.append('text') |
Matteo Scandolo | 7dbb191 | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 164 | .attr({ |
| 165 | x: d => d.children ? -serviceTopologyConfig.circle.selectedRadius -3 : serviceTopologyConfig.circle.selectedRadius + 3, |
| 166 | dy: '.35em', |
| 167 | transform: d => { |
| 168 | if (d.children && d.parent){ |
| 169 | if(d.parent.x < d.x){ |
| 170 | return 'rotate(-30)'; |
| 171 | } |
| 172 | return 'rotate(30)'; |
| 173 | } |
| 174 | }, |
| 175 | 'text-anchor': d => d.children ? 'end' : 'start' |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 176 | }) |
Matteo Scandolo | 7dbb191 | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 177 | .text(d => d.name) |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 178 | .style('fill-opacity', 1e-6); |
| 179 | |
| 180 | // Transition nodes to their new position. |
| 181 | var nodeUpdate = node.transition() |
| 182 | .duration(serviceTopologyConfig.duration) |
Matteo Scandolo | 7dbb191 | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 183 | .attr({ |
| 184 | 'transform': d => `translate(${d.y},${d.x})` |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 185 | }); |
| 186 | |
| 187 | nodeUpdate.select('circle') |
Matteo Scandolo | 7dbb191 | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 188 | .attr('r', d => d.selected ? serviceTopologyConfig.circle.selectedRadius : serviceTopologyConfig.circle.radius) |
| 189 | .style('fill', d => d.selected ? 'lightsteelblue' : '#fff'); |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 190 | |
| 191 | nodeUpdate.select('text') |
| 192 | .style('fill-opacity', 1); |
| 193 | |
| 194 | // Transition exiting nodes to the parent's new position. |
| 195 | var nodeExit = node.exit().transition() |
| 196 | .duration(serviceTopologyConfig.duration) |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 197 | .remove(); |
| 198 | |
| 199 | nodeExit.select('circle') |
| 200 | .attr('r', 1e-6); |
| 201 | |
| 202 | nodeExit.select('text') |
| 203 | .style('fill-opacity', 1e-6); |
| 204 | |
| 205 | // Update the links… |
| 206 | var link = svg.selectAll('path.link') |
| 207 | .data(links, function(d) { return d.target.id; }); |
| 208 | |
| 209 | // Enter any new links at the parent's previous position. |
| 210 | link.enter().insert('path', 'g') |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 211 | .attr('class', d => `link ${d.target.type} ${d.target.active ? '' : 'active'}`) |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 212 | .attr('d', function(d) { |
| 213 | var o = {x: source.x0, y: source.y0}; |
| 214 | return diagonal({source: o, target: o}); |
| 215 | }); |
| 216 | |
| 217 | // Transition links to their new position. |
| 218 | link.transition() |
| 219 | .duration(serviceTopologyConfig.duration) |
| 220 | .attr('d', diagonal); |
| 221 | |
| 222 | // Transition exiting nodes to the parent's new position. |
| 223 | link.exit().transition() |
| 224 | .duration(serviceTopologyConfig.duration) |
| 225 | .attr('d', function(d) { |
| 226 | var o = {x: source.x, y: source.y}; |
| 227 | return diagonal({source: o, target: o}); |
| 228 | }) |
| 229 | .remove(); |
| 230 | |
| 231 | // Stash the old positions for transition. |
| 232 | nodes.forEach(function(d) { |
| 233 | d.x0 = d.x; |
| 234 | d.y0 = d.y; |
| 235 | }); |
| 236 | }; |
| 237 | |
| 238 | const serviceClick = function(d) { |
| 239 | |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 240 | $log.info('TODO emit an event to highlight VMs'); |
| 241 | |
Matteo Scandolo | 7dbb191 | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 242 | if(!d.service){ |
| 243 | return; |
| 244 | } |
| 245 | |
Matteo Scandolo | 39f4947 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 246 | // toggling selected status |
| 247 | d.selected = !d.selected; |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 248 | |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 249 | var selectedNode = d3.select(this); |
| 250 | |
| 251 | selectedNode |
| 252 | .transition() |
| 253 | .duration(serviceTopologyConfig.duration) |
Matteo Scandolo | 7dbb191 | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 254 | .attr('r', serviceTopologyConfig.circle.selectedRadius); |
Matteo Scandolo | 4889f5a | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | this.updateTree = updateTree; |
Matteo Scandolo | 65d6fc4 | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 258 | this.drawLegend = drawLegend; |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 259 | }); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 260 | |
| 261 | }()); |