All files / app/codeCharta/ui/codeMap codeMap.render.service.ts

98% Statements 98/100
87.5% Branches 35/40
100% Functions 19/19
97.84% Lines 91/93

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 18414x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x   14x     14x 29x                 29x 29x 29x 29x 29x 29x 29x   29x       5x     29x 31x 30x   1x         3x 3x 3x   3x 3x 3x 3x       3x 3x 3x       5x 5x 5x 5x 5x 5x       29x 29x       29x 29x 29x         29x             31x 31x 1x 3x   221x       1x 3x 1x           7x   7x           7x 14x 14x 14x 3x 11x 11x 4x 7x 4x   3x               6x 11x         13x   13x 7x               6x   6x 5x   5x 15x 3x       5x 8x 3x           4x 4x 4x      
import { Injectable, OnDestroy } from "@angular/core"
import { CodeMapMesh } from "./rendering/codeMapMesh"
import { createTreemapNodes } from "../../util/algorithm/treeMapLayout/treeMapGenerator"
import { CodeMapLabelService } from "./codeMap.label.service"
import { ThreeSceneService } from "./threeViewer/threeSceneService"
import { CodeMapArrowService } from "./arrow/codeMap.arrow.service"
import { CcState, CodeMapNode, LayoutAlgorithm, Node } from "../../codeCharta.model"
import { isDeltaState } from "../../model/files/files.helper"
import { StreetLayoutGenerator } from "../../util/algorithm/streetLayout/streetLayoutGenerator"
import { ThreeStatsService } from "./threeViewer/threeStats.service"
import { CodeMapMouseEventService } from "./codeMap.mouseEvent.service"
import { isLoadingFileSelector } from "../../state/store/appSettings/isLoadingFile/isLoadingFile.selector"
import { Subscription, tap } from "rxjs"
import { metricDataSelector } from "../../state/selectors/accumulatedData/metricData/metricData.selector"
import { State, Store } from "@ngrx/store"
 
const MIN_BUILDING_LENGTH = 2
 
@Injectable({ providedIn: "root" })
export class CodeMapRenderService implements OnDestroy {
    private nodesByColor = {
        positive: [],
        neutral: [],
        negative: []
    }
    private unflattenedNodes
    private subscription: Subscription
 
    constructor(
        private store: Store<CcState>,
        private state: State<CcState>,
        private threeSceneService: ThreeSceneService,
        private codeMapLabelService: CodeMapLabelService,
        private codeMapArrowService: CodeMapArrowService,
        private threeStatsService: ThreeStatsService,
        private codeMapMouseEventService: CodeMapMouseEventService
    ) {
        this.subscription = this.store.select(isLoadingFileSelector).pipe(tap(this.onIsLoadingFileChanged)).subscribe()
    }
 
    ngOnDestroy(): void {
        this.subscription.unsubscribe()
    }
 
    onIsLoadingFileChanged = (isLoadingFile: boolean) => {
        if (isLoadingFile) {
            this.threeSceneService?.dispose()
        } else {
            this.threeStatsService?.resetPanels()
        }
    }
 
    render(map: CodeMapNode) {
        const nodes = this.getNodes(map)
        const visibleSortedNodes = this.sortVisibleNodesByHeightDescending(nodes)
        this.unflattenedNodes = visibleSortedNodes.filter(({ flat }) => !flat)
 
        this.setNewMapMesh(nodes, visibleSortedNodes)
        this.getNodesMatchingColorSelector(this.unflattenedNodes)
        this.setLabels(this.unflattenedNodes)
        this.setArrows(visibleSortedNodes)
    }
 
    private setNewMapMesh(allMeshNodes, visibleSortedNodes) {
        const state = this.state.getValue() as CcState
        const mapMesh = new CodeMapMesh(visibleSortedNodes, state, isDeltaState(state.files))
        this.threeSceneService.setMapMesh(allMeshNodes, mapMesh)
    }
 
