1+ define ( [ 'exports' , 'aurelia-templating-resources' , './utilities' ] , function ( exports , _aureliaTemplatingResources , _utilities ) {
2+ 'use strict' ;
3+
4+ Object . defineProperty ( exports , "__esModule" , {
5+ value : true
6+ } ) ;
7+ exports . ArrayVirtualRepeatStrategy = undefined ;
8+
9+
10+
11+ function _possibleConstructorReturn ( self , call ) {
12+ if ( ! self ) {
13+ throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ;
14+ }
15+
16+ return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ;
17+ }
18+
19+ function _inherits ( subClass , superClass ) {
20+ if ( typeof superClass !== "function" && superClass !== null ) {
21+ throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ;
22+ }
23+
24+ subClass . prototype = Object . create ( superClass && superClass . prototype , {
25+ constructor : {
26+ value : subClass ,
27+ enumerable : false ,
28+ writable : true ,
29+ configurable : true
30+ }
31+ } ) ;
32+ if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ;
33+ }
34+
35+ var ArrayVirtualRepeatStrategy = exports . ArrayVirtualRepeatStrategy = function ( _ArrayRepeatStrategy ) {
36+ _inherits ( ArrayVirtualRepeatStrategy , _ArrayRepeatStrategy ) ;
37+
38+ function ArrayVirtualRepeatStrategy ( ) {
39+
40+
41+ return _possibleConstructorReturn ( this , _ArrayRepeatStrategy . apply ( this , arguments ) ) ;
42+ }
43+
44+ ArrayVirtualRepeatStrategy . prototype . createFirstItem = function createFirstItem ( repeat ) {
45+ var overrideContext = ( 0 , _aureliaTemplatingResources . createFullOverrideContext ) ( repeat , repeat . items [ 0 ] , 0 , 1 ) ;
46+ repeat . addView ( overrideContext . bindingContext , overrideContext ) ;
47+ } ;
48+
49+ ArrayVirtualRepeatStrategy . prototype . instanceChanged = function instanceChanged ( repeat , items ) {
50+ this . _inPlaceProcessItems ( repeat , items ) ;
51+ } ;
52+
53+ ArrayVirtualRepeatStrategy . prototype . _standardProcessInstanceChanged = function _standardProcessInstanceChanged ( repeat , items ) {
54+ for ( var i = 1 , ii = repeat . _viewsLength ; i < ii ; ++ i ) {
55+ var overrideContext = ( 0 , _aureliaTemplatingResources . createFullOverrideContext ) ( repeat , items [ i ] , i , ii ) ;
56+ repeat . addView ( overrideContext . bindingContext , overrideContext ) ;
57+ }
58+ } ;
59+
60+ ArrayVirtualRepeatStrategy . prototype . _inPlaceProcessItems = function _inPlaceProcessItems ( repeat , items ) {
61+ var itemsLength = items . length ;
62+ var viewsLength = repeat . viewCount ( ) ;
63+ var first = repeat . _getIndexOfFirstView ( ) ;
64+
65+ while ( viewsLength > itemsLength ) {
66+ viewsLength -- ;
67+ repeat . removeView ( viewsLength , true ) ;
68+ }
69+
70+ var local = repeat . local ;
71+
72+ for ( var i = 0 ; i < viewsLength ; i ++ ) {
73+ var view = repeat . view ( i ) ;
74+ var last = i === itemsLength - 1 ;
75+ var middle = i !== 0 && ! last ;
76+
77+ if ( view . bindingContext [ local ] === items [ i + first ] && view . overrideContext . $middle === middle && view . overrideContext . $last === last ) {
78+ continue ;
79+ }
80+
81+ view . bindingContext [ local ] = items [ i + first ] ;
82+ view . overrideContext . $middle = middle ;
83+ view . overrideContext . $last = last ;
84+ repeat . updateBindings ( view ) ;
85+ }
86+
87+ var minLength = Math . min ( repeat . _viewsLength , items . length ) ;
88+ for ( var _i = viewsLength ; _i < minLength ; _i ++ ) {
89+ var overrideContext = ( 0 , _aureliaTemplatingResources . createFullOverrideContext ) ( repeat , items [ _i ] , _i , itemsLength ) ;
90+ repeat . addView ( overrideContext . bindingContext , overrideContext ) ;
91+ }
92+ } ;
93+
94+ ArrayVirtualRepeatStrategy . prototype . instanceMutated = function instanceMutated ( repeat , array , splices ) {
95+ this . _standardProcessInstanceMutated ( repeat , array , splices ) ;
96+ } ;
97+
98+ ArrayVirtualRepeatStrategy . prototype . _standardProcessInstanceMutated = function _standardProcessInstanceMutated ( repeat , array , splices ) {
99+ var _this2 = this ;
100+
101+ if ( repeat . __queuedSplices ) {
102+ for ( var i = 0 , ii = splices . length ; i < ii ; ++ i ) {
103+ var _splices$i = splices [ i ] ;
104+ var index = _splices$i . index ;
105+ var removed = _splices$i . removed ;
106+ var addedCount = _splices$i . addedCount ;
107+
108+ mergeSplice ( repeat . __queuedSplices , index , removed , addedCount ) ;
109+ }
110+ repeat . __array = array . slice ( 0 ) ;
111+ return ;
112+ }
113+
114+ var maybePromise = this . _runSplices ( repeat , array . slice ( 0 ) , splices ) ;
115+ if ( maybePromise instanceof Promise ) {
116+ ( function ( ) {
117+ var queuedSplices = repeat . __queuedSplices = [ ] ;
118+
119+ var runQueuedSplices = function runQueuedSplices ( ) {
120+ if ( ! queuedSplices . length ) {
121+ delete repeat . __queuedSplices ;
122+ delete repeat . __array ;
123+ return ;
124+ }
125+
126+ var nextPromise = _this2 . _runSplices ( repeat , repeat . __array , queuedSplices ) || Promise . resolve ( ) ;
127+ nextPromise . then ( runQueuedSplices ) ;
128+ } ;
129+
130+ maybePromise . then ( runQueuedSplices ) ;
131+ } ) ( ) ;
132+ }
133+ } ;
134+
135+ ArrayVirtualRepeatStrategy . prototype . _runSplices = function _runSplices ( repeat , array , splices ) {
136+ var _this3 = this ;
137+
138+ var removeDelta = 0 ;
139+ var rmPromises = [ ] ;
140+
141+ var allSplicesAreInplace = true ;
142+ for ( var i = 0 ; i < splices . length ; i ++ ) {
143+ var splice = splices [ i ] ;
144+ if ( splice . removed . length !== splice . addedCount ) {
145+ allSplicesAreInplace = false ;
146+ break ;
147+ }
148+ }
149+
150+ if ( allSplicesAreInplace ) {
151+ for ( var _i2 = 0 ; _i2 < splices . length ; _i2 ++ ) {
152+ var _splice = splices [ _i2 ] ;
153+ for ( var collectionIndex = _splice . index ; collectionIndex < _splice . index + _splice . addedCount ; collectionIndex ++ ) {
154+ if ( ! this . _isIndexBeforeViewSlot ( repeat , repeat . viewSlot , collectionIndex ) && ! this . _isIndexAfterViewSlot ( repeat , repeat . viewSlot , collectionIndex ) ) {
155+ var viewIndex = this . _getViewIndex ( repeat , repeat . viewSlot , collectionIndex ) ;
156+ var overrideContext = ( 0 , _aureliaTemplatingResources . createFullOverrideContext ) ( repeat , array [ collectionIndex ] , collectionIndex , array . length ) ;
157+ repeat . removeView ( viewIndex , true , true ) ;
158+ repeat . insertView ( viewIndex , overrideContext . bindingContext , overrideContext ) ;
159+ }
160+ }
161+ }
162+ } else {
163+ for ( var _i3 = 0 , ii = splices . length ; _i3 < ii ; ++ _i3 ) {
164+ var _splice2 = splices [ _i3 ] ;
165+ var removed = _splice2 . removed ;
166+ var removedLength = removed . length ;
167+ for ( var j = 0 , jj = removedLength ; j < jj ; ++ j ) {
168+ var viewOrPromise = this . _removeViewAt ( repeat , _splice2 . index + removeDelta + rmPromises . length , true , j , removedLength ) ;
169+ if ( viewOrPromise instanceof Promise ) {
170+ rmPromises . push ( viewOrPromise ) ;
171+ }
172+ }
173+ removeDelta -= _splice2 . addedCount ;
174+ }
175+
176+ if ( rmPromises . length > 0 ) {
177+ return Promise . all ( rmPromises ) . then ( function ( ) {
178+ _this3 . _handleAddedSplices ( repeat , array , splices ) ;
179+ ( 0 , _utilities . updateVirtualOverrideContexts ) ( repeat , 0 ) ;
180+ } ) ;
181+ }
182+ this . _handleAddedSplices ( repeat , array , splices ) ;
183+ ( 0 , _utilities . updateVirtualOverrideContexts ) ( repeat , 0 ) ;
184+ }
185+
186+ return undefined ;
187+ } ;
188+
189+ ArrayVirtualRepeatStrategy . prototype . _removeViewAt = function _removeViewAt ( repeat , collectionIndex , returnToCache , j , removedLength ) {
190+ var viewOrPromise = void 0 ;
191+ var view = void 0 ;
192+ var viewSlot = repeat . viewSlot ;
193+ var viewCount = repeat . viewCount ( ) ;
194+ var viewAddIndex = void 0 ;
195+ var removeMoreThanInDom = removedLength > viewCount ;
196+ if ( repeat . _viewsLength <= j ) {
197+ repeat . _bottomBufferHeight = repeat . _bottomBufferHeight - repeat . itemHeight ;
198+ repeat . _adjustBufferHeights ( ) ;
199+ return ;
200+ }
201+
202+ if ( ! this . _isIndexBeforeViewSlot ( repeat , viewSlot , collectionIndex ) && ! this . _isIndexAfterViewSlot ( repeat , viewSlot , collectionIndex ) ) {
203+ var viewIndex = this . _getViewIndex ( repeat , viewSlot , collectionIndex ) ;
204+ viewOrPromise = repeat . removeView ( viewIndex , returnToCache ) ;
205+ if ( repeat . items . length > viewCount ) {
206+ var collectionAddIndex = void 0 ;
207+ if ( repeat . _bottomBufferHeight > repeat . itemHeight ) {
208+ viewAddIndex = viewCount ;
209+ if ( ! removeMoreThanInDom ) {
210+ var lastViewItem = repeat . _getLastViewItem ( ) ;
211+ collectionAddIndex = repeat . items . indexOf ( lastViewItem ) + 1 ;
212+ } else {
213+ collectionAddIndex = j ;
214+ }
215+ repeat . _bottomBufferHeight = repeat . _bottomBufferHeight - repeat . itemHeight ;
216+ } else if ( repeat . _topBufferHeight > 0 ) {
217+ viewAddIndex = 0 ;
218+ collectionAddIndex = repeat . _getIndexOfFirstView ( ) - 1 ;
219+ repeat . _topBufferHeight = repeat . _topBufferHeight - repeat . itemHeight ;
220+ }
221+ var data = repeat . items [ collectionAddIndex ] ;
222+ if ( data ) {
223+ var overrideContext = ( 0 , _aureliaTemplatingResources . createFullOverrideContext ) ( repeat , data , collectionAddIndex , repeat . items . length ) ;
224+ view = repeat . viewFactory . create ( ) ;
225+ view . bind ( overrideContext . bindingContext , overrideContext ) ;
226+ }
227+ }
228+ } else if ( this . _isIndexBeforeViewSlot ( repeat , viewSlot , collectionIndex ) ) {
229+ if ( repeat . _bottomBufferHeight > 0 ) {
230+ repeat . _bottomBufferHeight = repeat . _bottomBufferHeight - repeat . itemHeight ;
231+ ( 0 , _utilities . rebindAndMoveView ) ( repeat , repeat . view ( 0 ) , repeat . view ( 0 ) . overrideContext . $index , true ) ;
232+ } else {
233+ repeat . _topBufferHeight = repeat . _topBufferHeight - repeat . itemHeight ;
234+ }
235+ } else if ( this . _isIndexAfterViewSlot ( repeat , viewSlot , collectionIndex ) ) {
236+ repeat . _bottomBufferHeight = repeat . _bottomBufferHeight - repeat . itemHeight ;
237+ }
238+
239+ if ( viewOrPromise instanceof Promise ) {
240+ viewOrPromise . then ( function ( ) {
241+ repeat . viewSlot . insert ( viewAddIndex , view ) ;
242+ repeat . _adjustBufferHeights ( ) ;
243+ } ) ;
244+ } else if ( view ) {
245+ repeat . viewSlot . insert ( viewAddIndex , view ) ;
246+ }
247+ repeat . _adjustBufferHeights ( ) ;
248+ } ;
249+
250+ ArrayVirtualRepeatStrategy . prototype . _isIndexBeforeViewSlot = function _isIndexBeforeViewSlot ( repeat , viewSlot , index ) {
251+ var viewIndex = this . _getViewIndex ( repeat , viewSlot , index ) ;
252+ return viewIndex < 0 ;
253+ } ;
254+
255+ ArrayVirtualRepeatStrategy . prototype . _isIndexAfterViewSlot = function _isIndexAfterViewSlot ( repeat , viewSlot , index ) {
256+ var viewIndex = this . _getViewIndex ( repeat , viewSlot , index ) ;
257+ return viewIndex > repeat . _viewsLength - 1 ;
258+ } ;
259+
260+ ArrayVirtualRepeatStrategy . prototype . _getViewIndex = function _getViewIndex ( repeat , viewSlot , index ) {
261+ if ( repeat . viewCount ( ) === 0 ) {
262+ return - 1 ;
263+ }
264+
265+ var topBufferItems = repeat . _topBufferHeight / repeat . itemHeight ;
266+ return index - topBufferItems ;
267+ } ;
268+
269+ ArrayVirtualRepeatStrategy . prototype . _handleAddedSplices = function _handleAddedSplices ( repeat , array , splices ) {
270+ var arrayLength = array . length ;
271+ var viewSlot = repeat . viewSlot ;
272+ for ( var i = 0 , ii = splices . length ; i < ii ; ++ i ) {
273+ var splice = splices [ i ] ;
274+ var addIndex = splice . index ;
275+ var end = splice . index + splice . addedCount ;
276+ for ( ; addIndex < end ; ++ addIndex ) {
277+ var hasDistanceToBottomViewPort = ( 0 , _utilities . getElementDistanceToBottomViewPort ) ( repeat . templateStrategy . getLastElement ( repeat . bottomBuffer ) ) > 0 ;
278+ if ( repeat . viewCount ( ) === 0 || ! this . _isIndexBeforeViewSlot ( repeat , viewSlot , addIndex ) && ! this . _isIndexAfterViewSlot ( repeat , viewSlot , addIndex ) || hasDistanceToBottomViewPort ) {
279+ var overrideContext = ( 0 , _aureliaTemplatingResources . createFullOverrideContext ) ( repeat , array [ addIndex ] , addIndex , arrayLength ) ;
280+ repeat . insertView ( addIndex , overrideContext . bindingContext , overrideContext ) ;
281+ if ( ! repeat . _hasCalculatedSizes ) {
282+ repeat . _calcInitialHeights ( 1 ) ;
283+ } else if ( repeat . viewCount ( ) > repeat . _viewsLength ) {
284+ if ( hasDistanceToBottomViewPort ) {
285+ repeat . removeView ( 0 , true , true ) ;
286+ repeat . _topBufferHeight = repeat . _topBufferHeight + repeat . itemHeight ;
287+ repeat . _adjustBufferHeights ( ) ;
288+ } else {
289+ repeat . removeView ( repeat . viewCount ( ) - 1 , true , true ) ;
290+ repeat . _bottomBufferHeight = repeat . _bottomBufferHeight + repeat . itemHeight ;
291+ }
292+ }
293+ } else if ( this . _isIndexBeforeViewSlot ( repeat , viewSlot , addIndex ) ) {
294+ repeat . _topBufferHeight = repeat . _topBufferHeight + repeat . itemHeight ;
295+ } else if ( this . _isIndexAfterViewSlot ( repeat , viewSlot , addIndex ) ) {
296+ repeat . _bottomBufferHeight = repeat . _bottomBufferHeight + repeat . itemHeight ;
297+ repeat . isLastIndex = false ;
298+ }
299+ }
300+ }
301+ repeat . _adjustBufferHeights ( ) ;
302+ } ;
303+
304+ return ArrayVirtualRepeatStrategy ;
305+ } ( _aureliaTemplatingResources . ArrayRepeatStrategy ) ;
306+ } ) ;
0 commit comments