blob: 600e301c8aa13764ac49c0a9c2ad337903622276 [file] [log] [blame]
Matteo Scandolo8a64fa42016-01-21 11:21:03 -08001(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 Scandolo071ef462016-01-25 12:00:42 -080012 controller: function($element, $window, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, TreeLayout){
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080013
Matteo Scandolo9ef3c842016-01-25 13:55:09 -080014 // TODO draw legend
15
Matteo Scandolo6e8a75a2016-01-22 09:33:26 -080016 this.instances = [];
17 this.slices = [];
18
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080019 const width = $window.innerWidth - serviceTopologyConfig.widthMargin;
20 const height = $window.innerHeight - serviceTopologyConfig.heightMargin;
21
Matteo Scandolo071ef462016-01-25 12:00:42 -080022 const treeLayout = d3.layout.tree()
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080023 .size([height, width]);
24
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080025 const svg = d3.select($element[0])
26 .append('svg')
27 .style('width', `${$window.innerWidth}px`)
28 .style('height', `${$window.innerHeight}px`)
29 .append('g')
Matteo Scandolo071ef462016-01-25 12:00:42 -080030 .attr('transform', 'translate(' + serviceTopologyConfig.widthMargin+ ',' + serviceTopologyConfig.heightMargin + ')');
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080031
Matteo Scandolof16b3792016-01-21 14:23:28 -080032 //const resizeCanvas = () => {
33 // var targetSize = svg.node().getBoundingClientRect();
34 //
35 // d3.select(self.frameElement)
36 // .attr('width', `${targetSize.width}px`)
37 // .attr('height', `${targetSize.height}px`)
38 //};
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080039 //d3.select(window)
40 // .on('load', () => {
41 // resizeCanvas();
42 // });
43 //d3.select(window)
44 // .on('resize', () => {
45 // resizeCanvas();
46 // update(root);
47 // });
Matteo Scandolof16b3792016-01-21 14:23:28 -080048 var root;
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080049
Matteo Scandolof16b3792016-01-21 14:23:28 -080050 const draw = (tree) => {
51 root = tree;
52 root.x0 = $window.innerHeight / 2;
53 root.y0 = 0;
54
Matteo Scandolo071ef462016-01-25 12:00:42 -080055 TreeLayout.updateTree(svg, treeLayout, root);
Matteo Scandolof16b3792016-01-21 14:23:28 -080056 };
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080057
Matteo Scandolo3501ccb2016-01-21 16:02:57 -080058 var _this = this;
Matteo Scandolo06f45d62016-01-21 15:38:06 -080059
Matteo Scandoloff7df762016-01-22 16:36:34 -080060 Subscribers.query().$promise
61 .then((subscribers) => {
62 this.subscribers = subscribers;
Matteo Scandolof2c99012016-01-25 10:10:38 -080063 if(subscribers.length === 1){
64 this.selectedSubscriber = subscribers[0];
65 this.getServiceChain(this.selectedSubscriber);
66 }
Matteo Scandolof16b3792016-01-21 14:23:28 -080067 });
Matteo Scandolo06f45d62016-01-21 15:38:06 -080068
69 this.getInstances = (slice) => {
70 Instances.query({slice: slice.id}).$promise
71 .then((instances) => {
72 this.selectedSlice = slice;
73 this.instances = instances;
74 })
75 };
Matteo Scandolo4d42ddc2016-01-22 16:48:54 -080076
Matteo Scandolo071ef462016-01-25 12:00:42 -080077 // redraw when subrsbiber change
Matteo Scandolo4d42ddc2016-01-22 16:48:54 -080078 this.getServiceChain = (subscriber) => {
79 ServiceRelation.get(subscriber)
80 .then((tree) => {
81 draw(tree);
82 });
83 };
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080084 }
85 }
86 });
87
88}());