@@ -34,20 +34,17 @@ var VirtualList = React.createClass({displayName: "VirtualList",
3434
3535 state . height = props . items . length * props . itemHeight ;
3636
37- var container = props . container ;
38-
39- var viewHeight = typeof container . innerHeight !== 'undefined' ? container . innerHeight : container . clientHeight ;
37+ var viewBox = this . viewBox ( props ) ;
4038
4139 // no space to render
42- if ( viewHeight <= 0 ) return state ;
40+ if ( viewBox . height <= 0 ) return state ;
4341
44- var list = this . getDOMNode ( ) ;
45-
46- var offsetTop = utils . topDifference ( list , container ) ;
47-
48- var viewTop = utils . viewTop ( container ) ;
42+ viewBox . top = utils . viewTop ( props . container ) ;
43+ viewBox . bottom = viewBox . top + viewBox . height ;
44+
45+ var listBox = this . listBox ( props ) ;
4946
50- var renderStats = VirtualList . getItems ( viewTop , viewHeight , offsetTop , props . itemHeight , items . length , props . itemBuffer ) ;
47+ var renderStats = VirtualList . getItems ( viewBox , listBox , props . itemHeight , items . length , props . itemBuffer ) ;
5148
5249 // no items to render
5350 if ( renderStats . itemsInView . length === 0 ) return state ;
@@ -69,13 +66,40 @@ var VirtualList = React.createClass({displayName: "VirtualList",
6966
7067 return ! equal ;
7168 } ,
69+ viewBox : function viweBox ( nextProps ) {
70+ return ( this . view = this . view || this . _getViewBox ) ;
71+ } ,
72+ _getViewBox : function _getViewBox ( nextProps ) {
73+ return {
74+ height : typeof nextProps . container . innerHeight !== 'undefined' ? nextProps . container . innerHeight : nextProps . container . clientHeight
75+ } ;
76+ } ,
77+ _getListBox : function ( nextProps ) {
78+ var list = this . getDOMNode ( ) ;
79+
80+ var top = utils . topDifference ( list , nextProps . container ) ;
81+
82+ var height = nextProps . itemHeight * nextProps . items . length ;
83+
84+ return {
85+ top : top ,
86+ height : height ,
87+ bottom : top + height
88+ } ;
89+ } ,
90+ listBox : function listBox ( nextProps ) {
91+ return ( this . list = this . list || this . _getListBox ( nextProps ) ) ;
92+ } ,
7293 componentWillReceiveProps : function ( nextProps ) {
94+ // clear caches
95+ this . view = this . list = null ;
96+
7397 var state = this . getVirtualState ( nextProps ) ;
7498
7599 this . props . container . removeEventListener ( 'scroll' , this . onScrollDebounced ) ;
76100
77101 this . onScrollDebounced = utils . debounce ( this . onScroll , nextProps . scrollDelay , false ) ;
78-
102+
79103 nextProps . container . addEventListener ( 'scroll' , this . onScrollDebounced ) ;
80104
81105 this . setState ( state ) ;
@@ -92,6 +116,8 @@ var VirtualList = React.createClass({displayName: "VirtualList",
92116 } ,
93117 componentWillUnmount : function ( ) {
94118 this . props . container . removeEventListener ( 'scroll' , this . onScrollDebounced ) ;
119+
120+ this . view = this . list = null ;
95121 } ,
96122 onScroll : function ( ) {
97123 var state = this . getVirtualState ( this . props ) ;
@@ -111,7 +137,7 @@ var VirtualList = React.createClass({displayName: "VirtualList",
111137 }
112138} ) ;
113139
114- VirtualList . getBox = function ( view , list ) {
140+ VirtualList . getBox = function getBox ( view , list ) {
115141 list . height = list . height || list . bottom - list . top ;
116142
117143 return {
@@ -120,28 +146,11 @@ VirtualList.getBox = function(view, list) {
120146 } ;
121147} ;
122148
123- VirtualList . getItems = function ( viewTop , viewHeight , listTop , itemHeight , itemCount , itemBuffer ) {
149+ VirtualList . getItems = function ( viewBox , listBox , itemBuffer , itemHeight , itemCount ) {
124150 if ( itemCount === 0 || itemHeight === 0 ) return {
125151 itemsInView : 0
126152 } ;
127153
128- var listHeight = itemHeight * itemCount ;
129-
130- var listBox = {
131- top : listTop ,
132- height : listHeight ,
133- bottom : listTop + listHeight
134- } ;
135-
136- var bufferHeight = itemBuffer * itemHeight ;
137- viewTop -= bufferHeight ;
138- viewHeight += bufferHeight * 2 ;
139-
140- var viewBox = {
141- top : viewTop ,
142- bottom : viewTop + viewHeight
143- } ;
144-
145154 // list is below viewport
146155 if ( viewBox . bottom < listBox . top ) return {
147156 itemsInView : 0
@@ -154,8 +163,9 @@ VirtualList.getItems = function(viewTop, viewHeight, listTop, itemHeight, itemCo
154163
155164 var listViewBox = VirtualList . getBox ( viewBox , listBox ) ;
156165
157- var firstItemIndex = Math . max ( 0 , Math . floor ( listViewBox . top / itemHeight ) ) ;
158- var lastItemIndex = Math . ceil ( listViewBox . bottom / itemHeight ) - 1 ;
166+ //todo add itemBuffer here instead
167+ var firstItemIndex = Math . max ( 0 , Math . floor ( listViewBox . top / itemHeight ) - itemBuffer ) ;
168+ var lastItemIndex = Math . min ( itemCount , Math . ceil ( listViewBox . bottom / itemHeight ) + itemBuffer ) - 1 ;
159169
160170 var itemsInView = lastItemIndex - firstItemIndex + 1 ;
161171
@@ -165,6 +175,8 @@ VirtualList.getItems = function(viewTop, viewHeight, listTop, itemHeight, itemCo
165175 itemsInView : itemsInView ,
166176 } ;
167177
178+ //console.log('getItems.result', result);
179+
168180 return result ;
169181} ;
170182
0 commit comments