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

100% Statements 98/98
100% Branches 18/18
100% Functions 36/36
100% Lines 80/80

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 1654x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x                           4x     25x 25x 25x 25x   25x 25x   25x 27x 46x 46x     25x     3x       4x 4x 8x 4x     4x       4x 3x       4x 3x 4x 3x     1x     4x 1x         1x 1x       16x 8x 1x     7x 2x 6x 1x   10x     7x       4x 2x       8x 4x 4x 4x 4x       7x 14x   7x 7x       20x 6x 6x     14x 14x 11x   3x         4x 4x       7x 7x       11x 11x       28x 3x     23x 11x 3x     8x      
import { Component, OnDestroy, ViewChild } from "@angular/core"
import { filesSelector } from "../../../state/store/files/files.selector"
import { removeFiles, setStandard } from "../../../state/store/files/files.actions"
import { CCFile, CcState } from "../../../codeCharta.model"
import { FileSelectionState, FileState } from "../../../model/files/files"
import { Store } from "@ngrx/store"
import { MatSelect } from "@angular/material/select"
import { MatOption } from "@angular/material/core"
import { RemoveExtensionPipe } from "../../../util/pipes/removeExtension.pipe"
import { take } from "rxjs"
import { RemoveOrAddFileButtonComponent } from "./removeOrAddFileButton/removeOrAddFileButton.component"
import { NgStyle } from "@angular/common"
import { MatTooltip } from "@angular/material/tooltip"
 
type FileRemovedInUIState = {
    file: CCFile
    isRemoved: boolean
}
 
@Component({
    selector: "cc-file-panel-file-selector",
    templateUrl: "./filePanelFileSelector.component.html",
    styleUrls: ["./filePanelFileSelector.component.scss"],
    standalone: true,
    imports: [MatSelect, MatOption, RemoveOrAddFileButtonComponent, RemoveExtensionPipe, NgStyle, MatTooltip]
})
export class FilePanelFileSelectorComponent implements OnDestroy {
    @ViewChild("fileSelect") select: MatSelect
 
    filesInUI: FileRemovedInUIState[] = []
    selectedFilesInUI: CCFile[] = []
    filesInStore: FileState[] = []
    private closedByApply = false
 
    applyButtonTooltip = ""
    applyButtonDisabled = false
 
    private readonly filesSubscription = this.store.select(filesSelector).subscribe(fileStates => {
        this.filesInStore = fileStates
        this.filesInUI = fileStates.map(file => ({ file: file.file, isRemoved: false }))
        this.selectedFilesInUI = fileStates.filter(file => file.selectedAs === FileSelectionState.Partial).map(file => file.file)
    })
 
    constructor(private readonly store: Store<CcState>) {}
 
    ngOnDestroy(): void {
        this.filesSubscription.unsubscribe()
    }
 
    handleSelectedFilesChanged(selectedFiles: CCFile[]) {
        this.selectedFilesInUI = selectedFiles
        for (const file of this.filesInUI) {
            if (selectedFiles.includes(file.file)) {
                file.isRemoved = false
            }
        }
        this.updateApplyButtonState()
    }
 
    handleOpenedChanged(opened: boolean) {
        if (!this.closedByApply && !opened) {
            this.store
                .select(filesSelector)
                .pipe(take(1))
                .subscribe(fileStates => {
                    this.filesInUI = fileStates.map(file => ({ file: file.file, isRemoved: false }))
                    this.selectedFilesInUI = fileStates
                        .filter(file => file.selectedAs === FileSelectionState.Partial)
                        .map(file => file.file)
                })
        } else {
            this.closedByApply = false
        }
 
        if (opened) {
            this.setApplyButtonStateToNoChangesToApply()
        }
    }
 
    handleSelectZeroFiles() {
        this.selectedFilesInUI = []
        this.setApplyButtonStateToNoMapSelected()
    }
 
    handleInvertSelectedFiles() {
        const notRemovedFiles = this.filesInUI.filter(file => !file.isRemoved)
        if (notRemovedFiles.length === 0) {
            return
        }
 
        if (this.selectedFilesInUI.length === 0) {
            this.selectedFilesInUI = notRemovedFiles.map(file => file.file)
        } else if (this.selectedFilesInUI.length === notRemovedFiles.length) {
            this.selectedFilesInUI = []
        } else {
            this.selectedFilesInUI = notRemovedFiles.filter(file => !this.selectedFilesInUI.includes(file.file)).map(file => file.file)
        }
 
        this.updateApplyButtonState()
    }
 
    handleSelectAllFiles() {
        this.selectedFilesInUI = this.filesInUI.filter(file => !file.isRemoved).map(file => file.file)
        this.updateApplyButtonState()
    }
 
    handleApplyFileChanges() {
        const fileNamesToRemove = this.filesInUI.filter(file => file.isRemoved).map(file => file.file.fileMeta.fileName)
        this.store.dispatch(setStandard({ files: this.selectedFilesInUI }))
        this.store.dispatch(removeFiles({ fileNames: fileNamesToRemove }))
        this.closedByApply = true
        this.select.close()
    }
 
    handleAddOrRemoveFile(fileName: string) {
        this.filesInUI = this.filesInUI.map(file =>
            file.file.fileMeta.fileName === fileName ? { file: file.file, isRemoved: !file.isRemoved } : file
        )
        this.selectedFilesInUI = this.selectedFilesInUI.filter(file => file.fileMeta.fileName !== fileName)
        this.updateApplyButtonState()
    }
 
    private updateApplyButtonState() {
        if (this.selectedFilesInUI.length === 0) {
            this.setApplyButtonStateToNoMapSelected()
            return
        }
 
        const uiDiffersFromStore = this.uiSelectionDiffersFromStore()
        if (uiDiffersFromStore) {
            this.setApplyButtonStateEnabled()
        } else {
            this.setApplyButtonStateToNoChangesToApply()
        }
    }
 
    private setApplyButtonStateToNoChangesToApply() {
        this.applyButtonTooltip = "No changes to apply"
        this.applyButtonDisabled = true
    }
 
    private setApplyButtonStateToNoMapSelected() {
        this.applyButtonTooltip = "Select at least one map"
        this.applyButtonDisabled = true
    }
 
    private setApplyButtonStateEnabled() {
        this.applyButtonTooltip = ""
        this.applyButtonDisabled = false
    }
 
    private uiSelectionDiffersFromStore() {
        if (this.filesInUI.some(file => file.isRemoved)) {
            return true
        }
 
        const selectedFilesInStore = this.filesInStore.filter(file => file.selectedAs === FileSelectionState.Partial).map(file => file.file)
        if (this.selectedFilesInUI.length !== selectedFilesInStore.length) {
            return true
        }
 
        return !this.selectedFilesInUI.every(file => selectedFilesInStore.includes(file)) //this combined with the assumption that the arrays are the same length and no file can appear more than once is enough to determine if the arrays have the same content
    }
}