All files / column-selection-dialog si-column-selection-dialog.service.ts

90% Statements 18/20
75% Branches 3/4
100% Functions 4/4
89.47% Lines 17/19

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                                        1x 1x                               1x 1x 1x   1x 1x   1x       1x     1x 1x 1x 1x 1x 1x         1x 1x                
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { inject, Injectable } from '@angular/core';
import {
  createModalConfig,
  ModalDependencyInjectionOptions,
  ModalOptions,
  SiModalService
} from '@siemens/element-ng/modal';
import { Observable } from 'rxjs';
 
import { SiColumnSelectionDialogComponent } from './si-column-selection-dialog.component';
import {
  ColumnSelectionDialogResult,
  SiColumnSelectionDialogConfig
} from './si-column-selection-dialog.types';
 
@Injectable({ providedIn: 'root' })
export class SiColumnSelectionDialogService {
  private modalService = inject(SiModalService);
 
  /**
   * Opens a column selection dialog.
   *
   * Despite other dialogs,
   * this dialog informs the consumer not ONLY with clicking `submit` or `cancel`,
   * but also with changing the place or visibility of a dialog row
   * thanks to the `instant` type of emitted event.
   *
   * {@label WITH_OBJECT}
   */
  showColumnSelectionDialog(
    dialogConfig: SiColumnSelectionDialogConfig,
    diOptions?: ModalDependencyInjectionOptions
  ): Observable<ColumnSelectionDialogResult> {
    return new Observable<ColumnSelectionDialogResult>(subscriber => {
      const config: ModalOptions<any> = createModalConfig(dialogConfig);
      Object.assign(config, diOptions);
 
      config.class += ' modal-dialog-scrollable';
      config.keyboard = true;
 
      const modalRef = this.modalService.show<SiColumnSelectionDialogComponent>(
        SiColumnSelectionDialogComponent,
        config
      );
      const subscription = modalRef.hidden.subscribe(
        (confirmationResult: ColumnSelectionDialogResult | undefined) => {
          const keepModalOpen =
            confirmationResult?.type === 'instant' || confirmationResult?.type === 'restoreDefault';
          confirmationResult ??= { type: 'cancel', columns: modalRef.content.backupColumns };
          subscriber.next(confirmationResult);
          if (!keepModalOpen) {
            subscription.unsubscribe();
            subscriber.complete();
          }
        }
      );
 
      return () => {
        Iif (!subscription.closed) {
          subscription.unsubscribe();
          modalRef.detach();
        }
      };
    });
  }
}