Passed
Push — master ( e14e22...172c7d )
by
unknown
03:44
created

src/details/root-node.ts   A

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 41
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 36
mnd 0
bc 0
fnc 2
dl 0
loc 41
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A root-node.ts ➔ createRootNode 0 28 1
A root-node.ts ➔ delayPromise 0 3 1
1
import { NodeSelection } from '../components/types';
2
import { ElementIds, Colors } from '../utils/AppConsts';
3
import { createNodeLabelPath, getTextDimensions } from '../overview/graph-nodes';
4
5
// these magic numbers(-30, -36.5) are dependant of how node labels are painted relative to label text
6
const labelPathWidthOffset = -30;
7
const labelPathHeightOffset = -36.5;
8
9
export async function createRootNode(
10
    container: NodeSelection<any>,
11
    viewboxWidth: number,
12
    viexboxHeight: number,
13
    rootNodeName: string,
14
    isConsumer: boolean,
15
    isProvider: boolean
16
) {
17
    const nodeContainer = container
18
        .append('svg')
19
        .attr('id', ElementIds.DETAILS_ROOT_NODE_CONTAINER)
20
        .attr('font-size', 15)
21
        // hard-coded magic numbers that translates root node to position of root of the tree graphs
22
        .attr('viewBox', `-${viewboxWidth / 3.2} -${viexboxHeight / 2} ${viewboxWidth} ${viexboxHeight}`)
23
        .attr('preserveAspectRatio', 'xMidYMid meet');
24
    const label = nodeContainer.append('path').attr('fill', Colors.CLIFTON_NAVY);
25
    const text = nodeContainer
26
        .append('text')
27
        .attr('text-anchor', 'middle')
28
        .attr('fill', Colors.WHITE)
29
        .text(rootNodeName);
30
    await delayPromise();
31
    const { height, y } = getTextDimensions(text.node()) || { height: 0, y: 0 };
32
    const labelPath = createNodeLabelPath(label.node(), isConsumer, isProvider);
33
    label.attr('d', labelPath).attr('transform', `translate(${labelPathWidthOffset}, ${y + labelPathHeightOffset})`);
34
    // center text vertically on label
35
    text.attr('y', y + height + 1);
36
}
37
38
function delayPromise(delay: number = 0) {
39
    return new Promise(resolve => setTimeout(resolve, delay));
40
}
41