projects/wms-framework/src/lib/regionsframework/adapters/behaviors/TabControlRegionSyncBehavior.ts
Behavior for the TabControl Region added items, and keeps sync the Selected Item.
Properties |
|
Methods |
|
Accessors |
Static Readonly BehaviorKey |
Type : string
|
Default value : 'TabControlRegionSyncBehavior'
|
TabControlRegionSyncBehavior key used to register the behavior in the collection |
Private hostControl |
Type : TabControlModel
|
Default value : null
|
The host control for the region |
Static IsGeneratedProperty |
Type : DependencyProperty
|
Default value : DependencyProperty.registerNewProperty(
'IsGenerated',
ReflectionHelper.getTypeInfo(Boolean),
ReflectionHelper.getTypeInfo(TabControlRegionSyncBehavior),
new PropertyMetadata(false)
)
|
IsGeneratedPropety dependency property |
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
|
Public ClearContainerForItem | ||||||||
ClearContainerForItem(tabItem: TabItemModel)
|
||||||||
Undoes the effects of the
Parameters :
Returns :
void
|
Public GetContainedItem | ||||||||
GetContainedItem(tabItem: TabItemModel)
|
||||||||
Gets the item contained in the
Parameters :
Returns :
any
The item contained in the |
Public GetContainerForItem | ||||||||||||
GetContainerForItem(item: any, itemCollection: ItemsCollectionModel)
|
||||||||||||
Creates or identifies the element that is used to display the given item.
Parameters :
Returns :
TabItemModel
The element that is used to display the given item. |
Private Static GetDataContext | ||||||
GetDataContext(item: any)
|
||||||
Return the appropriate data context. If the item is a FrameworkElement it cannot be a data context in Silverlight, so we use its data context. Otherwise, we just us the item as the data context.
Parameters :
Returns :
any
|
Private OnActiveViewsChanged | |||||||||
OnActiveViewsChanged(sender: any, e: CollectionChangeInfo)
|
|||||||||
Parameters :
Returns :
void
|
Public OnAttach |
OnAttach()
|
Inherited from
RegionBehavior
|
Defined in
RegionBehavior:122
|
Override this method to perform the logic after the behavior has been attached.
Returns :
void
|
Private OnSelectionChanged |
OnSelectionChanged(sender: any, e: any)
|
Returns :
void
|
Private OnViewsChanged | |||||||||
OnViewsChanged(sender: any, e: CollectionChangeInfo)
|
|||||||||
Parameters :
Returns :
void
|
Public PrepareContainerForItem | ||||||||||||
PrepareContainerForItem(item: any, parent: DependencyObject)
|
||||||||||||
Override to change how TabItem's are prepared for items.
Parameters :
Returns :
TabItemModel
A tab item that wraps the supplied |
Private SynchronizeItems |
SynchronizeItems()
|
Returns :
void
|
Attach |
Attach()
|
Inherited from
RegionBehavior
|
Defined in
RegionBehavior:75
|
Attaches the behavior to the region
Returns :
void
|
HostControl | ||||||
getHostControl()
|
||||||
Gets the HostControl
Returns :
DependencyObject
|
||||||
setHostControl(value: DependencyObject)
|
||||||
Sets the HostControl
Parameters :
Returns :
void
|
import { RegionBehavior } from '../../RegionBehavior';
import { DependencyProperty } from '../../../basecomponentmodel/DependencyProperty';
import { DependencyObject } from '../../../basecomponentmodel/DependencyObject';
import {
tryToConvertType,
InvalidOperationException,
ReflectionHelper,
convertTypeTo,
ArgumentNullException,
SimpleList,
CollectionChangeInfo,
CollectionChangeAction,
} from '../../../baseframework';
import {
TabItemModel,
TabControlModel,
ItemsCollectionModel,
} from '../../../models';
import { PropertyMetadata } from '../../../basecomponentmodel/PropertyMetadata';
import { FrameworkElement } from '../../../basecomponentmodel/FrameworkElement';
import { IHostAwareRegionBehavior } from '../../IRegionBehavior';
/**
* Behavior for the TabControl Region added items, and keeps sync the Selected Item.
*
* @export
* @class TabControlRegionSyncBehavior
* @extends {RegionBehavior}
* @implements {IHostAwareRegionBehavior}
* @wType Microsoft.Practices.Prism.Regions.Behaviors.TabControlRegionSyncBehavior
*/
export class TabControlRegionSyncBehavior
extends RegionBehavior
implements IHostAwareRegionBehavior
{
/**
* TabControlRegionSyncBehavior key used to register the behavior in the collection
*
* @static
* @type {string}
* @memberof TabControlRegionSyncBehavior
*/
public static readonly BehaviorKey: string = 'TabControlRegionSyncBehavior';
/**
* IsGeneratedPropety dependency property
*
* @static
* @type {DependencyProperty}
* @memberof TabControlRegionSyncBehavior
* @wIgnore
*/
public static IsGeneratedProperty: DependencyProperty =
DependencyProperty.registerNewProperty(
'IsGenerated',
ReflectionHelper.getTypeInfo(Boolean),
ReflectionHelper.getTypeInfo(TabControlRegionSyncBehavior),
new PropertyMetadata(false)
);
/**
* The host control for the region
*
* @private
* @type {TabControlModel}
* @memberof TabControlRegionSyncBehavior
*/
private hostControl: TabControlModel = 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: TabControlModel = tryToConvertType<TabControlModel>(
value,
TabControlModel
);
if (newValue == null) {
throw new InvalidOperationException('Host Control must be a TabControl');
}
if (this.IsAttached) {
throw new InvalidOperationException(
'HostControl Control can not be set after Attach'
);
}
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'
);
}
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)
);
}
/**
*
* Gets the item contained in the <see cref="TabItem"/>.
*
* @param tabItem The container item.
* @returns The item contained in the <paramref name="tabItem"/> if it was generated automatically by the behavior; otherwise <paramref name="tabItem"/>.
*/
public GetContainedItem(tabItem: TabItemModel): any {
if (
convertTypeTo<boolean>(
tabItem.getValue(TabControlRegionSyncBehavior.IsGeneratedProperty),
Boolean
)
) {
return tabItem.Content;
}
return tabItem;
}
/**
*
* Override to change how TabItem's are prepared for items.
*
* @param item The item to wrap in a TabItem
* @param parent The parent <see cref="DependencyObject"/>
* @returns A tab item that wraps the supplied <paramref name="item"/>
*/
public PrepareContainerForItem(
item: any,
parent: DependencyObject
): TabItemModel {
let container: TabItemModel = tryToConvertType<TabItemModel>(
item,
TabItemModel
);
if (container == null) {
let dataContext: any = TabControlRegionSyncBehavior.GetDataContext(item);
container = new TabItemModel();
container.Content = item;
container.DataContext = dataContext;
container.header = dataContext;
container.setValue(
TabControlRegionSyncBehavior.IsGeneratedProperty,
true
);
}
return container;
}
/**
*
* Undoes the effects of the <see cref="PrepareContainerForItem"/> method.
*
* @param tabItem The container element for the item.
*/
public ClearContainerForItem(tabItem: TabItemModel): void {
if (
convertTypeTo<boolean>(
tabItem.getValue(TabControlRegionSyncBehavior.IsGeneratedProperty),
Boolean
)
) {
tabItem.Content = null;
}
}
/**
*
* Creates or identifies the element that is used to display the given item.
*
* @param item The item to get the container for.
* @param itemCollection The parent's <see cref="ItemCollection"/>.
* @returns The element that is used to display the given item.
*/
public GetContainerForItem(
item: any,
itemCollection: ItemsCollectionModel
): TabItemModel {
if (itemCollection == null) {
throw new ArgumentNullException('itemCollection');
}
let container: TabItemModel = tryToConvertType<TabItemModel>(
item,
TabItemModel
);
if (
container != null &&
convertTypeTo<boolean>(
container.getValue(TabControlRegionSyncBehavior.IsGeneratedProperty),
Boolean
) == false
) {
return container;
}
for (let tabItemTmp of itemCollection) {
let tabItem = tabItemTmp as TabItemModel;
if (
convertTypeTo<boolean>(
tabItem.getValue(TabControlRegionSyncBehavior.IsGeneratedProperty),
Boolean
)
) {
if (tabItem.Content == item) {
return tabItem;
}
}
}
return null;
}
/**
*
* Return the appropriate data context. If the item is a FrameworkElement it cannot be a data context in Silverlight, so we use its data context.
* Otherwise, we just us the item as the data context.
*
*/
private static GetDataContext(item: any): any {
let frameworkElement: FrameworkElement = tryToConvertType<FrameworkElement>(
item,
FrameworkElement
);
return frameworkElement == null ? item : frameworkElement.DataContext;
}
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) {
let tabItem: TabItemModel = this.PrepareContainerForItem(
view,
this.hostControl
);
this.hostControl.items.add(tabItem);
}
for (let existingItem of existingItems) {
this.Region.Add(existingItem);
}
}
private OnSelectionChanged(sender: any, e: any): void {
if (this.hostControl == sender) {
for (let tabItem of e.RemovedItems) {
let item: any = this.GetContainedItem(tabItem);
if (
this.Region.Views.Contains(item) &&
this.Region.ActiveViews.Contains(item)
) {
this.Region.Deactivate(item);
}
}
for (let tabItem of e.AddedItems) {
let item: any = this.GetContainedItem(tabItem);
if (!this.Region.ActiveViews.Contains(item)) {
this.Region.Activate(item);
}
}
}
}
private OnActiveViewsChanged(sender: any, e: CollectionChangeInfo): void {
if (e.action == CollectionChangeAction.Add) {
this.hostControl.selectedItem = this.GetContainerForItem(
e.NewItems.getItem(0),
this.hostControl.items
);
} else if (
e.action == CollectionChangeAction.Remove &&
this.hostControl.selectedItem != null &&
e.OldItems.contains(
this.GetContainedItem(
convertTypeTo<TabItemModel>(
this.hostControl.selectedItem,
TabItemModel
)
)
)
) {
this.hostControl.selectedItem = null;
}
}
private OnViewsChanged(sender: any, e: CollectionChangeInfo): void {
if (e.action == CollectionChangeAction.Add) {
for (let newItem of e.NewItems) {
let tabItem: TabItemModel = this.PrepareContainerForItem(
newItem,
this.hostControl
);
this.hostControl.items.add(tabItem);
}
} else if (e.action == CollectionChangeAction.Remove) {
for (let oldItem of e.OldItems) {
let tabItem: TabItemModel = this.GetContainerForItem(
oldItem,
this.hostControl.items
);
this.hostControl.items.remove(tabItem);
this.ClearContainerForItem(tabItem);
}
}
}
}