All files / src/model si-widget-id-provider.ts

100% Statements 8/8
100% Branches 7/7
100% Functions 2/2
100% Lines 5/5

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                                        2x   12x           2x                               12x                       7x      
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { inject, Injectable, InjectionToken } from '@angular/core';
 
import type { WidgetConfig } from './widgets.model';
 
/**
 * Injection token to optionally inject the {@link SiWidgetIdProvider} implementation
 * to provide custom widget id generation logic. The default implementation
 * is {@link SiWidgetDefaultIdProvider}.
 *
 * * @example
 * The following shows how to provide your own widget id provider.
 * ```
 * providers: [..., { provide: SI_WIDGET_ID_PROVIDER, useClass: CustomWidgetIdProvider }]
 * ```
 *
 */
export const SI_WIDGET_ID_PROVIDER = new InjectionToken<SiWidgetIdProvider>(
  'Injection token to configure your widget id provider.',
  { providedIn: 'root', factory: () => inject(SiWidgetDefaultIdProvider) }
);
 
/**
 * Abstract class to provide widget id generation logic.
 */
export abstract class SiWidgetIdProvider {
  /**
   * Generates a transient widget id for newly added widgets in edit mode.
   *
   * @param widget - The widget instance config without any id.
   * @param dashboardId - The id of the dashboard where the widget is added.
   * @returns A transient widget id as a string.
   */
  abstract generateWidgetId(widget: Omit<WidgetConfig, 'id'>, dashboardId?: string): string;
}
 
/**
 * The default implementation of the {@link SiWidgetIdProvider} which
 * generates random widget ids.
 */
@Injectable({ providedIn: 'root' })
export class SiWidgetDefaultIdProvider extends SiWidgetIdProvider {
  /**
   * Generates a unique widget id.
   *
   * The method uses `crypto.randomUUID()` which generates a RFC4122 version 4 UUID
   * (a cryptographically secure random identifier with 122 bits of entropy).
   *
   * @param widget - The widget instance config without any id.
   * @param dashboardId - The id of the dashboard where the widget is added.
   * @returns A unique widget id string in the format `crypto.randomUUID()`.
   */
  override generateWidgetId(widget: Omit<WidgetConfig, 'id'>, dashboardId?: string): string {
    return crypto.randomUUID();
  }
}