File

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

Description

Implementation of view collection

Implements

IViewsCollection

Index

Properties
Methods

Properties

CollectionChanged
Type : SubscriptionEvent<void>
Default value : new SubscriptionEvent()

Collection changed event.

views
Type : Array<any>
Default value : []

Methods

Public add
add(view: unknown)

Adds a view to the collection

Parameters :
Name Type Optional
view unknown No
Returns : void
Contains
Contains(view: unknown)

Checks if a view is contained in the collection

Parameters :
Name Type Optional
view unknown No
Returns : boolean

True if the view is contained in the collection; otherwise false.

Public remove
remove(view: unknown)

Remove view from collection

Parameters :
Name Type Optional
view unknown No
Returns : void
()
Returns : Iterator<any, any, undefined>
import {
  CollectionChangeAction,
  CollectionChangeInfo,
  iuContains,
  iuFirstOrDefault,
  SimpleDictionary,
  SimpleList,
} from '../baseframework';
import { ClassInfo } from '../decorators/ClassInfo';
import { SubscriptionEvent } from '../utils/SubscriptionEvent';
import { IRegion } from './IRegion';
import { IRegionBehaviorCollection } from './IRegionBehaviorCollection';
import { IRegionManager } from './IRegionManager';
import { IViewsCollection } from './IViewsCollection';
import { RegionBehaviorCollection } from './RegionBehaviorCollection';

/**
 *  Region implementation
 *
 * @export
 * @class Region
 * @implements {IRegion}
 * @wType Microsoft.Practices.Prism.Regions.Region
 */
export class Region implements IRegion {
  #activeViews: IViewsCollection;
  #views: IViewsCollection;

  /**
   * The region name
   *
   * @type {string}
   * @memberof Region
   */
  Name: string = null;

  /**
   * The region manager of the region
   *
   * @type {IRegionManager}
   * @memberof Region
   */
  RegionManager: IRegionManager;

  /**
   * The registered view by name in the region
   *
   * @type {SimpleDictionary<string, unknown>}
   * @memberof Region
   */
  viewNames: SimpleDictionary<string, unknown> = new SimpleDictionary();

  /**
   * The region behaviors in the region
   *
   * @type {IRegionBehaviorCollection}
   * @memberof Region
   */
  Behaviors: IRegionBehaviorCollection = new RegionBehaviorCollection(this);

  /**
   *Creates an instance of Region.
   *
   * @memberof Region
   */
  constructor() {
    this.#views = new ViewsCollection();
    this.#activeViews = new ViewsCollection();
  }

  /**
   * Gets the views of the region
   *
   * @readonly
   * @type {IViewsCollection}
   * @memberof Region
   */
  get Views(): IViewsCollection {
    return this.#views;
  }

  /**
   * Gets the current active views of the region
   *
   * @readonly
   * @type {IViewsCollection}
   * @memberof Region
   */
  get ActiveViews(): IViewsCollection {
    return this.#activeViews;
  }

  /**
   * Activate a view in the region
   *
   * @param {unknown} view
   * @memberof Region
   */
  Activate(view: unknown): void {
    const viewInRegion = this.GetViewInRegion(view);
    if (!this.ActiveViews.Contains(viewInRegion)) {
      (this.ActiveViews as ViewsCollection).add(viewInRegion);
    }
  }

  /**
   * Adds a new view to the region.
   *
   * @param {unknown} view
   * @param {string} [viewName]
   * @param {boolean} [flag]
   * @return {*}  {IRegionManager}
   * @memberof Region
   */
  Add(view: unknown, viewName?: string, flag?: boolean): IRegionManager {
    if (flag === true) {
      console.warn(
        'Creation of a new RegionManager scope is not supported. This may produce unexpected results'
      );
    }
    if (this.Views.Contains(view)) {
      throw new Error('View already exist in the region');
    }
    if (typeof viewName === 'string') {
      if (this.viewNames.hasKey(viewName)) {
        throw new Error('Already register a view with the given name');
      }
      this.viewNames.setItem(viewName, view);
    }
    (this.Views as ViewsCollection).add(view);
    return this.RegionManager;
  }

