All files / photo-upload si-photo-upload.component.ts

92.75% Statements 128/138
67.74% Branches 21/31
96.29% Functions 26/27
92.42% Lines 122/132

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554                                                                                                                            1x 1x             11x             11x                     11x             11x                     11x     11x                         11x     11x                 11x               11x                   11x                   11x 11x                     11x 11x                     11x                   11x                   11x                   11x         11x             11x             11x               11x                 11x               11x                 11x               11x               11x                 11x           11x         11x         11x   11x 11x 11x     11x         11x           11x 11x 13x   11x 11x 11x               11x   11x                                         11x 11x 11x     9x 2x 2x     9x         9x 4x 4x 4x 4x 4x   9x             9x 1x         11x             3x     3x 1x 1x 1x 1x   2x                   2x 2x 1x 1x 1x           5x 5x 5x 5x     5x 5x   14x 5x 5x 1x 1x   4x 4x 4x 4x 4x 1x 1x   3x 3x 3x 3x 3x     3x     3x       2x 2x 2x           1x 1x 1x 1x 1x 1x                 1x 1x 1x         1x                             2x                   2x 2x             4x     4x         17x       7x 7x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { NgTemplateOutlet } from '@angular/common';
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  ElementRef,
  inject,
  input,
  model,
  OnChanges,
  OnDestroy,
  signal,
  SimpleChanges,
  TemplateRef,
  viewChild
} from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { SiAvatarBackgroundColorDirective } from '@siemens/element-ng/avatar';
import {
  addIcons,
  elementCancel,
  elementCircleFilled,
  elementStateExclamationMark,
  SiIconComponent
} from '@siemens/element-ng/icon';
import { ModalRef, SiModalService } from '@siemens/element-ng/modal';
import { SiTranslatePipe, t, TranslatableString } from '@siemens/element-translate-ng/translate';
import { CropperPosition, ImageCroppedEvent, ImageCropperComponent } from 'ngx-image-cropper';
 
import { SiImageCropperStyleComponent } from './si-image-cropper-style.component';
 
/**
 * A component used to upload, edit, and delete a user's photo. The user can upload
 * a photo either via file browser or a URL to the photo. You can set the source
 * photo with the `sourcePhoto` Data URL input or the `sourcePhotoUrl` URL input.
 * If you already have a cropped image you can set it with the `croppedPhoto` input.
 * Cropping changes are emitted via the `croppedPhotoChange` output.
 */
@Component({
  selector: 'si-photo-upload',
  imports: [
    NgTemplateOutlet,
    ImageCropperComponent,
    SiIconComponent,
    SiImageCropperStyleComponent,
    SiTranslatePipe
  ],
  templateUrl: './si-photo-upload.component.html',
  styleUrl: './si-photo-upload.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
  hostDirectives: [
    {
      directive: SiAvatarBackgroundColorDirective,
      inputs: ['color', 'autoColor']
    }
  ]
})
export class SiPhotoUploadComponent implements OnChanges, OnDestroy {
  private static idCounter = 0;
 
  /**
   * Indicate that changing or uploads are disabled.
   *
   * @defaultValue false
   */
  readonly readonly = input(false, { transform: booleanAttribute });
 
  /**
   * Optionally disable image cropping.
   *
   * @defaultValue false
   */
  readonly disabledCropping = input(false, { transform: booleanAttribute });
 
  /**
   * Accepted image formats for the file selection dialog.
   *
   * @defaultValue '.png, .jpg, .jpeg'
   *
   * @see
   *
   * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#attr-accept
   */
  readonly acceptedUploadFormats = input('.png, .jpg, .jpeg');
 
  /**
   * Maximum allowed file size of the uploaded file in kilobytes.
   *
   * @defaultValue 2048
   */
  readonly maxFileSize = input(2048);
 
  /**
   * If the uploaded file is of an unsupported type, this
   * error message will be displayed to the user.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.ERROR_FILE_TYPE:The image file is not valid. Please upload a PNG or JP(E)G.`)
   * ```
   */
  readonly uploadErrorWrongType = input(
    t(
      () =>
        $localize`:@@SI_PHOTO_UPLOAD.ERROR_FILE_TYPE:The image file is not valid. Please upload a PNG or JP(E)G.`
    )
  );
 
  /**
   * If the uploaded file exceeds the allowed upload size, this
   * error message will be displayed to the user.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.ERROR_FILE_SIZE_EXCEEDED:The actual file size {{mb}} MB exceeds the {{maxSizeMb}} MB limit.`)
   * ```
   */
  readonly uploadErrorTooBig = input(
    t(
      () =>
        $localize`:@@SI_PHOTO_UPLOAD.ERROR_FILE_SIZE_EXCEEDED:The actual file size {{mb}} MB exceeds the {{maxSizeMb}} MB limit.`
    )
  );
 
