6a0926da4b00f62431ba9148fc50bc5abe6194e3d2057369b5a200c6c093ffdc5b86f40ec3e5596dbae1840620323a279326da85905f080b10fd4b5038e3e7 12 KB

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