blob: d05b8e761c50f96f0051539c0a4e3528249c8d7c [file] [log] [blame]
Matteo Scandolo5bf51732018-01-10 15:51:33 -08001
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
18import './graph-legend.component.scss';
19
20import * as d3 from 'd3';
21import {IXosServiceGraphIcons} from '../../services/d3-helpers/graph-icons.service';
22
23interface ILegendElement {
24 label: string;
25 icon: string;
26}
27
28class XosServiceGraphLegendCtrl {
29
30 static $inject = [
31 '$log',
32 '$timeout',
33 'XosServiceGraphIcons'
34 ];
35
36 private svg;
37
38 private duration: number = 500;
39
40 private legendElements: ILegendElement[] = [
41 {
42 label: 'Services',
43 icon: 'service'
44 },
45 {
46 label: 'ServiceInstances',
47 icon: 'serviceinstance'
48 },
49 {
50 label: 'Instances',
51 icon: 'instance'
52 },
53 {
54 label: 'Networks',
55 icon: 'network'
56 }
57 ];
58
59 constructor(
60 private $log: ng.ILogService,
61 private $timeout: ng.ITimeoutService,
62 private XosServiceGraphIcons: IXosServiceGraphIcons
63 ) {
64 this.$log.debug('[XosServiceGraphLegend] Setup');
65 $timeout(() => {
66 // NOTE we need to wait for the component to be rendered before drawing
67 this.setupSvg();
68 this.render();
69 }, 500);
70 }
71
72 private setupSvg() {
73 this.svg = d3.select('xos-service-graph-legend svg');
74 }
75
76 private render() {
77 const step = 50;
78
79 const elements = this.svg.selectAll('g')
80 .data(this.legendElements);
81
82 const entering = elements.enter()
83 .append('g')
84 .attr({
85 class: d => `${d.icon}`,
86 transform: (d: ILegendElement, i: number) => `translate(50, ${step * (i + 1)})`
87 });
88
89 entering
90 .append('path')
91 .attr({
92 d: d => this.XosServiceGraphIcons.get(d.icon).path,
93 transform: d => this.XosServiceGraphIcons.get(d.icon).transform,
94 class: 'icon',
95 opacity: 0
96 })
97 .transition()
98 .duration(this.duration)
99 .delay((d, i) => i * (this.duration / 2))
100 .attr({
101 opacity: 1
102 });
103
104 entering
105 .append('text')
106 .text(d => d.label.toUpperCase())
107 .attr({
108 'text-anchor': 'left',
109 'alignment-baseline': 'bottom',
110 'font-size': 15,
111 x: 50,
112 y: 20,
113 opacity: 0
114 })
115 .transition()
116 .duration(this.duration)
117 .delay((d, i) => i * (this.duration / 2))
118 .attr({
119 opacity: 1
120 });
121 }
122}
123
124export const XosServiceGraphLegend: angular.IComponentOptions = {
125 template: `
126 <h1>Legend</h1>
127 <svg></svg>
128 `,
129 controllerAs: 'vm',
130 controller: XosServiceGraphLegendCtrl,
131};