Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
| 5 | .directive('serviceCanvas', function(){ |
| 6 | return { |
| 7 | restrict: 'E', |
| 8 | scope: {}, |
| 9 | bindToController: true, |
| 10 | controllerAs: 'vm', |
| 11 | templateUrl: 'templates/topology_canvas.tpl.html', |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame^] | 12 | controller: function($element, $window, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers){ |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 13 | |
Matteo Scandolo | 793e1dd | 2016-01-22 09:33:26 -0800 | [diff] [blame] | 14 | this.instances = []; |
| 15 | this.slices = []; |
| 16 | |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 17 | // count the mas depth of an object |
| 18 | const depthOf = (obj) => { |
| 19 | var depth = 0; |
| 20 | if (obj.children) { |
| 21 | obj.children.forEach(function (d) { |
| 22 | var tmpDepth = depthOf(d); |
| 23 | if (tmpDepth > depth) { |
| 24 | depth = tmpDepth |
| 25 | } |
| 26 | }) |
| 27 | } |
| 28 | return 1 + depth |
| 29 | }; |
| 30 | |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 31 | const width = $window.innerWidth - serviceTopologyConfig.widthMargin; |
| 32 | const height = $window.innerHeight - serviceTopologyConfig.heightMargin; |
| 33 | |
| 34 | const tree = d3.layout.tree() |
| 35 | .size([height, width]); |
| 36 | |
| 37 | const diagonal = d3.svg.diagonal() |
| 38 | .projection(d => [d.y, d.x]); |
| 39 | |
| 40 | const svg = d3.select($element[0]) |
| 41 | .append('svg') |
| 42 | .style('width', `${$window.innerWidth}px`) |
| 43 | .style('height', `${$window.innerHeight}px`) |
| 44 | .append('g') |
| 45 | .attr("transform", "translate(" + serviceTopologyConfig.widthMargin+ "," + serviceTopologyConfig.heightMargin + ")");; |
| 46 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 47 | //const resizeCanvas = () => { |
| 48 | // var targetSize = svg.node().getBoundingClientRect(); |
| 49 | // |
| 50 | // d3.select(self.frameElement) |
| 51 | // .attr('width', `${targetSize.width}px`) |
| 52 | // .attr('height', `${targetSize.height}px`) |
| 53 | //}; |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 54 | //d3.select(window) |
| 55 | // .on('load', () => { |
| 56 | // resizeCanvas(); |
| 57 | // }); |
| 58 | //d3.select(window) |
| 59 | // .on('resize', () => { |
| 60 | // resizeCanvas(); |
| 61 | // update(root); |
| 62 | // }); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 63 | var root; |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 64 | var i = 0; |
| 65 | var duration = 750; |
| 66 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 67 | const draw = (tree) => { |
| 68 | root = tree; |
| 69 | root.x0 = $window.innerHeight / 2; |
| 70 | root.y0 = 0; |
| 71 | |
| 72 | update(root); |
| 73 | }; |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 74 | |
| 75 | function update(source) { |
| 76 | |
| 77 | const maxDepth = depthOf(source); |
| 78 | |
| 79 | // Compute the new tree layout. |
| 80 | var nodes = tree.nodes(root).reverse(), |
| 81 | links = tree.links(nodes); |
| 82 | |
| 83 | // Normalize for fixed-depth. |
| 84 | nodes.forEach(function(d) { |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 85 | // position the child node horizontally |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 86 | d.y = d.depth * (($window.innerWidth - (serviceTopologyConfig.widthMargin * 2)) / maxDepth); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 87 | }); |
| 88 | |
| 89 | // Update the nodes… |
| 90 | var node = svg.selectAll('g.node') |
| 91 | .data(nodes, function(d) { return d.id || (d.id = ++i); }); |
| 92 | |
| 93 | // Enter any new nodes at the parent's previous position. |
| 94 | var nodeEnter = node.enter().append('g') |
| 95 | .attr('class', 'node') |
| 96 | .attr('transform', function(d) { |
| 97 | // this is the starting position |
| 98 | return 'translate(' + source.y0 + ',' + source.x0 + ')'; |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 99 | }); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 100 | |
| 101 | nodeEnter.append('circle') |
| 102 | .attr('r', 1e-6) |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 103 | .style('fill', function(d) { return d._children ? 'lightsteelblue' : '#fff'; }) |
| 104 | .on('click', click); |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 105 | |
| 106 | nodeEnter.append('text') |
| 107 | .attr('x', function(d) { return d.children || d._children ? -13 : 13; }) |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 108 | .attr('transform', function(d) { |
| 109 | if((d.children || d._children) && d.parent || d._parent){ |
| 110 | return 'rotate(30)'; |
| 111 | } |
| 112 | return; |
| 113 | }) |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 114 | .attr('dy', '.35em') |
| 115 | .attr('text-anchor', function(d) { return d.children || d._children ? 'end' : 'start'; }) |
| 116 | .text(function(d) { return d.name; }) |
| 117 | .style('fill-opacity', 1e-6); |
| 118 | |
| 119 | // Transition nodes to their new position. |
| 120 | var nodeUpdate = node.transition() |
| 121 | .duration(duration) |
| 122 | .attr('transform', function(d) { |
| 123 | return 'translate(' + d.y + ',' + d.x + ')'; |
| 124 | }); |
| 125 | |
| 126 | nodeUpdate.select('circle') |
| 127 | .attr('r', 10) |
| 128 | .style('fill', function(d) { return d._children ? 'lightsteelblue' : '#fff'; }); |
| 129 | |
| 130 | nodeUpdate.select('text') |
| 131 | .style('fill-opacity', 1); |
| 132 | |
| 133 | // Transition exiting nodes to the parent's new position. |
| 134 | var nodeExit = node.exit().transition() |
| 135 | .duration(duration) |
| 136 | .attr('transform', function(d) { return 'translate(' + source.y + ',' + source.x + ')'; }) |
| 137 | .remove(); |
| 138 | |
| 139 | nodeExit.select('circle') |
| 140 | .attr('r', 1e-6); |
| 141 | |
| 142 | nodeExit.select('text') |
| 143 | .style('fill-opacity', 1e-6); |
| 144 | |
| 145 | // Update the links… |
| 146 | var link = svg.selectAll('path.link') |
| 147 | .data(links, function(d) { return d.target.id; }); |
| 148 | |
| 149 | // Enter any new links at the parent's previous position. |
| 150 | link.enter().insert('path', 'g') |
| 151 | .attr('class', 'link') |
| 152 | .attr('d', function(d) { |
| 153 | var o = {x: source.x0, y: source.y0}; |
| 154 | return diagonal({source: o, target: o}); |
| 155 | }); |
| 156 | |
| 157 | // Transition links to their new position. |
| 158 | link.transition() |
| 159 | .duration(duration) |
| 160 | .attr('d', diagonal); |
| 161 | |
| 162 | // Transition exiting nodes to the parent's new position. |
| 163 | link.exit().transition() |
| 164 | .duration(duration) |
| 165 | .attr('d', function(d) { |
| 166 | var o = {x: source.x, y: source.y}; |
| 167 | return diagonal({source: o, target: o}); |
| 168 | }) |
| 169 | .remove(); |
| 170 | |
| 171 | // Stash the old positions for transition. |
| 172 | nodes.forEach(function(d) { |
| 173 | d.x0 = d.x; |
| 174 | d.y0 = d.y; |
| 175 | }); |
| 176 | } |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 177 | |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 178 | var _this = this; |
| 179 | const click = function(d) { |
| 180 | |
Matteo Scandolo | 793e1dd | 2016-01-22 09:33:26 -0800 | [diff] [blame] | 181 | // empty panel |
| 182 | _this.slices = []; |
| 183 | _this.instances = []; |
| 184 | |
| 185 | var nodes = d3.selectAll('circle') |
| 186 | .transition() |
| 187 | .duration(duration) |
| 188 | .attr('r', 10); |
| 189 | |
| 190 | d3.selectAll('rect.slice-detail') |
| 191 | .remove(); |
| 192 | |
| 193 | d3.selectAll('text.slice-name') |
| 194 | .remove(); |
| 195 | |
| 196 | var selectedNode = d3.select(this); |
| 197 | |
| 198 | selectedNode |
| 199 | .transition() |
| 200 | .duration(duration) |
| 201 | .attr('r', 30); |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 202 | |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame^] | 203 | if(!d.service){ |
| 204 | return; |
| 205 | } |
| 206 | |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 207 | _this.selectedService = { |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 208 | id: d.id, |
| 209 | name: d.name |
| 210 | }; |
Matteo Scandolo | 793e1dd | 2016-01-22 09:33:26 -0800 | [diff] [blame] | 211 | |
Matteo Scandolo | 148f22b | 2016-01-22 13:17:33 -0800 | [diff] [blame] | 212 | Slice.query({service: d.service.id}).$promise |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 213 | .then(slices => { |
Matteo Scandolo | 53a0226 | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 214 | _this.slices = slices; |
Matteo Scandolo | 793e1dd | 2016-01-22 09:33:26 -0800 | [diff] [blame] | 215 | |
| 216 | if(slices.length > 0){ |
| 217 | const parentNode = d3.select(this.parentNode); |
| 218 | |
| 219 | parentNode |
| 220 | .append('rect') |
| 221 | .style('opacity', 0) |
| 222 | .attr({ |
| 223 | width: 150, |
| 224 | height: 50, |
| 225 | y: 35, |
| 226 | x: -75, |
| 227 | class: 'slice-detail' |
| 228 | }) |
| 229 | .transition() |
| 230 | .duration(duration) |
| 231 | .style('opacity', 1); |
| 232 | |
| 233 | parentNode |
| 234 | .append('text') |
| 235 | .style('opacity', 0) |
| 236 | .attr({ |
| 237 | y: 65, |
| 238 | x: -60, |
| 239 | class: 'slice-name' |
| 240 | }) |
| 241 | .text(() => { |
| 242 | if(slices[0]){ |
Matteo Scandolo | 793e1dd | 2016-01-22 09:33:26 -0800 | [diff] [blame] | 243 | return slices[0].humanReadableName; |
| 244 | } |
| 245 | |
| 246 | return ''; |
| 247 | }) |
| 248 | .transition() |
| 249 | .duration(duration) |
| 250 | .style('opacity', 1); |
| 251 | } |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 252 | }) |
| 253 | }; |
| 254 | |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame^] | 255 | Subscribers.query().$promise |
| 256 | .then((subscribers) => { |
| 257 | this.subscribers = subscribers; |
| 258 | return ServiceRelation.get(subscribers[0]) |
| 259 | }) |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 260 | .then((tree) => { |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 261 | draw(tree); |
| 262 | }); |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 263 | |
| 264 | this.getInstances = (slice) => { |
| 265 | Instances.query({slice: slice.id}).$promise |
| 266 | .then((instances) => { |
| 267 | this.selectedSlice = slice; |
| 268 | this.instances = instances; |
| 269 | }) |
| 270 | }; |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | }); |
| 274 | |
| 275 | }()); |