  /**
   * Alternative text for the photo.
   *
   * @defaultValue ''
   */
  readonly photoAltText = input<TranslatableString>('');
 
  /**
   * Alternative text for the photo´s placeholder.
   * The value will be used to calculate the background color when `autoColor` is true.
   *
   * @defaultValue ''
   */
  readonly placeholderAltText = input<TranslatableString>('');
 
  /**
   * Text for the button changing the photo.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.CHANGE_PHOTO:Change`)
   * ```
   */
  readonly changePhotoText = input(t(() => $localize`:@@SI_PHOTO_UPLOAD.CHANGE_PHOTO:Change`));
 
  /**
   * Cropper frame aria label.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.CROPPER_FRAME_LABEL:Crop photo`)
   * ```
   */
  readonly cropperFrameAriaLabel = input(
    t(() => $localize`:@@SI_PHOTO_UPLOAD.CROPPER_FRAME_LABEL:Crop photo`)
  );
 
  /**
   * Text for the button uploading a photo.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.UPLOAD_PHOTO:Upload photo`)
   * ```
   */
  readonly uploadPhotoText = input(
    t(() => $localize`:@@SI_PHOTO_UPLOAD.UPLOAD_PHOTO:Upload photo`)
  );
 
  /**
   * Text for the button removing the photo.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.REMOVE:Remove`)
   * ```
   */
  readonly removePhotoText = input(t(() => $localize`:@@SI_PHOTO_UPLOAD.REMOVE:Remove`));
 
  /**
   * Text for the button cancelling the editing process.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.CANCEL:Cancel`)
   * ```
   */
  readonly cancelEditText = input(t(() => $localize`:@@SI_PHOTO_UPLOAD.CANCEL:Cancel`));
 
  /**
   * Text for the button applying the edited photo.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.APPLY_PHOTO:Apply`)
   * ```
   */
  readonly applyEditText = input(t(() => $localize`:@@SI_PHOTO_UPLOAD.APPLY_PHOTO:Apply`));
 
  /**
   * Text displayed as header of the editing modal.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PHOTO_UPLOAD.MODAL_TITLE:Avatar photo`)
   * ```
   */
  readonly modalHeader = input(t(() => $localize`:@@SI_PHOTO_UPLOAD.MODAL_TITLE:Avatar photo`));
 
  /**
   * Text displayed as description of the editing modal.
   */
  readonly modalDescription = input<TranslatableString>();
 
  /**
   * Output format of the edited image.
   *
   * @defaultValue 'jpeg'
   */
  readonly cropperImageFormat = input<'png' | 'jpeg'>('jpeg');
 
  /**
   * The width / height ratio (e.g. 1 / 1 for a square, 4 / 3, 16 / 9 ...).
   *
   * @defaultValue 1
   */
  readonly cropperAspectRatio = input(1);
 
  /**
   * Whether to keep the width and height of the cropped image equal according
   * to the aspectRatio.
   *
   * @defaultValue true
   */
  readonly cropperMaintainAspectRatio = input(true, { transform: booleanAttribute });
 
  /**
   * When set to true, padding will be added around the image to make it fit to
   * the aspect ratio. Be aware that this transformation will cause the loaded
   * image to be a png file with an increased base64 string payload.
   *
   * @defaultValue false
   */
  readonly cropperContainWithinAspectRatio = input(false, { transform: booleanAttribute });
 
  /**
   * The cropper´s width cannot be made smaller than this number of pixels
   * (relative to original image´s size). (0 = disabled).
   *
   * @defaultValue 50
   */
  readonly cropperMinWidth = input(50);
 
  /**
   * The cropper´s height cannot be made smaller than this number of pixels
   * (relative to original image´s size). Will be ignored if
   * `cropperMaintainAspectRatio` is set. (0 = disabled).
   *
   * @defaultValue 50
   */
  readonly cropperMinHeight = input(50);
 
  /**
   * The cropper´s width cannot be made bigger than this number of pixels.
   * Default is 0 (disabled).
   *
   * @defaultValue 0
   */
  readonly cropperMaxWidth = input(0);
 
  /**
   * The cropper´s height cannot be made bigger than this number of pixels.
   * Default is 0 (disabled).
   *
   * @defaultValue 0
   */
  readonly cropperMaxHeight = input(0);
 
