All files / app/codeCharta/ui/ribbonBar/showScenariosButton scenario.service.ts

95.38% Statements 62/65
80% Branches 12/15
85.71% Functions 6/7
95.31% Lines 61/64

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 1175x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x   9x 9x 9x 9x 9x               2x 2x 2x   2x 2x       2x 2x     2x 2x   2x     2x 2x   2x 2x   2x 2x       2x     2x     2x 2x       2x       2x   2x   2x         2x 1x 1x             1x                  
import { Injectable } from "@angular/core"
import { MatDialog } from "@angular/material/dialog"
import { State, Store } from "@ngrx/store"
import { first } from "rxjs"
import { CcState } from "../../../codeCharta.model"
import { getNumberOfTopLabels } from "../../../state/effects/updateVisibleTopLabels/getNumberOfTopLabels"
import { codeMapNodesSelector } from "../../../state/selectors/accumulatedData/codeMapNodes.selector"
import { metricDataSelector } from "../../../state/selectors/accumulatedData/metricData/metricData.selector"
import { selectedColorMetricDataSelector } from "../../../state/selectors/accumulatedData/metricData/selectedColorMetricData.selector"
import { setAmountOfEdgePreviews } from "../../../state/store/appSettings/amountOfEdgePreviews/amountOfEdgePreviews.actions"
import { defaultAmountOfEdgesPreviews } from "../../../state/store/appSettings/amountOfEdgePreviews/amountOfEdgePreviews.reducer"
import { setAmountOfTopLabels } from "../../../state/store/appSettings/amountOfTopLabels/amountOfTopLabels.actions"
import { setEdgeHeight } from "../../../state/store/appSettings/edgeHeight/edgeHeight.actions"
import { defaultEdgeHeight } from "../../../state/store/appSettings/edgeHeight/edgeHeight.reducer"
import { setMapColors } from "../../../state/store/appSettings/mapColors/mapColors.actions"
import { defaultMapColors } from "../../../state/store/appSettings/mapColors/mapColors.reducer"
import { setScaling } from "../../../state/store/appSettings/scaling/scaling.actions"
import { defaultScaling } from "../../../state/store/appSettings/scaling/scaling.reducer"
import { calculateInitialColorRange } from "../../../state/store/dynamicSettings/colorRange/calculateInitialColorRange"
import { setColorRange } from "../../../state/store/dynamicSettings/colorRange/colorRange.actions"
import { setEdgeMetric } from "../../../state/store/dynamicSettings/edgeMetric/edgeMetric.actions"
import { setMargin } from "../../../state/store/dynamicSettings/margin/margin.actions"
import { defaultMargin } from "../../../state/store/dynamicSettings/margin/margin.reducer"
import { setState } from "../../../state/store/state.actions"
import { ThreeCameraService } from "../../codeMap/threeViewer/threeCamera.service"
import { ThreeMapControlsService } from "../../codeMap/threeViewer/threeMapControls.service"
import { ErrorDialogComponent } from "../../dialogs/errorDialog/errorDialog.component"
import { ScenarioHelper } from "./scenarioHelper"
 
@Injectable({ providedIn: "root" })
export class ScenarioService {
    constructor(
        private state: State<CcState>,
        private store: Store<CcState>,
        private dialog: MatDialog,
        private threeCameraService: ThreeCameraService,
        private threeOrbitControlsService: ThreeMapControlsService
    ) {}
 
    getScenarios() {
        return ScenarioHelper.getScenarioItems(metricDataSelector(this.state.getValue()))
    }
 
    applyScenario(name: string) {
        const scenario = ScenarioHelper.scenarios.get(name)
        const scenarioSettings = ScenarioHelper.getScenarioSettings(scenario)
        this.store.dispatch(setState({ value: scenarioSettings }))
 
        if (!scenarioSettings.appSettings.amountOfTopLabels) {
            this.store
                .select(codeMapNodesSelector)
                .pipe(first())
                .subscribe(codeMapNodes => {
                    const amountOfTopLabels = getNumberOfTopLabels(codeMapNodes)
                    this.store.dispatch(setAmountOfTopLabels({ value: amountOfTopLabels }))
                })
        }
        if (!scenarioSettings.appSettings.mapColors) {
            this.store.dispatch(setMapColors({ value: defaultMapColors }))
        }
        Iif (!scenarioSettings.appSettings.edgeHeight) {
            this.store.dispatch(setEdgeHeight({ value: defaultEdgeHeight }))
        }
        if (!scenarioSettings.appSettings.amountOfEdgePreviews) {
            this.store.dispatch(setAmountOfEdgePreviews({ value: defaultAmountOfEdgesPreviews }))
        }
        if (!scenarioSettings.appSettings.scaling) {
            this.store.dispatch(setScaling({ value: defaultScaling }))
        }
        if (!scenarioSettings.dynamicSettings.colorRange) {
            this.store
                .select(selectedColorMetricDataSelector)
                .pipe(first())
                .subscribe(selectedColorMetricData => {
                    this.store.dispatch(setColorRange({ value: calculateInitialColorRange(selectedColorMetricData) }))
                })
        }
        Iif (!scenarioSettings.dynamicSettings.margin) {
            this.store.dispatch(setMargin({ value: defaultMargin }))
        }
        if (!scenarioSettings.dynamicSettings.edgeMetric) {
            this.store
                .select(metricDataSelector)
                .pipe(first())
                .subscribe(metricData => {
                    this.store.dispatch(setEdgeMetric({ value: metricData.edgeMetricData[0]?.name }))
                })
        }
 
        if (scenario.camera) {
            // @ts-ignore -- we know that it is not a partial when it is set
            this.threeCameraService.setPosition(scenario.camera.camera)
            // @ts-ignore -- we know that it is not a partial when it is set
            this.threeOrbitControlsService.setControlTarget(scenario.camera.cameraTarget)
        }
    }
 
    removeScenario(name) {
        if (name !== "Complexity") {
            ScenarioHelper.deleteScenario(name)
            this.dialog.open(ErrorDialogComponent, {
                data: {
                    title: "Info",
                    message: `${name} deleted.`
                }
            })
        } else {
            this.dialog.open(ErrorDialogComponent, {
                data: {
                    title: "Error",
                    message: `${name} cannot be deleted as it is the default Scenario.`
                }
            })
        }
    }
}