File

projects/storefrontlib/layout/launch-dialog/services/launch-dialog.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(renderStrategies: LaunchRenderStrategy[], layoutConfig: LayoutConfig)
Parameters :
Name Type Optional
renderStrategies LaunchRenderStrategy[] No
layoutConfig LayoutConfig No

Methods

clear
clear(caller: LAUNCH_CALLER | string)

Util method to remove element from rendered elements list

Parameters :
Name Type Optional Description
caller LAUNCH_CALLER | string No

LAUNCH_CALLER

Returns : void
closeDialog
closeDialog(reason: string)
Parameters :
Name Type Optional
reason string No
Returns : void
Protected findConfiguration
findConfiguration(caller: LAUNCH_CALLER | string)

Returns the configuration for the caller

Parameters :
Name Type Optional Description
caller LAUNCH_CALLER | string No

LAUNCH_CALLER

Protected getStrategy
getStrategy(config: LaunchOptions)

Returns the render strategy based on the configuration

Parameters :
Name Type Optional Description
config LaunchOptions No

Configuration for launch

launch
launch(caller: LAUNCH_CALLER | string, vcr?: ViewContainerRef, data?: any)

Render the element based on the strategy from the launch configuration

Parameters :
Name Type Optional Description
caller LAUNCH_CALLER | string No

LAUNCH_CALLER

vcr ViewContainerRef Yes

View Container Ref of the container for inline rendering

data any Yes
Returns : void | Observable
openDialog
openDialog(caller: LAUNCH_CALLER | string, openElement?: ElementRef, vcr?: ViewContainerRef, data?: any)

Open the dialog

Parameters :
Name Type Optional Description
caller LAUNCH_CALLER | string No

LAUNCH_CALLER

openElement ElementRef Yes

button's Element ref

vcr ViewContainerRef Yes

View Container Ref of the container for inline rendering

data any Yes

optional data which could be passed to dialog

openDialogAndSubscribe
openDialogAndSubscribe(caller: LAUNCH_CALLER | string, openElement?: ElementRef, data?: any)

Opens dialog and subscribe in the service. Should be used if the trigger component might get destroyed while the component is open.

Parameters :
Name Type Optional Description
caller LAUNCH_CALLER | string No

Launch Caller

openElement ElementRef Yes

Element to open

data any Yes

Data to provide to the rendered element

Returns : void

Properties

Private _dataSubject
Default value : new BehaviorSubject<any>(undefined)
Private _dialogClose
Default value : new BehaviorSubject<string>(undefined)

Accessors

data$
getdata$()
dialogClose
getdialogClose()
import {
  ComponentRef,
  ElementRef,
  Inject,
  Injectable,
  isDevMode,
  ViewContainerRef,
} from '@angular/core';
import { resolveApplicable } from '@spartacus/core';
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
import { filter, map, take, tap } from 'rxjs/operators';
import { LayoutConfig } from '../../config/layout-config';
import { LaunchOptions, LAUNCH_CALLER } from '../config/launch-config';
import { LaunchRenderStrategy } from './launch-render.strategy';

@Injectable({ providedIn: 'root' })
export class LaunchDialogService {
  private _dialogClose = new BehaviorSubject<string>(undefined);
  private _dataSubject = new BehaviorSubject<any>(undefined);

  get data$(): Observable<any> {
    return this._dataSubject.asObservable();
  }

  constructor(
    @Inject(LaunchRenderStrategy)
    protected renderStrategies: LaunchRenderStrategy[],
    protected layoutConfig: LayoutConfig
  ) {
    this.renderStrategies = this.renderStrategies || [];
  }

  /**
   * Open the dialog
   *
   * @param caller LAUNCH_CALLER
   * @param openElement button's Element ref
   * @param vcr View Container Ref of the container for inline rendering
   * @param data optional data which could be passed to dialog
   */
  openDialog(
    caller: LAUNCH_CALLER | string,
    openElement?: ElementRef,
    vcr?: ViewContainerRef,
    data?: any
  ): Observable<any> | undefined {
    const component = this.launch(caller, vcr, data);

    if (component) {
      return combineLatest([component, this.dialogClose]).pipe(
        filter(([, close]) => close !== undefined),
        tap(([comp]) => {
          openElement?.nativeElement.focus();
          this.clear(caller);
          comp.destroy();
        }),
        map(([comp]) => comp)
      );
    }
  }
  /**
   * Render the element based on the strategy from the launch configuration
   *
   * @param caller LAUNCH_CALLER
   * @param vcr View Container Ref of the container for inline rendering
   */
  launch(
    caller: LAUNCH_CALLER | string,
    vcr?: ViewContainerRef,
    data?: any
  ): void | Observable<ComponentRef<any> | undefined> {
    const config = this.findConfiguration(caller);
    if (config) {
      const renderer = this.getStrategy(config);

      // Render if the strategy exists
      if (renderer) {
        this._dialogClose.next(undefined);
        this._dataSubject.next(data);

        return renderer.render(config, caller, vcr);
      }
    } else if (isDevMode()) {
      console.warn('No configuration provided for caller ' + caller);
    }
  }

  /**
   * Opens dialog and subscribe in the service. Should be used if the trigger component might get destroyed while the component is open.
   *
   * @param caller Launch Caller
   * @param openElement Element to open
   * @param data Data to provide to the rendered element
   */
  openDialogAndSubscribe(
    caller: LAUNCH_CALLER | string,
    openElement?: ElementRef,
    data?: any
  ): void {
    this.openDialog(caller, openElement, undefined, data)
      ?.pipe(take(1))
      .subscribe();
  }

  /**
   * Util method to remove element from rendered elements list
   *
   * @param caller LAUNCH_CALLER
   */
  clear(caller: LAUNCH_CALLER | string): void {
    const config = this.findConfiguration(caller);

    if (config) {
      const renderer = this.getStrategy(config);

      // Render if the strategy exists
      if (renderer) {
        renderer.remove(caller, config);
      }
    }
  }

  get dialogClose(): Observable<string> {
    return this._dialogClose.asObservable();
  }

  closeDialog(reason: string) {
    this._dialogClose.next(reason);
  }

  /**
   * Returns the configuration for the caller
   *
   * @param caller LAUNCH_CALLER
   */
  protected findConfiguration(
    caller: LAUNCH_CALLER | string
  ): LaunchOptions | undefined {
    if (this.layoutConfig?.launch) {
      return this.layoutConfig.launch[caller];
    }
    return undefined;
  }

  /**
   * Returns the render strategy based on the configuration
   *
   * @param config Configuration for launch
   */
  protected getStrategy(config: LaunchOptions): LaunchRenderStrategy {
    return resolveApplicable(this.renderStrategies, [config]);
  }
}

result-matching ""

    No results matching ""