11import { ICollectionObserverSplice , mergeSplice } from 'aurelia-binding' ;
22import { ViewSlot } from 'aurelia-templating' ;
33import { ArrayRepeatStrategy , createFullOverrideContext } from 'aurelia-templating-resources' ;
4- import { IView , IVirtualRepeatStrategy } from './interfaces' ;
4+ import { IView , IVirtualRepeatStrategy , VirtualizationCalculation } from './interfaces' ;
55import {
66 Math$abs ,
77 Math$floor ,
@@ -22,12 +22,12 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
2222 return repeat . addView ( overrideContext . bindingContext , overrideContext ) ;
2323 }
2424
25- initCalculation ( repeat : VirtualRepeat , items : any [ ] ) : boolean {
25+ initCalculation ( repeat : VirtualRepeat , items : any [ ] ) : VirtualizationCalculation {
2626 const itemCount = items . length ;
2727 // when there is no item, bails immediately
2828 // and return false to notify calculation finished unsuccessfully
2929 if ( ! ( itemCount > 0 ) ) {
30- return false ;
30+ return VirtualizationCalculation . reset ;
3131 }
3232 // before invoking instance changed, there needs to be basic calculation on how
3333 // the required vairables such as item height and elements required
@@ -38,19 +38,21 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
3838 }
3939 const isFixedHeightContainer = repeat . _fixedHeightContainer = hasOverflowScroll ( containerEl ) ;
4040 const firstView = repeat . _firstView ( ) ;
41- const itemHeight = repeat . itemHeight = calcOuterHeight ( firstView . firstChild as Element ) ;
41+ const itemHeight = calcOuterHeight ( firstView . firstChild as Element ) ;
4242 // when item height is 0, bails immediately
4343 // and return false to notify calculation has finished unsuccessfully
4444 // it cannot be processed further when item is 0
4545 if ( itemHeight === 0 ) {
46- return false ;
46+ return VirtualizationCalculation . none ;
4747 }
48+ repeat . itemHeight = itemHeight ;
4849 const scroll_el_height = isFixedHeightContainer
4950 ? calcScrollHeight ( containerEl )
5051 : document . documentElement . clientHeight ;
52+ // console.log({ scroll_el_height })
5153 const elementsInView = repeat . elementsInView = Math$floor ( scroll_el_height / itemHeight ) + 1 ;
5254 const viewsCount = repeat . _viewsLength = elementsInView * 2 ;
53- return true ;
55+ return VirtualizationCalculation . has_sizing | VirtualizationCalculation . observe_scroller ;
5456 }
5557
5658 /**
@@ -88,8 +90,7 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
8890 if ( currItemCount === 0 ) {
8991 repeat . removeAllViews ( /*return to cache?*/ true , /*skip animation?*/ false ) ;
9092 repeat . _resetCalculation ( ) ;
91- delete repeat . __queuedSplices ;
92- delete repeat . __array ;
93+ repeat . __queuedSplices = repeat . __array = undefined ;
9394 return false ;
9495 }
9596 /*
@@ -99,26 +100,32 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
99100 To figure out that one, we're going to have to know where we are in our scrolling so we can know how far down we've gone to show the first view
100101 That "first" is calculated and passed into here
101102 */
102- // remove unneeded views.
103+
104+ // if the number of items shrinks to less than number of active views
105+ // remove all unneeded views
103106 let realViewsCount = repeat . viewCount ( ) ;
104- // console.log('0. Start inplace process item', { realViewsCount, currItemCount, els: repeat.elementsInView, max: repeat._viewsLength })
105107 while ( realViewsCount > currItemCount ) {
106108 realViewsCount -- ;
107109 repeat . removeView ( realViewsCount , /*return to cache?*/ true , /*skip animation?*/ false ) ;
108110 }
109- // console.log(realViewsCount, repeat._viewsLength)
111+ // there is situation when container height shrinks
112+ // the real views count will be greater than new maximum required view count
113+ // remove all unnecessary view
114+ while ( realViewsCount > repeat . _viewsLength ) {
115+ realViewsCount -- ;
116+ repeat . removeView ( realViewsCount , /*return to cache?*/ true , /*skip animation?*/ false ) ;
117+ }
110118 realViewsCount = Math$min ( realViewsCount , repeat . _viewsLength ) ;
119+
111120 const local = repeat . local ;
112121 const lastIndex = currItemCount - 1 ;
113122
114- // console.log('1. firstIndex, realCount, lastIndex', { firstIndex, realViewsCount, lastIndex })
115123 if ( firstIndex + realViewsCount > lastIndex ) {
116124 // first = currItemCount - realViewsCount instead of: first = currItemCount - 1 - realViewsCount;
117125 // this is because during view update
118126 // view(i) starts at 0 and ends at less than last
119127 firstIndex = Math$max ( 0 , currItemCount - realViewsCount ) ;
120128 }
121- // console.log('2. firstIndex, realCount, lastIndex', { firstIndex, realViewsCount, lastIndex })
122129
123130 repeat . _first = firstIndex ;
124131 // re-evaluate bindings on existing views.
@@ -151,9 +158,6 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
151158 }
152159 // add new views
153160 const minLength = Math$min ( repeat . _viewsLength , currItemCount ) ;
154- // console.log('4. After updating existing views',
155- // { firstIndex, minLength, maxView: repeat._viewsLength, currItemCount, realViewsCount }
156- // )
157161 for ( let i = realViewsCount ; i < minLength ; i ++ ) {
158162 const overrideContext = createFullOverrideContext ( repeat , items [ i ] , i , currItemCount ) ;
159163 repeat . addView ( overrideContext . bindingContext , overrideContext ) ;
@@ -165,7 +169,7 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
165169 _standardProcessInstanceMutated ( repeat : VirtualRepeat , array : Array < any > , splices : ICollectionObserverSplice [ ] ) : void {
166170 if ( repeat . __queuedSplices ) {
167171 for ( let i = 0 , ii = splices . length ; i < ii ; ++ i ) {
168- let { index, removed, addedCount} = splices [ i ] ;
172+ const { index, removed, addedCount } = splices [ i ] ;
169173 mergeSplice ( repeat . __queuedSplices , index , removed , addedCount ) ;
170174 }
171175 repeat . __array = array . slice ( 0 ) ;
@@ -174,23 +178,21 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
174178 if ( array . length === 0 ) {
175179 repeat . removeAllViews ( /*return to cache?*/ true , /*skip animation?*/ false ) ;
176180 repeat . _resetCalculation ( ) ;
177- delete repeat . __queuedSplices ;
178- delete repeat . __array ;
181+ repeat . __queuedSplices = repeat . __array = undefined ;
179182 return ;
180183 }
181184
182- let maybePromise = this . _runSplices ( repeat , array . slice ( 0 ) , splices ) ;
185+ const maybePromise = this . _runSplices ( repeat , array . slice ( 0 ) , splices ) ;
183186 if ( maybePromise instanceof Promise ) {
184- let queuedSplices = repeat . __queuedSplices = [ ] ;
187+ const queuedSplices = repeat . __queuedSplices = [ ] ;
185188
186- let runQueuedSplices = ( ) => {
189+ const runQueuedSplices = ( ) => {
187190 if ( ! queuedSplices . length ) {
188- delete repeat . __queuedSplices ;
189- delete repeat . __array ;
191+ repeat . __queuedSplices = repeat . __array = undefined ;
190192 return ;
191193 }
192194
193- let nextPromise = this . _runSplices ( repeat , repeat . __array , queuedSplices ) || Promise . resolve ( ) ;
195+ const nextPromise = this . _runSplices ( repeat , repeat . __array , queuedSplices ) || Promise . resolve ( ) ;
194196 nextPromise . then ( runQueuedSplices ) ;
195197 } ;
196198
0 commit comments