  /**
   * Deactivate view in region
   *
   * @param {unknown} view
   * @memberof Region
   */
  Deactivate(view: unknown): void {
    const viewInRegion = this.GetViewInRegion(view);
    if (this.ActiveViews.Contains(viewInRegion)) {
      (this.ActiveViews as ViewsCollection).remove(viewInRegion);
    }
  }

  /**
   *  Returns a view registered with the given name
   *
   * @param {string} name
   * @return {*}  {unknown}
   * @memberof Region
   */
  GetView(name: string): unknown {
    let result = null;
    this.viewNames.tryGetValue(name, (view) => {
      result = view;
    });
    return result;
  }

  /**
   * Remove view from region
   *
   * @param {unknown} view
   * @memberof Region
   */
  Remove(view: unknown): void {
    (this.ActiveViews as ViewsCollection).remove(view);
    (this.Views as ViewsCollection).remove(view);

    // we need to look for the entry matching the given view
    let entry = iuFirstOrDefault(this.viewNames, (e) => e[1] == view);
    if (entry) {
      let viewName: string = entry[0];
      this.viewNames.removeEntry(viewName);
    }
  }

  private GetViewInRegion(view: unknown): any {
    if (view == null) {
      throw new Error('Missing view argument');
    }
    const viewInRegion = iuFirstOrDefault(this.Views, (v) => v == view);
    if (viewInRegion == null) {
      throw new Error('The view is not in the region');
    }

    return viewInRegion;
  }
}

/**
 *  Implementation of view collection
 *
 * @class ViewsCollection
 * @implements {IViewsCollection}
 * @wType Microsoft.Practices.Prism.Regions.ViewsCollection
 */
@ClassInfo({
  classId: 'ViewsCollection',
  implements: ['System.Collections.Specialized.INotifyCollectionChanged'],
})
export class ViewsCollection implements IViewsCollection {
  views: Array<any> = [];
  /**
   * Collection changed event.
   *
   * @memberof ViewsCollection
   * @wIgnore
   */
  CollectionChanged: SubscriptionEvent<
    (e: any, args: CollectionChangeInfo) => void
  > = new SubscriptionEvent();

  /**
   * Adds a view to the collection
   *
   * @param {unknown} view
   * @memberof ViewsCollection
   */
  public add(view: unknown): void {
    this.views.push(view);
    const info = new CollectionChangeInfo(CollectionChangeAction.Add);
    const newItems = new SimpleList();
    newItems.add(view);
    info.NewStartingIndex = this.views.length - 1;
    info.NewItems = newItems;
    this.CollectionChanged.fire([this, info]);
    const infoReset = new CollectionChangeInfo(CollectionChangeAction.Reset);
    this.CollectionChanged.fire([this, infoReset]);
  }

  /**
   * Remove view from collection
   *
   * @param {unknown} view
   * @memberof ViewsCollection
   */
  public remove(view: unknown): void {
    const index = this.views.indexOf(view, 0);
    if (index > -1) {
      this.views.splice(index, 1);
    }
    const info = new CollectionChangeInfo(CollectionChangeAction.Remove);
    const removedItems = new SimpleList();
    removedItems.add(view);
    info.OldItems = removedItems;
    info.OldStartingIndex = index;
    this.CollectionChanged.fire([this, info]);
  }

  /**
   * Checks if a view is contained in the collection
   *
   * @param {unknown} view
   * @returns {boolean} True if the view is contained in the collection; otherwise false.
   * @memberof ViewsCollection
   */
  Contains(view: unknown): boolean {
    return iuContains(this.views, view);
  }

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

result-matching ""

    No results matching ""