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 | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 5 | .service('ServiceTopologyHelper', function($rootScope, $window, $log, lodash, ServiceRelation, serviceTopologyConfig, d3){ |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 6 | |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 7 | var _svg, _layout, _source, _el; |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 8 | |
| 9 | var i = 0; |
| 10 | |
| 11 | // given a canvas, a layout and a data source, draw a tree layout |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 12 | const updateTree = (svg, layout, source, el = _el) => { |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 13 | if(el){ |
| 14 | _el = el; |
| 15 | } |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 16 | |
| 17 | let targetWidth = _el.clientWidth - serviceTopologyConfig.widthMargin * 2; |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 18 | |
| 19 | //cache data |
| 20 | _svg = svg; |
| 21 | _layout = layout; |
| 22 | _source = source; |
| 23 | |
| 24 | const maxDepth = ServiceRelation.depthOf(source); |
| 25 | |
| 26 | const diagonal = d3.svg.diagonal() |
| 27 | .projection(d => [d.y, d.x]); |
| 28 | |
| 29 | // Compute the new tree layout. |
| 30 | var nodes = layout.nodes(source).reverse(), |
| 31 | links = layout.links(nodes); |
| 32 | |
| 33 | // Normalize for fixed-depth. |
| 34 | nodes.forEach(function(d) { |
| 35 | // position the child node horizontally |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 36 | const step = ((targetWidth - (serviceTopologyConfig.widthMargin * 2)) / (maxDepth - 1)); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 37 | d.y = d.depth * step; |
| 38 | }); |
| 39 | |
| 40 | // Update the nodes… |
| 41 | var node = svg.selectAll('g.node') |
| 42 | .data(nodes, function(d) { return d.id || (d.id = ++i); }); |
| 43 | |
| 44 | // Enter any new nodes at the parent's previous position. |
| 45 | var nodeEnter = node.enter().append('g') |
| 46 | .attr({ |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 47 | class: d => { |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 48 | return `node ${d.type}` |
| 49 | }, |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 50 | transform: d => (d.x && d.y) ? `translate(${d.y}, ${d.x})` : `translate(${source.y0}, ${source.x0})` |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | const subscriberNodes = nodeEnter.filter('.subscriber'); |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 54 | const internetNodes = nodeEnter.filter('.router'); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 55 | const serviceNodes = nodeEnter.filter('.service'); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 56 | |
| 57 | subscriberNodes.append('rect') |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 58 | .attr(serviceTopologyConfig.square) |
| 59 | // add event listener to subscriber |
| 60 | .on('click', () => { |
| 61 | $rootScope.$emit('subscriber.modal.open'); |
| 62 | }); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 63 | |
| 64 | internetNodes.append('rect') |
| 65 | .attr(serviceTopologyConfig.square); |
| 66 | |
| 67 | serviceNodes.append('circle') |
| 68 | .attr('r', 1e-6) |
| 69 | .style('fill', d => d._children ? 'lightsteelblue' : '#fff') |
| 70 | .on('click', serviceClick); |
| 71 | |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 72 | nodeEnter.append('text') |
| 73 | .attr({ |
Matteo Scandolo | f81130f | 2016-03-11 11:16:58 -0800 | [diff] [blame] | 74 | x: d => d.children ? -serviceTopologyConfig.circle.selectedRadius -5 : serviceTopologyConfig.circle.selectedRadius + 5, |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 75 | dy: '.35em', |
Matteo Scandolo | f81130f | 2016-03-11 11:16:58 -0800 | [diff] [blame] | 76 | y: d => { |
| 77 | if (d.children && d.parent){ |
| 78 | return '-5'; |
| 79 | } |
| 80 | }, |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 81 | transform: d => { |
| 82 | if (d.children && d.parent){ |
| 83 | if(d.parent.x < d.x){ |
| 84 | return 'rotate(-30)'; |
| 85 | } |
| 86 | return 'rotate(30)'; |
| 87 | } |
| 88 | }, |
| 89 | 'text-anchor': d => d.children ? 'end' : 'start' |
| 90 | }) |
| 91 | .text(d => d.name) |
| 92 | .style('fill-opacity', 1e-6); |
| 93 | |
| 94 | // Transition nodes to their new position. |
| 95 | var nodeUpdate = node.transition() |
| 96 | .duration(serviceTopologyConfig.duration) |
| 97 | .attr({ |
| 98 | 'transform': d => `translate(${d.y},${d.x})` |
| 99 | }); |
| 100 | |
| 101 | nodeUpdate.select('circle') |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 102 | .attr('r', d => { |
| 103 | return d.selected ? serviceTopologyConfig.circle.selectedRadius : serviceTopologyConfig.circle.radius |
| 104 | }) |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 105 | .style('fill', d => d.selected ? 'lightsteelblue' : '#fff'); |
| 106 | |
| 107 | nodeUpdate.select('text') |
| 108 | .style('fill-opacity', 1); |
| 109 | |
| 110 | // Transition exiting nodes to the parent's new position. |
| 111 | var nodeExit = node.exit().transition() |
| 112 | .duration(serviceTopologyConfig.duration) |
| 113 | .remove(); |
| 114 | |
| 115 | nodeExit.select('circle') |
| 116 | .attr('r', 1e-6); |
| 117 | |
| 118 | nodeExit.select('text') |
| 119 | .style('fill-opacity', 1e-6); |
| 120 | |
| 121 | // Update the links… |
| 122 | var link = svg.selectAll('path.link') |
| 123 | .data(links, function(d) { return d.target.id; }); |
| 124 | |
| 125 | // Enter any new links at the parent's previous position. |
| 126 | link.enter().insert('path', 'g') |
| 127 | .attr('class', d => `link ${d.target.type} ${d.target.active ? '' : 'active'}`) |
| 128 | .attr('d', function(d) { |
| 129 | var o = {x: source.x0, y: source.y0}; |
| 130 | return diagonal({source: o, target: o}); |
| 131 | }); |
| 132 | |
| 133 | // Transition links to their new position. |
| 134 | link.transition() |
| 135 | .duration(serviceTopologyConfig.duration) |
| 136 | .attr('d', diagonal); |
| 137 | |
| 138 | // Transition exiting nodes to the parent's new position. |
| 139 | link.exit().transition() |
| 140 | .duration(serviceTopologyConfig.duration) |
| 141 | .attr('d', function(d) { |
| 142 | var o = {x: source.x, y: source.y}; |
| 143 | return diagonal({source: o, target: o}); |
| 144 | }) |
| 145 | .remove(); |
| 146 | |
| 147 | // Stash the old positions for transition. |
| 148 | nodes.forEach(function(d) { |
| 149 | d.x0 = d.x; |
| 150 | d.y0 = d.y; |
| 151 | }); |
| 152 | }; |
| 153 | |
| 154 | const serviceClick = function(d) { |
| 155 | |
Matteo Scandolo | c49ff70 | 2016-02-17 15:11:33 -0800 | [diff] [blame] | 156 | // if was selected |
| 157 | if(d.selected){ |
| 158 | d.selected = !d.selected; |
| 159 | $rootScope.$emit('instance.detail.hide', {}); |
| 160 | return updateTree(_svg, _layout, _source); |
| 161 | } |
Matteo Scandolo | 45fba73 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 162 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 163 | $rootScope.$emit('instance.detail', {name: d.name, service: d.service, tenant: d.tenant}); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 164 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 165 | // unselect all |
| 166 | _svg.selectAll('circle') |
| 167 | .each(d => d.selected = false); |
| 168 | |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 169 | // toggling selected status |
| 170 | d.selected = !d.selected; |
| 171 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 172 | updateTree(_svg, _layout, _source); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | this.updateTree = updateTree; |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 176 | }); |
| 177 | |
| 178 | }()); |