common.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. export function spec() {
  2. return currentSpec;
  3. };
  4. export function createDataArray(rows, cols) {
  5. spec().data = [];
  6. rows = typeof rows === 'number' ? rows : 100;
  7. cols = typeof cols === 'number' ? cols : 4;
  8. for (var i = 0; i < rows; i++) {
  9. var row = [];
  10. if (cols > 0) {
  11. row.push(i);
  12. for (var j = 0; j < cols - 1; j++) {
  13. /* eslint-disable no-mixed-operators */
  14. /* eslint-disable no-bitwise */
  15. row.push(String.fromCharCode(65 + j % 20).toLowerCase() + (j / 20 | 0 || '')); // | 0 is parseInt - see http://jsperf.com/math-floor-vs-math-round-vs-parseint/18
  16. }
  17. }
  18. spec().data.push(row);
  19. }
  20. };
  21. export function getData(row, col) {
  22. return spec().data[row][col];
  23. };
  24. export function getTotalRows() {
  25. return spec().data.length;
  26. };
  27. export function getTotalColumns() {
  28. return spec().data[0] ? spec().data[0].length : 0;
  29. };
  30. var currentSpec;
  31. beforeEach(function () {
  32. currentSpec = this;
  33. var matchers = {
  34. toBeInArray: function toBeInArray() {
  35. return {
  36. compare: function compare(actual, expected) {
  37. return {
  38. pass: Array.isArray(expected) && expected.indexOf(actual) > -1
  39. };
  40. }
  41. };
  42. },
  43. toBeFunction: function toBeFunction() {
  44. return {
  45. compare: function compare(actual, expected) {
  46. return {
  47. pass: typeof actual === 'function'
  48. };
  49. }
  50. };
  51. },
  52. toBeAroundValue: function toBeAroundValue() {
  53. return {
  54. compare: function compare(actual, expected, diff) {
  55. diff = diff || 1;
  56. var pass = actual >= expected - diff && actual <= expected + diff;
  57. var message = 'Expected ' + actual + ' to be around ' + expected + ' (between ' + (expected - diff) + ' and ' + (expected + diff) + ')';
  58. if (!pass) {
  59. message = 'Expected ' + actual + ' NOT to be around ' + expected + ' (between ' + (expected - diff) + ' and ' + (expected + diff) + ')';
  60. }
  61. return {
  62. pass: pass,
  63. message: message
  64. };
  65. }
  66. };
  67. }
  68. };
  69. jasmine.addMatchers(matchers);
  70. });
  71. afterEach(function () {
  72. window.scrollTo(0, 0);
  73. });
  74. export function getTableWidth(elem) {
  75. return $(elem).outerWidth() || $(elem).find('tbody').outerWidth() || $(elem).find('thead').outerWidth(); // IE8 reports 0 as <table> offsetWidth
  76. };
  77. export function range(from, to) {
  78. if (!arguments.length) {
  79. return [];
  80. }
  81. if (arguments.length == 1) {
  82. to = from;
  83. from = 0;
  84. }
  85. if (to > from) {
  86. from = [to, to = from][0]; // one-liner for swapping two values
  87. }
  88. var result = [];
  89. while (to++ < from) {
  90. result.push(to);
  91. }
  92. return result;
  93. };
  94. /**
  95. * Rewrite all existing selections from selections[0] etc. to selections.current etc
  96. * @param instance
  97. * @returns {object} modified instance
  98. */
  99. export function shimSelectionProperties(instance) {
  100. if (instance.selections[0]) {
  101. instance.selections.current = instance.selections[0];
  102. }
  103. if (instance.selections[1]) {
  104. instance.selections.area = instance.selections[1];
  105. }
  106. if (instance.selections[2]) {
  107. instance.selections.highlight = instance.selections[2];
  108. }
  109. if (instance.selections[3]) {
  110. instance.selections.fill = instance.selections[3];
  111. }
  112. return instance;
  113. }
  114. export function getTableTopClone() {
  115. return $('.ht_clone_top');
  116. }
  117. export function getTableLeftClone() {
  118. return $('.ht_clone_left');
  119. }
  120. export function getTableCornerClone() {
  121. return $('.ht_clone_top_left_corner');
  122. }
  123. export function createSpreadsheetData(rows, columns) {
  124. var _rows = [],
  125. i,
  126. j;
  127. for (i = 0; i < rows; i++) {
  128. var row = [];
  129. for (j = 0; j < columns; j++) {
  130. row.push(spreadsheetColumnLabel(j) + (i + 1));
  131. }
  132. _rows.push(row);
  133. }
  134. return _rows;
  135. }
  136. var COLUMN_LABEL_BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  137. var COLUMN_LABEL_BASE_LENGTH = COLUMN_LABEL_BASE.length;
  138. /**
  139. * Generates spreadsheet-like column names: A, B, C, ..., Z, AA, AB, etc.
  140. *
  141. * @param {Number} index Column index.
  142. * @returns {String}
  143. */
  144. export function spreadsheetColumnLabel(index) {
  145. var dividend = index + 1;
  146. var columnLabel = '';
  147. var modulo = void 0;
  148. while (dividend > 0) {
  149. modulo = (dividend - 1) % COLUMN_LABEL_BASE_LENGTH;
  150. columnLabel = String.fromCharCode(65 + modulo) + columnLabel;
  151. dividend = parseInt((dividend - modulo) / COLUMN_LABEL_BASE_LENGTH, 10);
  152. }
  153. return columnLabel;
  154. }
  155. export function walkontableCalculateScrollbarWidth() {
  156. var inner = document.createElement('div');
  157. inner.style.height = '200px';
  158. inner.style.width = '100%';
  159. var outer = document.createElement('div');
  160. outer.style.boxSizing = 'content-box';
  161. outer.style.height = '150px';
  162. outer.style.left = '0px';
  163. outer.style.overflow = 'hidden';
  164. outer.style.position = 'absolute';
  165. outer.style.top = '0px';
  166. outer.style.width = '200px';
  167. outer.style.visibility = 'hidden';
  168. outer.appendChild(inner);
  169. (document.body || document.documentElement).appendChild(outer);
  170. var w1 = inner.offsetWidth;
  171. outer.style.overflow = 'scroll';
  172. var w2 = inner.offsetWidth;
  173. if (w1 == w2) {
  174. w2 = outer.clientWidth;
  175. }
  176. (document.body || document.documentElement).removeChild(outer);
  177. return w1 - w2;
  178. }
  179. var cachedScrollbarWidth;
  180. export function getScrollbarWidth() {
  181. if (cachedScrollbarWidth === void 0) {
  182. cachedScrollbarWidth = walkontableCalculateScrollbarWidth();
  183. }
  184. return cachedScrollbarWidth;
  185. }