File

projects/wms-framework/src/lib/regionsframework/IRegionBehaviorFactory.ts

Description

Region behavior factory

Implements

IRegionBehaviorFactory

Index

Properties
Methods

Constructor

constructor(container: DependencyContainer)
Parameters :
Name Type Optional
container DependencyContainer No

Properties

Private behaviors
Type : SimpleDictionary<string | RuntimeTypeInfo>
Default value : new SimpleDictionary()

Methods

AddIfMissing
AddIfMissing(key: string, type: RuntimeTypeInfo)

Adds new behavior with the given key

Parameters :
Name Type Optional
key string No
type RuntimeTypeInfo No
Returns : void
ContainsKey
ContainsKey(key: string)

Verifies if the given key is already used in the behaviors collection

Parameters :
Name Type Optional
key string No
Returns : boolean

{boolean}

CreateFromKey
CreateFromKey(key: string)

Creates a new behavior from the given key

Parameters :
Name Type Optional
key string No
Returns : IRegionBehavior

{IRegionBehavior}

()
Returns : Iterator<string, any, undefined>
import { RuntimeTypeInfo, SimpleDictionary } from '../baseframework';
import { IRegionBehavior } from './IRegionBehavior';
import { DependencyContainer, inject, injectable } from 'tsyringe';
import { containerInjectionToken } from './injectionTokens';

/**
 * Interface for behavior factories
 *
 * @export
 * @interface IRegionBehaviorFactory
 * @wInterface Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory
 */
export interface IRegionBehaviorFactory extends Iterable<string> {
  /**
   *  Add a new behavior
   *
   * @param {string} key
   * @param {RuntimeTypeInfo} type
   * @memberof IRegionBehaviorFactory
   */
  AddIfMissing(key: string, type: RuntimeTypeInfo);

  /**
   *  Verifies if the given key is associated to a registered region
   *
   * @param {string} key
   * @return {*}  {boolean}
   * @memberof IRegionBehaviorFactory
   */
  ContainsKey(key: string): boolean;

  /**
   *  Create a behavior from a key
   *
   * @param {string} key
   * @return {*}  {IRegionBehavior}
   * @memberof IRegionBehaviorFactory
   */
  CreateFromKey(key: string): IRegionBehavior;
}

/**
 *  Region behavior factory
 *
 * @export
 * @class RegionBehaviorFactory
 * @implements {IRegionBehaviorFactory}
 * @wType Microsoft.Practices.Prism.Regions.RegionBehaviorFactory
 */
@injectable()
export class RegionBehaviorFactory implements IRegionBehaviorFactory {
  private behaviors: SimpleDictionary<string, RuntimeTypeInfo> =
    new SimpleDictionary();

  constructor(
    @inject(containerInjectionToken) private container: DependencyContainer
  ) {}

  [Symbol.iterator](): Iterator<string, any, undefined> {
    return this.behaviors.keys[Symbol.iterator]();
  }

  /**
   *  Adds new behavior with the given key
   *
   * @param {string} key
   * @param {RuntimeTypeInfo} type
   * @memberof RegionBehaviorFactory
   */
  AddIfMissing(key: string, type: RuntimeTypeInfo) {
    if (!this.behaviors.containsKey(key)) {
      this.behaviors.setItem(key, type);
    }
  }

  /**
   *  Verifies if the given key is already used in the behaviors collection
   *
   * @param {string} key
   * @return {*}  {boolean}
   * @memberof RegionBehaviorFactory
   */
  ContainsKey(key: string): boolean {
    return this.behaviors.containsKey(key);
  }

  /**
   *  Creates a new behavior from the given key
   *
   * @param {string} key
   * @return {*}  {IRegionBehavior}
   * @memberof RegionBehaviorFactory
   */
  CreateFromKey(key: string): IRegionBehavior {
    const type = this.behaviors.getItem(key);
    if (type) {
      return this.container.resolve(type.JSType);
    } else {
      return null;
    }
  }
}

result-matching ""

    No results matching ""