blob: 76368c1300274def3a2c39cf9d96227bb01ac8b7 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolofcdbed32016-02-08 16:55:44 -080019(function () {
20 'use strict';
21
Matteo Scandolo04564952016-02-24 11:22:48 -080022 angular.module('xos.diagnostic')
Matteo Scandolofcdbed32016-02-08 16:55:44 -080023 .directive('serviceTopology', function(){
24 return {
25 restrict: 'E',
26 scope: {
27 serviceChain: '='
28 },
29 bindToController: true,
30 controllerAs: 'vm',
31 template: '',
Matteo Scandoloe64dcc02016-08-02 11:53:22 -070032 controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Instances, Subscribers, ServiceTopologyHelper){
Matteo Scandolofcdbed32016-02-08 16:55:44 -080033
34 const el = $element[0];
35
Matteo Scandolo7547f042016-02-09 09:13:30 -080036 d3.select(window)
Matteo Scandolo79108192016-03-08 09:33:26 -080037 .on('resize.service', () => {
Matteo Scandolo7547f042016-02-09 09:13:30 -080038 draw(this.serviceChain);
39 });
Matteo Scandolofcdbed32016-02-08 16:55:44 -080040
Matteo Scandolo7547f042016-02-09 09:13:30 -080041 var root, svg;
Matteo Scandolofcdbed32016-02-08 16:55:44 -080042
43 const draw = (tree) => {
Matteo Scandolo7547f042016-02-09 09:13:30 -080044
Matteo Scandolo70ac2162016-02-24 15:40:22 -080045 if(!tree){
46 console.error('Tree is missing');
47 return;
48 }
49
Matteo Scandolo219b1a72016-02-09 11:19:22 -080050 // TODO update instead clear and redraw
51
Matteo Scandolo7547f042016-02-09 09:13:30 -080052 // clean
53 d3.select($element[0]).select('svg').remove();
54
Matteo Scandolo574c73f2016-03-01 17:08:45 -080055
Matteo Scandolo7547f042016-02-09 09:13:30 -080056 const width = el.clientWidth - (serviceTopologyConfig.widthMargin * 2);
57 const height = el.clientHeight - (serviceTopologyConfig.heightMargin * 2);
58
59 const treeLayout = d3.layout.tree()
60 .size([height, width]);
61
62 svg = d3.select($element[0])
63 .append('svg')
64 .style('width', `${el.clientWidth}px`)
65 .style('height', `${el.clientHeight}px`)
66
67 const treeContainer = svg.append('g')
Matteo Scandolo79108192016-03-08 09:33:26 -080068 .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 2},${serviceTopologyConfig.heightMargin})`);
Matteo Scandolo7547f042016-02-09 09:13:30 -080069
Matteo Scandolofcdbed32016-02-08 16:55:44 -080070 root = tree;
71 root.x0 = height / 2;
72 root.y0 = width / 2;
73
Matteo Scandoloaf286372016-02-09 14:46:14 -080074 // ServiceTopologyHelper.drawLegend(svg);
Matteo Scandolo574c73f2016-03-01 17:08:45 -080075 ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root, el);
Matteo Scandolofcdbed32016-02-08 16:55:44 -080076 };
Matteo Scandolofcdbed32016-02-08 16:55:44 -080077
78 $scope.$watch(() => this.serviceChain, (chain) => {
Matteo Scandolo70ac2162016-02-24 15:40:22 -080079 if(angular.isDefined(chain)){
Matteo Scandolofcdbed32016-02-08 16:55:44 -080080 draw(chain);
81 }
82 });
83 }
84 }
85 });
86
87}());