@@ -414,7 +414,10 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
414414
415415 if ( this . igxForScrollOrientation === 'vertical' ) {
416416 this . dc . instance . _viewContainer . element . nativeElement . style . top = '0px' ;
417- this . scrollComponent = vc . createComponent ( VirtualHelperComponent ) . instance ;
417+ this . scrollComponent = this . syncScrollService . getScrollMaster ( this . igxForScrollOrientation ) ;
418+ if ( ! this . scrollComponent || ! this . document . contains ( this . scrollComponent . elementRef . nativeElement ) ) {
419+ this . scrollComponent = vc . createComponent ( VirtualHelperComponent ) . instance
420+ }
418421 this . _maxHeight = this . _calcMaxBrowserHeight ( ) ;
419422 this . scrollComponent . size = this . igxForOf ? this . _calcHeight ( ) : 0 ;
420423 this . syncScrollService . setScrollMaster ( this . igxForScrollOrientation , this . scrollComponent ) ;
@@ -721,6 +724,34 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
721724 return scroll ;
722725 }
723726
727+ /**
728+ * Returns the index of the element at the specified offset.
729+ * ```typescript
730+ * this.parentVirtDir.getIndexAtScroll(100);
731+ * ```
732+ */
733+ public getIndexAtScroll ( scrollOffset : number ) {
734+ return this . getIndexAt ( scrollOffset , this . sizesCache ) ;
735+ }
736+ /**
737+ * Returns whether the target index is outside the view.
738+ * ```typescript
739+ * this.parentVirtDir.isIndexOutsideView(10);
740+ * ```
741+ */
742+ public isIndexOutsideView ( index : number ) {
743+ const targetNode = index >= this . state . startIndex && index <= this . state . startIndex + this . state . chunkSize ?
744+ this . _embeddedViews . map ( view =>
745+ view . rootNodes . find ( node => node . nodeType === Node . ELEMENT_NODE ) || view . rootNodes [ 0 ] . nextElementSibling ) [ index - this . state . startIndex ] : null ;
746+ const rowHeight = this . getSizeAt ( index ) ;
747+ const containerSize = parseInt ( this . igxForContainerSize , 10 ) ;
748+ const containerOffset = - ( this . scrollPosition - this . sizesCache [ this . state . startIndex ] ) ;
749+ const endTopOffset = targetNode ? targetNode . offsetTop + rowHeight + containerOffset : containerSize + rowHeight ;
750+ return ! targetNode || targetNode . offsetTop < Math . abs ( containerOffset )
751+ || containerSize && endTopOffset - containerSize > 5 ;
752+ }
753+
754+
724755 /**
725756 * @hidden
726757 * Function that recalculates and updates cache sizes.
@@ -1115,20 +1146,16 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
11151146 protected initSizesCache ( items : any [ ] ) : number {
11161147 let totalSize = 0 ;
11171148 let size = 0 ;
1118- const dimension = this . igxForScrollOrientation === 'horizontal' ?
1119- this . igxForSizePropName : 'height' ;
1149+ const dimension = this . igxForSizePropName || 'height' ;
11201150 let i = 0 ;
11211151 this . sizesCache = [ ] ;
11221152 this . heightCache = [ ] ;
11231153 this . sizesCache . push ( 0 ) ;
11241154 const count = this . isRemote ? this . totalItemCount : items . length ;
11251155 for ( i ; i < count ; i ++ ) {
1126- if ( dimension === 'height' ) {
1127- // cols[i][dimension] = parseInt(this.igxForItemSize, 10) || 0;
1128- size = parseInt ( this . igxForItemSize , 10 ) || 0 ;
1156+ size = this . _getItemSize ( items [ i ] , dimension ) ;
1157+ if ( this . igxForScrollOrientation === 'vertical' ) {
11291158 this . heightCache . push ( size ) ;
1130- } else {
1131- size = this . _getItemSize ( items [ i ] , dimension ) ;
11321159 }
11331160 totalSize += size ;
11341161 this . sizesCache . push ( totalSize ) ;
@@ -1359,6 +1386,11 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
13591386 this . _virtScrollTop = realPercentScrolled * maxVirtScrollTop ;
13601387 }
13611388
1389+ protected _getItemSize ( item , dimension : string ) : number {
1390+ const dim = item ? item [ dimension ] : null ;
1391+ return typeof dim === 'number' ? dim : parseInt ( this . igxForItemSize , 10 ) || 0 ;
1392+ }
1393+
13621394 private _updateVScrollOffset ( ) {
13631395 let scrollOffset = 0 ;
13641396 let currentScrollTop = this . scrollPosition ;
@@ -1380,10 +1412,6 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
13801412 this . dc . instance . _viewContainer . element . nativeElement . style . left = - scrollOffset + 'px' ;
13811413 }
13821414
1383- private _getItemSize ( item , dimension : string ) : number {
1384- const dim = item [ dimension ] ;
1385- return typeof dim === 'number' ? dim : parseInt ( this . igxForItemSize , 10 ) || 0 ;
1386- }
13871415
13881416 private _adjustScrollPositionAfterSizeChange ( sizeDiff ) {
13891417 // if data has been changed while container is scrolled
@@ -1436,10 +1464,14 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
14361464 * @internal
14371465 */
14381466 public get sizesCache ( ) : number [ ] {
1439- if ( this . syncService . isMaster ( this ) ) {
1467+ if ( this . igxForScrollOrientation === 'horizontal' ) {
1468+ if ( this . syncService . isMaster ( this ) ) {
1469+ return this . _sizesCache ;
1470+ }
1471+ return this . syncService . sizesCache ( this . igxForScrollOrientation ) ;
1472+ } else {
14401473 return this . _sizesCache ;
14411474 }
1442- return this . syncService . sizesCache ( this . igxForScrollOrientation ) ;
14431475 }
14441476 /**
14451477 * @hidden
@@ -1450,7 +1482,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
14501482 }
14511483
14521484 protected get itemsDimension ( ) {
1453- return this . igxForScrollOrientation === 'horizontal' ? this . igxForSizePropName : 'height' ;
1485+ return this . igxForSizePropName || 'height' ;
14541486 }
14551487
14561488 /**
@@ -1578,10 +1610,9 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
15781610
15791611 protected getItemSize ( item ) {
15801612 let size = 0 ;
1581- const dimension = this . igxForScrollOrientation === 'horizontal' ?
1582- this . igxForSizePropName : 'height' ;
1583- if ( dimension === 'height' ) {
1584- size = parseInt ( this . igxForItemSize , 10 ) || 0 ;
1613+ const dimension = this . igxForSizePropName || 'height' ;
1614+ if ( this . igxForScrollOrientation === 'vertical' ) {
1615+ size = this . _getItemSize ( item , dimension ) ;
15851616 if ( item && item . summaries ) {
15861617 size = item . max ;
15871618 } else if ( item && item . groups && item . height ) {
@@ -1594,7 +1625,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
15941625 }
15951626
15961627 protected initSizesCache ( items : any [ ] ) : number {
1597- if ( ! this . syncService . isMaster ( this ) ) {
1628+ if ( ! this . syncService . isMaster ( this ) && this . igxForScrollOrientation === 'horizontal' ) {
15981629 const masterSizesCache = this . syncService . sizesCache ( this . igxForScrollOrientation ) ;
15991630 return masterSizesCache [ masterSizesCache . length - 1 ] ;
16001631 }
@@ -1607,7 +1638,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
16071638 const count = this . isRemote ? this . totalItemCount : items . length ;
16081639 for ( i ; i < count ; i ++ ) {
16091640 size = this . getItemSize ( items [ i ] ) ;
1610- if ( this . itemsDimension === 'height ' ) {
1641+ if ( this . igxForScrollOrientation === 'vertical ' ) {
16111642 this . heightCache . push ( size ) ;
16121643 }
16131644 totalSize += size ;
@@ -1744,10 +1775,15 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
17441775 * @hidden
17451776 */
17461777 protected _calcMaxChunkSize ( ) : number {
1747- if ( this . syncService . isMaster ( this ) ) {
1778+ if ( this . igxForScrollOrientation === 'horizontal' ) {
1779+ if ( this . syncService . isMaster ( this ) ) {
1780+ return super . _calcMaxChunkSize ( ) ;
1781+ }
1782+ return this . syncService . chunkSize ( this . igxForScrollOrientation ) ;
1783+ } else {
17481784 return super . _calcMaxChunkSize ( ) ;
17491785 }
1750- return this . syncService . chunkSize ( this . igxForScrollOrientation ) ;
1786+
17511787 }
17521788}
17531789
0 commit comments