dataSource.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 _object = require('./helpers/object');
  5. var _array = require('./helpers/array');
  6. var _number = require('./helpers/number');
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. /**
  9. * @class DataSource
  10. * @private
  11. */
  12. var DataSource = function () {
  13. function DataSource(hotInstance) {
  14. var dataSource = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  15. _classCallCheck(this, DataSource);
  16. /**
  17. * Instance of Handsontable.
  18. *
  19. * @type {Handsontable}
  20. */
  21. this.hot = hotInstance;
  22. /**
  23. * Data source
  24. *
  25. * @type {Array}
  26. */
  27. this.data = dataSource;
  28. /**
  29. * Type of data source.
  30. *
  31. * @type {String}
  32. * @default 'array'
  33. */
  34. this.dataType = 'array';
  35. this.colToProp = function () {};
  36. this.propToCol = function () {};
  37. }
  38. /**
  39. * Get all data.
  40. *
  41. * @param {Boolean} [toArray=false] If `true` return source data as an array of arrays even when source data was provided
  42. * in another format.
  43. * @returns {Array}
  44. */
  45. _createClass(DataSource, [{
  46. key: 'getData',
  47. value: function getData() {
  48. var toArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  49. var result = this.data;
  50. if (toArray) {
  51. result = this.getByRange({ row: 0, col: 0 }, { row: Math.max(this.countRows() - 1, 0), col: Math.max(this.countColumns() - 1, 0) }, true);
  52. }
  53. return result;
  54. }
  55. /**
  56. * Set new data source.
  57. *
  58. * @param data {Array}
  59. */
  60. }, {
  61. key: 'setData',
  62. value: function setData(data) {
  63. this.data = data;
  64. }
  65. /**
  66. * Returns array of column values from the data source. `column` is the index of the row in the data source.
  67. *
  68. * @param {Number} column
  69. * @returns {Array}
  70. */
  71. }, {
  72. key: 'getAtColumn',
  73. value: function getAtColumn(column) {
  74. var _this = this;
  75. var result = [];
  76. (0, _array.arrayEach)(this.data, function (row) {
  77. var property = _this.colToProp(column);
  78. if (typeof property === 'string') {
  79. row = (0, _object.getProperty)(row, property);
  80. } else {
  81. row = row[property];
  82. }
  83. result.push(row);
  84. });
  85. return result;
  86. }
  87. /**
  88. * Returns a single row of the data (array or object, depending on what you have). `row` is the index of the row in the data source.
  89. *
  90. * @param {Number} row
  91. * @returns {Array|Object}
  92. */
  93. }, {
  94. key: 'getAtRow',
  95. value: function getAtRow(row) {
  96. return this.data[row];
  97. }
  98. /**
  99. * Returns a single value from the data.
  100. *
  101. * @param {Number} row Row index.
  102. * @param {Number} column Column index.
  103. * @returns {*}
  104. */
  105. }, {
  106. key: 'getAtCell',
  107. value: function getAtCell(row, column) {
  108. var result = null;
  109. var modifyRowData = this.hot.runHooks('modifyRowData', row);
  110. var dataRow = isNaN(modifyRowData) ? modifyRowData : this.data[row];
  111. if (dataRow) {
  112. var prop = this.colToProp(column);
  113. if (typeof prop === 'string') {
  114. result = (0, _object.getProperty)(dataRow, prop);
  115. } else if (typeof prop === 'function') {
  116. result = prop(this.data.slice(row, row + 1)[0]);
  117. } else {
  118. result = dataRow[prop];
  119. }
  120. }
  121. return result;
  122. }
  123. /**
  124. * Returns source data by passed range.
  125. *
  126. * @param {Object} start Object with `row` and `col` keys.
  127. * @param {Object} end Object with `row` and `col` keys.
  128. * @param {Boolean} [toArray=false] If `true` return source data as an array of arrays even when source data was provided
  129. * in another format.
  130. * @returns {Array}
  131. */
  132. }, {
  133. key: 'getByRange',
  134. value: function getByRange(start, end) {
  135. var _this2 = this;
  136. var toArray = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  137. var startRow = Math.min(start.row, end.row);
  138. var startCol = Math.min(start.col, end.col);
  139. var endRow = Math.max(start.row, end.row);
  140. var endCol = Math.max(start.col, end.col);
  141. var result = [];
  142. (0, _number.rangeEach)(startRow, endRow, function (currentRow) {
  143. var row = _this2.getAtRow(currentRow);
  144. var newRow = void 0;
  145. if (_this2.dataType === 'array') {
  146. newRow = row.slice(startCol, endCol + 1);
  147. } else if (_this2.dataType === 'object') {
  148. newRow = toArray ? [] : {};
  149. (0, _number.rangeEach)(startCol, endCol, function (column) {
  150. var prop = _this2.colToProp(column);
  151. if (toArray) {
  152. newRow.push(row[prop]);
  153. } else {
  154. newRow[prop] = row[prop];
  155. }
  156. });
  157. }
  158. result.push(newRow);
  159. });
  160. return result;
  161. }
  162. /**
  163. * Count number of rows.
  164. *
  165. * @returns {Number}
  166. */
  167. }, {
  168. key: 'countRows',
  169. value: function countRows() {
  170. return Array.isArray(this.data) ? this.data.length : 0;
  171. }
  172. /**
  173. * Count number of columns.
  174. *
  175. * @returns {Number}
  176. */
  177. }, {
  178. key: 'countColumns',
  179. value: function countColumns() {
  180. var result = 0;
  181. if (Array.isArray(this.data)) {
  182. if (this.dataType === 'array') {
  183. result = this.data[0].length;
  184. } else if (this.dataType === 'object') {
  185. result = Object.keys(this.data[0]).length;
  186. }
  187. }
  188. return result;
  189. }
  190. /**
  191. * Destroy instance.
  192. */
  193. }, {
  194. key: 'destroy',
  195. value: function destroy() {
  196. this.data = null;
  197. this.hot = null;
  198. }
  199. }]);
  200. return DataSource;
  201. }();
  202. exports.default = DataSource;