Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -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 | da19dff | 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 | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 14 | controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, ServiceTopologyHelper){ |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 15 | |
| 16 | const el = $element[0]; |
| 17 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 18 | d3.select(window) |
Matteo Scandolo | d54a7fd | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 19 | .on('resize.service', () => { |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 20 | draw(this.serviceChain); |
| 21 | }); |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 23 | var root, svg; |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 24 | |
| 25 | const draw = (tree) => { |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 26 | |
Matteo Scandolo | 16763e9 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 27 | if(!tree){ |
| 28 | console.error('Tree is missing'); |
| 29 | return; |
| 30 | } |
| 31 | |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 32 | // TODO update instead clear and redraw |
| 33 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 34 | // clean |
| 35 | d3.select($element[0]).select('svg').remove(); |
| 36 | |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 37 | |
Matteo Scandolo | 735606c | 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') |
Matteo Scandolo | d54a7fd | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 50 | .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 2},${serviceTopologyConfig.heightMargin})`); |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 51 | |
Matteo Scandolo | da19dff | 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 | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 56 | // ServiceTopologyHelper.drawLegend(svg); |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 57 | ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root, el); |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 58 | }; |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 59 | |
| 60 | $scope.$watch(() => this.serviceChain, (chain) => { |
Matteo Scandolo | 16763e9 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 61 | if(angular.isDefined(chain)){ |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 62 | draw(chain); |
| 63 | } |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | }()); |