    scaleMap() {
        this.codeMapMouseEventService.unhoverNode()
        this.codeMapLabelService.scale()
        this.codeMapArrowService.scale()
        this.threeSceneService.scaleHeight()
        this.codeMapLabelService.clearLabels()
        this.setLabels(this.unflattenedNodes)
    }
 
    getNodes(map: CodeMapNode) {
        const state = this.state.getValue() as CcState
        const nodeMetricData = metricDataSelector(state).nodeMetricData
        const {
            appSettings: { layoutAlgorithm },
            files
        } = state
        const deltaState = isDeltaState(files)
        switch (layoutAlgorithm) {
            case LayoutAlgorithm.StreetMap:
            case LayoutAlgorithm.TreeMapStreet:
                return StreetLayoutGenerator.createStreetLayoutNodes(map, state, nodeMetricData, deltaState)
            case LayoutAlgorithm.SquarifiedTreeMap:
                return createTreemapNodes(map, state, nodeMetricData, deltaState)
            default:
                return []
        }
    }
 
    sortVisibleNodesByHeightDescending(nodes: Node[]) {
        const experimentalFeaturesEnabled = this.state.getValue().appSettings.experimentalFeaturesEnabled
        if (experimentalFeaturesEnabled) {
            this.setMinBuildingLength(nodes)
            return nodes.filter(node => node.visible && node.width > 0).sort((a, b) => b.height - a.height)
        }
        return nodes.filter(node => node.visible && node.length > 0 && node.width > 0).sort((a, b) => b.height - a.height)
    }
 
    private setMinBuildingLength(nodes: Node[]) {
        for (const node of nodes) {
            if (node.length <= 0) {
                node.length = MIN_BUILDING_LENGTH
            }
        }
    }
 
    private getNodesMatchingColorSelector(sortedNodes: Node[]) {
        const dynamicSettings = this.state.getValue().dynamicSettings
 
        this.nodesByColor = {
            positive: [],
            negative: [],
            neutral: []
        }
 
        for (const node of sortedNodes) {
            if (node.isLeaf) {
                const metric = node.attributes[dynamicSettings.colorMetric]
                if (dynamicSettings.colorMetric === "unary") {
                    this.nodesByColor.positive.push(node)
                } else if (metric !== null) {
                    if (metric < dynamicSettings.colorRange.from) {
                        this.nodesByColor.positive.push(node)
                    } else if (metric < dynamicSettings.colorRange.to) {
                        this.nodesByColor.neutral.push(node)
                    } else {
                        this.nodesByColor.negative.push(node)
                    }
                }
            }
        }
    }
 
    private setBuildingLabel(nodes: Node[], highestNodeInSet: number) {
        for (const node of nodes) {
            this.codeMapLabelService.addLeafLabel(node, highestNodeInSet)
        }
    }
 
    private setLabels(sortedNodes: Node[]) {
        this.codeMapLabelService.clearLabels()
 
        if (sortedNodes === undefined || sortedNodes.length === 0) {
            return
        }
 
        const {
            showMetricLabelNodeName,
            showMetricLabelNameValue,
            colorLabels: colorLabelOptions,
            amountOfTopLabels
        } = this.state.getValue().appSettings
 
        if (showMetricLabelNodeName || showMetricLabelNameValue) {
            const highestNodeInSet = sortedNodes[0].height
 
            for (const colorType of ["positive", "neutral", "negative"]) {
                if (colorLabelOptions[colorType]) {
                    this.setBuildingLabel(this.nodesByColor[colorType], highestNodeInSet)
                }
            }
 
            if (!(colorLabelOptions.negative || colorLabelOptions.neutral || colorLabelOptions.positive)) {
                const nodes = sortedNodes.filter(node => node.isLeaf).slice(0, amountOfTopLabels)
                this.setBuildingLabel(nodes, highestNodeInSet)
            }
        }
    }
 
    private setArrows(sortedNodes: Node[]) {
        this.codeMapArrowService.clearArrows()
        this.codeMapArrowService.addEdgeMapBasedOnNodes(sortedNodes)
        this.codeMapArrowService.addEdgePreview()
    }
}