4740cd698569dcbdf6352ddda43056183c725d7066eebeb6d0bd0d061e30dae1246d9dabc2ebf619828e1e6c2cc63409326c2d9b24ec56a019ffb1d2072904 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var buildGrid = require('../builders/build-grid.js');
  4. var defaults = require('../defaults.js');
  5. var types = require('../../../../utils/types.js');
  6. const { max, min, floor } = Math;
  7. const ACCESS_SIZER_KEY_MAP = {
  8. column: "columnWidth",
  9. row: "rowHeight"
  10. };
  11. const ACCESS_LAST_VISITED_KEY_MAP = {
  12. column: "lastVisitedColumnIndex",
  13. row: "lastVisitedRowIndex"
  14. };
  15. const getItemFromCache = (props, index, gridCache, type) => {
  16. const [cachedItems, sizer, lastVisited] = [
  17. gridCache[type],
  18. props[ACCESS_SIZER_KEY_MAP[type]],
  19. gridCache[ACCESS_LAST_VISITED_KEY_MAP[type]]
  20. ];
  21. if (index > lastVisited) {
  22. let offset = 0;
  23. if (lastVisited >= 0) {
  24. const item = cachedItems[lastVisited];
  25. offset = item.offset + item.size;
  26. }
  27. for (let i = lastVisited + 1; i <= index; i++) {
  28. const size = sizer(i);
  29. cachedItems[i] = {
  30. offset,
  31. size
  32. };
  33. offset += size;
  34. }
  35. gridCache[ACCESS_LAST_VISITED_KEY_MAP[type]] = index;
  36. }
  37. return cachedItems[index];
  38. };
  39. const bs = (props, gridCache, low, high, offset, type) => {
  40. while (low <= high) {
  41. const mid = low + floor((high - low) / 2);
  42. const currentOffset = getItemFromCache(props, mid, gridCache, type).offset;
  43. if (currentOffset === offset) {
  44. return mid;
  45. } else if (currentOffset < offset) {
  46. low = mid + 1;
  47. } else {
  48. high = mid - 1;
  49. }
  50. }
  51. return max(0, low - 1);
  52. };
  53. const es = (props, gridCache, idx, offset, type) => {
  54. const total = type === "column" ? props.totalColumn : props.totalRow;
  55. let exponent = 1;
  56. while (idx < total && getItemFromCache(props, idx, gridCache, type).offset < offset) {
  57. idx += exponent;
  58. exponent *= 2;
  59. }
  60. return bs(props, gridCache, floor(idx / 2), min(idx, total - 1), offset, type);
  61. };
  62. const findItem = (props, gridCache, offset, type) => {
  63. const [cache, lastVisitedIndex] = [
  64. gridCache[type],
  65. gridCache[ACCESS_LAST_VISITED_KEY_MAP[type]]
  66. ];
  67. const lastVisitedItemOffset = lastVisitedIndex > 0 ? cache[lastVisitedIndex].offset : 0;
  68. if (lastVisitedItemOffset >= offset) {
  69. return bs(props, gridCache, 0, lastVisitedIndex, offset, type);
  70. }
  71. return es(props, gridCache, max(0, lastVisitedIndex), offset, type);
  72. };
  73. const getEstimatedTotalHeight = ({ totalRow }, { estimatedRowHeight, lastVisitedRowIndex, row }) => {
  74. let sizeOfVisitedRows = 0;
  75. if (lastVisitedRowIndex >= totalRow) {
  76. lastVisitedRowIndex = totalRow - 1;
  77. }
  78. if (lastVisitedRowIndex >= 0) {
  79. const item = row[lastVisitedRowIndex];
  80. sizeOfVisitedRows = item.offset + item.size;
  81. }
  82. const unvisitedItems = totalRow - lastVisitedRowIndex - 1;
  83. const sizeOfUnvisitedItems = unvisitedItems * estimatedRowHeight;
  84. return sizeOfVisitedRows + sizeOfUnvisitedItems;
  85. };
  86. const getEstimatedTotalWidth = ({ totalColumn }, { column, estimatedColumnWidth, lastVisitedColumnIndex }) => {
  87. let sizeOfVisitedColumns = 0;
  88. if (lastVisitedColumnIndex > totalColumn) {
  89. lastVisitedColumnIndex = totalColumn - 1;
  90. }
  91. if (lastVisitedColumnIndex >= 0) {
  92. const item = column[lastVisitedColumnIndex];
  93. sizeOfVisitedColumns = item.offset + item.size;
  94. }
  95. const unvisitedItems = totalColumn - lastVisitedColumnIndex - 1;
  96. const sizeOfUnvisitedItems = unvisitedItems * estimatedColumnWidth;
  97. return sizeOfVisitedColumns + sizeOfUnvisitedItems;
  98. };
  99. const ACCESS_ESTIMATED_SIZE_KEY_MAP = {
  100. column: getEstimatedTotalWidth,
  101. row: getEstimatedTotalHeight
  102. };
  103. const getOffset = (props, index, alignment, scrollOffset, cache, type, scrollBarWidth) => {
  104. const [size, estimatedSizeAssociates] = [
  105. type === "row" ? props.height : props.width,
  106. ACCESS_ESTIMATED_SIZE_KEY_MAP[type]
  107. ];
  108. const item = getItemFromCache(props, index, cache, type);
  109. const estimatedSize = estimatedSizeAssociates(props, cache);
  110. const maxOffset = max(0, min(estimatedSize - size, item.offset));
  111. const minOffset = max(0, item.offset - size + scrollBarWidth + item.size);
  112. if (alignment === defaults.SMART_ALIGNMENT) {
  113. if (scrollOffset >= minOffset - size && scrollOffset <= maxOffset + size) {
  114. alignment = defaults.AUTO_ALIGNMENT;
  115. } else {
  116. alignment = defaults.CENTERED_ALIGNMENT;
  117. }
  118. }
  119. switch (alignment) {
  120. case defaults.START_ALIGNMENT: {
  121. return maxOffset;
  122. }
  123. case defaults.END_ALIGNMENT: {
  124. return minOffset;
  125. }
  126. case defaults.CENTERED_ALIGNMENT: {
  127. return Math.round(minOffset + (maxOffset - minOffset) / 2);
  128. }
  129. case defaults.AUTO_ALIGNMENT:
  130. default: {
  131. if (scrollOffset >= minOffset && scrollOffset <= maxOffset) {
  132. return scrollOffset;
  133. } else if (minOffset > maxOffset) {
  134. return minOffset;
  135. } else if (scrollOffset < minOffset) {
  136. return minOffset;
  137. } else {
  138. return maxOffset;
  139. }
  140. }
  141. }
  142. };
  143. const DynamicSizeGrid = buildGrid["default"]({
  144. name: "ElDynamicSizeGrid",
  145. getColumnPosition: (props, idx, cache) => {
  146. const item = getItemFromCache(props, idx, cache, "column");
  147. return [item.size, item.offset];
  148. },
  149. getRowPosition: (props, idx, cache) => {
  150. const item = getItemFromCache(props, idx, cache, "row");
  151. return [item.size, item.offset];
  152. },
  153. getColumnOffset: (props, columnIndex, alignment, scrollLeft, cache, scrollBarWidth) => getOffset(props, columnIndex, alignment, scrollLeft, cache, "column", scrollBarWidth),
  154. getRowOffset: (props, rowIndex, alignment, scrollTop, cache, scrollBarWidth) => getOffset(props, rowIndex, alignment, scrollTop, cache, "row", scrollBarWidth),
  155. getColumnStartIndexForOffset: (props, scrollLeft, cache) => findItem(props, cache, scrollLeft, "column"),
  156. getColumnStopIndexForStartIndex: (props, startIndex, scrollLeft, cache) => {
  157. const item = getItemFromCache(props, startIndex, cache, "column");
  158. const maxOffset = scrollLeft + props.width;
  159. let offset = item.offset + item.size;
  160. let stopIndex = startIndex;
  161. while (stopIndex < props.totalColumn - 1 && offset < maxOffset) {
  162. stopIndex++;
  163. offset += getItemFromCache(props, startIndex, cache, "column").size;
  164. }
  165. return stopIndex;
  166. },
  167. getEstimatedTotalHeight,
  168. getEstimatedTotalWidth,
  169. getRowStartIndexForOffset: (props, scrollTop, cache) => findItem(props, cache, scrollTop, "row"),
  170. getRowStopIndexForStartIndex: (props, startIndex, scrollTop, cache) => {
  171. const { totalRow, height } = props;
  172. const item = getItemFromCache(props, startIndex, cache, "row");
  173. const maxOffset = scrollTop + height;
  174. let offset = item.size + item.offset;
  175. let stopIndex = startIndex;
  176. while (stopIndex < totalRow - 1 && offset < maxOffset) {
  177. stopIndex++;
  178. offset += getItemFromCache(props, stopIndex, cache, "row").size;
  179. }
  180. return stopIndex;
  181. },
  182. injectToInstance: (instance, cache) => {
  183. const resetAfter = ({ columnIndex, rowIndex }, forceUpdate) => {
  184. var _a, _b;
  185. forceUpdate = types.isUndefined(forceUpdate) ? true : forceUpdate;
  186. if (types.isNumber(columnIndex)) {
  187. cache.value.lastVisitedColumnIndex = Math.min(cache.value.lastVisitedColumnIndex, columnIndex - 1);
  188. }
  189. if (types.isNumber(rowIndex)) {
  190. cache.value.lastVisitedRowIndex = Math.min(cache.value.lastVisitedRowIndex, rowIndex - 1);
  191. }
  192. (_a = instance.exposed) == null ? void 0 : _a.getItemStyleCache.value(-1, null, null);
  193. if (forceUpdate)
  194. (_b = instance.proxy) == null ? void 0 : _b.$forceUpdate();
  195. };
  196. const resetAfterColumnIndex = (columnIndex, forceUpdate) => {
  197. resetAfter({
  198. columnIndex
  199. }, forceUpdate);
  200. };
  201. const resetAfterRowIndex = (rowIndex, forceUpdate) => {
  202. resetAfter({
  203. rowIndex
  204. }, forceUpdate);
  205. };
  206. Object.assign(instance.proxy, {
  207. resetAfterColumnIndex,
  208. resetAfterRowIndex,
  209. resetAfter
  210. });
  211. },
  212. initCache: ({
  213. estimatedColumnWidth = defaults.DEFAULT_DYNAMIC_LIST_ITEM_SIZE,
  214. estimatedRowHeight = defaults.DEFAULT_DYNAMIC_LIST_ITEM_SIZE
  215. }) => {
  216. const cache = {
  217. column: {},
  218. estimatedColumnWidth,
  219. estimatedRowHeight,
  220. lastVisitedColumnIndex: -1,
  221. lastVisitedRowIndex: -1,
  222. row: {}
  223. };
  224. return cache;
  225. },
  226. clearCache: false,
  227. validateProps: ({ columnWidth, rowHeight }) => {
  228. }
  229. });
  230. exports["default"] = DynamicSizeGrid;
  231. //# sourceMappingURL=dynamic-size-grid.js.map