All files / app/codeCharta/util customConfigBuilder.ts

100% Statements 22/22
100% Branches 9/9
100% Functions 5/5
100% Lines 22/22

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 14210x   10x 10x   10x   10x           2x   2x                                       2x 2x 2x     2x   2x 2x       2x                                                                                                 2x                                     2x                 15x   91x 6x     85x   72x         13x        
import { CcState, stateObjectReplacer } from "../codeCharta.model"
import { CustomConfig } from "../model/customConfig/customConfig.api.model"
import md5 from "md5"
import { visibleFilesBySelectionModeSelector } from "../ui/customConfigs/visibleFilesBySelectionMode.selector"
 
const CUSTOM_CONFIG_API_VERSION = "1.0.0"
 
export function buildCustomConfigFromState(
    configName: string,
    state: CcState,
    camera: CustomConfig["camera"],
    note: string | undefined
): CustomConfig {
    const { mapSelectionMode, assignedMaps } = visibleFilesBySelectionModeSelector(state)
 
    const customConfig: CustomConfig = {
        id: "",
        name: configName,
 
        creationTime: Date.now(),
        mapSelectionMode,
        assignedMaps,
        customConfigVersion: CUSTOM_CONFIG_API_VERSION,
        stateSettings: {
            appSettings: undefined,
            dynamicSettings: undefined,
            fileSettings: undefined
        },
        camera,
        ...(note && { note })
    }
 
    // Initialize all necessary state settings with default values right here
    // Any changes to the state properties must also be adapted here
    // You must handle breaking changes of the CustomConfig API
    initializeAppSettings(customConfig)
    initializeDynamicSettings(customConfig)
    initializeFileSettings(customConfig)
 
    // Override the default state settings with the stored CustomConfig values
    deepMapOneToOther(state, customConfig.stateSettings)
 
    customConfig.id = md5(JSON.stringify(customConfig, stateObjectReplacer))
    return customConfig
}
 
function initializeAppSettings(target: CustomConfig) {
    target.stateSettings.appSettings = {
        showMetricLabelNameValue: false,
        showMetricLabelNodeName: false,
        colorLabels: {
            positive: false,
            negative: false,
            neutral: false
        },
        amountOfEdgePreviews: 0,
        amountOfTopLabels: 0,
        edgeHeight: 0,
        hideFlatBuildings: false,
        invertHeight: false,
        invertArea: false,
        isLoadingFile: false,
        isLoadingMap: false,
        isPresentationMode: false,
        isWhiteBackground: false,
        resetCameraIfNewFileIsLoaded: false,
        scaling: undefined,
        showOnlyBuildingsWithEdges: false,
        isEdgeMetricVisible: true,
        sortingOrderAscending: false,
        isSearchPanelPinned: false,
        experimentalFeaturesEnabled: false,
        screenshotToClipboardEnabled: false,
        layoutAlgorithm: undefined,
        maxTreeMapFiles: 0,
        sharpnessMode: undefined,
        isColorMetricLinkedToHeightMetric: false,
        enableFloorLabels: true,
        mapColors: {
            labelColorAndAlpha: { alpha: 0, rgb: "" },
            base: "",
            flat: "",
            incomingEdge: "",
            markingColors: [],
            negative: "",
            negativeDelta: "",
            neutral: "",
            outgoingEdge: "",
            positive: "",
            positiveDelta: "",
            selected: ""
        }
    }
}
 
function initializeDynamicSettings(target: CustomConfig) {
    target.stateSettings.dynamicSettings = {
        areaMetric: "",
        colorMetric: "",
        distributionMetric: "",
        edgeMetric: "",
        focusedNodePath: [],
        heightMetric: "",
        margin: 0,
        searchPattern: "",
        sortingOption: undefined,
        colorRange: {
            from: 0,
            to: 0
        },
        colorMode: undefined
    }
}
 
function initializeFileSettings(target: CustomConfig) {
    target.stateSettings.fileSettings = {
        blacklist: undefined,
        edges: [],
        attributeDescriptors: {},
        markedPackages: []
    }
}
 
function deepMapOneToOther<T>(source: CcState, target: T) {
    for (const [key, value] of Object.entries(source)) {
        // if a property of source is missing, we don't want to copy it into target.
        if (!Object.prototype.hasOwnProperty.call(target, key)) {
            continue
        }
 
        if (typeof value !== "object" || Array.isArray(value) || value === null || target[key] === undefined) {
            // Assign primitive values to target
            target[key] = value
        } else {
            // We have to map an object with nested properties here.
            // source and target have the same properties specified for the nested object.
            // Thus, map properties of the next deeper level.
            deepMapOneToOther(value, target[key])
        }
    }
}