  /**
   * Set this to true for a round cropper. Resulting image will still
   * be square, but visually clipped with a border-radius: 50% on the
   * resulting image to show it as round.
   *
   * @defaultValue true
   */
  readonly roundImage = input(true, { transform: booleanAttribute });
 
  /**
   * The input photo to be used for cropping. A string in [Data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs) format
   * with base64 encoding.
   */
  readonly sourcePhoto = model<string>();
 
  /**
   * URL to a photo to be used for cropping.
   */
  readonly sourcePhotoUrl = input<string>();
 
  /**
   * The photo to be displayed and edited (when not readonly).
   */
  readonly croppedPhoto = model<string>();
 
  protected readonly editPhotoTemplate = viewChild.required<TemplateRef<any>>('editPhotoTemplate');
  protected readonly fileInput = viewChild.required<ElementRef<any>>('fileInput');
  protected readonly imageCropper = viewChild<ImageCropperComponent>('imageCropper');
 
  // used to label the dialog
  protected readonly titleId = `__si-photo-upload-${SiPhotoUploadComponent.idCounter++}`;
 
  /**
   * The trusted photo url string which is used to display the photo.
   */
  protected readonly sanitizedPhotoUrl = signal<SafeResourceUrl | undefined>(undefined);
 
  /**
   * The appropriate error message displayed to the user. Might be
   * `uploadErrorWrongType` or `uploadErrorTooBig`.
   */
  protected readonly uploadErrorMessage = signal<string | undefined>(undefined);
  protected readonly editButtonText = computed(() =>
    this.sanitizedPhotoUrl() ? this.changePhotoText() : this.uploadPhotoText()
  );
  protected readonly currentFileSizeKilobytes = signal(-1);
  protected readonly currentFileSizeMegabytes = signal(-1);
  protected readonly maxSizeMb = computed(() => this.maxFileSize() / 1024);
 
  /**
   * The photo instance to be used in the image cropper. We need a different
   * reference than `sourcePhoto` to support the cancel after uploading
   * a new photo. While `sourcePhoto` is A and image cropper uploads B,
   * we should not replace `sourcePhoto` A until user presses apply.
   */
  protected readonly imageCropperPhoto = signal<string | undefined>(undefined);
 
  protected readonly icons = addIcons({
    elementCancel,
    elementCircleFilled,
    elementStateExclamationMark
  });
  /**
   * Reference to the modal displaying the photo to edit.
   */
  protected modalRef?: ModalRef;
  /**
   * The last cropped event of the image cropper component.
   * Will be set on every mouse drag of the user. It contains
   * the cropped image and the position. When user completes
   * the cropping, the data from the last event is used.
   */
  private imageCroppedEvent?: ImageCroppedEvent;
  /**
   * Applied cropper position necessary to restore the cropper position when the use press the Change button.
   */
  private cropperPosition?: CropperPosition;
 
  private readonly sanitizer = inject(DomSanitizer);
  private readonly modalService = inject(SiModalService);
  private readonly autoBackgroundColorDirective = inject(SiAvatarBackgroundColorDirective);
 
  ngOnChanges(changes: SimpleChanges): void {
    if (changes.readonly) {
      this.resetErrorMessage();
      this.resetFileInputValue();
    }
 
    Iif (changes.sourcePhotoUrl) {
      this.setPhoto(this.sourcePhotoUrl());
      this.cropperPosition = undefined;
      this.croppedPhoto.set(undefined);
    }
    if (changes.sourcePhoto && changes.sourcePhoto.currentValue !== this.imageCropperPhoto()) {
      const sourcePhoto = this.sourcePhoto();
      this.setPhoto(sourcePhoto);
      this.cropperPosition = undefined;
      this.croppedPhoto.set(undefined);
      this.imageCropperPhoto.set(sourcePhoto);
    }
    Iif (
      changes.croppedPhoto &&
      changes.croppedPhoto.previousValue?.length > 0 &&
      changes.croppedPhoto.previousValue !== changes.croppedPhoto.currentValue
    ) {
      this.setPhoto(this.croppedPhoto());
    }
    if (changes.placeholderAltText) {
      this.autoBackgroundColorDirective.calculateColorFromInitials(this.placeholderAltText());
    }
  }
 
  ngOnDestroy(): void {
    this.modalRef?.detach();
  }
 
