common.js 5.9 KB

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