blob: b358abfda25b5e3895537e74a9a2264b1fbe9ed1 [file] [log] [blame]
Matteo Scandoloda19dff2016-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 Scandoloeeb9c082016-02-09 11:19:22 -080014 controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, ServiceTopologyHelper){
Matteo Scandoloda19dff2016-02-08 16:55:44 -080015
16 const el = $element[0];
17
Matteo Scandolo735606c2016-02-09 09:13:30 -080018 d3.select(window)
19 .on('resize', () => {
Matteo Scandolo735606c2016-02-09 09:13:30 -080020 draw(this.serviceChain);
21 });
Matteo Scandoloda19dff2016-02-08 16:55:44 -080022
Matteo Scandolo735606c2016-02-09 09:13:30 -080023 var root, svg;
Matteo Scandoloda19dff2016-02-08 16:55:44 -080024
25 const draw = (tree) => {
Matteo Scandolo735606c2016-02-09 09:13:30 -080026
Matteo Scandoloeeb9c082016-02-09 11:19:22 -080027 // TODO update instead clear and redraw
28
Matteo Scandolo735606c2016-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
35 const treeLayout = d3.layout.tree()
36 .size([height, width]);
37
38 svg = d3.select($element[0])
39 .append('svg')
40 .style('width', `${el.clientWidth}px`)
41 .style('height', `${el.clientHeight}px`)
42
43 const treeContainer = svg.append('g')
44 .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 4},${serviceTopologyConfig.heightMargin})`);
45
Matteo Scandoloda19dff2016-02-08 16:55:44 -080046 root = tree;
47 root.x0 = height / 2;
48 root.y0 = width / 2;
49
Matteo Scandolo11dc8c42016-02-09 14:46:14 -080050 // ServiceTopologyHelper.drawLegend(svg);
Matteo Scandoloeeb9c082016-02-09 11:19:22 -080051 ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root);
Matteo Scandoloda19dff2016-02-08 16:55:44 -080052 };
53
Matteo Scandoloda19dff2016-02-08 16:55:44 -080054 this.getInstances = (slice) => {
55 Instances.query({slice: slice.id}).$promise
56 .then((instances) => {
57 this.selectedSlice = slice;
58 this.instances = instances;
59 })
Matteo Scandoloba2d63d2016-02-17 13:54:11 -080060 .catch(e => {
61 this.errors = e;
62 throw new Error(e);
63 })
Matteo Scandoloda19dff2016-02-08 16:55:44 -080064 };
65
66 $scope.$watch(() => this.serviceChain, (chain) => {
67 if(chain){
68 draw(chain);
69 }
70 });
71 }
72 }
73 });
74
75}());