All files / src/components si-widget-editor-base.ts

65.9% Statements 29/44
51.61% Branches 16/31
54.54% Functions 6/11
63.15% Lines 24/38

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                                                                      125x   28x     28x   28x         28x   28x 28x 28x     28x             8x             6x 6x     6x     6x       6x           6x           6x                           34x 34x 34x 34x 34x 34x           8x                        
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import {
  ComponentRef,
  Directive,
  EnvironmentInjector,
  inject,
  Injector,
  isSignal,
  OnDestroy,
  OutputRefSubscription,
  signal,
  viewChild,
  ViewContainerRef
} from '@angular/core';
import { Observable, Subscription } from 'rxjs';
 
import {
  WidgetComponentFactory,
  WidgetConfig,
  WidgetConfigStatus,
  WidgetInstanceEditor,
  WidgetInstanceEditorWizard,
  WidgetInstanceEditorWizardState
} from '../model/widgets.model';
import { setupWidgetEditor } from '../widget-loader';
 
/**
 * Base class encapsulating common widget editor lifecycle management.
 * Both `SiWidgetCatalogComponent` and `SiWidgetInstanceEditorDialogComponent`
 * extend this class to share editor setup, teardown, and status handling logic.
 */
@Directive()
export abstract class SiWidgetEditorBase implements OnDestroy {
  /** Indicates if the current config is valid or not. */
  protected readonly invalidConfig = signal(false);
 
  /** Marks the widget configuration as modified. */
  protected readonly widgetConfigModified = signal(false);
 
  protected readonly editorWizardState = signal<WidgetInstanceEditorWizardState | undefined>(
    undefined
  );
 
  protected widgetInstanceEditor?: WidgetInstanceEditor;
  protected subscriptions: (Subscription | OutputRefSubscription)[] = [];
 
  protected readonly editorHost = viewChild.required('editorHost', { read: ViewContainerRef });
  protected injector = inject(Injector);
  protected envInjector = inject(EnvironmentInjector);
 
  ngOnDestroy(): void {
    this.tearDownEditor();
  }
 
  protected loadWidgetEditor(
    widgetComponentFactory: WidgetComponentFactory,
    host: ViewContainerRef
  ): Observable<ComponentRef<WidgetInstanceEditor>> {
    return setupWidgetEditor(widgetComponentFactory, host, this.injector, this.envInjector);
  }
 
  protected initializeEditor(
    componentRef: ComponentRef<WidgetInstanceEditor>,
    config: WidgetConfig | Omit<WidgetConfig, 'id'>
  ): void {
    this.widgetInstanceEditor = componentRef.instance;
    Iif (isSignal(this.widgetInstanceEditor.config)) {
      componentRef.setInput('config', config);
    } else {
      this.widgetInstanceEditor.config = config;
    }
    // To be used by webcomponent wrapper
    Iif ('statusChangesHandler' in this.widgetInstanceEditor) {
      this.widgetInstanceEditor.statusChangesHandler = this.handleStatusChanges.bind(this);
    }
 
    Iif (this.widgetInstanceEditor.statusChanges) {
      this.subscriptions.push(
        this.widgetInstanceEditor.statusChanges.subscribe(statusChanges =>
          this.handleStatusChanges(statusChanges)
        )
      );
    I} else if (this.widgetInstanceEditor.configChange) {
      this.subscriptions.push(
        this.widgetInstanceEditor.configChange.subscribe(() => this.widgetConfigModified.set(true))
      );
    }
 
    Iif (this.isEditorWizard(this.widgetInstanceEditor)) {
      this.editorWizardState.set(this.widgetInstanceEditor.state);
 
      if (this.widgetInstanceEditor.stateChange) {
        this.subscriptions.push(
          this.widgetInstanceEditor.stateChange.subscribe(state =>
            this.editorWizardState.set(state)
          )
        );
      }
    }
  }
 
  protected tearDownEditor(): void {
    this.invalidConfig.set(false);
    this.widgetConfigModified.set(false);
    this.editorWizardState.set(undefined);
    this.widgetInstanceEditor = undefined;
    this.subscriptions.forEach(s => s.unsubscribe());
    this.subscriptions = [];
  }
 
  protected isEditorWizard(
    editor?: WidgetInstanceEditor | WidgetInstanceEditorWizard
  ): editor is WidgetInstanceEditorWizard {
    return !!editor && 'state' in editor;
  }
 
  protected handleStatusChanges(statusChanges: Partial<WidgetConfigStatus>): void {
    if (statusChanges.invalid !== undefined) {
      this.invalidConfig.set(statusChanges.invalid);
    }
    if (statusChanges.modified !== undefined) {
      this.widgetConfigModified.set(statusChanges.modified);
    }
  }
}