projects/wms-framework/src/lib/baseframework/VirtualCollection.ts
VirtualCollection class
INotifyCollectionChanged
INotifyPropertyChanged
IPagedCollectionView
Properties |
|
Methods |
|
Accessors |
constructor(anticipatedCount?: number, loadSize?: number)
|
Creates an instance of VirtualCollection. |
Private anticipatedCount |
Type : number
|
AnticipatedCount property. |
CanChangePage |
Type : boolean
|
Default value : true
|
A flag to verify if the page can be changed |
Public CollectionChanged |
Type : SubscriptionEvent<void>
|
Default value : new SubscriptionEvent()
|
Event to indicate that the collection have changed. |
Private innerPageSize |
Type : number
|
Default value : 10
|
Page size backing field |
Protected Static interfacesInitialized |
Default value : false
|
Inherited from
SimpleList
|
Defined in
SimpleList:81
|
Flag to indicate when interfaces have been initialized. |
Private internalData |
Type : literal type
|
Default value : {}
|
Internal page data This object keeps position index vs. page contents |
IsPageChanging |
Type : boolean
|
Default value : false
|
A flag to determine if a page is changing |
ItemCount |
Type : number
|
Default value : 0
|
Items count |
Public ItemDataRequested |
Type : SubscriptionEvent<void>
|
Default value : new SubscriptionEvent()
|
Event to indicate that the ItemDataRequested have changed. |
Protected ItemDataRequestedFired |
Default value : false
|
Flag to indicate if |
Public LoadSize |
Type : number
|
LoadSize property. |
Public NewItem |
Type : any
|
Default value : {}
|
NewItem pendingTo commit |
PageChanged |
Type : SubscriptionEvent<void>
|
Default value : new SubscriptionEvent()
|
Event triggered the when page changed |
PageChanging |
Type : SubscriptionEvent<void>
|
Default value : new SubscriptionEvent()
|
Event triggered the current page is changing |
PageIndex |
Type : number
|
Default value : 0
|
Index of the current page |
pendingItemDataRequest |
Type : any
|
Public PropertyChanged |
Type : SubscriptionEvent<void>
|
Default value : new SubscriptionEvent()
|
Event to indicate that a property have changed. |
TotalItemCount |
Type : number
|
Default value : 0
|
Total items count |
Protected _internalArray |
Type : T[]
|
Default value : []
|
Inherited from
SimpleList
|
Defined in
SimpleList:276
|
add | ||||||
add(value: T)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:409
|
||||||
add value to the collection
Parameters :
Returns :
void
|
addNew |
addNew()
|
Adds a new item to the underlying collection.
Returns :
any
new |
commitNew |
commitNew()
|
Ends the add transaction and saves the pending new item
Returns :
void
|
Private ensureCurrentPageExists |
ensureCurrentPageExists()
|
Ensures that data exists for current page
Returns :
void
|
Protected Static initializeSupportedInterfaces |
initializeSupportedInterfaces()
|
Inherited from
SimpleList
|
Defined in
SimpleList:331
|
Initialize the interfaces supported by this collection.
Returns :
void
|
loadItems | |||||||||
loadItems(index: number, items: Iterable<T>)
|
|||||||||
Loads the list of items into the collection cache.
Parameters :
Returns :
void
|
MoveToFirstPage |
MoveToFirstPage()
|
Move to the first page
Returns :
boolean
{boolean} |
MoveToLastPage |
MoveToLastPage()
|
Move to the last page
Returns :
boolean
{boolean} |
MoveToNextPage |
MoveToNextPage()
|
Move to the next page
Returns :
boolean
{boolean} |
MoveToPage | ||||||
MoveToPage(pageIndex: number)
|
||||||
Move to a specific page
Parameters :
Returns :
boolean
{boolean} |
MoveToPreviousPage |
MoveToPreviousPage()
|
Move to previous page
Returns :
boolean
{boolean} |
onCollectionChanged | ||||||
onCollectionChanged(info: CollectionChangeInfo)
|
||||||
Raises the CollectionChanged event.
Parameters :
Returns :
void
|
onPropertyChanged | ||||||
onPropertyChanged(info: literal type)
|
||||||
Raises the CurrentChanged event
Parameters :
Returns :
void
|
Private populatePages | |||||||||
populatePages(index: number, items: Iterable<T>)
|
|||||||||
Process the items and generates the corresponding pages in the internalData.
Parameters :
Returns :
void
|
Private ResetData |
ResetData()
|
Reset internal data
Returns :
void
|
addRange | ||||||
addRange(range: Iterable<T>)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:452
|
||||||
Adds elements of an iterable to the list
Parameters :
Returns :
void
|
clear |
clear()
|
Inherited from
SimpleList
|
Defined in
SimpleList:398
|
Clears the list
Returns :
void
|
contains | ||||||
contains(value: T)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:410
|
||||||
Check if a value is contained in the list
Parameters :
Returns :
boolean
|
copyTo | |||||||||
copyTo(target: T[], index: number)
|
|||||||||
Inherited from
SimpleList
|
|||||||||
Defined in
SimpleList:513
|
|||||||||
Copies the list to a target array
Parameters :
Returns :
void
|
forEach | ||||||
forEach(action: (e: T) => void)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:439
|
||||||
Performs an action on each element of the list
Parameters :
Returns :
void
|
getItem | ||||||
getItem(index: number)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:323
|
||||||
Parameters :
Returns :
T
|
indexOf | ||||||
indexOf(value: T)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:343
|
||||||
Index of value in list
Parameters :
Returns :
any
|
insert | |||||||||
insert(index: number, value: T)
|
|||||||||
Inherited from
SimpleList
|
|||||||||
Defined in
SimpleList:355
|
|||||||||
Inserts element in index
Parameters :
Returns :
void
|
insertRange | |||||||||
insertRange(position: number, range: Iterable<T>)
|
|||||||||
Inherited from
SimpleList
|
|||||||||
Defined in
SimpleList:466
|
|||||||||
Inserts elements of an interable to the list in a position
Parameters :
Returns :
void
|
remove | ||||||
remove(value: T)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:422
|
||||||
Removes a value from the list
Parameters :
Returns :
boolean
|
Public removeAll | ||||||
removeAll(predicate: (e: T) => void)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:527
|
||||||
Removes all elements from the collection by a predicate
Parameters :
Returns :
number
|
removeAt | ||||||
removeAt(index: number)
|
||||||
Inherited from
SimpleList
|
||||||
Defined in
SimpleList:366
|
||||||
Removes element at index
Parameters :
Returns :
void
|
setItem | |||||||||
setItem(index: number, value: T)
|
|||||||||
Inherited from
SimpleList
|
|||||||||
Defined in
SimpleList:329
|
|||||||||
Parameters :
Returns :
void
|
sort |
sort()
|
Inherited from
SimpleList
|
Defined in
SimpleList:479
|
Sorts the list
Returns :
any
|
Public toArray |
toArray()
|
Inherited from
SimpleList
|
Defined in
SimpleList:545
|
Converts the list to an array
Returns :
{}
|
()
|
Inherited from
SimpleList
|
Defined in
SimpleList:549
|
Returns :
Iterator<T, any, undefined>
|
PageSize | ||||||
getPageSize()
|
||||||
PageSize property
Returns :
number
|
||||||
setPageSize(value: number)
|
||||||
Parameters :
Returns :
void
|
AnticipatedCount | ||||||
getAnticipatedCount()
|
||||||
setAnticipatedCount(value: number)
|
||||||
Parameters :
Returns :
void
|
internalArray | ||||||
getinternalArray()
|
||||||
Gets or sets the internal array.
Returns :
T[]
|
||||||
setinternalArray(value: T[])
|
||||||
Parameters :
Returns :
void
|
loadSize | ||||||
getloadSize()
|
||||||
Gets loadSize property
Returns :
number
|
||||||
setloadSize(value: number)
|
||||||
Set load size property
Parameters :
Returns :
void
|
pageSize | ||||||
getpageSize()
|
||||||
Gets page size |
||||||
setpageSize(value: number)
|
||||||
Sets PageSIze
Parameters :
Returns :
void
|
import { INotifyPropertyChanged } from '../basecomponentmodel/INotifyPropertyChanged';
import { supportedInterfacesCompatibilityMetadataKey } from '../decorators/ClassInfo';
import { SubscriptionEvent } from '../utils';
import {
ObjectModelCollection,
INotifyCollectionChanged,
IPagedCollectionView,
CollectionChangeInfo,
ItemDataRequestedEventArgs,
CollectionChangeAction,
SimpleList,
PageChangingEventArgs,
} from './collections';
/**
* VirtualCollection class
*
* @export
* @class VirtualCollection
* @extends {ObjectModelCollection<T>}
* @implements {INotifyCollectionChanged}
* @implements {INotifyPropertyChanged}
* @template T
* @wType Infragistics.Collections.VirtualCollection`1
*/
export class VirtualCollection<T>
extends ObjectModelCollection<T>
implements
INotifyCollectionChanged,
INotifyPropertyChanged,
IPagedCollectionView
{
pendingItemDataRequest: any;
/**
* Reset internal data
*
* @memberof VirtualCollection
*/
private ResetData(): void {
this.internalData = {};
this.ItemDataRequestedFired = false;
this.MoveToPage(0);
}
/**
* Internal page data
*
* This object keeps position index vs. page contents
*
* @private
* @type {{ [pageIndex: number]: any[]; }}
* @memberof VirtualCollection
*/
private internalData: { [pageIndex: number]: any[] } = {};
/**
* Flag to indicate when interfaces have been initialized.
*
* @protected
* @static
* @memberof VirtualCollection
*/
protected static interfacesInitialized = false;
/**
* Event to indicate that a property have changed.
*
* @memberof ObservableCollection
*/
public PropertyChanged: SubscriptionEvent<
(e: any, args: { PropertyName: string }) => void
> = new SubscriptionEvent();
/**
* Event to indicate that the collection have changed.
*
* @memberof ObservableCollection
*/
public CollectionChanged: SubscriptionEvent<
(e: any, args: CollectionChangeInfo) => void
> = new SubscriptionEvent();
/**
* Event to indicate that the ItemDataRequested have changed.
*
* @memberof ObservableCollection
*/
public ItemDataRequested: SubscriptionEvent<
(e: any, args: ItemDataRequestedEventArgs) => void
> = new SubscriptionEvent();
/**
* LoadSize property.
*
* @type {number}
* @memberof VirtualCollection
*/
public LoadSize: number;
/**
* Page size backing field
*
* @private
* @type {number}
* @memberof VirtualCollection
*/
private innerPageSize: number = 10;
/**
* PageSize property
*
* @type {number}
* @memberof VirtualCollection
*/
public get PageSize(): number {
return this.innerPageSize;
}
public set PageSize(value: number) {
if (this.innerPageSize !== value) {
this.innerPageSize = value;
this.ResetData();
}
}
/**
* AnticipatedCount property.
*
* @type {number}
* @memberof VirtualCollection
*/
private anticipatedCount: number;
public get AnticipatedCount(): number {
return this.anticipatedCount;
}
public set AnticipatedCount(value: number) {
if (this.anticipatedCount !== value) {
this.anticipatedCount = value;
this.TotalItemCount = value;
this.ResetData();
}
}
/**
* NewItem pendingTo commit
*
* @type {*}
* @memberof VirtualCollection
*/
public NewItem: any = {};
/**
* Flag to indicate if `ItemDataRequested` event was already fired.
*
* @protected
* @memberof VirtualCollection
*/
protected ItemDataRequestedFired = false;
/**
* Creates an instance of VirtualCollection.
*
* @param {number} [anticipatedCount]
* @param {number} [loadSize]
* @memberof VirtualCollection
*/
constructor(anticipatedCount?: number, loadSize?: number) {
super();
this.AnticipatedCount = anticipatedCount ?? 0;
this.LoadSize = loadSize ?? 0;
VirtualCollection.initializeSupportedInterfaces();
}
/**
* A flag to verify if the page can be changed
*
* @type {boolean}
* @memberof VirtualCollection
*/
CanChangePage: boolean = true;
/**
* A flag to determine if a page is changing
*
* @type {boolean}
* @memberof VirtualCollection
*/
IsPageChanging: boolean = false;
/**
* Items count
*
* @type {number}
* @memberof VirtualCollection
*/
ItemCount: number = 0;
/**
* Index of the current page
*
* @type {number}
* @memberof VirtualCollection
*/
PageIndex: number = 0;
/**
* Total items count
*
* @type {number}
* @memberof VirtualCollection
*/
TotalItemCount: number = 0;
/**
* Event triggered the when page changed
*
* @memberof VirtualCollection
*/
PageChanged: SubscriptionEvent<(e: any, args: CollectionChangeInfo) => void> =
new SubscriptionEvent();
/**
* Event triggered the current page is changing
*
* @memberof VirtualCollection
*/
PageChanging: SubscriptionEvent<
(e: any, args: PageChangingEventArgs) => void
> = new SubscriptionEvent();
/**
* Move to the first page
*
* @return {*} {boolean}
* @memberof VirtualCollection
*/
MoveToFirstPage(): boolean {
return this.MoveToPage(0);
}
/**
* Move to the last page
*
* @return {*} {boolean}
* @memberof VirtualCollection
*/
MoveToLastPage(): boolean {
return this.MoveToPage(Math.ceil(this.TotalItemCount / this.pageSize) - 1);
}
/**
* Move to the next page
*
* @return {*} {boolean}
* @memberof VirtualCollection
*/
MoveToNextPage(): boolean {
return this.MoveToPage(this.PageIndex + 1);
}
/**
*
* Move to a specific page
* @param {number} pageIndex
* @return {*} {boolean}
* @memberof VirtualCollection
*/
MoveToPage(pageIndex: number): boolean {
if (pageIndex < 0) {
return false;
}
const entryInInternalData = pageIndex * this.pageSize;
if (!(entryInInternalData in this.internalData)) {
if (this.TotalItemCount >= entryInInternalData) {
if (this.pendingItemDataRequest) {
clearTimeout(this.pendingItemDataRequest);
}
this.pendingItemDataRequest = setTimeout(() => {
const args = new ItemDataRequestedEventArgs();
args.StartIndex = pageIndex * this.pageSize;
args.ItemsCount = this.pageSize ?? this.AnticipatedCount;
this.ItemDataRequested.fire([this, args]);
});
} else {
return false;
}
}
this.PageChanging.fire([this, new PageChangingEventArgs(pageIndex)]);
this.PageIndex = pageIndex;
this.PageChanged.fire([
this,
new CollectionChangeInfo(CollectionChangeAction.Reset),
]);
return true;
}
/**
* Move to previous page
*
* @return {*} {boolean}
* @memberof VirtualCollection
*/
MoveToPreviousPage(): boolean {
return this.MoveToPage(this.PageIndex - 1);
}
/**
* Initialize the interfaces supported by this collection.
*
* @private
* @static
* @memberof VirtualCollection
*/
protected static initializeSupportedInterfaces(): void {
if (!VirtualCollection.interfacesInitialized) {
const setMetadataInfo = Reflect.metadata(
supportedInterfacesCompatibilityMetadataKey,
[
'System.ComponentModel.INotifyPropertyChanged',
'System.Collections.IEnumerable',
'System.Collections.IList',
'System.Collections.Specialized.INotifyCollectionChanged',
'System.ComponentModel.IPagedCollectionView',
]
);
setMetadataInfo(VirtualCollection);
VirtualCollection.interfacesInitialized = true;
}
}
/**
* Gets or sets the internal array.
*
* @type {T[]}
* @memberof VirtualCollection
*/
get internalArray(): T[] {
if (!this.ItemDataRequestedFired) {
this.ItemDataRequestedFired = true;
this.MoveToPage(0);
}
return this.internalData[this.PageIndex * this.PageSize] ?? [];
}
set internalArray(value: T[]) {
super.internalArray = value;
}
/**
* Gets loadSize property
*
* @type {number}
* @memberof VirtualCollection
*/
get loadSize(): number {
return this.LoadSize;
}
/**
* Set load size property
*
* @memberof VirtualCollection
*/
set loadSize(value: number) {
this.LoadSize = value;
}
/**
* Gets page size
*
* @memberof VirtualCollection
*/
get pageSize() {
return this.PageSize;
}
/**
* Sets PageSIze
*
* @memberof VirtualCollection
*/
set pageSize(value: number) {
this.PageSize = value;
}
/**
* add value to the collection
*
* @param {T} value
* @memberof VirtualCollection
*/
add(value: T): void {
this.ensureCurrentPageExists();
super.add(value);
const changeEventInfo = new CollectionChangeInfo(
CollectionChangeAction.Add
);
changeEventInfo.NewItems = new SimpleList([value]);
changeEventInfo.NewStartingIndex = this.count - 1;
this.onCollectionChanged(changeEventInfo);
this.onPropertyChanged({ PropertyName: 'Count' });
}
/**
* Raises the CollectionChanged event.
*
* @param {CollectionChangeInfo} info
* @memberof VirtualCollection
*/
onCollectionChanged(info: CollectionChangeInfo): void {
this.CollectionChanged.fire([this, info]);
}
/**
* Raises the CurrentChanged event
*
* @param {{ PropertyName: string }} info
* @memberof VirtualCollection
*/
onPropertyChanged(info: { PropertyName: string }): void {
this.PropertyChanged.fire([this, info]);
}
/**
* Ends the add transaction and saves the pending new item
*
* @memberof VirtualCollection
*/
commitNew(): void {
this.add(this.NewItem);
}
/**
* Adds a new item to the underlying collection.
*
* @returns new
*/
addNew(): any {
return this.NewItem;
}
/**
* Loads the list of items into the collection cache.
*
* @param {number} index
* @param {Iterable<T>} items
* @memberof VirtualCollection
*/
loadItems(index: number, items: Iterable<T>): void {
this.populatePages(index, items);
this.CollectionChanged.fire([
this,
new CollectionChangeInfo(CollectionChangeAction.Reset),
]);
}
/**
* Process the items and generates the corresponding pages in the internalData.
*
* @param {number} index
* @param {Iterable<T>} items
* @memberof VirtualCollection
*/
private populatePages(index: number, items: Iterable<T>) {
let tempArray = [...items];
const count = tempArray.length;
let lastIndex = count > this.PageSize ? this.PageSize : count;
this.internalData[index] = tempArray.splice(0, lastIndex);
if (tempArray.length > 0) {
this.populatePages(index + this.PageSize, tempArray);
}
}
/**
* Ensures that data exists for current page
*
* @memberof VirtualCollection
*/
private ensureCurrentPageExists() {
const internalDataEntryForPage = this.PageIndex * this.PageSize;
if (!(internalDataEntryForPage in this.internalData)) {
this.internalData[internalDataEntryForPage] = [];
}
}
}