selection.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. describe('Walkontable.Selection', function () {
  2. var $table,
  3. $container,
  4. $wrapper,
  5. debug = false;
  6. beforeEach(function () {
  7. $wrapper = $('<div></div>').css({ overflow: 'hidden' });
  8. $wrapper.width(100).height(200);
  9. $container = $('<div></div>');
  10. $table = $('<table></table>'); // create a table that is not attached to document
  11. $wrapper.append($container);
  12. $container.append($table);
  13. $wrapper.appendTo('body');
  14. createDataArray();
  15. });
  16. afterEach(function () {
  17. if (!debug) {
  18. $('.wtHolder').remove();
  19. }
  20. $wrapper.remove();
  21. });
  22. it('should add/remove class to selection when cell is clicked', function () {
  23. var wt = new Walkontable.Core({
  24. table: $table[0],
  25. data: getData,
  26. totalRows: getTotalRows,
  27. totalColumns: getTotalColumns,
  28. selections: [new Walkontable.Selection({
  29. className: 'current'
  30. })],
  31. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  32. wt.selections.current.clear();
  33. wt.selections.current.add(coords);
  34. wt.draw();
  35. }
  36. });
  37. shimSelectionProperties(wt);
  38. wt.draw();
  39. var $td1 = $table.find('tbody td:eq(0)');
  40. var $td2 = $table.find('tbody td:eq(1)');
  41. $td1.simulate('mousedown');
  42. expect($td1.hasClass('current')).toEqual(true);
  43. $td2.simulate('mousedown');
  44. expect($td1.hasClass('current')).toEqual(false);
  45. expect($td2.hasClass('current')).toEqual(true);
  46. });
  47. it('should add class to selection on all overlays', function () {
  48. $wrapper.width(300).height(300);
  49. this.data = createSpreadsheetData(10, 10);
  50. var wt = new Walkontable.Core({
  51. table: $table[0],
  52. data: getData,
  53. totalRows: getTotalRows,
  54. totalColumns: getTotalColumns,
  55. selections: [new Walkontable.Selection({
  56. className: 'current'
  57. }), new Walkontable.Selection({
  58. className: 'area'
  59. })],
  60. fixedColumnsLeft: 2,
  61. fixedRowsTop: 2
  62. });
  63. shimSelectionProperties(wt);
  64. wt.selections.area.add(new Walkontable.CellCoords(1, 1));
  65. wt.selections.area.add(new Walkontable.CellCoords(1, 2));
  66. wt.selections.area.add(new Walkontable.CellCoords(2, 1));
  67. wt.selections.area.add(new Walkontable.CellCoords(2, 2));
  68. wt.draw();
  69. var tds = $wrapper.find('td:contains(B2), td:contains(B3), td:contains(C2), td:contains(C3)');
  70. expect(tds.length).toBeGreaterThan(4);
  71. for (var i = 0, ilen = tds.length; i < ilen; i++) {
  72. expect(tds[i].className).toContain('area');
  73. }
  74. });
  75. it('should not add class to selection until it is rerendered', function () {
  76. var wt = new Walkontable.Core({
  77. table: $table[0],
  78. data: getData,
  79. totalRows: getTotalRows,
  80. totalColumns: getTotalColumns,
  81. selections: [new Walkontable.Selection({
  82. className: 'current'
  83. })]
  84. });
  85. shimSelectionProperties(wt);
  86. wt.draw();
  87. wt.selections.current.add(new Walkontable.CellCoords(0, 0));
  88. var $td1 = $table.find('tbody td:eq(0)');
  89. expect($td1.hasClass('current')).toEqual(false);
  90. wt.draw();
  91. expect($td1.hasClass('current')).toEqual(true);
  92. });
  93. it('should add/remove border to selection when cell is clicked', function (done) {
  94. var wt = new Walkontable.Core({
  95. table: $table[0],
  96. data: getData,
  97. totalRows: getTotalRows,
  98. totalColumns: getTotalColumns,
  99. selections: [new Walkontable.Selection({
  100. border: {
  101. width: 1,
  102. color: 'red',
  103. style: 'solid'
  104. }
  105. })],
  106. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  107. wt.selections.current.clear();
  108. wt.selections.current.add(coords);
  109. wt.draw();
  110. }
  111. });
  112. shimSelectionProperties(wt);
  113. wt.draw();
  114. setTimeout(function () {
  115. var $td1 = $table.find('tbody tr:eq(1) td:eq(0)');
  116. var $td2 = $table.find('tbody tr:eq(2) td:eq(1)');
  117. var $top = $(wt.selections.current.getBorder(wt).top); // cheat... get border for ht_master
  118. $td1.simulate('mousedown');
  119. var pos1 = $top.position();
  120. expect(pos1.top).toBeGreaterThan(0);
  121. expect(pos1.left).toBe(0);
  122. $td2.simulate('mousedown');
  123. var pos2 = $top.position();
  124. expect(pos2.top).toBeGreaterThan(pos1.top);
  125. expect(pos2.left).toBeGreaterThan(pos1.left);
  126. done();
  127. }, 1500);
  128. });
  129. it('should add a selection that is outside of the viewport', function () {
  130. var wt = new Walkontable.Core({
  131. table: $table[0],
  132. data: getData,
  133. totalRows: getTotalRows,
  134. totalColumns: getTotalColumns,
  135. selections: [new Walkontable.Selection({
  136. border: {
  137. width: 1,
  138. color: 'red',
  139. style: 'solid'
  140. }
  141. })]
  142. });
  143. shimSelectionProperties(wt);
  144. wt.draw();
  145. wt.selections.current.add([20, 0]);
  146. expect(wt.wtTable.getCoords($table.find('tbody tr:first td:first')[0])).toEqual(new Walkontable.CellCoords(0, 0));
  147. });
  148. it('should not scroll the viewport after selection is cleared', function () {
  149. var wt = new Walkontable.Core({
  150. table: $table[0],
  151. data: getData,
  152. totalRows: getTotalRows,
  153. totalColumns: getTotalColumns,
  154. selections: [new Walkontable.Selection({
  155. border: {
  156. width: 1,
  157. color: 'red',
  158. style: 'solid'
  159. }
  160. })]
  161. });
  162. shimSelectionProperties(wt);
  163. wt.draw();
  164. wt.selections.current.add(new Walkontable.CellCoords(0, 0));
  165. wt.draw();
  166. expect(wt.wtTable.getFirstVisibleRow()).toEqual(0);
  167. wt.scrollVertical(10).draw();
  168. expect(wt.wtTable.getFirstVisibleRow()).toEqual(10);
  169. expect(wt.wtTable.getLastVisibleRow()).toBeAroundValue(17);
  170. wt.selections.current.clear();
  171. expect(wt.wtTable.getFirstVisibleRow()).toEqual(10);
  172. expect(wt.wtTable.getLastVisibleRow()).toBeAroundValue(17);
  173. });
  174. it('should clear a selection that has more than one cell', function () {
  175. var wt = new Walkontable.Core({
  176. table: $table[0],
  177. data: getData,
  178. totalRows: getTotalRows,
  179. totalColumns: getTotalColumns,
  180. selections: [new Walkontable.Selection({
  181. border: {
  182. width: 1,
  183. color: 'red',
  184. style: 'solid'
  185. }
  186. })]
  187. });
  188. shimSelectionProperties(wt);
  189. wt.draw();
  190. wt.selections.current.add(new Walkontable.CellCoords(0, 0));
  191. wt.selections.current.add(new Walkontable.CellCoords(0, 1));
  192. wt.selections.current.clear();
  193. expect(wt.selections.current.cellRange).toEqual(null);
  194. });
  195. it('should highlight cells in selected row & column', function () {
  196. $wrapper.width(300);
  197. var wt = new Walkontable.Core({
  198. table: $table[0],
  199. data: getData,
  200. totalRows: getTotalRows,
  201. totalColumns: getTotalColumns,
  202. selections: [new Walkontable.Selection({
  203. highlightRowClassName: 'highlightRow',
  204. highlightColumnClassName: 'highlightColumn'
  205. })]
  206. });
  207. shimSelectionProperties(wt);
  208. wt.draw();
  209. wt.selections.current.add(new Walkontable.CellCoords(0, 0));
  210. wt.selections.current.add(new Walkontable.CellCoords(0, 1));
  211. wt.draw(true);
  212. expect($table.find('.highlightRow').length).toEqual(2);
  213. expect($table.find('.highlightColumn').length).toEqual(wt.wtTable.getRenderedRowsCount() * 2 - 2);
  214. });
  215. it('should highlight cells in selected row & column, when same class is shared between 2 selection definitions', function () {
  216. $wrapper.width(300);
  217. var wt = new Walkontable.Core({
  218. table: $table[0],
  219. data: getData,
  220. totalRows: getTotalRows,
  221. totalColumns: getTotalColumns,
  222. selections: [new Walkontable.Selection({
  223. highlightRowClassName: 'highlightRow',
  224. highlightColumnClassName: 'highlightColumn'
  225. }), new Walkontable.Selection({
  226. highlightRowClassName: 'highlightRow',
  227. highlightColumnClassName: 'highlightColumn'
  228. })]
  229. });
  230. shimSelectionProperties(wt);
  231. wt.draw();
  232. wt.selections.current.add(new Walkontable.CellCoords(0, 0));
  233. wt.draw(true);
  234. expect($table.find('.highlightRow').length).toEqual(3);
  235. expect($table.find('.highlightColumn').length).toEqual(wt.wtTable.getRenderedRowsCount() - 1);
  236. });
  237. it('should remove highlight when selection is deselected', function () {
  238. var wt = new Walkontable.Core({
  239. table: $table[0],
  240. data: getData,
  241. totalRows: getTotalRows,
  242. totalColumns: getTotalColumns,
  243. selections: [new Walkontable.Selection({
  244. highlightRowClassName: 'highlightRow',
  245. highlightColumnClassName: 'highlightColumn'
  246. })]
  247. });
  248. shimSelectionProperties(wt);
  249. wt.draw();
  250. wt.selections.current.add(new Walkontable.CellCoords(0, 0));
  251. wt.selections.current.add(new Walkontable.CellCoords(0, 1));
  252. wt.draw();
  253. wt.selections.current.clear();
  254. wt.draw();
  255. expect($table.find('.highlightRow').length).toEqual(0);
  256. expect($table.find('.highlightColumn').length).toEqual(0);
  257. });
  258. it('should add/remove appropriate class to the row/column headers of selected cells', function () {
  259. $wrapper.width(300);
  260. var wt = new Walkontable.Core({
  261. table: $table[0],
  262. data: getData,
  263. totalRows: getTotalRows,
  264. totalColumns: getTotalColumns,
  265. rowHeaders: [function (row, TH) {
  266. TH.innerHTML = row + 1;
  267. }],
  268. columnHeaders: [function (row, TH) {
  269. TH.innerHTML = row + 1;
  270. }],
  271. selections: [new Walkontable.Selection({
  272. highlightRowClassName: 'highlightRow',
  273. highlightColumnClassName: 'highlightColumn'
  274. })]
  275. });
  276. shimSelectionProperties(wt);
  277. wt.draw();
  278. wt.selections.current.add(new Walkontable.CellCoords(1, 1));
  279. wt.selections.current.add(new Walkontable.CellCoords(2, 2));
  280. wt.draw();
  281. // left side:
  282. // -2 -> because one row is partially visible
  283. // right side:
  284. // *2 -> because there are 2 columns selected
  285. // +2 -> because there are the headers
  286. // -4 -> because 4 cells are selected = there are overlapping highlightRow class
  287. expect($table.find('.highlightRow').length).toEqual(wt.wtViewport.columnsVisibleCalculator.count * 2 + 2 - 4);
  288. expect($table.find('.highlightColumn').length - 2).toEqual(wt.wtViewport.rowsVisibleCalculator.count * 2 + 2 - 4);
  289. expect($table.find('.highlightColumn').length).toEqual(14);
  290. expect(getTableTopClone().find('.highlightColumn').length).toEqual(2);
  291. expect(getTableTopClone().find('.highlightRow').length).toEqual(0);
  292. expect(getTableLeftClone().find('.highlightColumn').length).toEqual(0);
  293. expect(getTableLeftClone().find('.highlightRow').length).toEqual(2);
  294. var $colHeaders = $table.find('thead tr:first-child th'),
  295. $rowHeaders = $table.find('tbody tr th:first-child');
  296. expect($colHeaders.eq(2).hasClass('highlightColumn')).toBe(true);
  297. expect($colHeaders.eq(3).hasClass('highlightColumn')).toBe(true);
  298. expect($rowHeaders.eq(1).hasClass('highlightRow')).toBe(true);
  299. expect($rowHeaders.eq(2).hasClass('highlightRow')).toBe(true);
  300. wt.selections.current.clear();
  301. wt.draw();
  302. expect($table.find('.highlightRow').length).toEqual(0);
  303. expect($table.find('.highlightColumn').length).toEqual(0);
  304. expect(getTableTopClone().find('.highlightColumn').length).toEqual(0);
  305. expect(getTableTopClone().find('.highlightRow').length).toEqual(0);
  306. expect(getTableLeftClone().find('.highlightColumn').length).toEqual(0);
  307. expect(getTableLeftClone().find('.highlightRow').length).toEqual(0);
  308. });
  309. describe('replace', function () {
  310. it('should replace range from property and return true', function () {
  311. var wt = new Walkontable.Core({
  312. table: $table[0],
  313. data: getData,
  314. totalRows: getTotalRows,
  315. totalColumns: getTotalColumns,
  316. selections: [new Walkontable.Selection({
  317. border: {
  318. width: 1,
  319. color: 'red',
  320. style: 'solid'
  321. }
  322. })]
  323. });
  324. shimSelectionProperties(wt);
  325. wt.selections.current.add(new Walkontable.CellCoords(1, 1));
  326. wt.selections.current.add(new Walkontable.CellCoords(3, 3));
  327. var result = wt.selections.current.replace(new Walkontable.CellCoords(3, 3), new Walkontable.CellCoords(4, 4));
  328. expect(result).toBe(true);
  329. expect(wt.selections.current.getCorners()).toEqual([1, 1, 4, 4]);
  330. });
  331. });
  332. });