All files / app/codeCharta/util customConfigHelper.ts

84.72% Statements 61/72
83.33% Branches 20/24
89.47% Functions 17/19
84.5% Lines 60/71

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 20821x                 21x 21x     21x         21x 21x 21x 21x   21x 21x 21x     6x         6x 6x       23x 23x       23x         23x     12x 12x                     6x               35x 4x         3x       32x       3x     4x       2x       1x   1x 3x     3x 1x       2x     1x     2x                       2x         1x           1x   1x       3x       9x   9x 18x 18x 7x       9x       5x 5x   5x       1x 2x     1x       1x 1x       3x 2x     2x 1x       2x                                      
import { LocalStorageCustomConfigs, stateObjectReplacer, stateObjectReviver } from "../codeCharta.model"
import { CustomConfigItemGroup } from "../ui/customConfigs/customConfigs.component"
import {
    CustomConfig,
    CustomConfigMapSelectionMode,
    CustomConfigsDownloadFile,
    ExportCustomConfig,
    MapNamesByChecksum
} from "../model/customConfig/customConfig.api.model"
import { FileNameHelper } from "./fileNameHelper"
import { FileDownloader } from "./fileDownloader"
import { ThreeCameraService } from "../ui/codeMap/threeViewer/threeCamera.service"
import { ThreeMapControlsService } from "../ui/codeMap/threeViewer/threeMapControls.service"
import { BehaviorSubject } from "rxjs"
import { VisibleFilesBySelectionMode } from "../ui/customConfigs/visibleFilesBySelectionMode.selector"
import { Store } from "@ngrx/store"
import { ThreeRendererService } from "../ui/codeMap/threeViewer/threeRenderer.service"
 
export const CUSTOM_CONFIG_FILE_EXTENSION = ".cc.config.json"
const CUSTOM_CONFIGS_LOCAL_STORAGE_VERSION = "1.0.1"
const CUSTOM_CONFIGS_DOWNLOAD_FILE_VERSION = "1.0.1"
export const CUSTOM_CONFIGS_LOCAL_STORAGE_ELEMENT = "CodeCharta::customConfigs"
 
export class CustomConfigHelper {
    private static customConfigs: Map<string, CustomConfig> = CustomConfigHelper.loadCustomConfigsFromLocalStorage()
    static customConfigChange$: BehaviorSubject<null> = new BehaviorSubject(null)
 
    static setCustomConfigsToLocalStorage() {
        const newLocalStorageElement: LocalStorageCustomConfigs = {
            version: CUSTOM_CONFIGS_LOCAL_STORAGE_VERSION,
            customConfigs: [...CustomConfigHelper.customConfigs]
        }
 
        localStorage.setItem(CUSTOM_CONFIGS_LOCAL_STORAGE_ELEMENT, JSON.stringify(newLocalStorageElement, stateObjectReplacer))
        CustomConfigHelper.customConfigChange$.next(null)
    }
 
    static loadCustomConfigsFromLocalStorage() {
        const ccLocalStorage = this.getCcLocalStorage()
        return new Map(ccLocalStorage?.customConfigs)
    }
 
    private static getCcLocalStorage() {
        const ccLocalStorage: LocalStorageCustomConfigs = JSON.parse(
            localStorage.getItem(CUSTOM_CONFIGS_LOCAL_STORAGE_ELEMENT),
            stateObjectReviver
        )
 
        return ccLocalStorage
    }
    static addCustomConfig(newCustomConfig: CustomConfig) {
        CustomConfigHelper.customConfigs.set(newCustomConfig.id, newCustomConfig)
        CustomConfigHelper.setCustomConfigsToLocalStorage()
    }
 
    static editCustomConfigNote(configId: string, configNote: string) {
        const config = CustomConfigHelper.customConfigs.get(configId)
        config.note = configNote
        CustomConfigHelper.customConfigs.set(configId, config)
        CustomConfigHelper.setCustomConfigsToLocalStorage()
    }
 
    static getCustomConfigSettings(configId: string): CustomConfig | undefined {
        return CustomConfigHelper.customConfigs.get(configId)
    }
 
    static hasCustomConfigByName(
        mapSelectionMode: CustomConfigMapSelectionMode,
        assignedMaps: MapNamesByChecksum,
        configName: string
    ): boolean {
        for (const customConfig of CustomConfigHelper.customConfigs.values()) {
            if (
                customConfig.name === configName &&
                customConfig.mapSelectionMode === mapSelectionMode &&
                this.areEqual(assignedMaps, customConfig.assignedMaps)
            ) {
                return true
            }
        }
 
        return false
    }
 
    private static areEqual(map1: MapNamesByChecksum, map2: MapNamesByChecksum) {
        Iif (map1.size !== map2.size) {
            return false
        }
        return [...map1.entries()].every(([checksum, mapName]) => map2.has(checksum) && map2.get(checksum) === mapName)
    }
 
