All files / app/codeCharta/util/algorithm/streetLayout/strip horizontalStrip.ts

70.96% Statements 22/31
62.5% Branches 5/8
75% Functions 3/4
70.96% Lines 22/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 6315x 15x 15x     15x 15x 15x     15x   1x                     1x   1x     1x   1x 1x 1x 1x   1x 1x 1x 1x 1x           1x   1x                              
import Strip from "./strip"
import Rectangle from "../rectangle"
import { Vector2 } from "three"
import { CodeMapNode } from "../../../../codeCharta.model"
 
export enum HorizontalOrder {
    leftToRight,
    rightToLeft
}
 
export default class HorizontalStrip extends Strip {
    constructor(nodes: CodeMapNode[]) {
        super(nodes)
    }
 
    layout(
        rect: Rectangle,
        rootSize: number,
        metricName: string,
        currentTreemapDepth: number,
        margin: number,
        order: HorizontalOrder = HorizontalOrder.leftToRight
    ): CodeMapNode[] {
        let offsetX = rect.topLeft.x
 
        Iif (order !== HorizontalOrder.leftToRight) {
            this.nodes.reverse()
        }
        const nodes = this.nodes
 
        const rootArea = rect.area()
        const width = rect.width
        const height = this.totalScaledSize(nodes, metricName, rootSize, rootArea) / width
        const stripNodes: CodeMapNode[] = []
 
        for (const node of nodes) {
            const nodeSize = this.scaledSize(node, rootSize, rootArea, metricName)
            const nodeWidth = height > 0 ? nodeSize / height : 0
            const newRect = new Rectangle(new Vector2(offsetX, rect.topLeft.y), nodeWidth, height)
            stripNodes.push({
                ...node,
                value: node.type === "File" ? rootSize : 0,
                rect: this.applyNodeMargin(newRect, margin),
                zOffset: currentTreemapDepth
            })
            offsetX += nodeWidth
        }
        return stripNodes
    }
 
    worstAspectRatio(nodes: CodeMapNode[], rect: Rectangle, rootSize: number, metricName: string): number {
        const width = rect.width
        const rootArea = rect.area()
        const totalSize = this.totalScaledSize(nodes, metricName, rootSize, rootArea)
        const stripMin = this.min(nodes, metricName, rootSize, rootArea)
        const stripMax = this.max(nodes, metricName, rootSize, rootArea)
        const widthSquared = Math.pow(width, 2)
        const totalSizeSquared = Math.pow(totalSize, 2)
 
        return Math.max((widthSquared * stripMax) / totalSizeSquared, totalSizeSquared / (widthSquared * stripMin))
    }
}