blob: b53d29f70cd908fc5712014cb5c1bdf6c01cbe72 [file] [log] [blame]
Matteo Scandolobe9b13d2016-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 Scandolo4889f5a2016-01-25 12:00:42 -080012 controller: function($element, $window, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, TreeLayout){
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080013
Matteo Scandolo793e1dd2016-01-22 09:33:26 -080014 this.instances = [];
15 this.slices = [];
16
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080017 const width = $window.innerWidth - serviceTopologyConfig.widthMargin;
18 const height = $window.innerHeight - serviceTopologyConfig.heightMargin;
19
Matteo Scandolo4889f5a2016-01-25 12:00:42 -080020 const treeLayout = d3.layout.tree()
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080021 .size([height, width]);
22
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080023 const svg = d3.select($element[0])
24 .append('svg')
25 .style('width', `${$window.innerWidth}px`)
26 .style('height', `${$window.innerHeight}px`)
27 .append('g')
Matteo Scandolo4889f5a2016-01-25 12:00:42 -080028 .attr('transform', 'translate(' + serviceTopologyConfig.widthMargin+ ',' + serviceTopologyConfig.heightMargin + ')');
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080029
Matteo Scandolo85aad312016-01-21 14:23:28 -080030 //const resizeCanvas = () => {
31 // var targetSize = svg.node().getBoundingClientRect();
32 //
33 // d3.select(self.frameElement)
34 // .attr('width', `${targetSize.width}px`)
35 // .attr('height', `${targetSize.height}px`)
36 //};
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080037 //d3.select(window)
38 // .on('load', () => {
39 // resizeCanvas();
40 // });
41 //d3.select(window)
42 // .on('resize', () => {
43 // resizeCanvas();
44 // update(root);
45 // });
Matteo Scandolo85aad312016-01-21 14:23:28 -080046 var root;
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080047
Matteo Scandolo85aad312016-01-21 14:23:28 -080048 const draw = (tree) => {
49 root = tree;
50 root.x0 = $window.innerHeight / 2;
51 root.y0 = 0;
52
Matteo Scandolo4889f5a2016-01-25 12:00:42 -080053 TreeLayout.updateTree(svg, treeLayout, root);
Matteo Scandolo85aad312016-01-21 14:23:28 -080054 };
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080055
Matteo Scandolo53a02262016-01-21 16:02:57 -080056 var _this = this;
Matteo Scandolo68236262016-01-21 15:38:06 -080057
Matteo Scandolo89276392016-01-22 16:36:34 -080058 Subscribers.query().$promise
59 .then((subscribers) => {
60 this.subscribers = subscribers;
Matteo Scandolofb46f5b2016-01-25 10:10:38 -080061 if(subscribers.length === 1){
62 this.selectedSubscriber = subscribers[0];
63 this.getServiceChain(this.selectedSubscriber);
64 }
Matteo Scandolo85aad312016-01-21 14:23:28 -080065 });
Matteo Scandolo68236262016-01-21 15:38:06 -080066
67 this.getInstances = (slice) => {
68 Instances.query({slice: slice.id}).$promise
69 .then((instances) => {
70 this.selectedSlice = slice;
71 this.instances = instances;
72 })
73 };
Matteo Scandolo5f525542016-01-22 16:48:54 -080074
Matteo Scandolo4889f5a2016-01-25 12:00:42 -080075 // redraw when subrsbiber change
Matteo Scandolo5f525542016-01-22 16:48:54 -080076 this.getServiceChain = (subscriber) => {
77 ServiceRelation.get(subscriber)
78 .then((tree) => {
79 draw(tree);
80 });
81 };
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080082 }
83 }
84 });
85
86}());