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