dataMap.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. import SheetClip from './../lib/SheetClip/SheetClip.js';
  3. import { cellMethodLookupFactory } from './helpers/data';
  4. import { columnFactory } from './helpers/setting';
  5. import { createObjectPropListener, duckSchema, deepExtend, deepClone, isObject, deepObjectSize, hasOwnProperty } from './helpers/object';
  6. import { extendArray, to2dArray } from './helpers/array';
  7. import Interval from './utils/interval';
  8. import { rangeEach } from './helpers/number';
  9. import MultiMap from './multiMap';
  10. import Hooks from './pluginHooks';
  11. /**
  12. * Utility class that gets and saves data from/to the data source using mapping of columns numbers to object property names
  13. * @todo refactor arguments of methods getRange, getText to be numbers (not objects)
  14. * @todo remove priv, GridSettings from object constructor
  15. *
  16. * @param {Object} instance Instance of Handsontable
  17. * @param {*} priv
  18. * @param {*} GridSettings Grid settings
  19. * @util
  20. * @class DataMap
  21. */
  22. function DataMap(instance, priv, GridSettings) {
  23. var _this = this;
  24. this.instance = instance;
  25. this.priv = priv;
  26. this.GridSettings = GridSettings;
  27. this.dataSource = this.instance.getSettings().data;
  28. this.cachedLength = null;
  29. this.skipCache = false;
  30. this.latestSourceRowsCount = 0;
  31. if (this.dataSource && this.dataSource[0]) {
  32. this.duckSchema = this.recursiveDuckSchema(this.dataSource[0]);
  33. } else {
  34. this.duckSchema = {};
  35. }
  36. this.createMap();
  37. this.interval = Interval.create(function () {
  38. return _this.clearLengthCache();
  39. }, '15fps');
  40. this.instance.addHook('skipLengthCache', function (delay) {
  41. return _this.onSkipLengthCache(delay);
  42. });
  43. this.onSkipLengthCache(500);
  44. }
  45. DataMap.prototype.DESTINATION_RENDERER = 1;
  46. DataMap.prototype.DESTINATION_CLIPBOARD_GENERATOR = 2;
  47. /**
  48. * @param {Object|Array} object
  49. * @returns {Object|Array}
  50. */
  51. DataMap.prototype.recursiveDuckSchema = function (object) {
  52. return duckSchema(object);
  53. };
  54. /**
  55. * @param {Object} schema
  56. * @param {Number} lastCol
  57. * @param {Number} parent
  58. * @returns {Number}
  59. */
  60. DataMap.prototype.recursiveDuckColumns = function (schema, lastCol, parent) {
  61. var prop, i;
  62. if (typeof lastCol === 'undefined') {
  63. lastCol = 0;
  64. parent = '';
  65. }
  66. if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object' && !Array.isArray(schema)) {
  67. for (i in schema) {
  68. if (hasOwnProperty(schema, i)) {
  69. if (schema[i] === null) {
  70. prop = parent + i;
  71. this.colToPropCache.push(prop);
  72. this.propToColCache.set(prop, lastCol);
  73. lastCol++;
  74. } else {
  75. lastCol = this.recursiveDuckColumns(schema[i], lastCol, i + '.');
  76. }
  77. }
  78. }
  79. }
  80. return lastCol;
  81. };
  82. DataMap.prototype.createMap = function () {
  83. var i = void 0;
  84. var schema = this.getSchema();
  85. if (typeof schema === 'undefined') {
  86. throw new Error('trying to create `columns` definition but you didn\'t provide `schema` nor `data`');
  87. }
  88. this.colToPropCache = [];
  89. this.propToColCache = new MultiMap();
  90. var columns = this.instance.getSettings().columns;
  91. if (columns) {
  92. var maxCols = this.instance.getSettings().maxCols;
  93. var columnsLen = Math.min(maxCols, columns.length);
  94. var filteredIndex = 0;
  95. var columnsAsFunc = false;
  96. var schemaLen = deepObjectSize(schema);
  97. if (typeof columns === 'function') {
  98. columnsLen = schemaLen > 0 ? schemaLen : this.instance.countSourceCols();
  99. columnsAsFunc = true;
  100. }
  101. for (i = 0; i < columnsLen; i++) {
  102. var column = columnsAsFunc ? columns(i) : columns[i];
  103. if (isObject(column)) {
  104. if (typeof column.data !== 'undefined') {
  105. var index = columnsAsFunc ? filteredIndex : i;
  106. this.colToPropCache[index] = column.data;
  107. this.propToColCache.set(column.data, index);
  108. }
  109. filteredIndex++;
  110. }
  111. }
  112. } else {
  113. this.recursiveDuckColumns(schema);
  114. }
  115. };
  116. /**
  117. * Returns property name that corresponds with the given column index.
  118. *
  119. * @param {Number} col
  120. * @returns {Number}
  121. */
  122. DataMap.prototype.colToProp = function (col) {
  123. col = this.instance.runHooks('modifyCol', col);
  124. if (!isNaN(col) && this.colToPropCache && typeof this.colToPropCache[col] !== 'undefined') {
  125. return this.colToPropCache[col];
  126. }
  127. return col;
  128. };
  129. /**
  130. * @param {Object} prop
  131. * @fires Hooks#modifyCol
  132. * @returns {*}
  133. */
  134. DataMap.prototype.propToCol = function (prop) {
  135. var col;
  136. if (typeof this.propToColCache.get(prop) === 'undefined') {
  137. col = prop;
  138. } else {
  139. col = this.propToColCache.get(prop);
  140. }
  141. col = this.instance.runHooks('unmodifyCol', col);
  142. return col;
  143. };
  144. /**
  145. * @returns {Object}
  146. */
  147. DataMap.prototype.getSchema = function () {
  148. var schema = this.instance.getSettings().dataSchema;
  149. if (schema) {
  150. if (typeof schema === 'function') {
  151. return schema();
  152. }
  153. return schema;
  154. }
  155. return this.duckSchema;
  156. };
  157. /**
  158. * Creates row at the bottom of the data array.
  159. *
  160. * @param {Number} [index] Index of the row before which the new row will be inserted.
  161. * @param {Number} [amount] An amount of rows to add.
  162. * @param {String} [source] Source of method call.
  163. * @fires Hooks#afterCreateRow
  164. * @returns {Number} Returns number of created rows.
  165. */
  166. DataMap.prototype.createRow = function (index, amount, source) {
  167. var row,
  168. colCount = this.instance.countCols(),
  169. numberOfCreatedRows = 0,
  170. currentIndex;
  171. if (!amount) {
  172. amount = 1;
  173. }
  174. if (typeof index !== 'number' || index >= this.instance.countSourceRows()) {
  175. index = this.instance.countSourceRows();
  176. }
  177. this.instance.runHooks('beforeCreateRow', index, amount, source);
  178. currentIndex = index;
  179. var maxRows = this.instance.getSettings().maxRows;
  180. while (numberOfCreatedRows < amount && this.instance.countSourceRows() < maxRows) {
  181. if (this.instance.dataType === 'array') {
  182. if (this.instance.getSettings().dataSchema) {
  183. // Clone template array
  184. row = deepClone(this.getSchema());
  185. } else {
  186. row = [];
  187. /* eslint-disable no-loop-func */
  188. rangeEach(colCount - 1, function () {
  189. return row.push(null);
  190. });
  191. }
  192. } else if (this.instance.dataType === 'function') {
  193. row = this.instance.getSettings().dataSchema(index);
  194. } else {
  195. row = {};
  196. deepExtend(row, this.getSchema());
  197. }
  198. if (index === this.instance.countSourceRows()) {
  199. this.dataSource.push(row);
  200. } else {
  201. this.spliceData(index, 0, row);
  202. }
  203. numberOfCreatedRows++;
  204. currentIndex++;
  205. }
  206. this.instance.runHooks('afterCreateRow', index, numberOfCreatedRows, source);
  207. this.instance.forceFullRender = true; // used when data was changed
  208. return numberOfCreatedRows;
  209. };
  210. /**
  211. * Creates col at the right of the data array.
  212. *
  213. * @param {Number} [index] Index of the column before which the new column will be inserted
  214. * @param {Number} [amount] An amount of columns to add.
  215. * @param {String} [source] Source of method call.
  216. * @fires Hooks#afterCreateCol
  217. * @returns {Number} Returns number of created columns
  218. */
  219. DataMap.prototype.createCol = function (index, amount, source) {
  220. if (!this.instance.isColumnModificationAllowed()) {
  221. throw new Error('Cannot create new column. When data source in an object, ' + 'you can only have as much columns as defined in first data row, data schema or in the \'columns\' setting.' + 'If you want to be able to add new columns, you have to use array datasource.');
  222. }
  223. var rlen = this.instance.countSourceRows(),
  224. data = this.dataSource,
  225. constructor,
  226. numberOfCreatedCols = 0,
  227. currentIndex;
  228. if (!amount) {
  229. amount = 1;
  230. }
  231. if (typeof index !== 'number' || index >= this.instance.countCols()) {
  232. index = this.instance.countCols();
  233. }
  234. this.instance.runHooks('beforeCreateCol', index, amount, source);
  235. currentIndex = index;
  236. var maxCols = this.instance.getSettings().maxCols;
  237. while (numberOfCreatedCols < amount && this.instance.countCols() < maxCols) {
  238. constructor = columnFactory(this.GridSettings, this.priv.columnsSettingConflicts);
  239. if (typeof index !== 'number' || index >= this.instance.countCols()) {
  240. if (rlen > 0) {
  241. for (var r = 0; r < rlen; r++) {
  242. if (typeof data[r] === 'undefined') {
  243. data[r] = [];
  244. }
  245. data[r].push(null);
  246. }
  247. } else {
  248. data.push([null]);
  249. }
  250. // Add new column constructor
  251. this.priv.columnSettings.push(constructor);
  252. } else {
  253. for (var _r = 0; _r < rlen; _r++) {
  254. data[_r].splice(currentIndex, 0, null);
  255. }
  256. // Add new column constructor at given index
  257. this.priv.columnSettings.splice(currentIndex, 0, constructor);
  258. }
  259. numberOfCreatedCols++;
  260. currentIndex++;
  261. }
  262. this.instance.runHooks('afterCreateCol', index, numberOfCreatedCols, source);
  263. this.instance.forceFullRender = true; // used when data was changed
  264. return numberOfCreatedCols;
  265. };
  266. /**
  267. * Removes row from the data array.
  268. *
  269. * @param {Number} [index] Index of the row to be removed. If not provided, the last row will be removed
  270. * @param {Number} [amount] Amount of the rows to be removed. If not provided, one row will be removed
  271. * @param {String} [source] Source of method call.
  272. * @fires Hooks#beforeRemoveRow
  273. * @fires Hooks#afterRemoveRow
  274. */
  275. DataMap.prototype.removeRow = function (index, amount, source) {
  276. if (!amount) {
  277. amount = 1;
  278. }
  279. if (typeof index !== 'number') {
  280. index = -amount;
  281. }
  282. amount = this.instance.runHooks('modifyRemovedAmount', amount, index);
  283. index = (this.instance.countSourceRows() + index) % this.instance.countSourceRows();
  284. var logicRows = this.physicalRowsToLogical(index, amount);
  285. var actionWasNotCancelled = this.instance.runHooks('beforeRemoveRow', index, amount, logicRows, source);
  286. if (actionWasNotCancelled === false) {
  287. return;
  288. }
  289. var data = this.dataSource;
  290. var newData = void 0;
  291. newData = this.filterData(index, amount);
  292. if (newData) {
  293. data.length = 0;
  294. Array.prototype.push.apply(data, newData);
  295. }
  296. this.instance.runHooks('afterRemoveRow', index, amount, logicRows, source);
  297. this.instance.forceFullRender = true; // used when data was changed
  298. };
  299. /**
  300. * Removes column from the data array.
  301. *
  302. * @param {Number} [index] Index of the column to be removed. If not provided, the last column will be removed
  303. * @param {Number} [amount] Amount of the columns to be removed. If not provided, one column will be removed
  304. * @param {String} [source] Source of method call.
  305. * @fires Hooks#beforeRemoveCol
  306. * @fires Hooks#afterRemoveCol
  307. */
  308. DataMap.prototype.removeCol = function (index, amount, source) {
  309. if (this.instance.dataType === 'object' || this.instance.getSettings().columns) {
  310. throw new Error('cannot remove column with object data source or columns option specified');
  311. }
  312. if (!amount) {
  313. amount = 1;
  314. }
  315. if (typeof index !== 'number') {
  316. index = -amount;
  317. }
  318. index = (this.instance.countCols() + index) % this.instance.countCols();
  319. var logicColumns = this.physicalColumnsToLogical(index, amount);
  320. var descendingLogicColumns = logicColumns.slice(0).sort(function (a, b) {
  321. return b - a;
  322. });
  323. var actionWasNotCancelled = this.instance.runHooks('beforeRemoveCol', index, amount, logicColumns, source);
  324. if (actionWasNotCancelled === false) {
  325. return;
  326. }
  327. var isTableUniform = true;
  328. var removedColumnsCount = descendingLogicColumns.length;
  329. var data = this.dataSource;
  330. for (var c = 0; c < removedColumnsCount; c++) {
  331. if (isTableUniform && logicColumns[0] !== logicColumns[c] - c) {
  332. isTableUniform = false;
  333. }
  334. }
  335. if (isTableUniform) {
  336. for (var r = 0, rlen = this.instance.countSourceRows(); r < rlen; r++) {
  337. data[r].splice(logicColumns[0], amount);
  338. }
  339. } else {
  340. for (var _r2 = 0, _rlen = this.instance.countSourceRows(); _r2 < _rlen; _r2++) {
  341. for (var _c = 0; _c < removedColumnsCount; _c++) {
  342. data[_r2].splice(descendingLogicColumns[_c], 1);
  343. }
  344. }
  345. for (var _c2 = 0; _c2 < removedColumnsCount; _c2++) {
  346. this.priv.columnSettings.splice(logicColumns[_c2], 1);
  347. }
  348. }
  349. this.instance.runHooks('afterRemoveCol', index, amount, logicColumns, source);
  350. this.instance.forceFullRender = true; // used when data was changed
  351. };
  352. /**
  353. * Add/Removes data from the column.
  354. *
  355. * @param {Number} col Index of column in which do you want to do splice
  356. * @param {Number} index Index at which to start changing the array. If negative, will begin that many elements from the end
  357. * @param {Number} amount An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed
  358. * @returns {Array} Returns removed portion of columns
  359. */
  360. DataMap.prototype.spliceCol = function (col, index, amount /* , elements...*/) {
  361. var elements = arguments.length >= 4 ? [].slice.call(arguments, 3) : [];
  362. var colData = this.instance.getDataAtCol(col);
  363. var removed = colData.slice(index, index + amount);
  364. var after = colData.slice(index + amount);
  365. extendArray(elements, after);
  366. var i = 0;
  367. while (i < amount) {
  368. elements.push(null); // add null in place of removed elements
  369. i++;
  370. }
  371. to2dArray(elements);
  372. this.instance.populateFromArray(index, col, elements, null, null, 'spliceCol');
  373. return removed;
  374. };
  375. /**
  376. * Add/Removes data from the row.
  377. *
  378. * @param {Number} row Index of row in which do you want to do splice
  379. * @param {Number} index Index at which to start changing the array. If negative, will begin that many elements from the end
  380. * @param {Number} amount An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed
  381. * @returns {Array} Returns removed portion of rows
  382. */
  383. DataMap.prototype.spliceRow = function (row, index, amount /* , elements...*/) {
  384. var elements = arguments.length >= 4 ? [].slice.call(arguments, 3) : [];
  385. var rowData = this.instance.getSourceDataAtRow(row);
  386. var removed = rowData.slice(index, index + amount);
  387. var after = rowData.slice(index + amount);
  388. extendArray(elements, after);
  389. var i = 0;
  390. while (i < amount) {
  391. elements.push(null); // add null in place of removed elements
  392. i++;
  393. }
  394. this.instance.populateFromArray(row, index, [elements], null, null, 'spliceRow');
  395. return removed;
  396. };
  397. /**
  398. * Add/remove row(s) to/from the data source.
  399. *
  400. * @param {Number} index Index of the element to remove.
  401. * @param {Number} amount Number of rows to add/remove.
  402. * @param {Object} element Row to add.
  403. */
  404. DataMap.prototype.spliceData = function (index, amount, element) {
  405. var continueSplicing = this.instance.runHooks('beforeDataSplice', index, amount, element);
  406. if (continueSplicing !== false) {
  407. this.dataSource.splice(index, amount, element);
  408. }
  409. };
  410. /**
  411. * Filter unwanted data elements from the data source.
  412. *
  413. * @param {Number} index Index of the element to remove.
  414. * @param {Number} amount Number of rows to add/remove.
  415. * @returns {Array}
  416. */
  417. DataMap.prototype.filterData = function (index, amount) {
  418. var logicRows = this.physicalRowsToLogical(index, amount);
  419. var continueSplicing = this.instance.runHooks('beforeDataFilter', index, amount, logicRows);
  420. if (continueSplicing !== false) {
  421. var newData = this.dataSource.filter(function (row, index) {
  422. return logicRows.indexOf(index) == -1;
  423. });
  424. return newData;
  425. }
  426. };
  427. /**
  428. * Returns single value from the data array.
  429. *
  430. * @param {Number} row
  431. * @param {Number} prop
  432. */
  433. DataMap.prototype.get = function (row, prop) {
  434. row = this.instance.runHooks('modifyRow', row);
  435. var dataRow = this.dataSource[row];
  436. // TODO: To remove, use 'modifyData' hook instead (see below)
  437. var modifiedRowData = this.instance.runHooks('modifyRowData', row);
  438. dataRow = isNaN(modifiedRowData) ? modifiedRowData : dataRow;
  439. //
  440. var value = null;
  441. // try to get value under property `prop` (includes dot)
  442. if (dataRow && dataRow.hasOwnProperty && hasOwnProperty(dataRow, prop)) {
  443. value = dataRow[prop];
  444. } else if (typeof prop === 'string' && prop.indexOf('.') > -1) {
  445. var sliced = prop.split('.');
  446. var out = dataRow;
  447. if (!out) {
  448. return null;
  449. }
  450. for (var i = 0, ilen = sliced.length; i < ilen; i++) {
  451. out = out[sliced[i]];
  452. if (typeof out === 'undefined') {
  453. return null;
  454. }
  455. }
  456. value = out;
  457. } else if (typeof prop === 'function') {
  458. /**
  459. * allows for interacting with complex structures, for example
  460. * d3/jQuery getter/setter properties:
  461. *
  462. * {columns: [{
  463. * data: function(row, value){
  464. * if(arguments.length === 1){
  465. * return row.property();
  466. * }
  467. * row.property(value);
  468. * }
  469. * }]}
  470. */
  471. value = prop(this.dataSource.slice(row, row + 1)[0]);
  472. }
  473. if (this.instance.hasHook('modifyData')) {
  474. var valueHolder = createObjectPropListener(value);
  475. this.instance.runHooks('modifyData', row, this.propToCol(prop), valueHolder, 'get');
  476. if (valueHolder.isTouched()) {
  477. value = valueHolder.value;
  478. }
  479. }
  480. return value;
  481. };
  482. var copyableLookup = cellMethodLookupFactory('copyable', false);
  483. /**
  484. * Returns single value from the data array (intended for clipboard copy to an external application).
  485. *
  486. * @param {Number} row
  487. * @param {Number} prop
  488. * @returns {String}
  489. */
  490. DataMap.prototype.getCopyable = function (row, prop) {
  491. if (copyableLookup.call(this.instance, row, this.propToCol(prop))) {
  492. return this.get(row, prop);
  493. }
  494. return '';
  495. };
  496. /**
  497. * Saves single value to the data array.
  498. *
  499. * @param {Number} row
  500. * @param {Number} prop
  501. * @param {String} value
  502. * @param {String} [source] Source of hook runner.
  503. */
  504. DataMap.prototype.set = function (row, prop, value, source) {
  505. row = this.instance.runHooks('modifyRow', row, source || 'datamapGet');
  506. var dataRow = this.dataSource[row];
  507. // TODO: To remove, use 'modifyData' hook instead (see below)
  508. var modifiedRowData = this.instance.runHooks('modifyRowData', row);
  509. dataRow = isNaN(modifiedRowData) ? modifiedRowData : dataRow;
  510. //
  511. if (this.instance.hasHook('modifyData')) {
  512. var valueHolder = createObjectPropListener(value);
  513. this.instance.runHooks('modifyData', row, this.propToCol(prop), valueHolder, 'set');
  514. if (valueHolder.isTouched()) {
  515. value = valueHolder.value;
  516. }
  517. }
  518. // try to set value under property `prop` (includes dot)
  519. if (dataRow && dataRow.hasOwnProperty && hasOwnProperty(dataRow, prop)) {
  520. dataRow[prop] = value;
  521. } else if (typeof prop === 'string' && prop.indexOf('.') > -1) {
  522. var sliced = prop.split('.');
  523. var out = dataRow;
  524. var i = 0;
  525. var ilen = void 0;
  526. for (i = 0, ilen = sliced.length - 1; i < ilen; i++) {
  527. if (typeof out[sliced[i]] === 'undefined') {
  528. out[sliced[i]] = {};
  529. }
  530. out = out[sliced[i]];
  531. }
  532. out[sliced[i]] = value;
  533. } else if (typeof prop === 'function') {
  534. /* see the `function` handler in `get` */
  535. prop(this.dataSource.slice(row, row + 1)[0], value);
  536. } else {
  537. dataRow[prop] = value;
  538. }
  539. };
  540. /**
  541. * This ridiculous piece of code maps rows Id that are present in table data to those displayed for user.
  542. * The trick is, the physical row id (stored in settings.data) is not necessary the same
  543. * as the logical (displayed) row id (e.g. when sorting is applied).
  544. *
  545. * @param {Number} index
  546. * @param {Number} amount
  547. * @fires Hooks#modifyRow
  548. * @returns {Number}
  549. */
  550. DataMap.prototype.physicalRowsToLogical = function (index, amount) {
  551. var totalRows = this.instance.countSourceRows();
  552. var physicRow = (totalRows + index) % totalRows;
  553. var logicRows = [];
  554. var rowsToRemove = amount;
  555. var row;
  556. while (physicRow < totalRows && rowsToRemove) {
  557. row = this.instance.runHooks('modifyRow', physicRow);
  558. logicRows.push(row);
  559. rowsToRemove--;
  560. physicRow++;
  561. }
  562. return logicRows;
  563. };
  564. /**
  565. *
  566. * @param index
  567. * @param amount
  568. * @returns {Array}
  569. */
  570. DataMap.prototype.physicalColumnsToLogical = function (index, amount) {
  571. var totalCols = this.instance.countCols();
  572. var physicalCol = (totalCols + index) % totalCols;
  573. var logicalCols = [];
  574. var colsToRemove = amount;
  575. while (physicalCol < totalCols && colsToRemove) {
  576. var col = this.instance.runHooks('modifyCol', physicalCol);
  577. logicalCols.push(col);
  578. colsToRemove--;
  579. physicalCol++;
  580. }
  581. return logicalCols;
  582. };
  583. /**
  584. * Clears the data array.
  585. */
  586. DataMap.prototype.clear = function () {
  587. for (var r = 0; r < this.instance.countSourceRows(); r++) {
  588. for (var c = 0; c < this.instance.countCols(); c++) {
  589. this.set(r, this.colToProp(c), '');
  590. }
  591. }
  592. };
  593. /**
  594. * Clear cached data length.
  595. */
  596. DataMap.prototype.clearLengthCache = function () {
  597. this.cachedLength = null;
  598. };
  599. /**
  600. * Get data length.
  601. *
  602. * @returns {Number}
  603. */
  604. DataMap.prototype.getLength = function () {
  605. var _this2 = this;
  606. var maxRows = void 0,
  607. maxRowsFromSettings = this.instance.getSettings().maxRows;
  608. if (maxRowsFromSettings < 0 || maxRowsFromSettings === 0) {
  609. maxRows = 0;
  610. } else {
  611. maxRows = maxRowsFromSettings || Infinity;
  612. }
  613. var length = this.instance.countSourceRows();
  614. if (this.instance.hasHook('modifyRow')) {
  615. var reValidate = this.skipCache;
  616. this.interval.start();
  617. if (length !== this.latestSourceRowsCount) {
  618. reValidate = true;
  619. }
  620. this.latestSourceRowsCount = length;
  621. if (this.cachedLength === null || reValidate) {
  622. rangeEach(length - 1, function (row) {
  623. row = _this2.instance.runHooks('modifyRow', row);
  624. if (row === null) {
  625. --length;
  626. }
  627. });
  628. this.cachedLength = length;
  629. } else {
  630. length = this.cachedLength;
  631. }
  632. } else {
  633. this.interval.stop();
  634. }
  635. return Math.min(length, maxRows);
  636. };
  637. /**
  638. * Returns the data array.
  639. *
  640. * @returns {Array}
  641. */
  642. DataMap.prototype.getAll = function () {
  643. var start = {
  644. row: 0,
  645. col: 0
  646. };
  647. var end = {
  648. row: Math.max(this.instance.countSourceRows() - 1, 0),
  649. col: Math.max(this.instance.countCols() - 1, 0)
  650. };
  651. if (start.row - end.row === 0 && !this.instance.countSourceRows()) {
  652. return [];
  653. }
  654. return this.getRange(start, end, DataMap.prototype.DESTINATION_RENDERER);
  655. };
  656. /**
  657. * Returns data range as array.
  658. *
  659. * @param {Object} [start] Start selection position
  660. * @param {Object} [end] End selection position
  661. * @param {Number} destination Destination of datamap.get
  662. * @returns {Array}
  663. */
  664. DataMap.prototype.getRange = function (start, end, destination) {
  665. var r,
  666. rlen,
  667. c,
  668. clen,
  669. output = [],
  670. row;
  671. var maxRows = this.instance.getSettings().maxRows;
  672. var maxCols = this.instance.getSettings().maxCols;
  673. if (maxRows === 0 || maxCols === 0) {
  674. return [];
  675. }
  676. var getFn = destination === this.DESTINATION_CLIPBOARD_GENERATOR ? this.getCopyable : this.get;
  677. rlen = Math.min(Math.max(maxRows - 1, 0), Math.max(start.row, end.row));
  678. clen = Math.min(Math.max(maxCols - 1, 0), Math.max(start.col, end.col));
  679. for (r = Math.min(start.row, end.row); r <= rlen; r++) {
  680. row = [];
  681. var physicalRow = this.instance.runHooks('modifyRow', r);
  682. for (c = Math.min(start.col, end.col); c <= clen; c++) {
  683. if (physicalRow === null) {
  684. break;
  685. }
  686. row.push(getFn.call(this, r, this.colToProp(c)));
  687. }
  688. if (physicalRow !== null) {
  689. output.push(row);
  690. }
  691. }
  692. return output;
  693. };
  694. /**
  695. * Return data as text (tab separated columns).
  696. *
  697. * @param {Object} [start] Start selection position
  698. * @param {Object} [end] End selection position
  699. * @returns {String}
  700. */
  701. DataMap.prototype.getText = function (start, end) {
  702. return SheetClip.stringify(this.getRange(start, end, this.DESTINATION_RENDERER));
  703. };
  704. /**
  705. * Return data as copyable text (tab separated columns intended for clipboard copy to an external application).
  706. *
  707. * @param {Object} [start] Start selection position
  708. * @param {Object} [end] End selection position
  709. * @returns {String}
  710. */
  711. DataMap.prototype.getCopyableText = function (start, end) {
  712. return SheetClip.stringify(this.getRange(start, end, this.DESTINATION_CLIPBOARD_GENERATOR));
  713. };
  714. /**
  715. * `skipLengthCache` callback.
  716. * @private
  717. * @param {Number} delay Time of the delay in milliseconds.
  718. */
  719. DataMap.prototype.onSkipLengthCache = function (delay) {
  720. var _this3 = this;
  721. this.skipCache = true;
  722. setTimeout(function () {
  723. _this3.skipCache = false;
  724. }, delay);
  725. };
  726. /**
  727. * Destroy instance.
  728. */
  729. DataMap.prototype.destroy = function () {
  730. this.interval.stop();
  731. this.interval = null;
  732. this.instance = null;
  733. this.priv = null;
  734. this.GridSettings = null;
  735. this.dataSource = null;
  736. this.cachedLength = null;
  737. this.duckSchema = null;
  738. };
  739. export default DataMap;