Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -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 | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 5 | .directive('serviceTopology', function(){ |
| 6 | return { |
| 7 | restrict: 'E', |
| 8 | scope: { |
| 9 | serviceChain: '=' |
| 10 | }, |
| 11 | bindToController: true, |
| 12 | controllerAs: 'vm', |
| 13 | template: '', |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 14 | controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, ServiceTopologyHelper){ |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 15 | |
| 16 | const el = $element[0]; |
| 17 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 18 | d3.select(window) |
| 19 | .on('resize', () => { |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 20 | draw(this.serviceChain); |
| 21 | }); |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 23 | var root, svg; |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 24 | |
| 25 | const draw = (tree) => { |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 26 | |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 27 | if(!tree){ |
| 28 | console.error('Tree is missing'); |
| 29 | return; |
| 30 | } |
| 31 | |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 32 | // TODO update instead clear and redraw |
| 33 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 34 | // clean |
| 35 | d3.select($element[0]).select('svg').remove(); |
| 36 | |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 37 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 38 | const width = el.clientWidth - (serviceTopologyConfig.widthMargin * 2); |
| 39 | const height = el.clientHeight - (serviceTopologyConfig.heightMargin * 2); |
| 40 | |
| 41 | const treeLayout = d3.layout.tree() |
| 42 | .size([height, width]); |
| 43 | |
| 44 | svg = d3.select($element[0]) |
| 45 | .append('svg') |
| 46 | .style('width', `${el.clientWidth}px`) |
| 47 | .style('height', `${el.clientHeight}px`) |
| 48 | |
| 49 | const treeContainer = svg.append('g') |
| 50 | .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 4},${serviceTopologyConfig.heightMargin})`); |
| 51 | |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 52 | root = tree; |
| 53 | root.x0 = height / 2; |
| 54 | root.y0 = width / 2; |
| 55 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 56 | // ServiceTopologyHelper.drawLegend(svg); |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 57 | ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root, el); |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 58 | }; |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 59 | |
| 60 | $scope.$watch(() => this.serviceChain, (chain) => { |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 61 | if(angular.isDefined(chain)){ |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 62 | draw(chain); |
| 63 | } |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | }()); |