All files / app/codeCharta/ui/filePanel/filePanelDeltaSelector filePanelDeltaSelector.component.ts

73.21% Statements 41/56
48% Branches 12/25
68.42% Functions 13/19
79.16% Lines 38/48

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 6x 6x 18x 6x 6x         6x 6x   6x 6x       3x 3x       1x 1x       4x 1x         2x           1x                     2x       2x                                       1x     1x           1x       2x          
import { Component } from "@angular/core"
import { Store } from "@ngrx/store"
import { map } from "rxjs"
import { CCFile, CcState, CodeMapNode } from "../../../codeCharta.model"
import { FileSelectionState } from "../../../model/files/files"
import { referenceFileSelector } from "../../../state/selectors/referenceFile/referenceFile.selector"
import { setDeltaComparison, setDeltaReference, switchReferenceAndComparison } from "../../../state/store/files/files.actions"
import { filesSelector } from "../../../state/store/files/files.selector"
import { pictogramBackgroundSelector } from "./pictogramBackground.selector"
import { MatDialog } from "@angular/material/dialog"
import { ALERT_ON_INCOMPATIBLE_MAPS, IncompatibleMapsDialogComponent } from "./incompatibleMapsDialog/incompatibleMapsDialog.component"
import { MatSelect } from "@angular/material/select"
import { MatOption } from "@angular/material/core"
import { AsyncPipe } from "@angular/common"
import { RemoveExtensionPipe } from "../../../util/pipes/removeExtension.pipe"
 
@Component({
    selector: "cc-file-panel-delta-selector",
    templateUrl: "./filePanelDeltaSelector.component.html",
    styleUrls: ["./filePanelDeltaSelector.component.scss"],
    standalone: true,
    imports: [MatSelect, MatOption, AsyncPipe, RemoveExtensionPipe]
})
export class FilePanelDeltaSelectorComponent {
    files$ = this.store.select(filesSelector)
    referenceFile$ = this.store.select(referenceFileSelector)
    comparisonFile$ = this.files$.pipe(map(files => files.find(file => file.selectedAs === FileSelectionState.Comparison)?.file))
    possibleComparisonFiles$ = this.files$.pipe(map(files => files.filter(file => file.selectedAs !== FileSelectionState.Reference)))
    pictogramBackground$ = this.store.select(pictogramBackgroundSelector)
    referenceFile: CCFile
    comparisonFile: CCFile
 
    constructor(
        private store: Store<CcState>,
        private dialog: MatDialog
    ) {
        this.referenceFile$.subscribe(file => (this.referenceFile = file))
        this.comparisonFile$.subscribe(file => (this.comparisonFile = file))
    }
 
    handleDeltaReferenceFileChange(file: CCFile) {
        this.store.dispatch(setDeltaReference({ file }))
        this.showAlertWhenFilesAreIncompatible()
    }
 
    handleDeltaComparisonFileChange(file: CCFile) {
        this.store.dispatch(setDeltaComparison({ file }))
        this.showAlertWhenFilesAreIncompatible()
    }
 
    showAlertWhenFilesAreIncompatible() {
        if (this.alertOnIncompatibleMaps() && this.areMapsIncompatible()) {
            this.openIncompatibleMapsDialog()
        }
    }
 
    alertOnIncompatibleMaps() {
        return localStorage.getItem(ALERT_ON_INCOMPATIBLE_MAPS)
            ? JSON.parse(localStorage.getItem(ALERT_ON_INCOMPATIBLE_MAPS)) === true
            : true
    }
 
    openIncompatibleMapsDialog() {
        this.dialog.open(IncompatibleMapsDialogComponent, {
            panelClass: "cc-incompatible-maps-dialog",
            data: {
                referenceFileName: this.getFileName(this.referenceFile),
                comparisonFileName: this.getFileName(this.comparisonFile),
                fileWithMccMetric: this.getFileWithMccMetric(this.referenceFile, this.comparisonFile)
            }
        })
    }
 
    private getFileName(file: CCFile) {
        return file?.fileMeta.fileName
    }
 
    private hasMccMetric(file: CCFile) {
        return file?.map.children.some(node => this.containsMCCAttribute(node))
    }
 
    private containsMCCAttribute(node: CodeMapNode): boolean {
        Iif (node.attributes["mcc"]) {
            return true
        }
 
        Iif (node.children) {
            for (const child of node.children) {
                Iif (this.containsMCCAttribute(child)) {
                    return true
                }
            }
        }
 
        return false
    }
 
    private getFileWithMccMetric(referenceFile: CCFile, comparisonFile: CCFile) {
        Iif (this.hasMccMetric(referenceFile)) {
            return this.getFileName(referenceFile)
        }
        Iif (this.hasMccMetric(comparisonFile)) {
            return this.getFileName(comparisonFile)
        }
    }
 
    switchReferenceAndComparison() {
        this.store.dispatch(switchReferenceAndComparison())
    }
 
    areMapsIncompatible() {
        Iif (this.referenceFile && this.comparisonFile) {
            return this.hasMccMetric(this.referenceFile) !== this.hasMccMetric(this.comparisonFile)
        }
    }
}