Matteo Scandolo | 8a64fa4 | 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 | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 8 | .service('TreeLayout', function($window, lodash, ServiceRelation, serviceTopologyConfig, Slice, Instances){ |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 9 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 10 | var _svg, _layout, _source; |
| 11 | |
| 12 | var i = 0; |
| 13 | |
| 14 | // given a canvas, a layout and a data source, draw a tree layout |
| 15 | const updateTree = (svg, layout, source) => { |
| 16 | |
| 17 | //cache data |
| 18 | _svg = svg; |
| 19 | _layout = layout; |
| 20 | _source = source; |
| 21 | |
| 22 | const maxDepth = ServiceRelation.depthOf(source); |
| 23 | |
| 24 | const diagonal = d3.svg.diagonal() |
| 25 | .projection(d => [d.y, d.x]); |
| 26 | |
| 27 | // Compute the new tree layout. |
| 28 | var nodes = layout.nodes(source).reverse(), |
| 29 | links = layout.links(nodes); |
| 30 | |
| 31 | // Normalize for fixed-depth. |
| 32 | nodes.forEach(function(d) { |
| 33 | // position the child node horizontally |
Matteo Scandolo | 9ef3c84 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 34 | const step = (($window.innerWidth - (serviceTopologyConfig.widthMargin * 2)) / maxDepth); |
| 35 | d.y = d.depth * step; |
| 36 | if(d.type === 'slice' || d.type === 'instance'){ |
| 37 | d.y = d.depth * step - (step / 2); |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 38 | } |
| 39 | }); |
| 40 | |
| 41 | // Update the nodes… |
| 42 | var node = svg.selectAll('g.node') |
| 43 | .data(nodes, function(d) { return d.id || (d.id = ++i); }); |
| 44 | |
| 45 | // Enter any new nodes at the parent's previous position. |
| 46 | var nodeEnter = node.enter().append('g') |
| 47 | .attr({ |
| 48 | class: d => `node ${d.type}`, |
| 49 | transform: d => `translate(${source.y0},${source.x0})` // this is the starting position |
| 50 | }); |
| 51 | |
Matteo Scandolo | 9ef3c84 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 52 | // TODO append different shapes base on type |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 53 | nodeEnter.append('circle') |
| 54 | .attr('r', 1e-6) |
| 55 | .style('fill', d => d._children ? 'lightsteelblue' : '#fff') |
| 56 | .on('click', serviceClick); |
| 57 | |
| 58 | nodeEnter.append('text') |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 59 | .attr({ |
| 60 | x: d => d.children ? -serviceTopologyConfig.circle.selectedRadius -3 : serviceTopologyConfig.circle.selectedRadius + 3, |
| 61 | dy: '.35em', |
| 62 | transform: d => { |
| 63 | if (d.children && d.parent){ |
| 64 | if(d.parent.x < d.x){ |
| 65 | return 'rotate(-30)'; |
| 66 | } |
| 67 | return 'rotate(30)'; |
| 68 | } |
| 69 | }, |
| 70 | 'text-anchor': d => d.children ? 'end' : 'start' |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 71 | }) |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 72 | .text(d => d.name) |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 73 | .style('fill-opacity', 1e-6); |
| 74 | |
| 75 | // Transition nodes to their new position. |
| 76 | var nodeUpdate = node.transition() |
| 77 | .duration(serviceTopologyConfig.duration) |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 78 | .attr({ |
| 79 | 'transform': d => `translate(${d.y},${d.x})` |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 80 | }); |
| 81 | |
| 82 | nodeUpdate.select('circle') |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 83 | .attr('r', d => d.selected ? serviceTopologyConfig.circle.selectedRadius : serviceTopologyConfig.circle.radius) |
| 84 | .style('fill', d => d.selected ? 'lightsteelblue' : '#fff'); |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 85 | |
| 86 | nodeUpdate.select('text') |
| 87 | .style('fill-opacity', 1); |
| 88 | |
| 89 | // Transition exiting nodes to the parent's new position. |
| 90 | var nodeExit = node.exit().transition() |
| 91 | .duration(serviceTopologyConfig.duration) |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 92 | .attr({ |
| 93 | 'transform': d => `translate(${source.y},${source.x})` |
| 94 | }) |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 95 | .remove(); |
| 96 | |
| 97 | nodeExit.select('circle') |
| 98 | .attr('r', 1e-6); |
| 99 | |
| 100 | nodeExit.select('text') |
| 101 | .style('fill-opacity', 1e-6); |
| 102 | |
| 103 | // Update the links… |
| 104 | var link = svg.selectAll('path.link') |
| 105 | .data(links, function(d) { return d.target.id; }); |
| 106 | |
| 107 | // Enter any new links at the parent's previous position. |
| 108 | link.enter().insert('path', 'g') |
Matteo Scandolo | 9ef3c84 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 109 | .attr('class', d => `link ${d.target.type}`) |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 110 | .attr('d', function(d) { |
| 111 | var o = {x: source.x0, y: source.y0}; |
| 112 | return diagonal({source: o, target: o}); |
| 113 | }); |
| 114 | |
| 115 | // Transition links to their new position. |
| 116 | link.transition() |
| 117 | .duration(serviceTopologyConfig.duration) |
| 118 | .attr('d', diagonal); |
| 119 | |
| 120 | // Transition exiting nodes to the parent's new position. |
| 121 | link.exit().transition() |
| 122 | .duration(serviceTopologyConfig.duration) |
| 123 | .attr('d', function(d) { |
| 124 | var o = {x: source.x, y: source.y}; |
| 125 | return diagonal({source: o, target: o}); |
| 126 | }) |
| 127 | .remove(); |
| 128 | |
| 129 | // Stash the old positions for transition. |
| 130 | nodes.forEach(function(d) { |
| 131 | d.x0 = d.x; |
| 132 | d.y0 = d.y; |
| 133 | }); |
| 134 | }; |
| 135 | |
| 136 | const serviceClick = function(d) { |
| 137 | |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 138 | if(!d.service){ |
| 139 | return; |
| 140 | } |
| 141 | |
Matteo Scandolo | 9ef3c84 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 142 | // toggling selected status |
| 143 | d.selected = !d.selected; |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 144 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 145 | var selectedNode = d3.select(this); |
| 146 | |
| 147 | selectedNode |
| 148 | .transition() |
| 149 | .duration(serviceTopologyConfig.duration) |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame^] | 150 | .attr('r', serviceTopologyConfig.circle.selectedRadius); |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 151 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 152 | ServiceRelation.getServiceInterfaces(d.service.id) |
| 153 | .then(interfaceTree => { |
| 154 | |
| 155 | const isDetailed = lodash.find(d.children, {type: 'slice'}); |
| 156 | if(isDetailed){ |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 157 | lodash.remove(d.children, {type: 'slice'}); |
| 158 | } |
| 159 | else { |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 160 | d.children = d.children.concat(interfaceTree); |
| 161 | } |
| 162 | |
| 163 | updateTree(_svg, _layout, _source); |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 164 | }); |
| 165 | }; |
| 166 | |
| 167 | this.updateTree = updateTree; |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 168 | }); |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 169 | |
| 170 | }()); |