77import Vue from ' vue'
88import _ from ' lodash'
99
10- import ItemSkeleton from ' ./ItemSkeleton.vue'
1110import { ACTIONS } from ' ../../store'
1211
1312const GRID_ITEM_HEIGHT = 200 + 10
@@ -22,7 +21,7 @@ export default Vue.extend({
2221 },
2322 data () {
2423 return {
25- viewport: { width : 0 , height : 0 } ,
24+ viewport: null ,
2625 scrollTop: 0 ,
2726 scrollHeight: 500 ,
2827 initialLoadingSkeleton: false ,
@@ -68,6 +67,12 @@ export default Vue.extend({
6867 },
6968 mounted () {
7069 this .onScroll ()
70+ this .$nextTick (() => {
71+ if (this .$el && this .$el .getBoundingClientRect ) {
72+ this .viewport = this .$el .getBoundingClientRect ()
73+ }
74+ })
75+
7176 window .addEventListener (' resize' , this .onScroll )
7277 },
7378 destroyed () {
@@ -126,41 +131,17 @@ export default Vue.extend({
126131 let lowerPaddingItems = 0
127132 const itemHeight = this .displayMode === ' 1' ? 44 : 111
128133 const padding = GRID_ITEM_HEIGHT
129- if (this .$slots .default && this .$el && this . $el . getBoundingClientRect ) {
134+ if (this .$slots .default && this .viewport ) {
130135 const childComponents = this .$slots .default .filter (child => !! child .componentOptions )
131- const viewport = this .$el .getBoundingClientRect ()
132- renderedItems = Math .floor ((viewport .height + padding + padding) / itemHeight)
136+ renderedItems = Math .floor ((this .viewport .height + padding + padding) / itemHeight)
133137 upperPaddingItems = Math .floor (Math .max (this .scrollTop - padding, 0 ) / itemHeight)
134138 children = childComponents .slice (upperPaddingItems, upperPaddingItems + renderedItems)
135139 renderedItems = children .length
136140 lowerPaddingItems = Math .max (childComponents .length - upperPaddingItems - renderedItems, 0 )
137141 this ._lowerPaddingItems = lowerPaddingItems
138142 this ._lastRendered = children
139-
140- }
141-
142- if (lowerPaddingItems === 0 ) {
143- if (upperPaddingItems + renderedItems + lowerPaddingItems === 0 ) {
144- if (! this .initialLoadingSkeleton ) {
145- // The first 350ms don't display skeletons
146- this .initialLoadingTimeout = setTimeout (() => {
147- this .initialLoadingSkeleton = true
148- this .$forceUpdate ()
149- }, 350 )
150- return h (' div' , { class: ' virtual-scroll' })
151- }
152- }
153-
154- children = [... children, ... Array (40 ).fill (0 ).map (() =>
155- h (ItemSkeleton),
156- )]
157- }
158-
159- if (upperPaddingItems + renderedItems + lowerPaddingItems > 0 ) {
160- this .initialLoadingSkeleton = false
161- if (this .initialLoadingTimeout ) {
162- clearTimeout (this .initialLoadingTimeout )
163- }
143+ } else {
144+ return h (' div' , { class: ' virtual-scroll' })
164145 }
165146
166147 const scrollTop = this .scrollTop
0 commit comments