All files / file-uploader si-file-dropzone.component.ts

97.29% Statements 36/37
75% Branches 3/4
92.3% Functions 12/13
97.22% Lines 35/36

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 165 166 167 168 169 170 171 172 173 174 175                                                    1x                 44x 44x                   44x                 44x 44x                   44x 44x                   44x 44x                   44x     44x             44x       44x             44x         44x         44x             44x   44x 6x 6x     44x   44x   44x     42x 42x 1x   41x   42x       1x 1x 1x               45x       10x             5x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
  output,
  viewChild
} from '@angular/core';
import { addIcons, elementUpload, SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
import { SiFileUploadDirective, FileUploadError } from './si-file-upload.directive';
import { UploadFile } from './si-file-uploader.model';
 
@Component({
  selector: 'si-file-dropzone',
  imports: [SiIconComponent, SiTranslatePipe, SiFileUploadDirective],
  templateUrl: './si-file-dropzone.component.html',
  styleUrl: './si-file-dropzone.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiFileDropzoneComponent {
  /**
   * Text or translation key of the input file selector (is combined with the `uploadTextRest`).
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.FILE_SELECT:click to upload`)
   * ```
   */
  readonly uploadTextFileSelect = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.FILE_SELECT:click to upload`)
  );
  /**
   * Text or translation key of the drag&drop field (is combined with the `uploadTextFileSelect`).
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.DROP:Drop files here or`)
   * ```
   */
  readonly uploadDropText = input(t(() => $localize`:@@SI_FILE_UPLOADER.DROP:Drop files here or`));
  /**
   * Text or translation key for max file size.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.MAX_SIZE:Maximum upload size`)
   * ```
   */
  readonly maxFileSizeText = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.MAX_SIZE:Maximum upload size`)
  );
  /**
   * Text or translation key for accepted types.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.ACCEPTED_FILE_TYPES:Accepted file types`)
   * ```
   */
  readonly acceptText = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.ACCEPTED_FILE_TYPES:Accepted file types`)
  );
  /**
   * Text or translation key of message title if incorrect file type is dragged / dropped.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_TYPE:Incorrect file type selected`)
   * ```
   */
  readonly errorTextFileType = input(
    t(() => $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_TYPE:Incorrect file type selected`)
  );
  /**
   * Message or translation key if file exceeds the maximum file size limit.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_SIZE_EXCEEDED:File exceeds allowed maximum size`)
   * ```
   */
  readonly errorTextFileMaxSize = input(
    t(
      () =>
        $localize`:@@SI_FILE_UPLOADER.ERROR_FILE_SIZE_EXCEEDED:File exceeds allowed maximum size`
    )
  );
  /**
   * Define which file types are suggested in file browser.
   * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#attr-accept
   */
  readonly accept = input<string>();
  /**
   * Define maximal allowed file size in bytes.
   */
  readonly maxFileSize = input<number>();
  /**
   * Defines whether the file input allows selecting multiple files.
   * When {@link directoryUpload} is enabled, this will have no effect.
   *
   * @defaultValue false
   */
  readonly multiple = input(false, { transform: booleanAttribute });
  /**
   * Event emitted when valid files are added.
   * Invalid files are also included here, but with status 'invalid' and an errorText describing why they were ignored.
   */
  readonly filesAdded = output<UploadFile[]>();
 
  /**
   * Event emitted when file validation errors occur, including files that were ignored due to size or type.
   */
  readonly fileError = output<FileUploadError>();
 
  /**
   * Enable directory upload.
   *
   * @defaultValue false
   */
  readonly directoryUpload = input(false, { transform: booleanAttribute });
 
  protected readonly maxFileSizeString = computed(() => {
    const maxFileSize = this.maxFileSize();
    return maxFileSize ? this.fileUploadDirective().fileSizeToString(maxFileSize) : '';
  });
 
  protected readonly icons = addIcons({ elementUpload });
 
  protected dragOver = false;
 
  private readonly fileUploadDirective = viewChild.required(SiFileUploadDirective);
 
  protected dropHandler(event: DragEvent): void {
    event.preventDefault();
    if (this.directoryUpload()) {
      this.fileUploadDirective().handleItems(event.dataTransfer!.items);
    } else {
      this.fileUploadDirective().handleFiles(event.dataTransfer!.files);
    }
    this.dragOver = false;
  }
 
  protected dragOverHandler(event: DragEvent): void {
    event.preventDefault();
    event.stopPropagation();
    this.dragOver = true;
  }
 
  protected inputEnterHandler(): void {
    this.fileUploadDirective().triggerClick();
  }
 
  protected onFilesAdded(files: UploadFile[]): void {
    this.filesAdded.emit(files);
  }
 
  protected onFileError(error: FileUploadError): void {
    this.fileError.emit(error);
  }
 
  /**
   * Reset all the files inside the native file input (and therefore the dropzone).
   */
  reset(): void {
    this.fileUploadDirective().reset();
  }
}