    static getCustomConfigs(): Map<string, CustomConfig> {
        return CustomConfigHelper.customConfigs
    }
 
    static importCustomConfigs(content: string) {
        const importedCustomConfigsFile: CustomConfigsDownloadFile = JSON.parse(content, stateObjectReviver)
 
        for (const exportedConfig of importedCustomConfigsFile.customConfigs.values()) {
            const alreadyExistingConfig = CustomConfigHelper.getCustomConfigSettings(exportedConfig.id)
 
            // Check for a duplicate Config by matching checksums
            if (alreadyExistingConfig) {
                continue
            }
 
            // Prevent different Configs with the same name
            if (
                CustomConfigHelper.hasCustomConfigByName(exportedConfig.mapSelectionMode, exportedConfig.assignedMaps, exportedConfig.name)
            ) {
                exportedConfig.name += ` (${FileNameHelper.getFormattedTimestamp(new Date(exportedConfig.creationTime))})`
            }
 
            const importedCustomConfig: CustomConfig = {
                id: exportedConfig.id,
                name: exportedConfig.name,
                creationTime: exportedConfig.creationTime,
                assignedMaps: exportedConfig.assignedMaps,
                customConfigVersion: exportedConfig.customConfigVersion,
                mapSelectionMode: exportedConfig.mapSelectionMode,
                stateSettings: exportedConfig.stateSettings,
                camera: exportedConfig.camera,
                ...(exportedConfig.note && { note: exportedConfig.note })
            }
 
            CustomConfigHelper.addCustomConfig(importedCustomConfig)
        }
    }
 
    static downloadCustomConfigs(customConfigs: Map<string, ExportCustomConfig>) {
        const customConfigsDownloadFile: CustomConfigsDownloadFile = {
            downloadApiVersion: CUSTOM_CONFIGS_DOWNLOAD_FILE_VERSION,
            timestamp: Date.now(),
            customConfigs
        }
 
        const fileName = FileNameHelper.getNewTimestamp() + CUSTOM_CONFIG_FILE_EXTENSION
 
        FileDownloader.downloadData(JSON.stringify(customConfigsDownloadFile, stateObjectReplacer), fileName)
    }
 
    static createExportCustomConfigFromConfig(customConfig: CustomConfig): ExportCustomConfig {
        return { ...customConfig }
    }
 
    static getCustomConfigsAmountByMapAndMode(mapNames: string, mapSelectionMode: CustomConfigMapSelectionMode): number {
        let count = 0
 
        for (const config of CustomConfigHelper.customConfigs.values()) {
            const configMapNames = [...config.assignedMaps.values()]
            if (configMapNames.join(" ") === mapNames && config.mapSelectionMode === mapSelectionMode) {
                count++
            }
        }
 
        return count
    }
 
    static getConfigNameSuggestionByFileState({ mapSelectionMode, assignedMaps }: VisibleFilesBySelectionMode): string {
        const suggestedConfigName = [...assignedMaps.values()].join(" ")
        const customConfigNumberSuffix = CustomConfigHelper.getCustomConfigsAmountByMapAndMode(suggestedConfigName, mapSelectionMode) + 1
 
        return `${suggestedConfigName} #${customConfigNumberSuffix}`
    }
 
    static deleteCustomConfigs(customConfigs: CustomConfig[]) {
        for (const customConfig of customConfigs) {
            CustomConfigHelper.customConfigs.delete(customConfig.id)
        }
 
        CustomConfigHelper.setCustomConfigsToLocalStorage()
    }
 
    static deleteCustomConfig(configId: string) {
        CustomConfigHelper.customConfigs.delete(configId)
        CustomConfigHelper.setCustomConfigsToLocalStorage()
    }
 
    static sortCustomConfigDropDownGroupList(a: CustomConfigItemGroup, b: CustomConfigItemGroup) {
        if (!b.hasApplicableItems) {
            Iif (a.hasApplicableItems || a.mapSelectionMode < b.mapSelectionMode) {
                return -1
            }
            if (a.mapSelectionMode === b.mapSelectionMode) {
                return 0
            }
        }
 
        return 1
    }
 
    static applyCustomConfig(
        configId: string,
        // biome-ignore lint/correctness/noUnusedVariables: <explanation>
        store: Store,
        threeCameraService: ThreeCameraService,
        threeOrbitControlsService: ThreeMapControlsService,
        threeRendererService: ThreeRendererService
    ) {
        const customConfig = this.getCustomConfigSettings(configId)
        Iif (customConfig.camera) {
            threeCameraService.setPosition(customConfig.camera.camera)
            threeOrbitControlsService.setControlTarget(customConfig.camera.cameraTarget)
        }
        threeRendererService.render()
    }
}