All files / app/codeCharta/ui/ribbonBar/showScenariosButton scenarioHelper.ts

96.66% Statements 87/90
90% Branches 27/30
100% Functions 15/15
96.55% Lines 84/87

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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262                    7x 7x   7x                                   7x 7x 7x 7x     3x   3x 3x                                                               3x       3x   3x 4x   3x         1x       2x       7x 7x 7x 28x   7x       28x 28x   28x 28x         28x 28x           28x 28x           28x               28x       8x       8x       7x 7x     7x 7x 7x       2x 2x 2x       4x   17x 16x   3x       3x     3x       3x     4x         4x     3x         3x     3x         3x             4x       4x 4x       5x 5x   5x 4x 3x 3x   4x 2x 2x 2x   4x 1x 1x 1x   4x 3x 3x 3x       5x       8x 32x 32x 1x 1x     8x       20x      
"use strict"
import {
    LocalStorageScenarios,
    DynamicSettings,
    RecursivePartial,
    Scenario,
    MetricData,
    AppSettings,
    Settings
} from "../../../codeCharta.model"
import { convertToVectors } from "../../../util/settingsHelper"
import defaultScenarios from "../../../assets/scenarios.json"
import { ExportScenario } from "../../../codeCharta.api.model"
import { Vector3 } from "three"
 
export type ScenarioMetricType = "Camera-Position" | "Edge-Metric" | "Area-Metric" | "Height-Metric" | "Color-Metric"
 
export type ScenarioMetricProperty = {
    metricType: ScenarioMetricType
    metricName: string
    isSelected: boolean
    isDisabled: boolean
    savedValues: unknown
}
 
export interface ScenarioItem {
    scenarioName: string
    isScenarioApplicable: boolean
    icons: { faIconClass: string; isSaved: boolean; tooltip: string }[]
}
 
export class ScenarioHelper {
    private static readonly SCENARIOS_LOCAL_STORAGE_VERSION = "1.0.0"
    private static readonly SCENARIOS_LOCAL_STORAGE_ELEMENT = "scenarios"
    static scenarios: Map<string, RecursivePartial<Scenario>> = ScenarioHelper.loadScenarios()
 
    static getScenarioItems(metricData: MetricData) {
        const scenarioItems: ScenarioItem[] = []
 
        for (const scenario of this.scenarios.values()) {
            scenarioItems.push({
                scenarioName: scenario.name,
                isScenarioApplicable: this.isScenarioApplicable(scenario, metricData),
                icons: [
                    {
                        faIconClass: "fa-video-camera",
                        isSaved: Boolean(scenario.camera),
                        tooltip: "Camera angle"
                    },
                    {
                        faIconClass: "fa-arrows-alt",
                        isSaved: Boolean(scenario.area),
                        tooltip: "Area metric"
                    },
                    {
                        faIconClass: "fa-arrows-v",
                        isSaved: Boolean(scenario.height),
                        tooltip: "Height metric"
                    },
                    {
                        faIconClass: "fa-paint-brush",
                        isSaved: Boolean(scenario.color),
                        tooltip: "Color metric"
                    },
                    {
                        faIconClass: "fa-exchange",
                        isSaved: Boolean(scenario.edge),
                        tooltip: "Edge metric"
                    }
                ]
            })
        }
        return scenarioItems
    }
 
    private static isScenarioApplicable(scenario: RecursivePartial<Scenario>, metricData: MetricData) {
        const { area, color, height, edge } = scenario
 
        if (area || color || height) {
            const nodeMetricSet = new Set(metricData.nodeMetricData.map(data => data.name))
 
            if (
                (area && !nodeMetricSet.has(area.areaMetric)) ||
                (color && !nodeMetricSet.has(color.colorMetric)) ||
                (height && !nodeMetricSet.has(height.heightMetric))
            ) {
                return false
            }
        }
 
        return !(edge && !metricData.edgeMetricData.some(x => x.name === edge.edgeMetric))
    }
 
    private static getPreLoadScenarios() {
        const scenariosAsSettings = this.importScenarios(defaultScenarios)
        const scenario: Map<string, RecursivePartial<Scenario>> = new Map()
        for (const setting of scenariosAsSettings) {
            scenario.set(setting.name, this.transformScenarioAsSettingsToScenario(setting))
        }
        return scenario
    }
 
    private static transformScenarioAsSettingsToScenario(exportScenario: ExportScenario) {
        const scenario: RecursivePartial<Scenario> = { name: exportScenario.name }
        const { dynamicSettings, appSettings } = exportScenario.settings
 
        if (dynamicSettings.areaMetric !== undefined) {
            scenario.area = {
                areaMetric: dynamicSettings.areaMetric,
                margin: dynamicSettings.margin
            }
        }
        if (dynamicSettings.heightMetric !== undefined) {
            scenario.height = {
                heightMetric: dynamicSettings.heightMetric,
                labelSlider: appSettings.amountOfTopLabels,
                heightSlider: appSettings.scaling
            }
        }
        if (dynamicSettings.colorMetric !== undefined) {
            scenario.color = {
                colorMetric: dynamicSettings.colorMetric,
                colorRange: dynamicSettings.colorRange,
                mapColors: appSettings.mapColors
            }
        }
        Iif (dynamicSettings.edgeMetric !== undefined) {
            scenario.edge = {
                edgeMetric: dynamicSettings.edgeMetric,
                edgeHeight: appSettings.edgeHeight,
                edgePreview: appSettings.amountOfEdgePreviews
            }
        }
 
        return scenario
    }
 
