blob: 27556204c48ddc27fc65bc2305d892f3865dd6a8 [file] [log] [blame]
Matteo Scandolofcdbed32016-02-08 16:55:44 -08001(function () {
2 'use strict';
3
Matteo Scandolo04564952016-02-24 11:22:48 -08004 angular.module('xos.diagnostic')
Matteo Scandolofcdbed32016-02-08 16:55:44 -08005 .directive('serviceTopology', function(){
6 return {
7 restrict: 'E',
8 scope: {
9 serviceChain: '='
10 },
11 bindToController: true,
12 controllerAs: 'vm',
13 template: '',
Matteo Scandolo219b1a72016-02-09 11:19:22 -080014 controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, ServiceTopologyHelper){
Matteo Scandolofcdbed32016-02-08 16:55:44 -080015
16 const el = $element[0];
17
Matteo Scandolo7547f042016-02-09 09:13:30 -080018 d3.select(window)
19 .on('resize', () => {
Matteo Scandolo7547f042016-02-09 09:13:30 -080020 draw(this.serviceChain);
21 });
Matteo Scandolofcdbed32016-02-08 16:55:44 -080022
Matteo Scandolo7547f042016-02-09 09:13:30 -080023 var root, svg;
Matteo Scandolofcdbed32016-02-08 16:55:44 -080024
25 const draw = (tree) => {
Matteo Scandolo7547f042016-02-09 09:13:30 -080026
Matteo Scandolo70ac2162016-02-24 15:40:22 -080027 if(!tree){
28 console.error('Tree is missing');
29 return;
30 }
31
Matteo Scandolo219b1a72016-02-09 11:19:22 -080032 // TODO update instead clear and redraw
33
Matteo Scandolo7547f042016-02-09 09:13:30 -080034 // clean
35 d3.select($element[0]).select('svg').remove();
36
37 const width = el.clientWidth - (serviceTopologyConfig.widthMargin * 2);
38 const height = el.clientHeight - (serviceTopologyConfig.heightMargin * 2);
39
40 const treeLayout = d3.layout.tree()
41 .size([height, width]);
42
43 svg = d3.select($element[0])
44 .append('svg')
45 .style('width', `${el.clientWidth}px`)
46 .style('height', `${el.clientHeight}px`)
47
48 const treeContainer = svg.append('g')
49 .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 4},${serviceTopologyConfig.heightMargin})`);
50
Matteo Scandolofcdbed32016-02-08 16:55:44 -080051 root = tree;
52 root.x0 = height / 2;
53 root.y0 = width / 2;
54
Matteo Scandoloaf286372016-02-09 14:46:14 -080055 // ServiceTopologyHelper.drawLegend(svg);
Matteo Scandolo219b1a72016-02-09 11:19:22 -080056 ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root);
Matteo Scandolofcdbed32016-02-08 16:55:44 -080057 };
Matteo Scandolofcdbed32016-02-08 16:55:44 -080058
59 $scope.$watch(() => this.serviceChain, (chain) => {
Matteo Scandolo70ac2162016-02-24 15:40:22 -080060 if(angular.isDefined(chain)){
Matteo Scandolofcdbed32016-02-08 16:55:44 -080061 draw(chain);
62 }
63 });
64 }
65 }
66 });
67
68}());