File

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

Description

Utility class for performing the region creation when it is needed

Index

Properties
Methods

Constructor

constructor(regionManager: IRegionManager)

Creates an instance of DelayedRegionCreationBehavior.

Parameters :
Name Type Optional
regionManager IRegionManager No

Properties

Private callback
Type : function
Public TargetElement
Type : unknown
Default value : null

Target element hosting the region

Methods

Attach
Attach()

Attach this utility class to an existing model

Returns : void
Detach
Detach()

Detach this utility class from events waiting performing the region registration

Returns : void
ForceRegistration
ForceRegistration()

Force this delayed registration to be performed

Returns : void
import { FrameworkElement } from '../basecomponentmodel/FrameworkElement';
import { UIElement } from '../basecomponentmodel/UIElement';
import { IRegionManager } from './IRegionManager';
import { RegionManager } from './RegionManager';

/**
 *  Utility class for performing the region creation when it is needed
 *
 * @export
 * @class DelayedRegionCreationBehavior
 * @wType Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior
 */
export class DelayedRegionCreationBehavior {
  private callback: () => void;

  /**
   * Target element hosting the region
   *
   * @type {unknown}
   * @memberof DelayedRegionCreationBehavior
   */
  public TargetElement: unknown = null;

  /**
   * Creates an instance of DelayedRegionCreationBehavior.
   * @param {IRegionManager} regionManager
   * @memberof DelayedRegionCreationBehavior
   */
  constructor(private regionManager: IRegionManager) {}

  /**
   *  Attach this utility class to an existing model
   *
   * @memberof DelayedRegionCreationBehavior
   */
  Attach(): void {
    this.callback = () => {
      this.Detach();
      const regionName = RegionManager.GetRegionName(this.TargetElement);
      if (this.TargetElement instanceof UIElement && regionName) {
        this.regionManager.RegisterRegionTarget(regionName, this.TargetElement);
      }
      if (this.TargetElement instanceof FrameworkElement) {
        const model = this.TargetElement;
        const removalFunction = (propName) => {
          if (propName === 'Parent' && !model?.Parent) {
            RegionManager.ProcessRegionHostBeingRemoved(regionName, model);
            model.change.removeHandler(removalFunction);
          }
        };
        this.TargetElement.change.addHandler(removalFunction);
      }
    };
    RegionManager.UpdatingRegions.addHandler(this.callback);
  }

  /**
   *  Force this delayed registration to be performed
   *
   * @memberof DelayedRegionCreationBehavior
   * @wIgnore
   */
  ForceRegistration() {
    let theCallback = this.callback;
    this.Detach();
    if (theCallback) {
      theCallback();
    }
  }

  /**
   * Detach this utility class from events waiting performing the region registration
   *
   * @memberof DelayedRegionCreationBehavior
   */
  Detach(): void {
    RegionManager.UpdatingRegions.removeHandler(this.callback);
    this.callback = null;
  }
}

result-matching ""

    No results matching ""