dataSource.js 6.2 KB

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