5eb5afdad865c18a105b05cb34cc1ba81d58daa4ea3cdd12838025d7dcb57b41a70249aa146b8c1a2db083494a3c2e2f76c80bb6d27f3cacf6bdc627e104a9 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {getCellType} from './../cellTypes';
  2. import {hasOwnProperty} from './object';
  3. const COLUMN_LABEL_BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  4. const COLUMN_LABEL_BASE_LENGTH = COLUMN_LABEL_BASE.length;
  5. /**
  6. * Generates spreadsheet-like column names: A, B, C, ..., Z, AA, AB, etc.
  7. *
  8. * @param {Number} index Column index.
  9. * @returns {String}
  10. */
  11. export function spreadsheetColumnLabel(index) {
  12. let dividend = index + 1;
  13. let columnLabel = '';
  14. let modulo;
  15. while (dividend > 0) {
  16. modulo = (dividend - 1) % COLUMN_LABEL_BASE_LENGTH;
  17. columnLabel = String.fromCharCode(65 + modulo) + columnLabel;
  18. dividend = parseInt((dividend - modulo) / COLUMN_LABEL_BASE_LENGTH, 10);
  19. }
  20. return columnLabel;
  21. }
  22. /**
  23. * Generates spreadsheet-like column index from theirs labels: A, B, C ...., Z, AA, AB, etc.
  24. *
  25. * @param {String} label Column label.
  26. * @returns {Number}
  27. */
  28. export function spreadsheetColumnIndex(label) {
  29. let result = 0;
  30. if (label) {
  31. for (let i = 0, j = label.length - 1; i < label.length; i += 1, j -= 1) {
  32. result += Math.pow(COLUMN_LABEL_BASE_LENGTH, j) * (COLUMN_LABEL_BASE.indexOf(label[i]) + 1);
  33. }
  34. }
  35. --result;
  36. return result;
  37. }
  38. /**
  39. * Creates 2D array of Excel-like values "A1", "A2", ...
  40. *
  41. * @param {Number} rows Number of rows to generate.
  42. * @param {Number} columns Number of columns to generate.
  43. * @returns {Array}
  44. */
  45. export function createSpreadsheetData(rows = 100, columns = 4) {
  46. var _rows = [],
  47. i,
  48. j;
  49. for (i = 0; i < rows; i++) {
  50. var row = [];
  51. for (j = 0; j < columns; j++) {
  52. row.push(spreadsheetColumnLabel(j) + (i + 1));
  53. }
  54. _rows.push(row);
  55. }
  56. return _rows;
  57. }
  58. /**
  59. * Creates 2D array of Excel-like values "A1", "A2", as an array of objects.
  60. *
  61. * @param {Number} rows Number of rows to generate.
  62. * @param {Number} colCount Number of columns to generate.
  63. * @returns {Array}
  64. */
  65. export function createSpreadsheetObjectData(rows = 100, colCount = 4) {
  66. var _rows = [],
  67. i,
  68. j;
  69. for (i = 0; i < rows; i++) {
  70. var row = {};
  71. for (j = 0; j < colCount; j++) {
  72. row[`prop${j}`] = spreadsheetColumnLabel(j) + (i + 1);
  73. }
  74. _rows.push(row);
  75. }
  76. return _rows;
  77. }
  78. /**
  79. * Generates an empty data object.
  80. *
  81. * @param {Number} rows Number of rows to generate.
  82. * @param {Number} columns Number of columns to generate
  83. * @returns {Array}
  84. */
  85. export function createEmptySpreadsheetData(rows, columns) {
  86. let data = [];
  87. let row;
  88. for (let i = 0; i < rows; i++) {
  89. row = [];
  90. for (let j = 0; j < columns; j++) {
  91. row.push('');
  92. }
  93. data.push(row);
  94. }
  95. return data;
  96. }
  97. export function translateRowsToColumns(input) {
  98. var i,
  99. ilen,
  100. j,
  101. jlen,
  102. output = [],
  103. olen = 0;
  104. for (i = 0, ilen = input.length; i < ilen; i++) {
  105. for (j = 0, jlen = input[i].length; j < jlen; j++) {
  106. if (j == olen) {
  107. output.push([]);
  108. olen++;
  109. }
  110. output[j].push(input[i][j]);
  111. }
  112. }
  113. return output;
  114. }
  115. /**
  116. * Factory that produces a function for searching methods (or any properties) which could be defined directly in
  117. * table configuration or implicitly, within cell type definition.
  118. *
  119. * For example: renderer can be defined explicitly using "renderer" property in column configuration or it can be
  120. * defined implicitly using "type" property.
  121. *
  122. * Methods/properties defined explicitly always takes precedence over those defined through "type".
  123. *
  124. * If the method/property is not found in an object, searching is continued recursively through prototype chain, until
  125. * it reaches the Object.prototype.
  126. *
  127. *
  128. * @param methodName {String} name of the method/property to search (i.e. 'renderer', 'validator', 'copyable')
  129. * @param allowUndefined {Boolean} [optional] if false, the search is continued if methodName has not been found in cell "type"
  130. * @returns {Function}
  131. */
  132. export function cellMethodLookupFactory(methodName, allowUndefined) {
  133. allowUndefined = typeof allowUndefined == 'undefined' ? true : allowUndefined;
  134. return function cellMethodLookup(row, col) {
  135. return (function getMethodFromProperties(properties) {
  136. if (!properties) {
  137. return; // method not found
  138. } else if (hasOwnProperty(properties, methodName) && properties[methodName] !== void 0) { // check if it is own and is not empty
  139. return properties[methodName]; // method defined directly
  140. } else if (hasOwnProperty(properties, 'type') && properties.type) { // check if it is own and is not empty
  141. var type;
  142. if (typeof properties.type != 'string') {
  143. throw new Error('Cell type must be a string ');
  144. }
  145. type = getCellType(properties.type);
  146. if (hasOwnProperty(type, methodName)) {
  147. return type[methodName]; // method defined in type.
  148. } else if (allowUndefined) {
  149. return; // method does not defined in type (eg. validator), returns undefined
  150. }
  151. }
  152. return getMethodFromProperties(Object.getPrototypeOf(properties));
  153. }(typeof row == 'number' ? this.getCellMeta(row, col) : row));
  154. };
  155. }