  /**
   * Opens a modal dialog with the cropping component.
   */
  protected showCroppingDialog(): void {
    Iif (this.modalRef) {
      return;
    }
    if (this.disabledCropping()) {
      this.sourcePhoto.set(this.imageCropperPhoto());
      const sourcePhoto = this.sourcePhoto();
      this.updateCroppedPhoto(sourcePhoto ?? '');
      return;
    }
    this.modalRef = this.modalService.show(
      this.editPhotoTemplate(),
      {
        ignoreBackdropClick: false,
        keyboard: true,
        class: 'modal-dialog-centered',
        ariaLabelledBy: this.titleId
      },
      'cancel'
    );
    this.resetErrorMessage();
    this.modalRef.hidden.subscribe(() => {
      this.resetErrorMessage();
      this.resetFileInputValue();
      this.modalRef = undefined;
    });
  }
 
  protected fileUpload(event: Event): void {
    // Initially reset a possible error message
    this.resetErrorMessage();
    const files = (event.target as HTMLInputElement).files;
    const file = files && files.length > 0 ? files[0] : undefined;
    Iif (!file) {
      return;
    }
    const fileType = file.type;
    const allowedFileTypes = this.acceptedUploadFormats()
      .split(',')
      .map(type => type.trim().replace('.', 'image/'));
    const fileTypeWrong = !allowedFileTypes.includes(fileType);
    if (fileTypeWrong) {
      this.uploadErrorMessage.set(this.uploadErrorWrongType());
      return;
    }
    const sizeInKb = file.size / 1024;
    this.currentFileSizeKilobytes.set(Math.round(sizeInKb * 10) / 10);
    this.currentFileSizeMegabytes.set(Math.round((sizeInKb / 1024) * 1000) / 1000);
    const fileSizeTooBig = sizeInKb > this.maxFileSize();
    if (fileSizeTooBig) {
      this.uploadErrorMessage.set(this.uploadErrorTooBig());
      return;
    }
    const reader = new FileReader();
    reader.addEventListener('loadend', () => {
      if (typeof reader.result === 'string') {
        this.cropperPosition = undefined;
        this.imageCropperPhoto.set(reader.result);
 
        // Bring up the editing modal if not already present
        this.showCroppingDialog();
      }
    });
    reader.readAsDataURL(file);
  }
 
  private updateCroppedPhoto(croppedPhoto: string, position?: CropperPosition): void {
    this.croppedPhoto.set(croppedPhoto);
    this.setPhoto(this.croppedPhoto());
    this.cropperPosition = position;
  }
 
  protected removePhoto(): void {
    // We emit undefined to notify consumers that the cropped
    // images is removed. This is a kind of special crop event.
    this.croppedPhoto.set(undefined);
    this.imageCropperPhoto.set(undefined);
    this.setPhoto(undefined);
    this.cropperPosition = undefined;
    this.resetFileInputValue();
    this.sourcePhoto.set(undefined);
  }
 
  /**
   * Invoked when user cropped the photo and pressed apply button.
   * Updates the current photo by the selected cropped photo and
   * closes the modal dialog.
   */
  protected imageCropperApplied(): void {
    if (this.imageCroppedEvent) {
      this.sourcePhoto.set(this.imageCropperPhoto());
      this.updateCroppedPhoto(
        this.imageCroppedEvent.base64!.toString(),
        this.imageCroppedEvent.cropperPosition
      );
    }
    this.modalRef?.hide();
  }
 
  protected imageCropperCanceled(): void {
    this.imageCropperPhoto.set(this.sourcePhoto());
    this.modalRef?.hide('cancel');
  }
 
  /**
   * Callback from the image cropper on every mouse drag invoking a cropping.
   *
   * @param event - Event containing the cropped image and the image cropped position.
   *
   */
  protected cropperImageCropped(event: ImageCroppedEvent): void {
    this.imageCroppedEvent = event;
  }
 
  /**
   * Lifecycle hook from the image cropper component. Informs
   * us when initialized and ready.
   */
  protected cropperReady(): void {
    // When the user opens cropper dialog multiple times we need to
    // apply existing cropper position.
    const imageCropper = this.imageCropper();
    Iif (this.cropperPosition && imageCropper) {
      imageCropper.cropper = { ...this.cropperPosition };
      imageCropper.crop();
    }
  }
 
  private resetFileInputValue(): void {
    if (this.fileInput()) {
      // Remove fileInput value to allow for selecting the same
      // file for being uploaded again.
      this.fileInput().nativeElement.value = null;
    }
  }
 
  private resetErrorMessage(): void {
    this.uploadErrorMessage.set(undefined);
  }
 
  private setPhoto(photo?: string): void {
    this.resetErrorMessage();
    this.sanitizedPhotoUrl.set(
      photo ? this.sanitizer.bypassSecurityTrustResourceUrl(photo) : undefined
    );
  }
}