virtual-scroller 1.7.9 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (283) hide show
  1. package/.gitlab-ci.yml +1 -1
  2. package/CHANGELOG.md +71 -1
  3. package/README.md +434 -151
  4. package/bundle/index-bypass.html +1 -1
  5. package/bundle/index-dom.html +1 -1
  6. package/bundle/index-grid.html +1 -2
  7. package/bundle/index-scrollableContainer.html +1 -1
  8. package/bundle/index-tbody-scrollableContainer.html +2 -0
  9. package/bundle/index-tbody.html +2 -0
  10. package/bundle/virtual-scroller-dom.js +1 -1
  11. package/bundle/virtual-scroller-dom.js.map +1 -1
  12. package/bundle/virtual-scroller-react.js +1 -1
  13. package/bundle/virtual-scroller-react.js.map +1 -1
  14. package/bundle/virtual-scroller.js +1 -1
  15. package/bundle/virtual-scroller.js.map +1 -1
  16. package/commonjs/BeforeResize.js +315 -0
  17. package/commonjs/BeforeResize.js.map +1 -0
  18. package/commonjs/DOM/Engine.js +46 -0
  19. package/commonjs/DOM/Engine.js.map +1 -0
  20. package/commonjs/DOM/ItemsContainer.js +78 -0
  21. package/commonjs/DOM/ItemsContainer.js.map +1 -0
  22. package/commonjs/DOM/{WaitForStylesToLoad.js → ListTopOffsetWatcher.js} +71 -44
  23. package/commonjs/DOM/ListTopOffsetWatcher.js.map +1 -0
  24. package/commonjs/DOM/ScrollableContainer.js +69 -101
  25. package/commonjs/DOM/ScrollableContainer.js.map +1 -1
  26. package/commonjs/DOM/VirtualScroller.js +37 -29
  27. package/commonjs/DOM/VirtualScroller.js.map +1 -1
  28. package/commonjs/DOM/tbody.js +17 -11
  29. package/commonjs/DOM/tbody.js.map +1 -1
  30. package/commonjs/ItemHeights.js +33 -34
  31. package/commonjs/ItemHeights.js.map +1 -1
  32. package/commonjs/Layout.js +591 -216
  33. package/commonjs/Layout.js.map +1 -1
  34. package/commonjs/Layout.test.js +196 -0
  35. package/commonjs/Layout.test.js.map +1 -0
  36. package/commonjs/ListHeightMeasurement.js +124 -0
  37. package/commonjs/ListHeightMeasurement.js.map +1 -0
  38. package/commonjs/Resize.js +50 -39
  39. package/commonjs/Resize.js.map +1 -1
  40. package/commonjs/Scroll.js +139 -95
  41. package/commonjs/Scroll.js.map +1 -1
  42. package/commonjs/VirtualScroller.columns.js +43 -0
  43. package/commonjs/VirtualScroller.columns.js.map +1 -0
  44. package/commonjs/VirtualScroller.constructor.js +408 -0
  45. package/commonjs/VirtualScroller.constructor.js.map +1 -0
  46. package/commonjs/VirtualScroller.items.js +305 -0
  47. package/commonjs/VirtualScroller.items.js.map +1 -0
  48. package/commonjs/VirtualScroller.js +160 -1021
  49. package/commonjs/VirtualScroller.js.map +1 -1
  50. package/commonjs/VirtualScroller.layout.js +562 -0
  51. package/commonjs/VirtualScroller.layout.js.map +1 -0
  52. package/commonjs/VirtualScroller.onRender.js +357 -0
  53. package/commonjs/VirtualScroller.onRender.js.map +1 -0
  54. package/commonjs/VirtualScroller.resize.js +186 -0
  55. package/commonjs/VirtualScroller.resize.js.map +1 -0
  56. package/commonjs/VirtualScroller.state.js +301 -0
  57. package/commonjs/VirtualScroller.state.js.map +1 -0
  58. package/commonjs/VirtualScroller.verticalSpacing.js +65 -0
  59. package/commonjs/VirtualScroller.verticalSpacing.js.map +1 -0
  60. package/commonjs/getItemCoordinates.js.map +1 -1
  61. package/commonjs/getItemsDiff.js.map +1 -1
  62. package/commonjs/getVerticalSpacing.js +8 -8
  63. package/commonjs/getVerticalSpacing.js.map +1 -1
  64. package/commonjs/package.json +5 -0
  65. package/commonjs/react/VirtualScroller.js +182 -628
  66. package/commonjs/react/VirtualScroller.js.map +1 -1
  67. package/commonjs/react/useClassName.js +26 -0
  68. package/commonjs/react/useClassName.js.map +1 -0
  69. package/commonjs/react/useHandleItemsChange.js +116 -0
  70. package/commonjs/react/useHandleItemsChange.js.map +1 -0
  71. package/commonjs/react/useInstanceMethods.js +37 -0
  72. package/commonjs/react/useInstanceMethods.js.map +1 -0
  73. package/commonjs/react/useItemKeys.js +60 -0
  74. package/commonjs/react/useItemKeys.js.map +1 -0
  75. package/commonjs/react/useOnItemHeightChange.js +32 -0
  76. package/commonjs/react/useOnItemHeightChange.js.map +1 -0
  77. package/commonjs/react/useOnItemStateChange.js +32 -0
  78. package/commonjs/react/useOnItemStateChange.js.map +1 -0
  79. package/commonjs/react/useState.js +140 -0
  80. package/commonjs/react/useState.js.map +1 -0
  81. package/commonjs/react/useStyle.js +29 -0
  82. package/commonjs/react/useStyle.js.map +1 -0
  83. package/commonjs/react/useVirtualScroller.js +62 -0
  84. package/commonjs/react/useVirtualScroller.js.map +1 -0
  85. package/commonjs/react/useVirtualScrollerStartStop.js +20 -0
  86. package/commonjs/react/useVirtualScrollerStartStop.js.map +1 -0
  87. package/commonjs/test/Engine.js +23 -0
  88. package/commonjs/test/Engine.js.map +1 -0
  89. package/commonjs/test/ItemsContainer.js +127 -0
  90. package/commonjs/test/ItemsContainer.js.map +1 -0
  91. package/commonjs/test/ScrollableContainer.js +130 -0
  92. package/commonjs/test/ScrollableContainer.js.map +1 -0
  93. package/commonjs/test/VirtualScroller.js +281 -0
  94. package/commonjs/test/VirtualScroller.js.map +1 -0
  95. package/commonjs/utility/debounce.js +28 -6
  96. package/commonjs/utility/debounce.js.map +1 -1
  97. package/commonjs/utility/debug.js +51 -12
  98. package/commonjs/utility/debug.js.map +1 -1
  99. package/commonjs/utility/getStateSnapshot.js +50 -0
  100. package/commonjs/utility/getStateSnapshot.js.map +1 -0
  101. package/commonjs/utility/px.js +1 -1
  102. package/commonjs/utility/px.js.map +1 -1
  103. package/commonjs/utility/px.test.js +14 -0
  104. package/commonjs/utility/px.test.js.map +1 -0
  105. package/commonjs/utility/shallowEqual.js +1 -1
  106. package/commonjs/utility/shallowEqual.js.map +1 -1
  107. package/commonjs/utility/throttle.js.map +1 -1
  108. package/dom/index.cjs +4 -0
  109. package/dom/index.cjs.js +9 -0
  110. package/dom/index.d.ts +25 -0
  111. package/dom/index.js +1 -1
  112. package/dom/package.json +10 -4
  113. package/index.cjs +4 -0
  114. package/index.cjs.js +9 -0
  115. package/index.d.ts +99 -0
  116. package/index.js +1 -1
  117. package/modules/BeforeResize.js +305 -0
  118. package/modules/BeforeResize.js.map +1 -0
  119. package/modules/DOM/Engine.js +27 -0
  120. package/modules/DOM/Engine.js.map +1 -0
  121. package/modules/DOM/ItemsContainer.js +71 -0
  122. package/modules/DOM/ItemsContainer.js.map +1 -0
  123. package/modules/DOM/{WaitForStylesToLoad.js → ListTopOffsetWatcher.js} +72 -44
  124. package/modules/DOM/ListTopOffsetWatcher.js.map +1 -0
  125. package/modules/DOM/ScrollableContainer.js +68 -100
  126. package/modules/DOM/ScrollableContainer.js.map +1 -1
  127. package/modules/DOM/VirtualScroller.js +32 -28
  128. package/modules/DOM/VirtualScroller.js.map +1 -1
  129. package/modules/DOM/tbody.js +11 -9
  130. package/modules/DOM/tbody.js.map +1 -1
  131. package/modules/ItemHeights.js +28 -33
  132. package/modules/ItemHeights.js.map +1 -1
  133. package/modules/Layout.js +585 -214
  134. package/modules/Layout.js.map +1 -1
  135. package/modules/Layout.test.js +190 -0
  136. package/modules/Layout.test.js.map +1 -0
  137. package/modules/ListHeightMeasurement.js +117 -0
  138. package/modules/ListHeightMeasurement.js.map +1 -0
  139. package/modules/Resize.js +50 -39
  140. package/modules/Resize.js.map +1 -1
  141. package/modules/Scroll.js +139 -94
  142. package/modules/Scroll.js.map +1 -1
  143. package/modules/VirtualScroller.columns.js +36 -0
  144. package/modules/VirtualScroller.columns.js.map +1 -0
  145. package/modules/VirtualScroller.constructor.js +371 -0
  146. package/modules/VirtualScroller.constructor.js.map +1 -0
  147. package/modules/VirtualScroller.items.js +288 -0
  148. package/modules/VirtualScroller.items.js.map +1 -0
  149. package/modules/VirtualScroller.js +159 -1014
  150. package/modules/VirtualScroller.js.map +1 -1
  151. package/modules/VirtualScroller.layout.js +549 -0
  152. package/modules/VirtualScroller.layout.js.map +1 -0
  153. package/modules/VirtualScroller.onRender.js +337 -0
  154. package/modules/VirtualScroller.onRender.js.map +1 -0
  155. package/modules/VirtualScroller.resize.js +176 -0
  156. package/modules/VirtualScroller.resize.js.map +1 -0
  157. package/modules/VirtualScroller.state.js +283 -0
  158. package/modules/VirtualScroller.state.js.map +1 -0
  159. package/modules/VirtualScroller.verticalSpacing.js +54 -0
  160. package/modules/VirtualScroller.verticalSpacing.js.map +1 -0
  161. package/modules/getItemCoordinates.js.map +1 -1
  162. package/modules/getItemsDiff.js.map +1 -1
  163. package/modules/getVerticalSpacing.js +8 -8
  164. package/modules/getVerticalSpacing.js.map +1 -1
  165. package/modules/react/VirtualScroller.js +179 -634
  166. package/modules/react/VirtualScroller.js.map +1 -1
  167. package/modules/react/useClassName.js +18 -0
  168. package/modules/react/useClassName.js.map +1 -0
  169. package/modules/react/useHandleItemsChange.js +108 -0
  170. package/modules/react/useHandleItemsChange.js.map +1 -0
  171. package/modules/react/useInstanceMethods.js +28 -0
  172. package/modules/react/useInstanceMethods.js.map +1 -0
  173. package/modules/react/useItemKeys.js +52 -0
  174. package/modules/react/useItemKeys.js.map +1 -0
  175. package/modules/react/useOnItemHeightChange.js +24 -0
  176. package/modules/react/useOnItemHeightChange.js.map +1 -0
  177. package/modules/react/useOnItemStateChange.js +24 -0
  178. package/modules/react/useOnItemStateChange.js.map +1 -0
  179. package/modules/react/useState.js +132 -0
  180. package/modules/react/useState.js.map +1 -0
  181. package/modules/react/useStyle.js +19 -0
  182. package/modules/react/useStyle.js.map +1 -0
  183. package/modules/react/useVirtualScroller.js +51 -0
  184. package/modules/react/useVirtualScroller.js.map +1 -0
  185. package/modules/react/useVirtualScrollerStartStop.js +12 -0
  186. package/modules/react/useVirtualScrollerStartStop.js.map +1 -0
  187. package/modules/test/Engine.js +11 -0
  188. package/modules/test/Engine.js.map +1 -0
  189. package/modules/test/ItemsContainer.js +120 -0
  190. package/modules/test/ItemsContainer.js.map +1 -0
  191. package/modules/test/ScrollableContainer.js +123 -0
  192. package/modules/test/ScrollableContainer.js.map +1 -0
  193. package/modules/test/VirtualScroller.js +270 -0
  194. package/modules/test/VirtualScroller.js.map +1 -0
  195. package/modules/utility/debounce.js +28 -6
  196. package/modules/utility/debounce.js.map +1 -1
  197. package/modules/utility/debug.js +47 -10
  198. package/modules/utility/debug.js.map +1 -1
  199. package/modules/utility/getStateSnapshot.js +43 -0
  200. package/modules/utility/getStateSnapshot.js.map +1 -0
  201. package/modules/utility/px.js +1 -1
  202. package/modules/utility/px.js.map +1 -1
  203. package/modules/utility/px.test.js +9 -0
  204. package/modules/utility/px.test.js.map +1 -0
  205. package/modules/utility/shallowEqual.js +1 -1
  206. package/modules/utility/shallowEqual.js.map +1 -1
  207. package/modules/utility/throttle.js.map +1 -1
  208. package/package.json +54 -29
  209. package/react/index.cjs +4 -0
  210. package/react/index.cjs.js +9 -0
  211. package/react/index.d.ts +28 -0
  212. package/react/index.js +1 -1
  213. package/react/package.json +10 -4
  214. package/rollup.config.mjs +62 -0
  215. package/runnable/create-commonjs-package-json.js +11 -0
  216. package/source/BeforeResize.js +312 -0
  217. package/source/DOM/Engine.js +30 -0
  218. package/source/DOM/ItemsContainer.js +48 -0
  219. package/source/DOM/{WaitForStylesToLoad.js → ListTopOffsetWatcher.js} +61 -30
  220. package/source/DOM/ScrollableContainer.js +51 -73
  221. package/source/DOM/VirtualScroller.js +33 -18
  222. package/source/DOM/tbody.js +30 -21
  223. package/source/ItemHeights.js +27 -27
  224. package/source/Layout.js +629 -252
  225. package/source/Layout.test.js +176 -0
  226. package/source/ListHeightMeasurement.js +95 -0
  227. package/source/Resize.js +56 -32
  228. package/source/Scroll.js +135 -82
  229. package/source/VirtualScroller.columns.js +26 -0
  230. package/source/VirtualScroller.constructor.js +336 -0
  231. package/source/VirtualScroller.items.js +302 -0
  232. package/source/VirtualScroller.js +162 -936
  233. package/source/VirtualScroller.layout.js +539 -0
  234. package/source/VirtualScroller.onRender.js +345 -0
  235. package/source/VirtualScroller.resize.js +189 -0
  236. package/source/VirtualScroller.state.js +284 -0
  237. package/source/VirtualScroller.verticalSpacing.js +51 -0
  238. package/source/getVerticalSpacing.js +7 -7
  239. package/source/react/VirtualScroller.js +243 -603
  240. package/source/react/useClassName.js +14 -0
  241. package/source/react/useHandleItemsChange.js +115 -0
  242. package/source/react/useInstanceMethods.js +25 -0
  243. package/source/react/useItemKeys.js +59 -0
  244. package/source/react/useOnItemHeightChange.js +28 -0
  245. package/source/react/useOnItemStateChange.js +28 -0
  246. package/source/react/useState.js +114 -0
  247. package/source/react/useStyle.js +20 -0
  248. package/source/react/useVirtualScroller.js +59 -0
  249. package/source/react/useVirtualScrollerStartStop.js +12 -0
  250. package/source/test/Engine.js +11 -0
  251. package/source/test/ItemsContainer.js +87 -0
  252. package/source/test/ScrollableContainer.js +88 -0
  253. package/source/test/VirtualScroller.js +232 -0
  254. package/source/utility/debounce.js +22 -5
  255. package/source/utility/debug.js +34 -3
  256. package/source/utility/getStateSnapshot.js +36 -0
  257. package/source/utility/px.js +1 -1
  258. package/source/utility/px.test.js +9 -0
  259. package/website/index-bypass.html +195 -0
  260. package/website/index-grid.html +0 -1
  261. package/website/index-scrollableContainer.html +208 -0
  262. package/website/index-tbody-scrollableContainer.html +68 -0
  263. package/website/index-tbody.html +55 -0
  264. package/commonjs/DOM/RenderingEngine.js +0 -33
  265. package/commonjs/DOM/RenderingEngine.js.map +0 -1
  266. package/commonjs/DOM/Screen.js +0 -87
  267. package/commonjs/DOM/Screen.js.map +0 -1
  268. package/commonjs/DOM/WaitForStylesToLoad.js.map +0 -1
  269. package/commonjs/RestoreScroll.js +0 -118
  270. package/commonjs/RestoreScroll.js.map +0 -1
  271. package/dom/index.commonjs.js +0 -4
  272. package/index.commonjs.js +0 -4
  273. package/modules/DOM/RenderingEngine.js +0 -19
  274. package/modules/DOM/RenderingEngine.js.map +0 -1
  275. package/modules/DOM/Screen.js +0 -80
  276. package/modules/DOM/Screen.js.map +0 -1
  277. package/modules/DOM/WaitForStylesToLoad.js.map +0 -1
  278. package/modules/RestoreScroll.js +0 -111
  279. package/modules/RestoreScroll.js.map +0 -1
  280. package/react/index.commonjs.js +0 -4
  281. package/source/DOM/RenderingEngine.js +0 -22
  282. package/source/DOM/Screen.js +0 -51
  283. package/source/RestoreScroll.js +0 -86
