All files / app/codeCharta/ui/ribbonBar/colorSettingsPanel colorSettingsPanel.component.ts

95.58% Statements 65/68
92.85% Branches 13/14
81.81% Functions 9/11
95.52% Lines 64/67

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 1233x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x                                       3x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x       13x 13x     13x 2x 2x 2x       13x   16x 16x         13x 1x 1x 1x   1x 1x   1x   1x 1x               1x       2x 2x       1x 1x     13x 2x 2x     13x          
import { Component } from "@angular/core"
import { MatCheckboxChange, MatCheckbox } from "@angular/material/checkbox"
import { State, Store } from "@ngrx/store"
import { map } from "rxjs"
import { CcState, ColorLabelOptions, ColorMode, ColorRange } from "../../../codeCharta.model"
import { selectedColorMetricDataSelector } from "../../../state/selectors/accumulatedData/metricData/selectedColorMetricData.selector"
import { isDeltaStateSelector } from "../../../state/selectors/isDeltaState.selector"
import { setColorLabels } from "../../../state/store/appSettings/colorLabels/colorLabels.actions"
import { colorLabelsSelector } from "../../../state/store/appSettings/colorLabels/colorLabels.selector"
import { invertColorRange, invertDeltaColors } from "../../../state/store/appSettings/mapColors/mapColors.actions"
import { colorMetricSelector } from "../../../state/store/dynamicSettings/colorMetric/colorMetric.selector"
import { setColorMode } from "../../../state/store/dynamicSettings/colorMode/colorMode.actions"
import { colorModeSelector } from "../../../state/store/dynamicSettings/colorMode/colorMode.selector"
import { calculateInitialColorRange } from "../../../state/store/dynamicSettings/colorRange/calculateInitialColorRange"
import { setColorRange } from "../../../state/store/dynamicSettings/colorRange/colorRange.actions"
import { debounce } from "../../../util/debounce"
import { HandleValueChange, MetricColorRangeSliderComponent } from "./metricColorRangeSlider/metricColorRangeSlider.component"
import { metricColorRangeColorsSelector } from "./selectors/metricColorRangeColors.selector"
import { metricColorRangeValuesSelector } from "./selectors/metricColorRangeValues.selector"
import { ResetSettingsButtonComponent } from "../../resetSettingsButton/resetSettingsButton.component"
import { MetricColorRangeDiagramComponent } from "./metricColorRangeDiagram/metricColorRangeDiagram.component"
import { MatFormField, MatLabel } from "@angular/material/form-field"
import { MatSelect } from "@angular/material/select"
import { MatOption } from "@angular/material/core"
import { ColorPickerForMapColorComponent } from "../../colorPickerForMapColor/colorPickerForMapColor.component"
import { AsyncPipe } from "@angular/common"
 
@Component({
    selector: "cc-color-settings-panel",
    templateUrl: "./colorSettingsPanel.component.html",
    styleUrls: ["./colorSettingsPanel.component.scss"],
    standalone: true,
    imports: [
        MetricColorRangeSliderComponent,
        ResetSettingsButtonComponent,
        MetricColorRangeDiagramComponent,
        MatFormField,
        MatLabel,
        MatSelect,
        MatOption,
        ColorPickerForMapColorComponent,
        MatCheckbox,
        AsyncPipe
    ]
})
export class ColorSettingsPanelComponent {
    colorMode$ = this.store.select(colorModeSelector)
    colorLabels$ = this.store.select(colorLabelsSelector)
    colorMetric$ = this.store.select(colorMetricSelector)
    isDeltaState$ = this.store.select(isDeltaStateSelector)
    sliderValues$ = this.store.select(metricColorRangeValuesSelector)
    sliderColors$ = this.store.select(metricColorRangeColorsSelector)
    isAttributeDescriptionInversed$ = this.checkIsAttributeDirectionReversed()
    isColorRangeInverted = false
    areDeltaColorsInverted = false
 
    private newLeftValue: null | number = null
    private newRightValue: null | number = null
    isAttributeDirectionInversed: boolean
 
    constructor(
        private store: Store<CcState>,
        private state: State<CcState>
    ) {}
 
    handleValueChange: HandleValueChange = ({ newLeftValue, newRightValue }) => {
        this.newLeftValue = newLeftValue ?? this.newLeftValue
        this.newRightValue = newRightValue ?? this.newRightValue
        this.updateColorRangeDebounced()
    }
 
    private checkIsAttributeDirectionReversed() {
        return this.colorMetric$.pipe(
            map(colorMetric => {
                const attributeDescriptors = this.state.getValue().fileSettings.attributeDescriptors
                return attributeDescriptors[colorMetric]?.direction === 1
            })
        )
    }
 
    private updateColorRangeDebounced = debounce(() => {
        const newColorRange: Partial<ColorRange> = {}
        if (this.newLeftValue !== null) {
            newColorRange.from = this.newLeftValue
        }
        if (this.newRightValue !== null) {
            newColorRange.to = this.newRightValue
        }
        this.store.dispatch(setColorRange({ value: newColorRange }))
 
        this.newLeftValue = null
        this.newRightValue = null
    }, 400)
 
    handleColorModeChange(gradient: ColorMode) {
        this.store.dispatch(setColorMode({ value: gradient }))
    }
 
    toggleColorLabel(change: MatCheckboxChange, colorLabelToToggle: keyof ColorLabelOptions) {
        this.store.dispatch(setColorLabels({ value: { [colorLabelToToggle]: change.checked } }))
    }
 
    handleIsColorRangeInvertedChange(isColorRangeInverted: boolean) {
        this.isColorRangeInverted = isColorRangeInverted
        this.store.dispatch(invertColorRange())
    }
 
    handleAreDeltaColorsInverted(areDeltaColorsInverted: boolean) {
        this.areDeltaColorsInverted = areDeltaColorsInverted
        this.store.dispatch(invertDeltaColors())
    }
 
    resetInvertColorCheckboxes = () => {
        this.isColorRangeInverted = false
        this.areDeltaColorsInverted = false
    }
 
    resetColorRange = () => {
        const selectedColorMetricData = selectedColorMetricDataSelector(this.state.getValue())
        this.store.dispatch(setColorRange({ value: calculateInitialColorRange(selectedColorMetricData) }))
    }
}