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