package/modules/Layout.js CHANGED
@@ -1,49 +1,74 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
1
7
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
8
 
3
9
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
4
10
 
5
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
11
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
6
12
 
7
- import log from './utility/debug';
13
+ import log, { warn } from './utility/debug.js';
8
14
 
9
- var Layout =
10
- /*#__PURE__*/
11
- function () {
15
+ var Layout = /*#__PURE__*/function () {
12
16
  function Layout(_ref) {
13
17
  var bypass = _ref.bypass,
14
18
  estimatedItemHeight = _ref.estimatedItemHeight,
15
19
  measureItemsBatchSize = _ref.measureItemsBatchSize,
20
+ getPrerenderMargin = _ref.getPrerenderMargin,
16
21
  getVerticalSpacing = _ref.getVerticalSpacing,
22
+ getVerticalSpacingBeforeResize = _ref.getVerticalSpacingBeforeResize,
17
23
  getColumnsCount = _ref.getColumnsCount,
24
+ getColumnsCountBeforeResize = _ref.getColumnsCountBeforeResize,
18
25
  getItemHeight = _ref.getItemHeight,
19
- getAverageItemHeight = _ref.getAverageItemHeight;
26
+ getItemHeightBeforeResize = _ref.getItemHeightBeforeResize,
27
+ getBeforeResizeItemsCount = _ref.getBeforeResizeItemsCount,
28
+ getAverageItemHeight = _ref.getAverageItemHeight,
29
+ getMaxVisibleAreaHeight = _ref.getMaxVisibleAreaHeight,
30
+ getPreviouslyCalculatedLayout = _ref.getPreviouslyCalculatedLayout;
20
31
 
21
32
  _classCallCheck(this, Layout);
22
33
 
23
34
  this.bypass = bypass;
24
35
  this.estimatedItemHeight = estimatedItemHeight;
25
36
  this.measureItemsBatchSize = measureItemsBatchSize;
37
+ this.getPrerenderMargin = getPrerenderMargin;
26
38
  this.getVerticalSpacing = getVerticalSpacing;
39
+ this.getVerticalSpacingBeforeResize = getVerticalSpacingBeforeResize;
27
40
  this.getColumnsCount = getColumnsCount;
41
+ this.getColumnsCountBeforeResize = getColumnsCountBeforeResize;
28
42
  this.getItemHeight = getItemHeight;
43
+ this.getItemHeightBeforeResize = getItemHeightBeforeResize;
44
+ this.getBeforeResizeItemsCount = getBeforeResizeItemsCount;
29
45
  this.getAverageItemHeight = getAverageItemHeight;
46
+ this.getMaxVisibleAreaHeight = getMaxVisibleAreaHeight; //
47
+ // The "previously calculated layout" feature is not currently used.
48
+ //
49
+ // The current layout snapshot could be stored as a "previously calculated layout" variable
50
+ // so that it could theoretically be used when calculating new layout incrementally
51
+ // rather than from scratch, which would be an optimization.
52
+ //
53
+
54
+ this.getPreviouslyCalculatedLayout = getPreviouslyCalculatedLayout;
30
55
  }
31
56
 
32
57
  _createClass(Layout, [{
33
58
  key: "getInitialLayoutValues",
34
59
  value: function getInitialLayoutValues(_ref2) {
35
- var bypass = _ref2.bypass,
36
- itemsCount = _ref2.itemsCount,
37
- visibleAreaHeightIncludingMargins = _ref2.visibleAreaHeightIncludingMargins;
38
- // On server side, at initialization time, there's no "visible area height",
39
- // so default to `1` estimated rows count.
40
- var estimatedRowsCount = visibleAreaHeightIncludingMargins ? this.getEstimatedRowsCountForHeight(visibleAreaHeightIncludingMargins) : 1;
60
+ var itemsCount = _ref2.itemsCount,
61
+ columnsCount = _ref2.columnsCount;
41
62
  var firstShownItemIndex;
42
63
  var lastShownItemIndex; // If there're no items then `firstShownItemIndex` stays `undefined`.
43
64
 
44
65
  if (itemsCount > 0) {
45
66
  firstShownItemIndex = 0;
46
- lastShownItemIndex = this.getLastShownItemIndex(firstShownItemIndex, itemsCount, estimatedRowsCount, bypass);
67
+ lastShownItemIndex = this.getInitialLastShownItemIndex({
68
+ itemsCount: itemsCount,
69
+ columnsCount: columnsCount,
70
+ firstShownItemIndex: firstShownItemIndex
71
+ });
47
72
  }
48
73
 
49
74
  return {
@@ -54,21 +79,35 @@ function () {
54
79
  };
55
80
  }
56
81
  }, {
57
- key: "getLastShownItemIndex",
58
- value: function getLastShownItemIndex(firstShownItemIndex, itemsCount, estimatedRowsCount, bypass) {
59
- if (this.bypass || bypass) {
82
+ key: "getInitialLastShownItemIndex",
83
+ value: function getInitialLastShownItemIndex(_ref3) {
84
+ var itemsCount = _ref3.itemsCount,
85
+ columnsCount = _ref3.columnsCount,
86
+ firstShownItemIndex = _ref3.firstShownItemIndex;
87
+
88
+ if (this.bypass) {
60
89
  return itemsCount - 1;
90
+ } // On server side, at initialization time,
91
+ // `scrollableContainer` is `undefined`,
92
+ // so default to `1` estimated rows count.
93
+
94
+
95
+ var estimatedRowsCount = 1;
96
+
97
+ if (this.getMaxVisibleAreaHeight()) {
98
+ estimatedRowsCount = this.getEstimatedRowsCountForHeight(this.getMaxVisibleAreaHeight() + this.getPrerenderMargin());
61
99
  }
62
100
 
63
- return Math.min(firstShownItemIndex + (estimatedRowsCount * this.getColumnsCount() - 1), itemsCount - 1);
101
+ return Math.min(firstShownItemIndex + (estimatedRowsCount * columnsCount - 1), itemsCount - 1);
64
102
  }
65
103
  }, {
66
104
  key: "getEstimatedRowsCountForHeight",
67
105
  value: function getEstimatedRowsCountForHeight(height) {
68
106
  var estimatedItemHeight = this.getEstimatedItemHeight();
107
+ var verticalSpacing = this.getVerticalSpacing();
69
108
 
70
109
  if (estimatedItemHeight) {
71
- return Math.ceil((height + this.getVerticalSpacing()) / (estimatedItemHeight + this.getVerticalSpacing()));
110
+ return Math.ceil((height + verticalSpacing) / (estimatedItemHeight + verticalSpacing));
72
111
  } else {
73
112
  // If no items have been rendered yet, and no `estimatedItemHeight` option
74
113
  // has been passed, then default to `1` estimated rows count in any `height`.
@@ -87,286 +126,616 @@ function () {
87
126
  return this.getAverageItemHeight() || this.estimatedItemHeight || 0;
88
127
  }
89
128
  }, {
90
- key: "updateLayoutForItemsDiff",
91
- value: function updateLayoutForItemsDiff(layout, _ref3, _ref4) {
92
- var prependedItemsCount = _ref3.prependedItemsCount,
93
- appendedItemsCount = _ref3.appendedItemsCount;
94
- var itemsCount = _ref4.itemsCount;
95
- layout.firstShownItemIndex += prependedItemsCount;
96
- layout.lastShownItemIndex += prependedItemsCount;
129
+ key: "getLayoutUpdateForItemsDiff",
130
+ value: function getLayoutUpdateForItemsDiff(_ref4, _ref5, _ref6) {
131
+ var firstShownItemIndex = _ref4.firstShownItemIndex,
132
+ lastShownItemIndex = _ref4.lastShownItemIndex,
133
+ beforeItemsHeight = _ref4.beforeItemsHeight,
134
+ afterItemsHeight = _ref4.afterItemsHeight;
135
+ var prependedItemsCount = _ref5.prependedItemsCount,
136
+ appendedItemsCount = _ref5.appendedItemsCount;
137
+ var itemsCount = _ref6.itemsCount,
138
+ columnsCount = _ref6.columnsCount,
139
+ shouldRestoreScrollPosition = _ref6.shouldRestoreScrollPosition,
140
+ onResetGridLayout = _ref6.onResetGridLayout;
141
+ // const layoutUpdate = {}
142
+ // If the layout stays the same, then simply increase
143
+ // the top and bottom margins proportionally to the amount
144
+ // of the items added.
145
+ var averageItemHeight = this.getAverageItemHeight();
146
+ var verticalSpacing = this.getVerticalSpacing();
147
+
148
+ if (appendedItemsCount > 0) {
149
+ var appendedRowsCount = Math.ceil(appendedItemsCount / columnsCount);
150
+ var addedHeightAfter = appendedRowsCount * (verticalSpacing + averageItemHeight);
151
+ afterItemsHeight += addedHeightAfter; // layoutUpdate = {
152
+ // ...layoutUpdate,
153
+ // afterItemsHeight
154
+ // }
155
+ }
156
+
157
+ if (prependedItemsCount > 0) {
158
+ var prependedRowsCount = Math.ceil(prependedItemsCount / columnsCount);
159
+ var addedHeightBefore = prependedRowsCount * (averageItemHeight + verticalSpacing);
160
+ firstShownItemIndex += prependedItemsCount;
161
+ lastShownItemIndex += prependedItemsCount;
162
+ beforeItemsHeight += addedHeightBefore; // If the currently shown items position on screen should be preserved
163
+ // when prepending new items, then it means that:
164
+ // * The current scroll position should be snapshotted.
165
+ // * The current list height should be snapshotted.
166
+ // * All prepended items should be shown so that their height could be
167
+ // measured after they're rendered. Based on the prepended items' height,
168
+ // the scroll position will be restored so that there's no "jump of content".
169
+
170
+ if (shouldRestoreScrollPosition) {
171
+ firstShownItemIndex = 0;
172
+ beforeItemsHeight = 0;
173
+ }
174
+
175
+ if (prependedItemsCount % columnsCount > 0) {
176
+ // Rows will be rebalanced as a result of prepending new items,
177
+ // and row heights can change as a result, so re-layout items
178
+ // after they've been measured (after the upcoming re-render).
179
+ //
180
+ // For example, consider a web page where item rows are `display: flex`.
181
+ // Suppose there're 3 columns and it shows items from 4 to 6.
182
+ //
183
+ // ------------------------------------------
184
+ // | Apples are | Bananas | Cranberries |
185
+ // | green | | |
186
+ // ------------------------------------------
187
+ // | Dates | Elderberry | Figs are |
188
+ // | | | tasty |
189
+ // ------------------------------------------
190
+ //
191
+ // Now, 1 item gets prepended. As a result, all existing rows will have
192
+ // a different set of items, which means that the row heights will change.
193
+ //
194
+ // ------------------------------------------
195
+ // | Zucchini | Apples are | Bananas |
196
+ // | | green | |
197
+ // ------------------------------------------
198
+ // | Cranberries | Dates | Elderberry |
199
+ // ------------------------------------------
200
+ // | Figs |
201
+ // | are tasty |
202
+ // ---------------
203
+ //
204
+ // As it can be seen above, the second row's height has changed from 2 to 1.
205
+ // Not only that, but `itemHeights` have changed as well, so if you thought
206
+ // that the library could easily recalculate row heights using `Math.max()` — 
207
+ // turns out it's not always the case.
208
+ //
209
+ // There could be an explicit opt-in option for automatically recalculating
210
+ // row heights, but I don't want to write code for such an extremely rare
211
+ // use case. Instead, use the `getColumnsCount()` parameter function when
212
+ // fetching previous items.
213
+ onResetGridLayout();
214
+ warn('~ Prepended items count', prependedItemsCount, 'is not divisible by Columns Count', columnsCount, '~');
215
+ warn('Layout reset required');
216
+ var shownItemsCountBeforeItemsUpdate = lastShownItemIndex - firstShownItemIndex + 1;
217
+ firstShownItemIndex = 0;
218
+ beforeItemsHeight = 0;
219
+
220
+ if (!shouldRestoreScrollPosition) {
221
+ // Limit shown items count if too many items have been prepended.
222
+ if (prependedItemsCount > shownItemsCountBeforeItemsUpdate) {
223
+ lastShownItemIndex = this.getInitialLastShownItemIndex({
224
+ itemsCount: itemsCount,
225
+ columnsCount: columnsCount,
226
+ firstShownItemIndex: firstShownItemIndex
227
+ }); // Approximate `afterItemsHeight` calculation.
228
+
229
+ var afterItemsCount = itemsCount - (lastShownItemIndex + 1);
230
+ afterItemsHeight = Math.ceil(afterItemsCount / columnsCount) * (verticalSpacing + averageItemHeight); // layoutUpdate = {
231
+ // ...layoutUpdate,
232
+ // afterItemsHeight
233
+ // }
234
+ }
235
+ }
236
+ } // layoutUpdate = {
237
+ // ...layoutUpdate,
238
+ // beforeItemsHeight,
239
+ // firstShownItemIndex,
240
+ // lastShownItemIndex
241
+ // }
242
+
243
+ } // return layoutUpdate
244
+ // Overwrite all four props in all scenarios.
245
+ // The reason is that only this way subsequent `setItems()` calls
246
+ // will be truly "stateless" when a chain of `setItems()` calls
247
+ // could be replaced with just the last one in a scenario when
248
+ // `updateState()` calls are "asynchronous" (delayed execution).
249
+ //
250
+ // So, for example, the user calls `setItems()` with one set of items.
251
+ // A `updateState()` call has been dispatched but the `state` hasn't been updated yet.
252
+ // Then the user calls `setItems()` with another set of items.
253
+ // If this function only returned a minimal set of properties that actually change,
254
+ // the other layout properties of the second `setItems()` call wouldn't overwrite the ones
255
+ // scheduled for update during the first `setItems()` call, resulting in an inconsistent `state`.
256
+ //
257
+ // For example, the first `setItems()` call does a `updateState()` call where it updates
258
+ // `afterItemsHeight`, and then the second `setItems()` call only updates `beforeItemsHeight`
259
+ // and `firstShownItemIndex` and `lastShownItemIndex`. If the second `setItems()` call was to
260
+ // overwrite any effects of the pending-but-not-yet-applied first `setItems()` call, it would
261
+ // have to call `updateState()` with an `afterItemsHeight` property too, even though it hasn't change.
262
+ // That would be just to revert the change to `afterItemsHeight` state property already scheduled
263
+ // by the first `setItems()` call.
264
+ //
265
+
266
+
267
+ return {
268
+ beforeItemsHeight: beforeItemsHeight,
269
+ afterItemsHeight: afterItemsHeight,
270
+ firstShownItemIndex: firstShownItemIndex,
271
+ lastShownItemIndex: lastShownItemIndex
272
+ };
273
+ } // If an item that hasn't been shown (and measured) yet is encountered
274
+ // then show such item and then retry after it has been measured.
275
+
276
+ }, {
277
+ key: "getItemNotMeasuredIndexes",
278
+ value: function getItemNotMeasuredIndexes(i, _ref7) {
279
+ var itemsCount = _ref7.itemsCount,
280
+ firstShownItemIndex = _ref7.firstShownItemIndex,
281
+ nonMeasuredAreaHeight = _ref7.nonMeasuredAreaHeight,
282
+ indexOfTheFirstItemInTheRow = _ref7.indexOfTheFirstItemInTheRow;
283
+ log('Item index', i, 'height is required for calculations but hasn\'t been measured yet. Mark the item as "shown", rerender the list, measure the item\'s height and redo the layout.');
97
284
  var columnsCount = this.getColumnsCount();
285
+ var itemsCountToRenderForMeasurement = Math.min(this.getEstimatedRowsCountForHeight(nonMeasuredAreaHeight) * columnsCount, this.measureItemsBatchSize || Infinity);
98
286
 
99
- if (prependedItemsCount % columnsCount === 0) {
100
- // If the layout stays the same, then simply increase
101
- // the top and bottom margins proportionally to the amount
102
- // of the items added.
103
- var prependedRowsCount = prependedItemsCount / columnsCount;
104
- var appendedRowsCount = Math.ceil(appendedItemsCount / columnsCount);
105
- var averageItemHeight = this.getAverageItemHeight();
106
- var verticalSpacing = this.getVerticalSpacing();
107
- layout.beforeItemsHeight += prependedRowsCount * (averageItemHeight + verticalSpacing);
108
- layout.afterItemsHeight += appendedRowsCount * (verticalSpacing + averageItemHeight);
109
- } else {
110
- // Rows will be rebalanced as a result of prepending the items,
111
- // and the row heights can change as a result, so recalculate
112
- // `beforeItemsHeight` and `afterItemsHeight` from scratch.
113
- // `this.itemHeights[]` and `firstShownItemIndex`/`lastShownItemIndex`
114
- // have already been updated at this point.
115
- layout.beforeItemsHeight = this.getBeforeItemsHeight(layout.firstShownItemIndex, layout.lastShownItemIndex);
116
- layout.afterItemsHeight = this.getAfterItemsHeight(layout.firstShownItemIndex, layout.lastShownItemIndex, itemsCount);
287
+ if (firstShownItemIndex === undefined) {
288
+ firstShownItemIndex = indexOfTheFirstItemInTheRow;
117
289
  }
290
+
291
+ var lastShownItemIndex = Math.min(indexOfTheFirstItemInTheRow + itemsCountToRenderForMeasurement - 1, // Guard against index overflow.
292
+ itemsCount - 1);
293
+ return {
294
+ firstNonMeasuredItemIndex: i,
295
+ firstShownItemIndex: firstShownItemIndex,
296
+ lastShownItemIndex: lastShownItemIndex
297
+ };
118
298
  }
299
+ /**
300
+ * Finds the indexes of the currently visible items.
301
+ * @return {object} `{ firstShownItemIndex: number, lastShownItemIndex: number, firstNonMeasuredItemIndex: number? }`
302
+ */
303
+
119
304
  }, {
120
- key: "_getVisibleItemIndexes",
121
- value: function _getVisibleItemIndexes(visibleAreaTop, visibleAreaBottom, listTopOffset, itemsCount) {
122
- var columnsCount = this.getColumnsCount();
123
- var firstShownItemIndex;
124
- var lastShownItemIndex;
125
- var previousRowsHeight = 0;
126
- var rowsCount = Math.ceil(itemsCount / columnsCount);
127
- var rowIndex = 0;
305
+ key: "getShownItemIndexes",
306
+ value: function getShownItemIndexes(_ref8) {
307
+ var itemsCount = _ref8.itemsCount,
308
+ visibleAreaTop = _ref8.visibleAreaTop,
309
+ visibleAreaBottom = _ref8.visibleAreaBottom;
310
+
311
+ var indexes = this._getShownItemIndex({
312
+ itemsCount: itemsCount,
313
+ fromIndex: 0,
314
+ visibleAreaTop: visibleAreaTop,
315
+ visibleAreaBottom: visibleAreaBottom,
316
+ findFirstShownItemIndex: true
317
+ });
318
+
319
+ if (indexes === null) {
320
+ return this.getNonVisibleListShownItemIndexes();
321
+ }
128
322
 
129
- while (rowIndex < rowsCount) {
130
- var hasMoreRows = itemsCount > (rowIndex + 1) * columnsCount;
131
- var verticalSpaceAfterCurrentRow = hasMoreRows ? this.getVerticalSpacing() : 0;
132
- var currentRowHeight = 0;
133
- var columnIndex = 0;
134
- var i = void 0;
323
+ if (indexes.firstNonMeasuredItemIndex !== undefined) {
324
+ return indexes;
325
+ }
135
326
 
136
- while (columnIndex < columnsCount && (i = rowIndex * columnsCount + columnIndex) < itemsCount) {
137
- var itemHeight = this.getItemHeight(i); // If an item that hasn't been shown (and measured) yet is encountered
138
- // then show such item and then retry after it has been measured.
327
+ var _indexes = indexes,
328
+ firstShownItemIndex = _indexes.firstShownItemIndex,
329
+ beforeItemsHeight = _indexes.beforeItemsHeight;
330
+ indexes = this._getShownItemIndex({
331
+ itemsCount: itemsCount,
332
+ fromIndex: firstShownItemIndex,
333
+ beforeItemsHeight: beforeItemsHeight,
334
+ visibleAreaTop: visibleAreaTop,
335
+ visibleAreaBottom: visibleAreaBottom,
336
+ findLastShownItemIndex: true
337
+ });
338
+
339
+ if (indexes === null) {
340
+ return this.getNonVisibleListShownItemIndexes();
341
+ }
139
342
 
140
- if (itemHeight === undefined) {
141
- log("Item index ".concat(i, " lies within the visible area or its \"margins\", but its height hasn't been measured yet. Mark the item as \"shown\", render the list, measure the item's height and redo the layout."));
343
+ if (indexes.firstNonMeasuredItemIndex !== undefined) {
344
+ return indexes;
345
+ }
142
346
 
143
- if (firstShownItemIndex === undefined) {
144
- firstShownItemIndex = rowIndex * columnsCount;
145
- }
347
+ var _indexes2 = indexes,
348
+ lastShownItemIndex = _indexes2.lastShownItemIndex;
349
+ return {
350
+ firstShownItemIndex: firstShownItemIndex,
351
+ lastShownItemIndex: lastShownItemIndex
352
+ };
353
+ }
354
+ }, {
355
+ key: "_getShownItemIndex",
356
+ value: function _getShownItemIndex(parameters) {
357
+ var beforeResize = parameters.beforeResize,
358
+ itemsCount = parameters.itemsCount,
359
+ visibleAreaTop = parameters.visibleAreaTop,
360
+ visibleAreaBottom = parameters.visibleAreaBottom,
361
+ findFirstShownItemIndex = parameters.findFirstShownItemIndex,
362
+ findLastShownItemIndex = parameters.findLastShownItemIndex;
363
+ var fromIndex = parameters.fromIndex,
364
+ beforeItemsHeight = parameters.beforeItemsHeight; // This function could potentially also use `this.getPreviouslyCalculatedLayout()`
365
+ // in order to skip calculating visible item indexes from scratch
366
+ // and instead just calculate the difference from a "previously calculated layout".
367
+ //
368
+ // I did a simple test in a web browser and found out that running the following
369
+ // piece of code is less than 10 milliseconds:
370
+ //
371
+ // var startedAt = Date.now()
372
+ // var i = 0
373
+ // while (i < 1000000) {
374
+ // i++
375
+ // }
376
+ // console.log(Date.now() - startedAt)
377
+ //
378
+ // Which becomes negligible in my project's use case (a couple thousands items max).
379
+ //
380
+ // If someone would attempt to use a "previously calculated layout" here
381
+ // then `shownItemsHeight` would also have to be returned from this function:
382
+ // the total height of all shown items including vertical spacing between them.
383
+ //
384
+ // If "previously calculated layout" would be used then it would first find
385
+ // `firstShownItemIndex` and then find `lastShownItemIndex` as part of two
386
+ // separate calls of this function, each with or without `backwards` flag,
387
+ // depending on whether `visibleAreaTop` and `visibleAreBottom` have shifted up or down.
388
+
389
+ var firstShownItemIndex;
390
+ var lastShownItemIndex; // It's not always required to pass `beforeItemsHeight` parameter:
391
+ // when `fromIndex` is `0`, it's also assumed to be `0`.
392
+
393
+ if (fromIndex === 0) {
394
+ beforeItemsHeight = 0;
395
+ }
396
+
397
+ if (beforeItemsHeight === undefined) {
398
+ throw new Error('[virtual-scroller] `beforeItemsHeight` not passed to `Layout.getShownItemIndexes()` when starting from index ' + fromIndex);
399
+ } // const backwards = false
400
+ // while (backwards ? i >= 0 : i < itemsCount) {}
401
+
402
+
403
+ if (!beforeResize) {
404
+ var beforeResizeItemsCount = this.getBeforeResizeItemsCount();
405
+
406
+ if (beforeResizeItemsCount > fromIndex) {
407
+ // First search for the item in "before resize" items.
408
+ var _this$_getShownItemIn = this._getShownItemIndex(_objectSpread(_objectSpread({}, parameters), {}, {
409
+ beforeResize: true,
410
+ itemsCount: beforeResizeItemsCount
411
+ })),
412
+ notFound = _this$_getShownItemIn.notFound,
413
+ beforeResizeItemsHeight = _this$_getShownItemIn.beforeItemsHeight,
414
+ _firstShownItemIndex = _this$_getShownItemIn.firstShownItemIndex,
415
+ _lastShownItemIndex = _this$_getShownItemIn.lastShownItemIndex; // If the item was not found in "before resize" items
416
+ // then search in regular items skipping "before resize" ones.
417
+
418
+
419
+ if (notFound) {
420
+ beforeItemsHeight = beforeResizeItemsHeight;
421
+ fromIndex += beforeResizeItemsCount;
422
+ } else {
423
+ // If the item was found in "before resize" items
424
+ // then return the result.
425
+ // Rebalance first / last shown item indexes based on
426
+ // the current columns count, if required.
427
+ var _columnsCount = this.getColumnsCount();
146
428
 
147
- var heightLeft = visibleAreaBottom - (listTopOffset + previousRowsHeight);
148
- lastShownItemIndex = Math.min((rowIndex + this.getEstimatedRowsCountForHeight(heightLeft)) * columnsCount - 1, // Guard against index overflow.
149
- itemsCount - 1);
150
429
  return {
151
- firstNonMeasuredItemIndex: i,
152
- firstShownItemIndex: firstShownItemIndex,
153
- lastShownItemIndex: lastShownItemIndex
430
+ firstShownItemIndex: _firstShownItemIndex === undefined ? undefined : Math.floor(_firstShownItemIndex / _columnsCount) * _columnsCount,
431
+ lastShownItemIndex: _lastShownItemIndex === undefined ? undefined : Math.floor(_lastShownItemIndex / _columnsCount) * _columnsCount,
432
+ beforeItemsHeight: beforeResizeItemsHeight
154
433
  };
155
434
  }
435
+ }
436
+ }
156
437
 
157
- currentRowHeight = Math.max(currentRowHeight, itemHeight); // If this is the first item visible
158
- // then start showing items from this row.
159
-
160
- if (firstShownItemIndex === undefined) {
161
- if (listTopOffset + previousRowsHeight + currentRowHeight > visibleAreaTop) {
162
- log('First shown row index', rowIndex);
163
- firstShownItemIndex = rowIndex * columnsCount;
164
- }
165
- } // If this item is the last one visible in the viewport then exit.
438
+ var columnsCount = beforeResize ? this.getColumnsCountBeforeResize() : this.getColumnsCount();
439
+ var verticalSpacing = beforeResize ? this.getVerticalSpacingBeforeResize() : this.getVerticalSpacing();
440
+ var i = fromIndex;
166
441
 
442
+ while (i < itemsCount) {
443
+ var currentRowFirstItemIndex = i;
444
+ var hasMoreRows = itemsCount > currentRowFirstItemIndex + columnsCount;
445
+ var verticalSpacingAfterCurrentRow = hasMoreRows ? verticalSpacing : 0;
446
+ var currentRowHeight = 0; // Calculate current row height.
167
447
 
168
- if (listTopOffset + previousRowsHeight + currentRowHeight + verticalSpaceAfterCurrentRow > visibleAreaBottom) {
169
- log('Last shown row index', rowIndex); // The list height is estimated until all items have been seen,
170
- // so it's possible that even when the list DOM element happens
171
- // to be in the viewport in reality the list isn't visible
172
- // in which case `firstShownItemIndex` will be `undefined`.
448
+ var columnIndex = 0;
173
449
 
174
- if (firstShownItemIndex !== undefined) {
175
- lastShownItemIndex = Math.min( // The index of the last item in the current row.
176
- (rowIndex + 1) * columnsCount - 1, // Guards against index overflow.
177
- itemsCount - 1);
178
- }
450
+ while (columnIndex < columnsCount && i < itemsCount) {
451
+ var itemHeight = beforeResize ? this.getItemHeightBeforeResize(i) : this.getItemHeight(i); // If this item hasn't been measured yet (or re-measured after a resize)
452
+ // then mark it as the first non-measured one.
453
+ //
454
+ // Can't happen by definition when `beforeResize` parameter is `true`.
455
+ //
179
456
 
180
- return {
181
- firstShownItemIndex: firstShownItemIndex,
182
- lastShownItemIndex: lastShownItemIndex
183
- };
457
+ if (itemHeight === undefined) {
458
+ return this.getItemNotMeasuredIndexes(i, {
459
+ itemsCount: itemsCount,
460
+ firstShownItemIndex: findLastShownItemIndex ? fromIndex : undefined,
461
+ indexOfTheFirstItemInTheRow: currentRowFirstItemIndex,
462
+ nonMeasuredAreaHeight: visibleAreaBottom + this.getPrerenderMargin() - beforeItemsHeight
463
+ });
184
464
  }
185
465
 
466
+ currentRowHeight = Math.max(currentRowHeight, itemHeight);
186
467
  columnIndex++;
468
+ i++;
187
469
  }
188
470
 
189
- previousRowsHeight += currentRowHeight; // If there're more rows below the current row, then add vertical spacing.
190
-
191
- previousRowsHeight += verticalSpaceAfterCurrentRow;
192
- rowIndex++;
193
- } // If there're no more items then the last item is the last one to show.
194
-
471
+ var itemsHeightFromFirstRowToThisRow = beforeItemsHeight + currentRowHeight;
472
+ var rowStepsIntoVisibleAreaTop = itemsHeightFromFirstRowToThisRow > visibleAreaTop - this.getPrerenderMargin();
473
+ var rowStepsOutOfVisibleAreaBottomOrIsAtTheBorder = itemsHeightFromFirstRowToThisRow + verticalSpacingAfterCurrentRow >= visibleAreaBottom + this.getPrerenderMargin(); // if (backwards) {
474
+ // if (findFirstShownItemIndex) {
475
+ // if (rowStepsOutOfVisibleAreaTop) {
476
+ // return {
477
+ // firstShownItemIndex: currentRowFirstItemIndex + columnsCount
478
+ // }
479
+ // }
480
+ // } else if (findLastShownItemIndex) {
481
+ // if (rowStepsIntoVisibleAreaBottom) {
482
+ // return {
483
+ // lastShownItemIndex: currentRowFirstItemIndex + columnsCount - 1
484
+ // }
485
+ // }
486
+ // }
487
+ // }
488
+
489
+ if (findFirstShownItemIndex) {
490
+ if (rowStepsIntoVisibleAreaTop) {
491
+ // If item is the first one visible in the viewport
492
+ // then start showing items from this row.
493
+ return {
494
+ firstShownItemIndex: currentRowFirstItemIndex,
495
+ beforeItemsHeight: beforeItemsHeight
496
+ };
497
+ }
498
+ } else if (findLastShownItemIndex) {
499
+ if (rowStepsOutOfVisibleAreaBottomOrIsAtTheBorder) {
500
+ return {
501
+ lastShownItemIndex: Math.min( // The index of the last item in the current row.
502
+ currentRowFirstItemIndex + columnsCount - 1, // Guards against index overflow.
503
+ itemsCount - 1)
504
+ };
505
+ }
506
+ }
195
507
 
196
- if (firstShownItemIndex !== undefined && lastShownItemIndex === undefined) {
197
- lastShownItemIndex = itemsCount - 1;
198
- log('Last item index (is fully visible)', lastShownItemIndex);
199
- }
508
+ beforeItemsHeight += currentRowHeight + verticalSpacingAfterCurrentRow; // if (backwards) {
509
+ // // Set `i` to be the first item of the current row.
510
+ // i -= columnsCount
511
+ // const prevoiusRowIsBeforeResize = i - 1 < this.getBeforeResizeItemsCount()
512
+ // const previousRowColumnsCount = prevoiusRowIsBeforeResize ? this.getColumnsCountBeforeResize() : this.getColumnsCount()
513
+ // // Set `i` to be the first item of the previous row.
514
+ // i -= previousRowColumnsCount
515
+ // }
516
+ } // if (backwards) {
517
+ // if (findFirstShownItemIndex) {
518
+ // warn('The list is supposed to be visible but no visible item has been found (while traversing backwards)')
519
+ // return null
520
+ // } else if (findLastShownItemIndex) {
521
+ // return {
522
+ // firstShownItemIndex: 0
523
+ // }
524
+ // }
525
+ // }
526
+
527
+
528
+ if (beforeResize) {
529
+ return {
530
+ notFound: true,
531
+ beforeItemsHeight: beforeItemsHeight
532
+ };
533
+ } // This case isn't supposed to happen but it could hypothetically happen
534
+ // because the list height is measured from the user's screen and
535
+ // not necessarily can be trusted.
200
536
 
201
- return {
202
- firstShownItemIndex: firstShownItemIndex,
203
- lastShownItemIndex: lastShownItemIndex
204
- };
205
- } // Finds the items which are displayed in the viewport.
206
537
 
207
- }, {
208
- key: "getVisibleItemIndexes",
209
- value: function getVisibleItemIndexes(visibleAreaTop, visibleAreaBottom, listTopOffset, itemsCount) {
210
- var _this$_getVisibleItem = this._getVisibleItemIndexes(visibleAreaTop, visibleAreaBottom, listTopOffset, itemsCount),
211
- firstNonMeasuredItemIndex = _this$_getVisibleItem.firstNonMeasuredItemIndex,
212
- firstShownItemIndex = _this$_getVisibleItem.firstShownItemIndex,
213
- lastShownItemIndex = _this$_getVisibleItem.lastShownItemIndex;
214
-
215
- var redoLayoutAfterMeasuringItemHeights = firstNonMeasuredItemIndex !== undefined; // If some items will be rendered in order to measure their height,
216
- // and it's not a `preserveScrollPositionOnPrependItems` case,
217
- // then limit the amount of such items being measured in a single pass.
218
-
219
- if (redoLayoutAfterMeasuringItemHeights && this.measureItemsBatchSize) {
220
- var maxAllowedLastShownItemIndex = firstNonMeasuredItemIndex + this.measureItemsBatchSize - 1;
221
- var columnsCount = this.getColumnsCount();
222
- lastShownItemIndex = Math.min( // Also guards against index overflow.
223
- lastShownItemIndex, // The index of the last item in the row.
224
- Math.ceil(maxAllowedLastShownItemIndex / columnsCount) * columnsCount - 1);
538
+ if (findFirstShownItemIndex) {
539
+ warn('The list is supposed to be visible but no visible item has been found');
540
+ return null;
541
+ } else if (findLastShownItemIndex) {
542
+ return {
543
+ lastShownItemIndex: itemsCount - 1
544
+ };
225
545
  }
226
-
227
- return {
228
- firstShownItemIndex: firstShownItemIndex,
229
- lastShownItemIndex: lastShownItemIndex,
230
- redoLayoutAfterMeasuringItemHeights: redoLayoutAfterMeasuringItemHeights
231
- };
232
546
  }
233
547
  }, {
234
548
  key: "getNonVisibleListShownItemIndexes",
235
549
  value: function getNonVisibleListShownItemIndexes() {
236
- return {
550
+ var layout = {
237
551
  firstShownItemIndex: 0,
238
- lastShownItemIndex: 0,
239
- redoLayoutAfterMeasuringItemHeights: this.getItemHeight(0) === undefined
552
+ lastShownItemIndex: 0
240
553
  };
241
- }
242
- }, {
243
- key: "getItemIndexes",
244
- value: function getItemIndexes(visibleAreaTop, visibleAreaBottom, listTopOffset, listHeight, itemsCount) {
245
- var isVisible = listTopOffset + listHeight > visibleAreaTop && listTopOffset < visibleAreaBottom;
246
-
247
- if (!isVisible) {
248
- log('The entire list is off-screen. No items are visible.');
249
- return;
250
- } // Find the items which are displayed in the viewport.
251
-
252
-
253
- var indexes = this.getVisibleItemIndexes(visibleAreaTop, visibleAreaBottom, listTopOffset, itemsCount); // The list height is estimated until all items have been seen,
254
- // so it's possible that even when the list DOM element happens
255
- // to be in the viewport, in reality the list isn't visible
256
- // in which case `firstShownItemIndex` will be `undefined`.
257
554
 
258
- if (indexes.firstShownItemIndex === undefined) {
259
- log('The entire list is off-screen. No items are visible.');
260
- return;
555
+ if (this.getItemHeight(0) === undefined) {
556
+ layout.firstNonMeasuredItemIndex = 0;
261
557
  }
262
558
 
263
- return indexes;
559
+ return layout;
264
560
  }
265
561
  /**
266
562
  * Measures "before" items height.
267
- * @param {number} firstShownItemIndexNew first shown item index.
268
- * @param {number} lastShownItemIndex — New last shown item index.
563
+ * @param {number} beforeItemsCountBasically, first shown item index.
269
564
  * @return {number}
270
565
  */
271
566
 
272
567
  }, {
273
568
  key: "getBeforeItemsHeight",
274
- value: function getBeforeItemsHeight(firstShownItemIndex, lastShownItemIndex) {
275
- var columnsCount = this.getColumnsCount();
276
- var firstShownRowIndex = Math.floor(firstShownItemIndex / columnsCount);
277
- var beforeItemsHeight = 0; // Add all "before" items height.
569
+ value: function getBeforeItemsHeight(beforeItemsCount) {
570
+ var _ref9 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
571
+ beforeResize = _ref9.beforeResize;
572
+
573
+ // This function could potentially also use `this.getPreviouslyCalculatedLayout()`
574
+ // in order to skip calculating visible item indexes from scratch
575
+ // and instead just calculate the difference from a "previously calculated layout".
576
+ //
577
+ // I did a simple test in a web browser and found out that running the following
578
+ // piece of code is less than 10 milliseconds:
579
+ //
580
+ // var startedAt = Date.now()
581
+ // var i = 0
582
+ // while (i < 1000000) {
583
+ // i++
584
+ // }
585
+ // console.log(Date.now() - startedAt)
586
+ //
587
+ // Which becomes negligible in my project's use case (a couple thousands items max).
588
+ var beforeItemsHeight = 0;
589
+ var i = 0;
590
+
591
+ if (!beforeResize) {
592
+ var beforeResizeItemsCount = this.getBeforeResizeItemsCount();
593
+
594
+ if (beforeResizeItemsCount > 0) {
595
+ // First add all "before resize" item heights.
596
+ beforeItemsHeight = this.getBeforeItemsHeight( // `firstShownItemIndex` (called `beforeItemsCount`) could be greater than
597
+ // `beforeResizeItemsCount` when the user scrolls down.
598
+ // `firstShownItemIndex` (called `beforeItemsCount`) could be less than
599
+ // `beforeResizeItemsCount` when the user scrolls up.
600
+ Math.min(beforeItemsCount, beforeResizeItemsCount), {
601
+ beforeResize: true
602
+ });
603
+ i = beforeResizeItemsCount;
604
+ }
605
+ }
278
606
 
279
- var rowIndex = 0;
607
+ var columnsCount = beforeResize ? this.getColumnsCountBeforeResize() : this.getColumnsCount();
608
+ var verticalSpacing = beforeResize ? this.getVerticalSpacingBeforeResize() : this.getVerticalSpacing();
280
609
 
281
- while (rowIndex < firstShownRowIndex) {
610
+ while (i < beforeItemsCount) {
611
+ var currentRowFirstItemIndex = i;
282
612
  var rowHeight = 0;
283
- var columnIndex = 0;
613
+ var columnIndex = 0; // Not checking for `itemsCount` overflow here because `i = beforeItemsCount`
614
+ // can only start at the start of a row, meaning that when calculating
615
+ // "before items height" it's not supposed to add item heights from the
616
+ // last row of items because in that case it would have to iterate from
617
+ // `i === beforeItemsCount` and that condition is already checked above.
618
+ // while (i < itemsCount) {
284
619
 
285
620
  while (columnIndex < columnsCount) {
286
- rowHeight = Math.max(rowHeight, this.getItemHeight(rowIndex * columnsCount + columnIndex) || this.getAverageItemHeight());
621
+ var itemHeight = beforeResize ? this.getItemHeightBeforeResize(i) : this.getItemHeight(i);
622
+
623
+ if (itemHeight === undefined) {
624
+ // `itemHeight` can only be `undefined` when not `beforeResize`.
625
+ // Use the current "average item height" as a substitute.
626
+ itemHeight = this.getAverageItemHeight();
627
+ }
628
+
629
+ rowHeight = Math.max(rowHeight, itemHeight);
630
+ i++;
287
631
  columnIndex++;
288
632
  }
289
633
 
290
634
  beforeItemsHeight += rowHeight;
291
- beforeItemsHeight += this.getVerticalSpacing();
292
- rowIndex++;
635
+ beforeItemsHeight += verticalSpacing;
293
636
  }
294
637
 
295
638
  return beforeItemsHeight;
296
639
  }
297
640
  /**
298
641
  * Measures "after" items height.
299
- * @param {number} firstShownItemIndexNew first shown item index.
300
- * @param {number} lastShownItemIndex — New last shown item index.
301
- * @param {number} averageItemHeight — Average item height.
302
- * @param {number} verticalSpacing — Item vertical spacing.
642
+ * @param {number} lastShownItemIndexLast shown item index.
303
643
  * @param {number} itemsCount — Items count.
304
644
  * @return {number}
305
645
  */
306
646
 
307
647
  }, {
308
648
  key: "getAfterItemsHeight",
309
- value: function getAfterItemsHeight(firstShownItemIndex, lastShownItemIndex, itemsCount) {
649
+ value: function getAfterItemsHeight(lastShownItemIndex, itemsCount) {
650
+ // This function could potentially also use `this.getPreviouslyCalculatedLayout()`
651
+ // in order to skip calculating visible item indexes from scratch
652
+ // and instead just calculate the difference from a "previously calculated layout".
653
+ //
654
+ // I did a simple test in a web browser and found out that running the following
655
+ // piece of code is less than 10 milliseconds:
656
+ //
657
+ // var startedAt = Date.now()
658
+ // var i = 0
659
+ // while (i < 1000000) {
660
+ // i++
661
+ // }
662
+ // console.log(Date.now() - startedAt)
663
+ //
664
+ // Which becomes negligible in my project's use case (a couple thousands items max).
310
665
  var columnsCount = this.getColumnsCount();
311
- var rowsCount = Math.ceil(itemsCount / columnsCount);
312
666
  var lastShownRowIndex = Math.floor(lastShownItemIndex / columnsCount);
313
667
  var afterItemsHeight = 0;
314
- var rowIndex = lastShownRowIndex + 1;
668
+ var i = lastShownItemIndex + 1;
315
669
 
316
- while (rowIndex < rowsCount) {
670
+ while (i < itemsCount) {
317
671
  var rowHeight = 0;
318
672
  var columnIndex = 0;
319
- var i = void 0;
320
673
 
321
- while (columnIndex < columnsCount && (i = rowIndex * columnsCount + columnIndex) < itemsCount) {
322
- rowHeight = Math.max(rowHeight, this.getItemHeight(i) || this.getAverageItemHeight());
674
+ while (columnIndex < columnsCount && i < itemsCount) {
675
+ var itemHeight = this.getItemHeight(i);
676
+
677
+ if (itemHeight === undefined) {
678
+ itemHeight = this.getAverageItemHeight();
679
+ }
680
+
681
+ rowHeight = Math.max(rowHeight, itemHeight);
682
+ i++;
323
683
  columnIndex++;
324
684
  } // Add all "after" items height.
325
685
 
326
686
 
327
687
  afterItemsHeight += this.getVerticalSpacing();
328
688
  afterItemsHeight += rowHeight;
329
- rowIndex++;
330
689
  }
331
690
 
332
691
  return afterItemsHeight;
333
692
  }
334
693
  /**
335
- * Finds the indexes of the currently visible items.
336
- * @return {object} `{ firstShownItemIndex: number, lastShownItemIndex: number, redoLayoutAfterMeasuringItemHeights: boolean }`
694
+ * Returns the items's top offset relative to the top edge of the first item.
695
+ * @param {number} i Item index
696
+ * @return {[number]} Returns `undefined` if any of the previous items haven't been rendered yet.
337
697
  */
338
698
 
339
699
  }, {
340
- key: "getShownItemIndexes",
341
- value: function getShownItemIndexes(_ref5) {
342
- var listHeight = _ref5.listHeight,
343
- itemsCount = _ref5.itemsCount,
344
- visibleAreaIncludingMargins = _ref5.visibleAreaIncludingMargins,
345
- listTopOffsetInsideScrollableContainer = _ref5.listTopOffsetInsideScrollableContainer;
700
+ key: "getItemTopOffset",
701
+ value: function getItemTopOffset(i) {
702
+ var topOffsetInsideScrollableContainer = 0;
703
+ var beforeResizeItemsCount = this.getBeforeResizeItemsCount();
704
+ var beforeResizeRowsCount = beforeResizeItemsCount === 0 ? 0 : Math.ceil(beforeResizeItemsCount / this.getColumnsCountBeforeResize());
705
+ var maxBeforeResizeRowsCount = i < beforeResizeItemsCount ? Math.floor(i / this.getColumnsCountBeforeResize()) : beforeResizeRowsCount;
706
+ var beforeResizeRowIndex = 0;
707
+
708
+ while (beforeResizeRowIndex < maxBeforeResizeRowsCount) {
709
+ var rowHeight = this.getItemHeightBeforeResize(beforeResizeRowIndex * this.getColumnsCountBeforeResize());
710
+ topOffsetInsideScrollableContainer += rowHeight;
711
+ topOffsetInsideScrollableContainer += this.getVerticalSpacingBeforeResize();
712
+ beforeResizeRowIndex++;
713
+ }
346
714
 
347
- if (this.bypass) {
348
- return {
349
- firstShownItemIndex: 0,
350
- lastShownItemIndex: itemsCount - 1
351
- };
352
- } // Finds the indexes of the items that are currently visible
353
- // (or close to being visible) in the scrollable container.
354
- // For scrollable containers other than the main screen, it could also
355
- // check the visibility of such scrollable container itself, because it
356
- // might be not visible.
357
- // If such kind of an optimization would hypothetically be implemented,
358
- // then it would also require listening for "scroll" events on the screen.
359
- // Overall, I suppose that such "actual visibility" feature would be
360
- // a very minor optimization and not something I'd deal with.
361
-
362
-
363
- return this.getItemIndexes(visibleAreaIncludingMargins.top, visibleAreaIncludingMargins.bottom, listTopOffsetInsideScrollableContainer, listHeight, itemsCount) || this.getNonVisibleListShownItemIndexes();
364
- }
365
- }, {
366
- key: "showItemsFromTheStart",
367
- value: function showItemsFromTheStart(layout) {
368
- layout.firstShownItemIndex = 0;
369
- layout.beforeItemsHeight = 0;
715
+ var itemRowIndex = Math.floor((i - beforeResizeItemsCount) / this.getColumnsCount());
716
+ var rowIndex = 0;
717
+
718
+ while (rowIndex < itemRowIndex) {
719
+ var _rowHeight = 0;
720
+ var columnIndex = 0;
721
+
722
+ while (columnIndex < this.getColumnsCount()) {
723
+ var itemHeight = this.getItemHeight(beforeResizeItemsCount + rowIndex * this.getColumnsCount() + columnIndex);
724
+
725
+ if (itemHeight === undefined) {
726
+ return;
727
+ }
728
+
729
+ _rowHeight = Math.max(_rowHeight, itemHeight);
730
+ columnIndex++;
731
+ }
732
+
733
+ topOffsetInsideScrollableContainer += _rowHeight;
734
+ topOffsetInsideScrollableContainer += this.getVerticalSpacing();
735
+ rowIndex++;
736
+ }
737
+
738
+ return topOffsetInsideScrollableContainer;
370
739
  }
371
740
  }]);
372
741
 
@@ -378,9 +747,11 @@ export var LAYOUT_REASON = {
378
747
  SCROLL: 'scroll',
379
748
  STOPPED_SCROLLING: 'stopped scrolling',
380
749
  MANUAL: 'manual',
381
- MOUNT: 'mount',
382
- ITEM_HEIGHT_NOT_MEASURED: 'some item height wasn\'t measured',
383
- RESIZE: 'resize',
750
+ STARTED: 'started',
751
+ NON_MEASURED_ITEMS_HAVE_BEEN_MEASURED: 'non-measured item heights have been measured',
752
+ VIEWPORT_WIDTH_CHANGED: 'viewport width changed',
753
+ VIEWPORT_HEIGHT_CHANGED: 'viewport height changed',
754
+ VIEWPORT_SIZE_UNCHANGED: 'viewport size unchanged',
384
755
  ITEM_HEIGHT_CHANGED: 'item height changed',
385
756
  ITEMS_CHANGED: 'items changed',
386
757
  TOP_OFFSET_CHANGED: 'list top offset changed'