383093538566d99aa87e4dbb34b57bf1bc7058ad419d4efb1abd4ca8c2a0b497382697b7a955a54dc7ed986a0e1d34086dbd519dccd3901f2e0a2123a6df42 9.8 KB

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