6fc3c16ce011a2af2f0c3217b7a9c32c49ec79e412b811045a3085bca5df21a99b9e93f673efaa6fea80b986d93a2ee81725619ab831cdc0ecbed1fd6125df 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. import { innerHeight, innerWidth, getScrollLeft, getScrollTop, offset } from './../../../helpers/dom/element';
  4. import { rangeEach, rangeEachReverse } from './../../../helpers/number';
  5. /**
  6. * @class Scroll
  7. */
  8. var Scroll = function () {
  9. /**
  10. * @param {Walkontable} wotInstance
  11. */
  12. function Scroll(wotInstance) {
  13. _classCallCheck(this, Scroll);
  14. this.wot = wotInstance;
  15. // legacy support
  16. this.instance = wotInstance;
  17. }
  18. /**
  19. * Scrolls viewport to a cell by minimum number of cells
  20. *
  21. * @param {CellCoords} coords
  22. */
  23. _createClass(Scroll, [{
  24. key: 'scrollViewport',
  25. value: function scrollViewport(coords) {
  26. if (!this.wot.drawn) {
  27. return;
  28. }
  29. var _getVariables2 = this._getVariables(),
  30. topOverlay = _getVariables2.topOverlay,
  31. leftOverlay = _getVariables2.leftOverlay,
  32. totalRows = _getVariables2.totalRows,
  33. totalColumns = _getVariables2.totalColumns,
  34. fixedRowsTop = _getVariables2.fixedRowsTop,
  35. fixedRowsBottom = _getVariables2.fixedRowsBottom,
  36. fixedColumnsLeft = _getVariables2.fixedColumnsLeft;
  37. if (coords.row < 0 || coords.row > Math.max(totalRows - 1, 0)) {
  38. throw new Error('row ' + coords.row + ' does not exist');
  39. }
  40. if (coords.col < 0 || coords.col > Math.max(totalColumns - 1, 0)) {
  41. throw new Error('column ' + coords.col + ' does not exist');
  42. }
  43. if (coords.row >= fixedRowsTop && coords.row < this.getFirstVisibleRow()) {
  44. topOverlay.scrollTo(coords.row);
  45. } else if (coords.row > this.getLastVisibleRow() && coords.row < totalRows - fixedRowsBottom) {
  46. topOverlay.scrollTo(coords.row, true);
  47. }
  48. if (coords.col >= fixedColumnsLeft && coords.col < this.getFirstVisibleColumn()) {
  49. leftOverlay.scrollTo(coords.col);
  50. } else if (coords.col > this.getLastVisibleColumn()) {
  51. leftOverlay.scrollTo(coords.col, true);
  52. }
  53. }
  54. /**
  55. * Get first visible row based on virtual dom and how table is visible in browser window viewport.
  56. *
  57. * @returns {Number}
  58. */
  59. }, {
  60. key: 'getFirstVisibleRow',
  61. value: function getFirstVisibleRow() {
  62. var _getVariables3 = this._getVariables(),
  63. topOverlay = _getVariables3.topOverlay,
  64. wtTable = _getVariables3.wtTable,
  65. wtViewport = _getVariables3.wtViewport,
  66. totalRows = _getVariables3.totalRows,
  67. fixedRowsTop = _getVariables3.fixedRowsTop;
  68. var firstVisibleRow = wtTable.getFirstVisibleRow();
  69. if (topOverlay.mainTableScrollableElement === window) {
  70. var rootElementOffset = offset(wtTable.wtRootElement);
  71. var totalTableHeight = innerHeight(wtTable.hider);
  72. var windowHeight = innerHeight(window);
  73. var windowScrollTop = getScrollTop(window);
  74. // Only calculate firstVisibleRow when table didn't filled (from up) whole viewport space
  75. if (rootElementOffset.top + totalTableHeight - windowHeight <= windowScrollTop) {
  76. var rowsHeight = wtViewport.getColumnHeaderHeight();
  77. rowsHeight += topOverlay.sumCellSizes(0, fixedRowsTop);
  78. rangeEachReverse(totalRows, 1, function (row) {
  79. rowsHeight += topOverlay.sumCellSizes(row - 1, row);
  80. if (rootElementOffset.top + totalTableHeight - rowsHeight <= windowScrollTop) {
  81. // Return physical row + 1
  82. firstVisibleRow = row;
  83. return false;
  84. }
  85. });
  86. }
  87. }
  88. return firstVisibleRow;
  89. }
  90. /**
  91. * Get last visible row based on virtual dom and how table is visible in browser window viewport.
  92. *
  93. * @returns {Number}
  94. */
  95. }, {
  96. key: 'getLastVisibleRow',
  97. value: function getLastVisibleRow() {
  98. var _getVariables4 = this._getVariables(),
  99. topOverlay = _getVariables4.topOverlay,
  100. wtTable = _getVariables4.wtTable,
  101. wtViewport = _getVariables4.wtViewport,
  102. totalRows = _getVariables4.totalRows;
  103. var lastVisibleRow = wtTable.getLastVisibleRow();
  104. if (topOverlay.mainTableScrollableElement === window) {
  105. var rootElementOffset = offset(wtTable.wtRootElement);
  106. var windowHeight = innerHeight(window);
  107. var windowScrollTop = getScrollTop(window);
  108. // Only calculate lastVisibleRow when table didn't filled (from bottom) whole viewport space
  109. if (rootElementOffset.top > windowScrollTop) {
  110. var rowsHeight = wtViewport.getColumnHeaderHeight();
  111. rangeEach(1, totalRows, function (row) {
  112. rowsHeight += topOverlay.sumCellSizes(row - 1, row);
  113. if (rootElementOffset.top + rowsHeight - windowScrollTop >= windowHeight) {
  114. // Return physical row - 1 (-2 because rangeEach gives row index + 1 - sumCellSizes requirements)
  115. lastVisibleRow = row - 2;
  116. return false;
  117. }
  118. });
  119. }
  120. }
  121. return lastVisibleRow;
  122. }
  123. /**
  124. * Get first visible column based on virtual dom and how table is visible in browser window viewport.
  125. *
  126. * @returns {Number}
  127. */
  128. }, {
  129. key: 'getFirstVisibleColumn',
  130. value: function getFirstVisibleColumn() {
  131. var _getVariables5 = this._getVariables(),
  132. leftOverlay = _getVariables5.leftOverlay,
  133. wtTable = _getVariables5.wtTable,
  134. wtViewport = _getVariables5.wtViewport,
  135. totalColumns = _getVariables5.totalColumns,
  136. fixedColumnsLeft = _getVariables5.fixedColumnsLeft;
  137. var firstVisibleColumn = wtTable.getFirstVisibleColumn();
  138. if (leftOverlay.mainTableScrollableElement === window) {
  139. var rootElementOffset = offset(wtTable.wtRootElement);
  140. var totalTableWidth = innerWidth(wtTable.hider);
  141. var windowWidth = innerWidth(window);
  142. var windowScrollLeft = getScrollLeft(window);
  143. // Only calculate firstVisibleColumn when table didn't filled (from left) whole viewport space
  144. if (rootElementOffset.left + totalTableWidth - windowWidth <= windowScrollLeft) {
  145. var columnsWidth = wtViewport.getRowHeaderWidth();
  146. rangeEachReverse(totalColumns, 1, function (column) {
  147. columnsWidth += leftOverlay.sumCellSizes(column - 1, column);
  148. if (rootElementOffset.left + totalTableWidth - columnsWidth <= windowScrollLeft) {
  149. // Return physical column + 1
  150. firstVisibleColumn = column;
  151. return false;
  152. }
  153. });
  154. }
  155. }
  156. return firstVisibleColumn;
  157. }
  158. /**
  159. * Get last visible column based on virtual dom and how table is visible in browser window viewport.
  160. *
  161. * @returns {Number}
  162. */
  163. }, {
  164. key: 'getLastVisibleColumn',
  165. value: function getLastVisibleColumn() {
  166. var _getVariables6 = this._getVariables(),
  167. leftOverlay = _getVariables6.leftOverlay,
  168. wtTable = _getVariables6.wtTable,
  169. wtViewport = _getVariables6.wtViewport,
  170. totalColumns = _getVariables6.totalColumns;
  171. var lastVisibleColumn = wtTable.getLastVisibleColumn();
  172. if (leftOverlay.mainTableScrollableElement === window) {
  173. var rootElementOffset = offset(wtTable.wtRootElement);
  174. var windowWidth = innerWidth(window);
  175. var windowScrollLeft = getScrollLeft(window);
  176. // Only calculate lastVisibleColumn when table didn't filled (from right) whole viewport space
  177. if (rootElementOffset.left > windowScrollLeft) {
  178. var columnsWidth = wtViewport.getRowHeaderWidth();
  179. rangeEach(1, totalColumns, function (column) {
  180. columnsWidth += leftOverlay.sumCellSizes(column - 1, column);
  181. if (rootElementOffset.left + columnsWidth - windowScrollLeft >= windowWidth) {
  182. // Return physical column - 1 (-2 because rangeEach gives column index + 1 - sumCellSizes requirements)
  183. lastVisibleColumn = column - 2;
  184. return false;
  185. }
  186. });
  187. }
  188. }
  189. return lastVisibleColumn;
  190. }
  191. /**
  192. * Returns collection of variables used to rows and columns visibility calculations.
  193. *
  194. * @returns {Object}
  195. * @private
  196. */
  197. }, {
  198. key: '_getVariables',
  199. value: function _getVariables() {
  200. var wot = this.wot;
  201. var topOverlay = wot.wtOverlays.topOverlay;
  202. var leftOverlay = wot.wtOverlays.leftOverlay;
  203. var wtTable = wot.wtTable;
  204. var wtViewport = wot.wtViewport;
  205. var totalRows = wot.getSetting('totalRows');
  206. var totalColumns = wot.getSetting('totalColumns');
  207. var fixedRowsTop = wot.getSetting('fixedRowsTop');
  208. var fixedRowsBottom = wot.getSetting('fixedRowsBottom');
  209. var fixedColumnsLeft = wot.getSetting('fixedColumnsLeft');
  210. return {
  211. topOverlay: topOverlay,
  212. leftOverlay: leftOverlay,
  213. wtTable: wtTable,
  214. wtViewport: wtViewport,
  215. totalRows: totalRows,
  216. totalColumns: totalColumns,
  217. fixedRowsTop: fixedRowsTop,
  218. fixedRowsBottom: fixedRowsBottom,
  219. fixedColumnsLeft: fixedColumnsLeft
  220. };
  221. }
  222. }]);
  223. return Scroll;
  224. }();
  225. export default Scroll;