a0aae923d0b42b555ec37fa18ae606bcea3483ccebeeb11ba0fc56d7cbf87e4bc0b49e380b325a54f9c3dade9d8f3959c4cefce2dbdd15b95ad3dd570cfa03 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 { addClass, fastInnerText, isVisible, removeClass } from './../../../helpers/dom/element';
  4. import { objectEach } from './../../../helpers/object';
  5. import { toUpperCaseFirst, randomString } from './../../../helpers/string';
  6. import Event from './event';
  7. import Overlays from './overlays';
  8. import Scroll from './scroll';
  9. import Settings from './settings';
  10. import Table from './table';
  11. import Viewport from './viewport';
  12. /**
  13. * @class Walkontable
  14. */
  15. var Walkontable = function () {
  16. /**
  17. * @param {Object} settings
  18. */
  19. function Walkontable(settings) {
  20. _classCallCheck(this, Walkontable);
  21. var originalHeaders = [];
  22. // this is the namespace for global events
  23. this.guid = 'wt_' + randomString();
  24. // bootstrap from settings
  25. if (settings.cloneSource) {
  26. this.cloneSource = settings.cloneSource;
  27. this.cloneOverlay = settings.cloneOverlay;
  28. this.wtSettings = settings.cloneSource.wtSettings;
  29. this.wtTable = new Table(this, settings.table, settings.wtRootElement);
  30. this.wtScroll = new Scroll(this);
  31. this.wtViewport = settings.cloneSource.wtViewport;
  32. this.wtEvent = new Event(this);
  33. this.selections = this.cloneSource.selections;
  34. } else {
  35. this.wtSettings = new Settings(this, settings);
  36. this.wtTable = new Table(this, settings.table);
  37. this.wtScroll = new Scroll(this);
  38. this.wtViewport = new Viewport(this);
  39. this.wtEvent = new Event(this);
  40. this.selections = this.getSetting('selections');
  41. this.wtOverlays = new Overlays(this);
  42. this.exportSettingsAsClassNames();
  43. }
  44. // find original headers
  45. if (this.wtTable.THEAD.childNodes.length && this.wtTable.THEAD.childNodes[0].childNodes.length) {
  46. for (var c = 0, clen = this.wtTable.THEAD.childNodes[0].childNodes.length; c < clen; c++) {
  47. originalHeaders.push(this.wtTable.THEAD.childNodes[0].childNodes[c].innerHTML);
  48. }
  49. if (!this.getSetting('columnHeaders').length) {
  50. this.update('columnHeaders', [function (column, TH) {
  51. fastInnerText(TH, originalHeaders[column]);
  52. }]);
  53. }
  54. }
  55. this.drawn = false;
  56. this.drawInterrupted = false;
  57. }
  58. /**
  59. * Force rerender of Walkontable
  60. *
  61. * @param {Boolean} [fastDraw=false] When `true`, try to refresh only the positions of borders without rerendering
  62. * the data. It will only work if Table.draw() does not force
  63. * rendering anyway
  64. * @returns {Walkontable}
  65. */
  66. _createClass(Walkontable, [{
  67. key: 'draw',
  68. value: function draw() {
  69. var fastDraw = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  70. this.drawInterrupted = false;
  71. if (!fastDraw && !isVisible(this.wtTable.TABLE)) {
  72. // draw interrupted because TABLE is not visible
  73. this.drawInterrupted = true;
  74. } else {
  75. this.wtTable.draw(fastDraw);
  76. }
  77. return this;
  78. }
  79. /**
  80. * Returns the TD at coords. If topmost is set to true, returns TD from the topmost overlay layer,
  81. * if not set or set to false, returns TD from the master table.
  82. *
  83. * @param {CellCoords} coords
  84. * @param {Boolean} [topmost=false]
  85. * @returns {Object}
  86. */
  87. }, {
  88. key: 'getCell',
  89. value: function getCell(coords) {
  90. var topmost = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  91. if (!topmost) {
  92. return this.wtTable.getCell(coords);
  93. }
  94. var totalRows = this.wtSettings.getSetting('totalRows');
  95. var fixedRowsTop = this.wtSettings.getSetting('fixedRowsTop');
  96. var fixedRowsBottom = this.wtSettings.getSetting('fixedRowsBottom');
  97. var fixedColumns = this.wtSettings.getSetting('fixedColumnsLeft');
  98. if (coords.row < fixedRowsTop && coords.col < fixedColumns) {
  99. return this.wtOverlays.topLeftCornerOverlay.clone.wtTable.getCell(coords);
  100. } else if (coords.row < fixedRowsTop) {
  101. return this.wtOverlays.topOverlay.clone.wtTable.getCell(coords);
  102. } else if (coords.col < fixedColumns && coords.row >= totalRows - fixedRowsBottom) {
  103. if (this.wtOverlays.bottomLeftCornerOverlay && this.wtOverlays.bottomLeftCornerOverlay.clone) {
  104. return this.wtOverlays.bottomLeftCornerOverlay.clone.wtTable.getCell(coords);
  105. }
  106. } else if (coords.col < fixedColumns) {
  107. return this.wtOverlays.leftOverlay.clone.wtTable.getCell(coords);
  108. } else if (coords.row < totalRows && coords.row > totalRows - fixedRowsBottom) {
  109. if (this.wtOverlays.bottomOverlay && this.wtOverlays.bottomOverlay.clone) {
  110. return this.wtOverlays.bottomOverlay.clone.wtTable.getCell(coords);
  111. }
  112. }
  113. return this.wtTable.getCell(coords);
  114. }
  115. /**
  116. * @param {Object} settings
  117. * @param {*} value
  118. * @returns {Walkontable}
  119. */
  120. }, {
  121. key: 'update',
  122. value: function update(settings, value) {
  123. return this.wtSettings.update(settings, value);
  124. }
  125. /**
  126. * Scroll the viewport to a row at the given index in the data source
  127. *
  128. * @param {Number} row
  129. * @returns {Walkontable}
  130. */
  131. }, {
  132. key: 'scrollVertical',
  133. value: function scrollVertical(row) {
  134. this.wtOverlays.topOverlay.scrollTo(row);
  135. this.getSetting('onScrollVertically');
  136. return this;
  137. }
  138. /**
  139. * Scroll the viewport to a column at the given index in the data source
  140. *
  141. * @param {Number} column
  142. * @returns {Walkontable}
  143. */
  144. }, {
  145. key: 'scrollHorizontal',
  146. value: function scrollHorizontal(column) {
  147. this.wtOverlays.leftOverlay.scrollTo(column);
  148. this.getSetting('onScrollHorizontally');
  149. return this;
  150. }
  151. /**
  152. * Scrolls the viewport to a cell (rerenders if needed)
  153. *
  154. * @param {CellCoords} coords
  155. * @returns {Walkontable}
  156. */
  157. }, {
  158. key: 'scrollViewport',
  159. value: function scrollViewport(coords) {
  160. this.wtScroll.scrollViewport(coords);
  161. return this;
  162. }
  163. /**
  164. * @returns {Array}
  165. */
  166. }, {
  167. key: 'getViewport',
  168. value: function getViewport() {
  169. return [this.wtTable.getFirstVisibleRow(), this.wtTable.getFirstVisibleColumn(), this.wtTable.getLastVisibleRow(), this.wtTable.getLastVisibleColumn()];
  170. }
  171. /**
  172. * Get overlay name
  173. *
  174. * @returns {String}
  175. */
  176. }, {
  177. key: 'getOverlayName',
  178. value: function getOverlayName() {
  179. return this.cloneOverlay ? this.cloneOverlay.type : 'master';
  180. }
  181. /**
  182. * Check overlay type of this Walkontable instance.
  183. *
  184. * @param {String} name Clone type @see {Overlay.CLONE_TYPES}.
  185. * @returns {Boolean}
  186. */
  187. }, {
  188. key: 'isOverlayName',
  189. value: function isOverlayName(name) {
  190. if (this.cloneOverlay) {
  191. return this.cloneOverlay.type === name;
  192. }
  193. return false;
  194. }
  195. /**
  196. * Export settings as class names added to the parent element of the table.
  197. */
  198. }, {
  199. key: 'exportSettingsAsClassNames',
  200. value: function exportSettingsAsClassNames() {
  201. var _this = this;
  202. var toExport = {
  203. rowHeaders: ['array'],
  204. columnHeaders: ['array']
  205. };
  206. var allClassNames = [];
  207. var newClassNames = [];
  208. objectEach(toExport, function (optionType, key) {
  209. if (optionType.indexOf('array') > -1 && _this.getSetting(key).length) {
  210. newClassNames.push('ht' + toUpperCaseFirst(key));
  211. }
  212. allClassNames.push('ht' + toUpperCaseFirst(key));
  213. });
  214. removeClass(this.wtTable.wtRootElement.parentNode, allClassNames);
  215. addClass(this.wtTable.wtRootElement.parentNode, newClassNames);
  216. }
  217. /**
  218. * Get/Set Walkontable instance setting
  219. *
  220. * @param {String} key
  221. * @param {*} [param1]
  222. * @param {*} [param2]
  223. * @param {*} [param3]
  224. * @param {*} [param4]
  225. * @returns {*}
  226. */
  227. }, {
  228. key: 'getSetting',
  229. value: function getSetting(key, param1, param2, param3, param4) {
  230. // this is faster than .apply - https://github.com/handsontable/handsontable/wiki/JavaScript-&-DOM-performance-tips
  231. return this.wtSettings.getSetting(key, param1, param2, param3, param4);
  232. }
  233. /**
  234. * Checks if setting exists
  235. *
  236. * @param {String} key
  237. * @returns {Boolean}
  238. */
  239. }, {
  240. key: 'hasSetting',
  241. value: function hasSetting(key) {
  242. return this.wtSettings.has(key);
  243. }
  244. /**
  245. * Destroy instance
  246. */
  247. }, {
  248. key: 'destroy',
  249. value: function destroy() {
  250. this.wtOverlays.destroy();
  251. this.wtEvent.destroy();
  252. }
  253. }]);
  254. return Walkontable;
  255. }();
  256. export default Walkontable;