    private static setScenariosToLocalStorage(scenarios: Map<string, RecursivePartial<Scenario>>) {
        const newLocalStorageElement: LocalStorageScenarios = {
            version: this.SCENARIOS_LOCAL_STORAGE_VERSION,
            scenarios: [...scenarios]
        }
        localStorage.setItem(this.SCENARIOS_LOCAL_STORAGE_ELEMENT, JSON.stringify(newLocalStorageElement))
    }
 
    private static loadScenarios() {
        const ccLocalStorage: LocalStorageScenarios = JSON.parse(localStorage.getItem(this.SCENARIOS_LOCAL_STORAGE_ELEMENT))
        Iif (ccLocalStorage) {
            return new Map(ccLocalStorage.scenarios)
        }
        const scenarios = this.getPreLoadScenarios()
        this.setScenariosToLocalStorage(scenarios)
        return scenarios
    }
 
    static addScenario(scenarioName: string, scenarioMetricProperties: ScenarioMetricProperty[]) {
        const newScenario = ScenarioHelper.createNewScenario(scenarioName, scenarioMetricProperties)
        this.scenarios.set(newScenario.name, newScenario)
        this.setScenariosToLocalStorage(this.scenarios)
    }
 
    static createNewScenario(scenarioName: string, scenarioMetricProperties: ScenarioMetricProperty[]) {
        const newScenario: RecursivePartial<Scenario> = { name: scenarioName }
 
        for (const property of scenarioMetricProperties.filter(p => p.isSelected)) {
            switch (property.metricType) {
                case "Camera-Position":
                    newScenario.camera = {
                        camera: property.savedValues["camera"],
                        cameraTarget: property.savedValues["cameraTarget"]
                    }
                    break
 
                case "Area-Metric":
                    newScenario.area = {
                        areaMetric: property.metricName,
                        margin: property.savedValues as number
                    }
                    break
 
                case "Height-Metric":
                    newScenario.height = {
                        heightMetric: property.metricName,
                        heightSlider: property.savedValues["heightSlider"],
                        labelSlider: property.savedValues["labelSlider"]
                    }
                    break
 
                case "Color-Metric":
                    newScenario.color = {
                        colorMetric: property.metricName,
                        colorRange: property.savedValues["colorRange"],
                        mapColors: property.savedValues["mapColors"]
                    }
                    break
 
                case "Edge-Metric":
                    newScenario.edge = {
                        edgeMetric: property.metricName,
                        edgePreview: property.savedValues["edgePreview"],
                        edgeHeight: property.savedValues["edgeHeight"]
                    }
                    break
 
                default:
                    throw new Error(`Unknown metric type "${property.metricType}" detected`)
            }
        }
 
        return newScenario
    }
 
    static deleteScenario(scenarioName: string) {
        this.scenarios.delete(scenarioName)
        this.setScenariosToLocalStorage(this.scenarios)
    }
 
    static getScenarioSettings(scenario: RecursivePartial<Scenario>): RecursivePartial<Settings> {
        const partialDynamicSettings: RecursivePartial<DynamicSettings> = {}
        const partialAppSettings: RecursivePartial<AppSettings> = {}
 
        if (scenario) {
            if (scenario.area) {
                partialDynamicSettings.areaMetric = scenario.area.areaMetric
                partialDynamicSettings.margin = scenario.area.margin
            }
            if (scenario.height) {
                partialDynamicSettings.heightMetric = scenario.height.heightMetric
                partialAppSettings.amountOfTopLabels = scenario.height.labelSlider
                partialAppSettings.scaling = scenario.height.heightSlider
            }
            if (scenario.color) {
                partialDynamicSettings.colorMetric = scenario.color.colorMetric
                partialDynamicSettings.colorRange = scenario.color.colorRange
                partialAppSettings.mapColors = scenario.color.mapColors
            }
            if (scenario.edge) {
                partialDynamicSettings.edgeMetric = scenario.edge.edgeMetric
                partialAppSettings.edgeHeight = scenario.edge.edgeHeight
                partialAppSettings.amountOfEdgePreviews = scenario.edge.edgePreview
            }
        }
 
        return { appSettings: partialAppSettings, dynamicSettings: partialDynamicSettings }
    }
 
    static importScenarios(scenarios: ExportScenario[]) {
        for (const scenario of scenarios) {
            convertToVectors(scenario.settings)
            if (scenario.camera) {
                scenario.camera.camera = new Vector3(scenario.camera.camera.x, scenario.camera.camera.y, scenario.camera.camera.z)
                scenario.camera.cameraTarget = new Vector3(scenario.camera.camera.x, scenario.camera.camera.y, scenario.camera.camera.z)
            }
        }
        return scenarios
    }
 
    static isScenarioExisting(scenarioName: string) {
        return this.scenarios.has(scenarioName)
    }
}