@@ -414,7 +414,10 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
414
414
415
415
if ( this . igxForScrollOrientation === 'vertical' ) {
416
416
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
+ }
418
421
this . _maxHeight = this . _calcMaxBrowserHeight ( ) ;
419
422
this . scrollComponent . size = this . igxForOf ? this . _calcHeight ( ) : 0 ;
420
423
this . syncScrollService . setScrollMaster ( this . igxForScrollOrientation , this . scrollComponent ) ;
@@ -721,6 +724,34 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
721
724
return scroll ;
722
725
}
723
726
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
+
724
755
/**
725
756
* @hidden
726
757
* Function that recalculates and updates cache sizes.
@@ -1115,20 +1146,16 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
1115
1146
protected initSizesCache ( items : any [ ] ) : number {
1116
1147
let totalSize = 0 ;
1117
1148
let size = 0 ;
1118
- const dimension = this . igxForScrollOrientation === 'horizontal' ?
1119
- this . igxForSizePropName : 'height' ;
1149
+ const dimension = this . igxForSizePropName || 'height' ;
1120
1150
let i = 0 ;
1121
1151
this . sizesCache = [ ] ;
1122
1152
this . heightCache = [ ] ;
1123
1153
this . sizesCache . push ( 0 ) ;
1124
1154
const count = this . isRemote ? this . totalItemCount : items . length ;
1125
1155
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' ) {
1129
1158
this . heightCache . push ( size ) ;
1130
- } else {
1131
- size = this . _getItemSize ( items [ i ] , dimension ) ;
1132
1159
}
1133
1160
totalSize += size ;
1134
1161
this . sizesCache . push ( totalSize ) ;
@@ -1359,6 +1386,11 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
1359
1386
this . _virtScrollTop = realPercentScrolled * maxVirtScrollTop ;
1360
1387
}
1361
1388
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
+
1362
1394
private _updateVScrollOffset ( ) {
1363
1395
let scrollOffset = 0 ;
1364
1396
let currentScrollTop = this . scrollPosition ;
@@ -1380,10 +1412,6 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
1380
1412
this . dc . instance . _viewContainer . element . nativeElement . style . left = - scrollOffset + 'px' ;
1381
1413
}
1382
1414
1383
- private _getItemSize ( item , dimension : string ) : number {
1384
- const dim = item [ dimension ] ;
1385
- return typeof dim === 'number' ? dim : parseInt ( this . igxForItemSize , 10 ) || 0 ;
1386
- }
1387
1415
1388
1416
private _adjustScrollPositionAfterSizeChange ( sizeDiff ) {
1389
1417
// if data has been changed while container is scrolled
@@ -1436,10 +1464,14 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
1436
1464
* @internal
1437
1465
*/
1438
1466
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 {
1440
1473
return this . _sizesCache ;
1441
1474
}
1442
- return this . syncService . sizesCache ( this . igxForScrollOrientation ) ;
1443
1475
}
1444
1476
/**
1445
1477
* @hidden
@@ -1450,7 +1482,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
1450
1482
}
1451
1483
1452
1484
protected get itemsDimension ( ) {
1453
- return this . igxForScrollOrientation === 'horizontal' ? this . igxForSizePropName : 'height' ;
1485
+ return this . igxForSizePropName || 'height' ;
1454
1486
}
1455
1487
1456
1488
/**
@@ -1578,10 +1610,9 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
1578
1610
1579
1611
protected getItemSize ( item ) {
1580
1612
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 ) ;
1585
1616
if ( item && item . summaries ) {
1586
1617
size = item . max ;
1587
1618
} else if ( item && item . groups && item . height ) {
@@ -1594,7 +1625,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
1594
1625
}
1595
1626
1596
1627
protected initSizesCache ( items : any [ ] ) : number {
1597
- if ( ! this . syncService . isMaster ( this ) ) {
1628
+ if ( ! this . syncService . isMaster ( this ) && this . igxForScrollOrientation === 'horizontal' ) {
1598
1629
const masterSizesCache = this . syncService . sizesCache ( this . igxForScrollOrientation ) ;
1599
1630
return masterSizesCache [ masterSizesCache . length - 1 ] ;
1600
1631
}
@@ -1607,7 +1638,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
1607
1638
const count = this . isRemote ? this . totalItemCount : items . length ;
1608
1639
for ( i ; i < count ; i ++ ) {
1609
1640
size = this . getItemSize ( items [ i ] ) ;
1610
- if ( this . itemsDimension === 'height ' ) {
1641
+ if ( this . igxForScrollOrientation === 'vertical ' ) {
1611
1642
this . heightCache . push ( size ) ;
1612
1643
}
1613
1644
totalSize += size ;
@@ -1744,10 +1775,15 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
1744
1775
* @hidden
1745
1776
*/
1746
1777
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 {
1748
1784
return super . _calcMaxChunkSize ( ) ;
1749
1785
}
1750
- return this . syncService . chunkSize ( this . igxForScrollOrientation ) ;
1786
+
1751
1787
}
1752
1788
}
1753
1789
0 commit comments