Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
| 5 | .directive('serviceCanvas', function(){ |
| 6 | return { |
| 7 | restrict: 'E', |
| 8 | scope: {}, |
| 9 | bindToController: true, |
| 10 | controllerAs: 'vm', |
| 11 | templateUrl: 'templates/topology_canvas.tpl.html', |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 12 | controller: function($element, $window, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, TreeLayout){ |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 13 | |
Matteo Scandolo | 2c33a4c | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 14 | // NOTE $window is not the right reference, we should refer to container size |
Matteo Scandolo | 9ef3c84 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 15 | |
Matteo Scandolo | 6e8a75a | 2016-01-22 09:33:26 -0800 | [diff] [blame] | 16 | this.instances = []; |
| 17 | this.slices = []; |
| 18 | |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 19 | const width = $window.innerWidth - serviceTopologyConfig.widthMargin; |
| 20 | const height = $window.innerHeight - serviceTopologyConfig.heightMargin; |
| 21 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 22 | const treeLayout = d3.layout.tree() |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 23 | .size([height, width]); |
| 24 | |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 25 | const svg = d3.select($element[0]) |
| 26 | .append('svg') |
| 27 | .style('width', `${$window.innerWidth}px`) |
| 28 | .style('height', `${$window.innerHeight}px`) |
Matteo Scandolo | 2c33a4c | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 29 | |
| 30 | const treeContainer = svg.append('g') |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 31 | .attr('transform', 'translate(' + serviceTopologyConfig.widthMargin+ ',' + serviceTopologyConfig.heightMargin + ')'); |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 32 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 33 | //const resizeCanvas = () => { |
| 34 | // var targetSize = svg.node().getBoundingClientRect(); |
| 35 | // |
| 36 | // d3.select(self.frameElement) |
| 37 | // .attr('width', `${targetSize.width}px`) |
| 38 | // .attr('height', `${targetSize.height}px`) |
| 39 | //}; |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 40 | //d3.select(window) |
| 41 | // .on('load', () => { |
| 42 | // resizeCanvas(); |
| 43 | // }); |
| 44 | //d3.select(window) |
| 45 | // .on('resize', () => { |
| 46 | // resizeCanvas(); |
| 47 | // update(root); |
| 48 | // }); |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 49 | var root; |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 50 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 51 | const draw = (tree) => { |
| 52 | root = tree; |
| 53 | root.x0 = $window.innerHeight / 2; |
| 54 | root.y0 = 0; |
| 55 | |
Matteo Scandolo | 2c33a4c | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 56 | TreeLayout.updateTree(treeContainer, treeLayout, root); |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 57 | }; |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | 2c33a4c | 2016-01-25 16:24:42 -0800 | [diff] [blame] | 59 | TreeLayout.drawLegend(svg); |
| 60 | |
Matteo Scandolo | 3501ccb | 2016-01-21 16:02:57 -0800 | [diff] [blame] | 61 | var _this = this; |
Matteo Scandolo | 06f45d6 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 63 | Subscribers.query().$promise |
| 64 | .then((subscribers) => { |
| 65 | this.subscribers = subscribers; |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 66 | if(subscribers.length === 1){ |
| 67 | this.selectedSubscriber = subscribers[0]; |
| 68 | this.getServiceChain(this.selectedSubscriber); |
| 69 | } |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 70 | }); |
Matteo Scandolo | 06f45d6 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 71 | |
| 72 | this.getInstances = (slice) => { |
| 73 | Instances.query({slice: slice.id}).$promise |
| 74 | .then((instances) => { |
| 75 | this.selectedSlice = slice; |
| 76 | this.instances = instances; |
| 77 | }) |
| 78 | }; |
Matteo Scandolo | 4d42ddc | 2016-01-22 16:48:54 -0800 | [diff] [blame] | 79 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 80 | // redraw when subrsbiber change |
Matteo Scandolo | 4d42ddc | 2016-01-22 16:48:54 -0800 | [diff] [blame] | 81 | this.getServiceChain = (subscriber) => { |
| 82 | ServiceRelation.get(subscriber) |
| 83 | .then((tree) => { |
| 84 | draw(tree); |
| 85 | }); |
| 86 | }; |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | }()); |