All files / src/app/components/file-upload file-upload.component.ts

84.61% Statements 22/26
62.5% Branches 5/8
66.66% Functions 4/6
84% Lines 21/25

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                                  120x 5x       5x 5x 5x 5x       5x             5x 5x         5x       1x 1x 1x               128x 128x 128x 128x       128x 128x   128x 115x    
import { Component, EventEmitter, inject, Input, Output, ViewChild } from '@angular/core';
import { CommonModule } from '@angular/common';
 
import { ToastModule } from 'primeng/toast';
import { FileSelectEvent, FileUpload, FileUploadModule } from 'primeng/fileupload';
import { ButtonModule } from 'primeng/button';
import { ProgressBarModule } from 'primeng/progressbar';
import { BadgeModule } from 'primeng/badge';
import { CardModule } from 'primeng/card';
import { PrimeNG } from 'primeng/config';
 
@Component({
  selector: 'app-file-upload',
  imports: [ToastModule, FileUploadModule, ButtonModule, ProgressBarModule, BadgeModule, CardModule, CommonModule],
  templateUrl: './file-upload.component.html',
  styleUrl: './file-upload.component.scss',
})
export class FileUploadComponent {
  private config = inject(PrimeNG);
 
  @ViewChild("fileUpload") fileUpload: FileUpload;
 
  @Input() headerText: string = "Excel-format device configuration";
  @Input() fileType: string = ".xlsx,.xls";
  @Input() fileIcon: string = "pi pi-file-excel";
  @Input() required: boolean = false;
 
  selectedFile: File | undefined;
 
  @Output() selectionChanged = new EventEmitter<File | undefined>();
 
  onOpenFileFinder(_: MouseEvent, openFileFinder: () => void) {
      openFileFinder();
  }
 
  updateSelectedFile(event: FileSelectEvent) {
    Eif (event.currentFiles.length > 0) {
        this.selectedFile = event.currentFiles[0];
    }
    else {
        this.selectedFile = undefined;
    }
    this.selectionChanged.emit(this.selectedFile);
  }
 
  onRemoveTemplatingFile(event: MouseEvent, removeFileCallback: (event: MouseEvent, index: number) => void, index: number) {
      removeFileCallback(event, index);
      this.selectedFile = undefined;
      this.selectionChanged.emit(this.selectedFile);
  }
 
  uploadEvent(callback: () => void) {
      callback();
  }
 
  formatSize(bytes: number) {
      const k = 1024;
      const dm = 3;
      const sizes = this.config.translation.fileSizeTypes ?? [];
      Iif (bytes === 0) {
          return `0 ${sizes[0]}`;
      }
 
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      const formattedSize = parseFloat((bytes / Math.pow(k, i)).toFixed(dm));
 
      return `${formattedSize} ${sizes[i]}`;
  }
}