projects/i-components/src/lib/directives/virtual-scroller.directive.ts
Directive to use when a virtual scroller is required
Selector | [wmVirtualScroller] |
Properties |
Methods |
Inputs |
Accessors |
hostComponent | |
Type : any
|
|
Defines the host component |
itemHeight | |
Type : number
|
|
Default value : this.viewportHeight / this.pageSize || 20
|
|
Defines the size of a single item in the list |
pageSize | |
Type : number
|
|
Defines the amount of displayed items in the list by default the value is 10 |
scrollDelay | |
Type : number
|
|
Defines the amount of time to retrieve the data after scrolling action |
totalItems | |
Type : number
|
|
Defines the total amount of items in the list |
virtualScroll | |
Type : boolean
|
|
Default value : false
|
|
Enables virtual scrolling functionality on host components. |
resetValues | ||||||
resetValues(totalItems: number)
|
||||||
Reset the default indexes values
Parameters :
Returns :
void
|
scrollHandlerDelayDefaultValue |
scrollHandlerDelayDefaultValue()
|
Defines the default value for the scroll delay
Returns :
number
{number} |
Private _tolerance |
Type : number
|
Default value : 0
|
Inner variable for tolerance |
Private _totalItems |
Type : number
|
Default value : 0
|
Inner variable for totalItems |
hostComponent |
Type : any
|
Decorators :
@Input()
|
Defines the host component |
itemHeight |
Default value : this.viewportHeight / this.pageSize || 20
|
Decorators :
@Input()
|
Defines the size of a single item in the list |
itemsAbove |
Default value : this.startIndex - this.tolerance - this.minIndex
|
Elements above the viewport |
minIndex |
Type : number
|
Default value : 0
|
Minimum index value |
scrollDelay |
Type : number
|
Decorators :
@Input()
|
Defines the amount of time to retrieve the data after scrolling action |
scrollTop |
Type : number
|
Default value : 0
|
Current scroll value |
startIndex |
Type : number
|
Default value : 0
|
The initial position of element |
topIndex |
Type : number
|
Default value : 0
|
Top index scrolling; |
virtualScroll |
Default value : false
|
Decorators :
@Input()
|
Enables virtual scrolling functionality on host components. |
virtualScrollRequired |
Default value : true
|
Inner variable for virtualScrollRequired |
totalItems | ||||||
gettotalItems()
|
||||||
Defines the total amount of items in the list
Returns :
number
|
||||||
settotalItems(val: number)
|
||||||
Defines the total amount of items in the list
Parameters :
Returns :
void
|
pageSize |
getpageSize()
|
Defines the amount of displayed items in the list by default the value is 10 |
tolerance | ||||||
gettolerance()
|
||||||
Defines a tolerance of items that are retrieved but not visible in the viewport
Returns :
number
|
||||||
settolerance(val: number)
|
||||||
Defines a tolerance of items that are retrieved but not visible in the viewport
Parameters :
Returns :
void
|
viewportHeight |
getviewportHeight()
|
Returns the height of viewport container
Returns :
number
|
totalHeight |
gettotalHeight()
|
Returns the total height of rendered items in the virtualized container
Returns :
number
|
toleranceHeight |
gettoleranceHeight()
|
Return the tolerance height
Returns :
number
|
bufferSize |
getbufferSize()
|
Returns the buffer size
Returns :
number
|
topPaddingHeight |
gettopPaddingHeight()
|
Calculates topPaddingHeight
Returns :
number
|
bottomPaddingHeight |
getbottomPaddingHeight()
|
Calculates the the bottom padding height value
Returns :
number
|
import { Directive, Input } from '@angular/core';
import { Utils } from '../utils';
/**
* Directive to use when a virtual scroller is required
*
* @export
* @class VirtualScrollerDirective
*/
@Directive({
selector: '[wmVirtualScroller]',
})
export class VirtualScrollerDirective {
/**
* Inner variable for totalItems
*
* @private
* @type {number}
* @memberof VirtualScrollerDirective
*/
private _totalItems = 0;
/**
* Inner variable for tolerance
*
* @private
* @type {number}
* @memberof VirtualScrollerDirective
*/
private _tolerance = 0;
/**
* Inner variable for virtualScrollRequired
*
* @memberof VirtualScrollerDirective
*/
virtualScrollRequired = true;
/**
* Current scroll value
*
* @type {number}
* @memberof VirtualScrollerDirective
*/
scrollTop = 0;
/**
* Enables virtual scrolling functionality on
* host components.
*
* @memberof VirtualScrollerDirective
*/
@Input()
virtualScroll = false;
/**
* Defines the size of a single item in the list
*
* @type {number}
* @memberof VirtualScrollerDirective
*/
@Input()
itemHeight = this.viewportHeight / this.pageSize || 20;
/**
* Defines the total amount of items in the list
*
* @type {number}
* @memberof VirtualScrollerDirective
*/
@Input()
get totalItems(): number {
return this._totalItems;
}
/**
* Defines the total amount of items in the list
*
* @memberof VirtualScrollerDirective
*/
set totalItems(val: number) {
this._totalItems = val;
}
/**
* Defines the amount of time to retrieve the data after scrolling action
*
* @type {number}
* @memberof VirtualScrollerDirective
*/
@Input()
scrollDelay: number;
/**
* Defines the host component
*
* @type {*}
* @memberof VirtualScrollerDirective
*/
@Input()
hostComponent: any;
/**
* Defines the amount of displayed items in the list
* by default the value is 10
*
* @memberof VirtualScrollerDirective
*/
@Input()
get pageSize() {
return this.totalItems < 10 ? this.totalItems : 10;
}
/**
* Defines a tolerance of items that are
* retrieved but not visible in the viewport
*
* @memberof VirtualScrollerDirective
*/
get tolerance(): number {
return this._tolerance;
}
/**
* Defines a tolerance of items that are
* retrieved but not visible in the viewport
*
* @memberof VirtualScrollerDirective
*/
set tolerance(val: number) {
this._tolerance = val;
}
/**
* The initial position of element
*
* @memberof VirtualScrollerDirective
*/
startIndex = 0;
/**
* Minimum index value
*
* @memberof VirtualScrollerDirective
*/
minIndex = 0;
/**
* Top index scrolling;
*
* @memberof VirtualScrollerDirective
*/
topIndex = 0;
/**
* Elements above the viewport
*
* @memberof VirtualScrollerDirective
*/
itemsAbove = this.startIndex - this.tolerance - this.minIndex;
/**
* Returns the height of viewport container
*
* @readonly
* @type {number}
* @memberof VirtualScrollerDirective
*/
get viewportHeight(): number {
return this.pageSize * this.itemHeight;
}
/**
* Returns the total height of rendered items in the virtualized container
*
* @readonly
* @type {number}
* @memberof VirtualScrollerDirective
*/
get totalHeight(): number {
return (this.totalItems + 1) * this.itemHeight;
}
/**
* Return the tolerance height
*
* @readonly
* @type {number}
* @memberof VirtualScrollerDirective
*/
get toleranceHeight(): number {
return this.tolerance * this.itemHeight;
}
/**
* Returns the buffer size
*
* @readonly
* @type {number}
* @memberof VirtualScrollerDirective
*/
get bufferSize(): number {
return this.pageSize + 2 * this.tolerance;
}
/**
* Calculates topPaddingHeight
*
* @readonly
* @type {number}
* @memberof VirtualScrollerDirective
*/
get topPaddingHeight(): number {
return this.itemsAbove * this.itemHeight;
}
/**
* Calculates the the bottom padding height value
*
* @readonly
* @type {number}
* @memberof VirtualScrollerDirective
*/
get bottomPaddingHeight(): number {
if (this.itemsAbove >= this.totalItems - this.bufferSize) {
return 0;
} else {
return this.totalHeight - this.topPaddingHeight;
}
}
/**
* Defines the default value for the scroll delay
*
* @return {*} {number}
* @memberof VirtualScrollerDirective
*/
scrollHandlerDelayDefaultValue(): number {
return Utils.isFirefoxBrowser() ? 300 : 100;
}
/**
* Reset the default indexes values
*
* @param {number} totalItems
* @memberof VirtualScrollerDirective
*/
resetValues(totalItems: number): void {
this.startIndex = 0;
this.minIndex = 0;
this.totalItems = totalItems;
}
}