blob: 3d602feaca6c90d49f9e1e04a4e429eda6a123ed [file] [log] [blame]
Matteo Scandolofcdbed32016-02-08 16:55:44 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
5 .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 Scandolo219b1a72016-02-09 11:19:22 -080027 // TODO update instead clear and redraw
28
Matteo Scandolo7547f042016-02-09 09:13:30 -080029 // clean
30 d3.select($element[0]).select('svg').remove();
31
32 const width = el.clientWidth - (serviceTopologyConfig.widthMargin * 2);
33 const height = el.clientHeight - (serviceTopologyConfig.heightMargin * 2);
34
Matteo Scandolo07246ca2016-02-22 09:16:46 -080035 console.log(el.clientWidth, el.clientHeight);
36
Matteo Scandolo7547f042016-02-09 09:13:30 -080037 const treeLayout = d3.layout.tree()
38 .size([height, width]);
39
40 svg = d3.select($element[0])
41 .append('svg')
42 .style('width', `${el.clientWidth}px`)
43 .style('height', `${el.clientHeight}px`)
44
45 const treeContainer = svg.append('g')
46 .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 4},${serviceTopologyConfig.heightMargin})`);
47
Matteo Scandolofcdbed32016-02-08 16:55:44 -080048 root = tree;
49 root.x0 = height / 2;
50 root.y0 = width / 2;
51
Matteo Scandoloaf286372016-02-09 14:46:14 -080052 // ServiceTopologyHelper.drawLegend(svg);
Matteo Scandolo219b1a72016-02-09 11:19:22 -080053 ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root);
Matteo Scandolofcdbed32016-02-08 16:55:44 -080054 };
55
Matteo Scandolofcdbed32016-02-08 16:55:44 -080056 this.getInstances = (slice) => {
57 Instances.query({slice: slice.id}).$promise
58 .then((instances) => {
59 this.selectedSlice = slice;
60 this.instances = instances;
61 })
Matteo Scandolo51031482016-02-17 13:54:11 -080062 .catch(e => {
63 this.errors = e;
64 throw new Error(e);
65 })
Matteo Scandolofcdbed32016-02-08 16:55:44 -080066 };
67
68 $scope.$watch(() => this.serviceChain, (chain) => {
69 if(chain){
70 draw(chain);
71 }
72 });
73 }
74 }
75 });
76
77}());