blob: bbacbdc9a78b32fd58189f6322a2e3a24522a9bb [file] [log] [blame]
Matteo Scandolo8cf33a32017-11-14 15:52:29 -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
18
19import * as d3 from 'd3';
20import {XosServiceGraphConfig as config} from '../../graph.config';
21
22export interface Id3BBox {
23 x?: number;
24 y?: number;
25 width: number;
26 height: number;
27}
28
29export interface IXosGraphHelpers {
30 parseElemClasses (classes: string): string;
31 getBBox(context: any): Id3BBox;
32 getSiblingTextBBox(contex: any /* D3 this */): Id3BBox;
33 getSiblingIconBBox(contex: any /* D3 this */): Id3BBox;
34 getSiblingBBox(contex: any): Id3BBox;
35}
36
37export class XosGraphHelpers implements IXosGraphHelpers {
38 public parseElemClasses (classes: string): string {
39 return angular.isDefined(classes) ? classes.split(' ')
40 .map(c => `ext-${c}`)
41 .join(' ') : '';
42 }
43
44 public getBBox(context: any): Id3BBox {
45 return d3.select(context).node().getBBox();
46 }
47
48 public getSiblingTextBBox(contex: any): Id3BBox {
49 const text: d3.Selection<any> = d3.select(contex.parentNode).select('text');
50 return text.empty() ? {width: 0, height: 0} : text.node().getBBox();
51 }
52
53 public getSiblingIconBBox(contex: any): Id3BBox {
54 return d3.select(contex.parentNode).select('path').node().getBBox();
55 }
56
57 public getSiblingBBox(contex: any): Id3BBox {
58 // NOTE consider that inside a node we can have 1 text and 1 icon
59 const textBBox: Id3BBox = this.getSiblingTextBBox(contex);
60 const iconBBox: Id3BBox = this.getSiblingIconBBox(contex);
61
62 return {
63 width: iconBBox.width + (textBBox.width ? config.node.padding + textBBox.width : 0),
64 height: iconBBox.height
65 };
66 }
67}