data.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { getCellType } from './../cellTypes';
  2. import { hasOwnProperty } from './object';
  3. var COLUMN_LABEL_BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  4. var 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. var dividend = index + 1;
  13. var columnLabel = '';
  14. var modulo = void 0;
  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. var result = 0;
  30. if (label) {
  31. for (var 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() {
  46. var rows = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 100;
  47. var columns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4;
  48. var _rows = [],
  49. i,
  50. j;
  51. for (i = 0; i < rows; i++) {
  52. var row = [];
  53. for (j = 0; j < columns; j++) {
  54. row.push(spreadsheetColumnLabel(j) + (i + 1));
  55. }
  56. _rows.push(row);
  57. }
  58. return _rows;
  59. }
  60. /**
  61. * Creates 2D array of Excel-like values "A1", "A2", as an array of objects.
  62. *
  63. * @param {Number} rows Number of rows to generate.
  64. * @param {Number} colCount Number of columns to generate.
  65. * @returns {Array}
  66. */
  67. export function createSpreadsheetObjectData() {
  68. var rows = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 100;
  69. var colCount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4;
  70. var _rows = [],
  71. i,
  72. j;
  73. for (i = 0; i < rows; i++) {
  74. var row = {};
  75. for (j = 0; j < colCount; j++) {
  76. row['prop' + j] = spreadsheetColumnLabel(j) + (i + 1);
  77. }
  78. _rows.push(row);
  79. }
  80. return _rows;
  81. }
  82. /**
  83. * Generates an empty data object.
  84. *
  85. * @param {Number} rows Number of rows to generate.
  86. * @param {Number} columns Number of columns to generate
  87. * @returns {Array}
  88. */
  89. export function createEmptySpreadsheetData(rows, columns) {
  90. var data = [];
  91. var row = void 0;
  92. for (var i = 0; i < rows; i++) {
  93. row = [];
  94. for (var j = 0; j < columns; j++) {
  95. row.push('');
  96. }
  97. data.push(row);
  98. }
  99. return data;
  100. }
  101. export function translateRowsToColumns(input) {
  102. var i,
  103. ilen,
  104. j,
  105. jlen,
  106. output = [],
  107. olen = 0;
  108. for (i = 0, ilen = input.length; i < ilen; i++) {
  109. for (j = 0, jlen = input[i].length; j < jlen; j++) {
  110. if (j == olen) {
  111. output.push([]);
  112. olen++;
  113. }
  114. output[j].push(input[i][j]);
  115. }
  116. }
  117. return output;
  118. }
  119. /**
  120. * Factory that produces a function for searching methods (or any properties) which could be defined directly in
  121. * table configuration or implicitly, within cell type definition.
  122. *
  123. * For example: renderer can be defined explicitly using "renderer" property in column configuration or it can be
  124. * defined implicitly using "type" property.
  125. *
  126. * Methods/properties defined explicitly always takes precedence over those defined through "type".
  127. *
  128. * If the method/property is not found in an object, searching is continued recursively through prototype chain, until
  129. * it reaches the Object.prototype.
  130. *
  131. *
  132. * @param methodName {String} name of the method/property to search (i.e. 'renderer', 'validator', 'copyable')
  133. * @param allowUndefined {Boolean} [optional] if false, the search is continued if methodName has not been found in cell "type"
  134. * @returns {Function}
  135. */
  136. export function cellMethodLookupFactory(methodName, allowUndefined) {
  137. allowUndefined = typeof allowUndefined == 'undefined' ? true : allowUndefined;
  138. return function cellMethodLookup(row, col) {
  139. return function getMethodFromProperties(properties) {
  140. if (!properties) {
  141. return; // method not found
  142. } else if (hasOwnProperty(properties, methodName) && properties[methodName] !== void 0) {
  143. // check if it is own and is not empty
  144. return properties[methodName]; // method defined directly
  145. } else if (hasOwnProperty(properties, 'type') && properties.type) {
  146. // check if it is own and is not empty
  147. var type;
  148. if (typeof properties.type != 'string') {
  149. throw new Error('Cell type must be a string ');
  150. }
  151. type = getCellType(properties.type);
  152. if (hasOwnProperty(type, methodName)) {
  153. return type[methodName]; // method defined in type.
  154. } else if (allowUndefined) {
  155. return; // method does not defined in type (eg. validator), returns undefined
  156. }
  157. }
  158. return getMethodFromProperties(Object.getPrototypeOf(properties));
  159. }(typeof row == 'number' ? this.getCellMeta(row, col) : row);
  160. };
  161. }