File

projects/storefrontlib/cms-structure/outlet/outlet.service.ts

Index

Properties
Methods

Methods

add
add(outlet: string, template: T, position?: OutletPosition)

Adds a template or ComponentFactory, so that UI outlets can be replaced dynamically. The UI position where this template or ComponentFactory is inserted is given by a string reference (called outlet) and optional OutletPosition. The OutletPosition is either before or after, or replaces the entire UI.

Parameters :
Name Type Optional Description
outlet string No

the UI location represented by a string

template T No

the TemplateRef that will be used to insert UI

position OutletPosition Yes

the OutletPosition in the UI

Returns : void
add
add(outlet: string, factory: T, position?: OutletPosition)
Parameters :
Name Type Optional Description
outlet string No
factory T No

The ComponentFactory that will be dynamically added to the outlet UI

position OutletPosition Yes
Returns : void
add
add(outlet: string, templateOrFactory: T, position: OutletPosition)
Parameters :
Name Type Optional Default value Description
outlet string No
templateOrFactory T No

A ComponentFactory that inserts a component dynamically.

position OutletPosition No OutletPosition.REPLACE
Returns : void
get
get(outlet: string, position: OutletPosition, stacked)

Returns a single object or multiple objects for the given outlet reference, depending on the stacked argument.

Parameters :
Name Type Optional Default value Description
outlet string No

The outlet reference

position OutletPosition No OutletPosition.REPLACE

the outlet position, OutletPosition.before, OutletPosition.AFTER or OutletPosition.REPLACE

stacked No AVOID_STACKED_OUTLETS

Indicates whether an array of outlet components is returned

Returns : [] | T
remove
remove(outlet: string, position: OutletPosition, value?: T)
Parameters :
Name Type Optional Default value
outlet string No
position OutletPosition No OutletPosition.REPLACE
value T Yes
Returns : void
Protected removeValueOrAll
removeValueOrAll(store: Map, outlet: string, value?: T)
Parameters :
Name Type Optional
store Map<string | T[]> No
outlet string No
value T Yes
Returns : void

Properties

Private templatesRefs
Type : object
Default value : { [OutletPosition.BEFORE]: new Map<string, T[]>(), [OutletPosition.REPLACE]: new Map<string, T[]>(), [OutletPosition.AFTER]: new Map<string, T[]>(), }
import { ComponentFactory, Injectable, TemplateRef } from '@angular/core';
import { AVOID_STACKED_OUTLETS, OutletPosition } from './outlet.model';

@Injectable({
  providedIn: 'root',
})
export class OutletService<T = TemplateRef<any> | ComponentFactory<any>> {
  private templatesRefs = {
    [OutletPosition.BEFORE]: new Map<string, T[]>(),
    [OutletPosition.REPLACE]: new Map<string, T[]>(),
    [OutletPosition.AFTER]: new Map<string, T[]>(),
  };

  /**
   * Adds a template or ComponentFactory, so that UI outlets can be replaced dynamically.
   * The UI position where this template or ComponentFactory is inserted is given by a
   * string reference (called `outlet`) and optional `OutletPosition`. The `OutletPosition`
   * is either before or after, or replaces the entire UI.
   *
   * @param outlet the UI location represented by a string
   * @param template the `TemplateRef` that will be used to insert UI
   * @param position the `OutletPosition` in the UI
   */
  add(outlet: string, template: T, position?: OutletPosition): void;
  /**
   * @param factory The `ComponentFactory` that will be dynamically added to the outlet UI
   */
  add(
    outlet: string,
    // eslint-disable-next-line @typescript-eslint/unified-signatures
    factory: T,
    position?: OutletPosition
  ): void;
  /**
   * @param templateOrFactory A `ComponentFactory` that inserts a component dynamically.
   */
  add(
    outlet: string,
    templateOrFactory: T,
    position: OutletPosition = OutletPosition.REPLACE
  ): void {
    const store = this.templatesRefs[position];
    if (store) {
      const existing = store.get(outlet) || [];
      const newValue: T[] = existing.concat([templateOrFactory]);
      store.set(outlet, newValue);
    }
  }

  /**
   *
   * Returns a single object or multiple objects for the given outlet reference,
   * depending on the `stacked` argument.
   *
   * @param outlet The outlet reference
   * @param position the outlet position, `OutletPosition.before`, `OutletPosition.AFTER` or `OutletPosition.REPLACE`
   * @param stacked Indicates whether an array of outlet components is returned
   */
  get(
    outlet: string,
    position: OutletPosition = OutletPosition.REPLACE,
    stacked = AVOID_STACKED_OUTLETS
  ): T[] | T {
    const store =
      this.templatesRefs[position] ||
      this.templatesRefs[OutletPosition.REPLACE];

    const templateRef: T[] = store.get(outlet);
    if (templateRef && !stacked) {
      return templateRef[0];
    }
    return templateRef;
  }

  remove(
    outlet: string,
    position: OutletPosition = OutletPosition.REPLACE,
    value?: T
  ): void {
    const store =
      this.templatesRefs[position] ||
      this.templatesRefs[OutletPosition.REPLACE];

    this.removeValueOrAll(store, outlet, value);
  }

  protected removeValueOrAll(
    store: Map<string, T[]>,
    outlet: string,
    value?: T
  ): void {
    if (!value && store.has(outlet)) {
      store.delete(outlet);
    } else if (value && store.has(outlet)) {
      let existing = store.get(outlet);

      existing = existing.filter((val) => val !== value);

      store.set(outlet, existing);
    }
  }
}

result-matching ""

    No results matching ""