Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 22 | angular.module('xos.diagnostic') |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 23 | .directive('serviceTopology', function(){ |
| 24 | return { |
| 25 | restrict: 'E', |
| 26 | scope: { |
| 27 | serviceChain: '=' |
| 28 | }, |
| 29 | bindToController: true, |
| 30 | controllerAs: 'vm', |
| 31 | template: '', |
Matteo Scandolo | e64dcc0 | 2016-08-02 11:53:22 -0700 | [diff] [blame] | 32 | controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Instances, Subscribers, ServiceTopologyHelper){ |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 33 | |
| 34 | const el = $element[0]; |
| 35 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 36 | d3.select(window) |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 37 | .on('resize.service', () => { |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 38 | draw(this.serviceChain); |
| 39 | }); |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 41 | var root, svg; |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 42 | |
| 43 | const draw = (tree) => { |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 44 | |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 45 | if(!tree){ |
| 46 | console.error('Tree is missing'); |
| 47 | return; |
| 48 | } |
| 49 | |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 50 | // TODO update instead clear and redraw |
| 51 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 52 | // clean |
| 53 | d3.select($element[0]).select('svg').remove(); |
| 54 | |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 56 | 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 Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 68 | .attr('transform', `translate(${serviceTopologyConfig.widthMargin * 2},${serviceTopologyConfig.heightMargin})`); |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 69 | |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 70 | root = tree; |
| 71 | root.x0 = height / 2; |
| 72 | root.y0 = width / 2; |
| 73 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 74 | // ServiceTopologyHelper.drawLegend(svg); |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 75 | ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root, el); |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 76 | }; |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 77 | |
| 78 | $scope.$watch(() => this.serviceChain, (chain) => { |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 79 | if(angular.isDefined(chain)){ |
Matteo Scandolo | fcdbed3 | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 80 | draw(chain); |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | }()); |