File

projects/wms-framework/src/lib/regionsframework/adapters/behaviors/SelectorItemsSourceSyncBehavior.ts

Description

Behavior for the Selector Region added items, and keeps sync the selector items source.

Extends

RegionBehavior

Implements

IHostAwareRegionBehavior

Index

Properties
Methods
Accessors

Properties

Static Readonly BehaviorKey
Type : string
Default value : 'SelectorItemsSourceSyncBehavior'

TabControlRegionSyncBehavior key used to register the behavior in the collection

Private hostControl
Type : SelectorModel
Default value : null

The host control for the region

Private updatingActiveViewsWhenSelectionChanged
Type : boolean
Default value : false
IsAttached
Type : boolean
Inherited from RegionBehavior
Defined in RegionBehavior:37

Value indicating that the region behavior is attached

region
Type : IRegion
Inherited from RegionBehavior
Defined in RegionBehavior:45

Methods

Private OnActiveViewsChanged
OnActiveViewsChanged(sender: any, e: CollectionChangeInfo)
Parameters :
Name Type Optional
sender any No
e CollectionChangeInfo No
Returns : void
Public OnAttach
OnAttach()
Inherited from RegionBehavior
Defined in RegionBehavior:91

Override this method to perform the logic after the behavior has been attached.

Returns : void
Private OnSelectionChanged
OnSelectionChanged(sender: any, e: any)
Parameters :
Name Type Optional
sender any No
e any No
Returns : void
Private OnViewsChanged
OnViewsChanged(sender: any, e: CollectionChangeInfo)
Parameters :
Name Type Optional
sender any No
e CollectionChangeInfo No
Returns : void
Private SynchronizeItems
SynchronizeItems()
Returns : void
Attach
Attach()
Inherited from RegionBehavior
Defined in RegionBehavior:75

Attaches the behavior to the region

Returns : void

Accessors

HostControl
getHostControl()

Gets the HostControl

Returns : DependencyObject
setHostControl(value: DependencyObject)

Sets the HostControl

Parameters :
Name Type Optional
value DependencyObject No
Returns : void
import { RegionBehavior } from '../../RegionBehavior';
import { DependencyObject } from '../../../basecomponentmodel/DependencyObject';
import { SelectorModel } from '../../../models/SelectorModel';
import { tryToConvertType } from '../../../baseframework/ReflectionSupport';
import { InvalidOperationException } from '../../../baseframework/Exceptions';
import {
  CollectionChangeAction,
  CollectionChangeInfo,
  SimpleList,
} from '../../../baseframework/collections';
import { IHostAwareRegionBehavior } from '../../IRegionBehavior';

/**
 * Behavior for the Selector Region added items, and keeps sync the selector items source.
 *
 * @export
 * @class SelectorItemsSourceSyncBehavior
 * @extends {RegionBehavior}
 * @implements {IHostAwareRegionBehavior}
 * @wType Microsoft.Practices.Prism.Regions.Behaviors.SelectorItemsSourceSyncBehavior
 */
export class SelectorItemsSourceSyncBehavior
  extends RegionBehavior
  implements IHostAwareRegionBehavior
{
  /**
   * TabControlRegionSyncBehavior key used to register the behavior in the collection
   *
   * @static
   * @type {string}
   * @memberof SelectorItemsSourceSyncBehavior
   */
  public static readonly BehaviorKey: string =
    'SelectorItemsSourceSyncBehavior';

  private updatingActiveViewsWhenSelectionChanged: boolean = false;

  /**
   * The host control for the region
   *
   * @private
   * @type {TabControlModel}
   * @memberof TabControlRegionSyncBehavior
   */
  private hostControl: SelectorModel = null;

  /**
   * Gets the HostControl
   *
   * @type {DependencyObject}
   * @memberof TabControlRegionSyncBehavior
   */
  get HostControl(): DependencyObject {
    return this.hostControl;
  }

  /**
   * Sets the HostControl
   *
   * @memberof TabControlRegionSyncBehavior
   */
  set HostControl(value: DependencyObject) {
    let newValue: SelectorModel = tryToConvertType<SelectorModel>(
      value,
      SelectorModel
    );
    this.hostControl = newValue;
  }

  /**
   *
   * Override this method to perform the logic after the behavior has been attached.
   *
   */
  public OnAttach(): void {
    if (this.hostControl == null) {
      throw new InvalidOperationException(
        'HostControl Control can not be Null'
      );
    }

    if (this.hostControl.ItemsSource != null) {
      throw new Error('Invalid operation, items control has items source');
    }

    this.SynchronizeItems();
    this.hostControl.SelectionChanged.addHandler((sender, e) =>
      this.OnSelectionChanged(sender, e)
    );
    this.Region.ActiveViews.CollectionChanged.addHandler((sender, e) =>
      this.OnActiveViewsChanged(sender, e)
    );
    this.Region.Views.CollectionChanged.addHandler((sender, e) =>
      this.OnViewsChanged(sender, e)
    );
  }

  private SynchronizeItems(): void {
    let existingItems: SimpleList<any> = new SimpleList<any>();
    if (this.hostControl.items.count > 0) {
      for (let childItem of this.hostControl.items) {
        existingItems.add(childItem);
      }
    }
    for (let view of this.Region.Views) {
      this.hostControl.items.add(view);
    }
    for (let existingItem of existingItems) {
      this.Region.Add(existingItem);
    }
  }

  private OnSelectionChanged(sender: any, e: any): void {
    try {
      this.updatingActiveViewsWhenSelectionChanged = true;
      for (let removedItem of e.RemovedItems) {
        if (
          this.Region.Views.Contains(removedItem) &&
          this.Region.ActiveViews.Contains(removedItem)
        ) {
          this.Region.Deactivate(removedItem);
        }
      }
      for (let addedItem of e.AddedItems) {
        if (
          this.Region.Views.Contains(addedItem) &&
          !this.Region.ActiveViews.Contains(addedItem)
        ) {
          this.Region.Activate(addedItem);
        }
      }
    } finally {
      this.updatingActiveViewsWhenSelectionChanged = false;
    }
  }

  private OnActiveViewsChanged(sender: any, e: CollectionChangeInfo): void {
    if (this.updatingActiveViewsWhenSelectionChanged) {
      return;
    }
    if (e.action == CollectionChangeAction.Add) {
      if (
        this.hostControl.SelectedItem != null &&
        this.hostControl.SelectedItem != e.NewItems.getItem(0) &&
        this.Region.ActiveViews.Contains(this.hostControl.SelectedItem)
      ) {
        this.Region.Deactivate(this.hostControl.SelectedItem);
      }
      this.hostControl.SelectedItem = e.NewItems.getItem(0);
    } else if (
      e.action == CollectionChangeAction.Remove &&
      e.OldItems.contains(this.hostControl.SelectedItem)
    ) {
      this.hostControl.SelectedItem = null;
    }
  }

  private OnViewsChanged(sender: any, e: CollectionChangeInfo): void {
    if (e.action == CollectionChangeAction.Add) {
      let startIndex = e.NewStartingIndex;
      for (let newItem of e.NewItems) {
        this.hostControl.items.insert(startIndex++, newItem);
      }
    } else if (e.action == CollectionChangeAction.Remove) {
      for (let oldItem of e.OldItems) {
        this.hostControl.items.remove(oldItem);
      }
    }
  }
}

result-matching ""

    No results matching ""