Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 1 | (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 Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 14 | controller: function($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, ServiceTopologyHelper){ |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 15 | |
| 16 | const el = $element[0]; |
| 17 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 18 | d3.select(window) |
| 19 | .on('resize', () => { |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 20 | draw(this.serviceChain); |
| 21 | }); |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 23 | var root, svg; |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 24 | |
| 25 | const draw = (tree) => { |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 26 | |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 27 | // TODO update instead clear and redraw |
| 28 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 29 | // 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 Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 46 | root = tree; |
| 47 | root.x0 = height / 2; |
| 48 | root.y0 = width / 2; |
| 49 | |
Matteo Scandolo | 11dc8c4 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 50 | // ServiceTopologyHelper.drawLegend(svg); |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 51 | ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root); |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 54 | this.getInstances = (slice) => { |
| 55 | Instances.query({slice: slice.id}).$promise |
| 56 | .then((instances) => { |
| 57 | this.selectedSlice = slice; |
| 58 | this.instances = instances; |
| 59 | }) |
Matteo Scandolo | ba2d63d | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 60 | .catch(e => { |
| 61 | this.errors = e; |
| 62 | throw new Error(e); |
| 63 | }) |
Matteo Scandolo | da19dff | 2016-02-08 16:55:44 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | $scope.$watch(() => this.serviceChain, (chain) => { |
| 67 | if(chain){ |
| 68 | draw(chain); |
| 69 | } |
| 70 | }); |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | }()); |