blob: 163ee340bff43740d51df5668ac91adb5d88c63f [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: '',
14 controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, TreeLayout){
15
16 const el = $element[0];
17
Matteo Scandolo7547f042016-02-09 09:13:30 -080018 d3.select(window)
19 .on('resize', () => {
20 console.log('resize');
21 draw(this.serviceChain);
22 });
Matteo Scandolofcdbed32016-02-08 16:55:44 -080023
Matteo Scandolo7547f042016-02-09 09:13:30 -080024 var root, svg;
Matteo Scandolofcdbed32016-02-08 16:55:44 -080025
26 const draw = (tree) => {
Matteo Scandolo7547f042016-02-09 09:13:30 -080027
28 // clean
29 d3.select($element[0]).select('svg').remove();
30
31 const width = el.clientWidth - (serviceTopologyConfig.widthMargin * 2);
32 const height = el.clientHeight - (serviceTopologyConfig.heightMargin * 2);
33
34 const treeLayout = d3.layout.tree()
35 .size([height, width]);
36
37 svg = d3.select($element[0])
38 .append('svg')
39 .style('width', `${el.clientWidth}px`)
40 .style('height', `${el.clientHeight}px`)
41
42 const treeContainer = svg.append('g')
43 .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 4},${serviceTopologyConfig.heightMargin})`);
44
Matteo Scandolofcdbed32016-02-08 16:55:44 -080045 root = tree;
46 root.x0 = height / 2;
47 root.y0 = width / 2;
48
Matteo Scandolo7547f042016-02-09 09:13:30 -080049 TreeLayout.drawLegend(svg);
Matteo Scandolofcdbed32016-02-08 16:55:44 -080050 TreeLayout.updateTree(treeContainer, treeLayout, root);
51 };
52
Matteo Scandolofcdbed32016-02-08 16:55:44 -080053 this.getInstances = (slice) => {
54 Instances.query({slice: slice.id}).$promise
55 .then((instances) => {
56 this.selectedSlice = slice;
57 this.instances = instances;
58 })
59 };
60
61 $scope.$watch(() => this.serviceChain, (chain) => {
62 if(chain){
63 draw(chain);
64 }
65 });
66 }
67 }
68 });
69
70}());