event.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. 'use strict';
  2. describe('WalkontableEvent', function () {
  3. var $table,
  4. debug = false;
  5. beforeEach(function () {
  6. $table = $('<table></table>'); // create a table that is not attached to document
  7. $table.appendTo('body');
  8. createDataArray();
  9. });
  10. afterEach(function () {
  11. if (!debug) {
  12. $('.wtHolder').remove();
  13. }
  14. });
  15. it('should call `onCellMouseDown` callback', function () {
  16. var myCoords = null,
  17. myTD = null,
  18. wt = new Walkontable.Core({
  19. table: $table[0],
  20. data: getData,
  21. totalRows: getTotalRows,
  22. totalColumns: getTotalColumns,
  23. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  24. myCoords = coords;
  25. myTD = TD;
  26. }
  27. });
  28. wt.draw();
  29. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  30. $td.simulate('mousedown');
  31. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  32. expect(myTD).toEqual($td[0]);
  33. });
  34. it('should call `onCellMouseOver` callback', function () {
  35. var myCoords = null,
  36. myTD = null,
  37. wt = new Walkontable.Core({
  38. table: $table[0],
  39. data: getData,
  40. totalRows: getTotalRows,
  41. totalColumns: getTotalColumns,
  42. onCellMouseOver: function onCellMouseOver(event, coords, TD) {
  43. myCoords = coords;
  44. myTD = TD;
  45. }
  46. });
  47. wt.draw();
  48. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  49. $td.simulate('mouseover');
  50. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  51. expect(myTD).toEqual($td[0]);
  52. });
  53. it('should call `onCellMouseOver` callback with correctly passed TD element when cell contains another table', function () {
  54. var fn = jasmine.createSpy();
  55. var wt = new Walkontable.Core({
  56. table: $table[0],
  57. data: [['<table style="width: 50px;"><tr><td class="test">TEST</td></tr></table>']],
  58. totalRows: 1,
  59. totalColumns: 1,
  60. onCellMouseOver: fn,
  61. cellRenderer: function cellRenderer(row, column, TD) {
  62. TD.innerHTML = wt.wtSettings.getSetting('data', row, column);
  63. }
  64. });
  65. wt.draw();
  66. var outerTD = $table.find('tbody td:not(td.test)');
  67. var innerTD = $table.find('tbody td.test');
  68. innerTD.simulate('mouseover');
  69. expect(fn.calls.argsFor(0)[2]).toBe(outerTD[0]);
  70. });
  71. it('should call `onCellMouseOut` callback', function () {
  72. var myCoords = null,
  73. myTD = null,
  74. wt = new Walkontable.Core({
  75. table: $table[0],
  76. data: getData,
  77. totalRows: getTotalRows,
  78. totalColumns: getTotalColumns,
  79. onCellMouseOut: function onCellMouseOut(event, coords, TD) {
  80. myCoords = coords;
  81. myTD = TD;
  82. }
  83. });
  84. wt.draw();
  85. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  86. $td.simulate('mouseover');
  87. $td.simulate('mouseout');
  88. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  89. expect(myTD).toEqual($td[0]);
  90. });
  91. it('should call `onCellMouseOut` callback with correctly passed TD element when cell contains another table', function () {
  92. var fn = jasmine.createSpy();
  93. var wt = new Walkontable.Core({
  94. table: $table[0],
  95. data: [['<table style="width: 50px;"><tr><td class="test">TEST</td></tr></table>']],
  96. totalRows: 1,
  97. totalColumns: 1,
  98. onCellMouseOut: fn,
  99. cellRenderer: function cellRenderer(row, column, TD) {
  100. TD.innerHTML = wt.wtSettings.getSetting('data', row, column);
  101. }
  102. });
  103. wt.draw();
  104. var outerTD = $table.find('tbody td:not(td.test)');
  105. var innerTD = $table.find('tbody td.test');
  106. innerTD.simulate('mouseover');
  107. innerTD.simulate('mouseout');
  108. expect(fn.calls.argsFor(0)[2]).toBe(outerTD[0]);
  109. });
  110. it('should call `onCellDblClick` callback', function () {
  111. var myCoords = null,
  112. myTD = null,
  113. wt = new Walkontable.Core({
  114. table: $table[0],
  115. data: getData,
  116. totalRows: getTotalRows,
  117. totalColumns: getTotalColumns,
  118. onCellDblClick: function onCellDblClick(event, coords, TD) {
  119. myCoords = coords;
  120. myTD = TD;
  121. }
  122. });
  123. wt.draw();
  124. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  125. $td.simulate('mousedown');
  126. $td.simulate('mouseup');
  127. $td.simulate('mousedown');
  128. $td.simulate('mouseup');
  129. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  130. expect(myTD).toEqual($td[0]);
  131. });
  132. it('should call `onCellDblClick` callback, even when it is set only after first click', function () {
  133. var myCoords = null,
  134. myTD = null,
  135. wt = new Walkontable.Core({
  136. table: $table[0],
  137. data: getData,
  138. totalRows: getTotalRows,
  139. totalColumns: getTotalColumns
  140. });
  141. wt.draw();
  142. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  143. $td.simulate('mousedown');
  144. $td.simulate('mouseup');
  145. $td.simulate('mousedown');
  146. wt.update('onCellDblClick', function (event, coords, TD) {
  147. myCoords = coords;
  148. myTD = TD;
  149. });
  150. $td.simulate('mouseup');
  151. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  152. expect(myTD).toEqual($td[0]);
  153. });
  154. it('should call `onCellMouseDown` callback when clicked on TH', function () {
  155. var called = false,
  156. wt = new Walkontable.Core({
  157. table: $table[0],
  158. data: getData,
  159. totalRows: getTotalRows,
  160. totalColumns: getTotalColumns,
  161. columnHeaders: [function (col, TH) {
  162. TH.innerHTML = col + 1;
  163. }],
  164. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  165. called = true;
  166. }
  167. });
  168. wt.draw();
  169. var $th = $table.find('th:first');
  170. $th.simulate('mousedown');
  171. expect(called).toEqual(true);
  172. });
  173. it('should not call `onCellMouseDown` callback when clicked on the focusable element (column headers)', function () {
  174. var opt = ['Maserati', 'Mazda', 'Mercedes', 'Mini', 'Mitsubishi'].map(function (opt) {
  175. return '<option value="' + opt + '">' + opt + '</option>';
  176. }).join('');
  177. var called = false;
  178. var wt = new Walkontable.Core({
  179. table: $table[0],
  180. data: getData,
  181. totalRows: getTotalRows,
  182. totalColumns: getTotalColumns,
  183. columnHeaders: [function (col, TH) {
  184. TH.innerHTML = '#' + col + '<select>' + opt + '</select>';
  185. }],
  186. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  187. called = true;
  188. }
  189. });
  190. wt.draw();
  191. var select = $table.find('th:first select');
  192. select.focus();
  193. select.simulate('mousedown');
  194. expect(called).toBe(false);
  195. });
  196. it('should not call `onCellMouseDown` callback when clicked on the focusable element (cell renderer)', function () {
  197. var opt = ['Maserati', 'Mazda', 'Mercedes', 'Mini', 'Mitsubishi'].map(function (opt) {
  198. return '<option value="' + opt + '">' + opt + '</option>';
  199. }).join('');
  200. var called = false;
  201. var wt = new Walkontable.Core({
  202. table: $table[0],
  203. data: getData,
  204. totalRows: getTotalRows,
  205. totalColumns: getTotalColumns,
  206. cellRenderer: function cellRenderer(row, column, TD) {
  207. TD.innerHTML = '<select>' + opt + '</select>';
  208. },
  209. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  210. called = true;
  211. }
  212. });
  213. wt.draw();
  214. var select = $table.find('td:first select');
  215. select.focus();
  216. select.simulate('mousedown');
  217. expect(called).toBe(false);
  218. });
  219. it('should call `onCellMouseOver` callback when clicked on TH', function () {
  220. var called = false,
  221. wt = new Walkontable.Core({
  222. table: $table[0],
  223. data: getData,
  224. totalRows: getTotalRows,
  225. totalColumns: getTotalColumns,
  226. columnHeaders: [function (col, TH) {
  227. TH.innerHTML = col + 1;
  228. }],
  229. onCellMouseOver: function onCellMouseOver(event, coords, TD) {
  230. called = coords;
  231. }
  232. });
  233. wt.draw();
  234. var $th = $table.find('th:first');
  235. $th.simulate('mouseover');
  236. expect(called.row).toEqual(-1);
  237. expect(called.col).toEqual(0);
  238. });
  239. it('should call `onCellDblClick` callback when clicked on TH', function () {
  240. var called = false,
  241. wt = new Walkontable.Core({
  242. table: $table[0],
  243. data: getData,
  244. totalRows: getTotalRows,
  245. totalColumns: getTotalColumns,
  246. columnHeaders: [function (col, TH) {
  247. TH.innerHTML = col + 1;
  248. }],
  249. onCellDblClick: function onCellDblClick(event, coords, TD) {
  250. called = true;
  251. }
  252. });
  253. wt.draw();
  254. var $th = $table.find('th:first');
  255. $th.simulate('mousedown');
  256. $th.simulate('mouseup');
  257. $th.simulate('mousedown');
  258. $th.simulate('mouseup');
  259. expect(called).toEqual(true);
  260. });
  261. it('should not call `onCellDblClick` callback when right-clicked', function () {
  262. var called = false,
  263. wt = new Walkontable.Core({
  264. table: $table[0],
  265. data: getData,
  266. totalRows: getTotalRows,
  267. totalColumns: getTotalColumns,
  268. onCellDblClick: function onCellDblClick(event, coords, TD) {
  269. called = true;
  270. }
  271. });
  272. wt.draw();
  273. var $td = $table.find('tbody tr:first td:first');
  274. var options = {
  275. button: 2
  276. };
  277. $td.simulate('mousedown', options);
  278. $td.simulate('mouseup', options);
  279. $td.simulate('mousedown', options);
  280. $td.simulate('mouseup', options);
  281. expect(called).toEqual(false);
  282. });
  283. it('should not call `onCellDblClick` when first mouse up came from mouse drag', function () {
  284. var called = false,
  285. wt = new Walkontable.Core({
  286. table: $table[0],
  287. data: getData,
  288. totalRows: getTotalRows,
  289. totalColumns: getTotalColumns,
  290. onCellDblClick: function onCellDblClick(event, coords, TD) {
  291. called = true;
  292. }
  293. });
  294. wt.draw();
  295. var $td = $table.find('tbody tr:first td:first');
  296. var $td2 = $table.find('tbody tr:first td:eq(1)');
  297. $td2.simulate('mousedown');
  298. $td.simulate('mouseup');
  299. $td.simulate('mousedown');
  300. $td.simulate('mouseup');
  301. expect(called).toEqual(false);
  302. });
  303. it('border click should call `onCellMouseDown` callback', function () {
  304. var myCoords = null,
  305. myTD = null,
  306. wt = new Walkontable.Core({
  307. table: $table[0],
  308. data: getData,
  309. totalRows: getTotalRows,
  310. totalColumns: getTotalColumns,
  311. selections: [new Walkontable.Selection({
  312. className: 'current',
  313. border: {
  314. width: 1,
  315. color: 'red',
  316. style: 'solid'
  317. }
  318. })],
  319. onCellMouseDown: function onCellMouseDown(event, coords, TD) {
  320. myCoords = coords;
  321. myTD = TD;
  322. }
  323. });
  324. shimSelectionProperties(wt);
  325. wt.selections.current.add(new Walkontable.CellCoords(1, 1));
  326. wt.draw();
  327. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  328. var $border = $table.parents('.wtHolder').find('.wtBorder:first');
  329. $border.simulate('mousedown');
  330. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  331. expect(myTD).toEqual($td[0]);
  332. });
  333. it('border click should call `onCellDblClick` callback', function () {
  334. var myCoords = null,
  335. myTD = null,
  336. wt = new Walkontable.Core({
  337. table: $table[0],
  338. data: getData,
  339. totalRows: getTotalRows,
  340. totalColumns: getTotalColumns,
  341. selections: [new Walkontable.Selection({
  342. className: 'current',
  343. border: {
  344. width: 1,
  345. color: 'red',
  346. style: 'solid'
  347. }
  348. })],
  349. onCellDblClick: function onCellDblClick(event, coords, TD) {
  350. myCoords = coords;
  351. myTD = TD;
  352. }
  353. });
  354. shimSelectionProperties(wt);
  355. wt.selections.current.add(new Walkontable.CellCoords(1, 1));
  356. wt.draw();
  357. var $td = $table.find('tbody tr:eq(1) td:eq(1)');
  358. var $border = $table.parents('.wtHolder').find('.wtBorder:first');
  359. $border.simulate('mousedown');
  360. $border.simulate('mouseup');
  361. $border.simulate('mousedown');
  362. $border.simulate('mouseup');
  363. expect(myCoords).toEqual(new Walkontable.CellCoords(1, 1));
  364. expect(myTD).toEqual($td[0]);
  365. });
  366. // corner
  367. it('should call `onCellCornerMouseDown` callback', function () {
  368. var clicked = false,
  369. wt = new Walkontable.Core({
  370. table: $table[0],
  371. data: getData,
  372. totalRows: getTotalRows,
  373. totalColumns: getTotalColumns,
  374. selections: [new Walkontable.Selection({
  375. className: 'current',
  376. border: {
  377. width: 1,
  378. color: 'red',
  379. style: 'solid'
  380. }
  381. })],
  382. onCellCornerMouseDown: function onCellCornerMouseDown(event) {
  383. clicked = true;
  384. }
  385. });
  386. shimSelectionProperties(wt);
  387. wt.selections.current.add(new Walkontable.CellCoords(10, 2));
  388. wt.draw();
  389. var $td = $table.parents('.wtHolder').find('.current.corner');
  390. $td.simulate('mousedown');
  391. expect(clicked).toEqual(true);
  392. });
  393. it('should call `onCellCornerDblClick` callback, even when it is set only after first click', function () {
  394. var clicked = false,
  395. wt = new Walkontable.Core({
  396. table: $table[0],
  397. data: getData,
  398. totalRows: getTotalRows,
  399. totalColumns: getTotalColumns,
  400. selections: [new Walkontable.Selection({
  401. className: 'current',
  402. border: {
  403. width: 1,
  404. color: 'red',
  405. style: 'solid'
  406. }
  407. })]
  408. });
  409. shimSelectionProperties(wt);
  410. wt.selections.current.add(new Walkontable.CellCoords(10, 2));
  411. wt.draw();
  412. var $td = $table.parents('.wtHolder').find('.current.corner');
  413. $td.simulate('mousedown');
  414. $td.simulate('mouseup');
  415. $td.simulate('mousedown');
  416. wt.update('onCellCornerDblClick', function (event) {
  417. clicked = true;
  418. });
  419. $td.simulate('mouseup');
  420. expect(clicked).toEqual(true);
  421. });
  422. it('should call `onDraw` callback after render', function () {
  423. var count = 0,
  424. wt = new Walkontable.Core({
  425. table: $table[0],
  426. data: getData,
  427. totalRows: getTotalRows,
  428. totalColumns: getTotalColumns,
  429. onDraw: function onDraw() {
  430. count++;
  431. }
  432. });
  433. wt.draw();
  434. expect(count).toEqual(1);
  435. });
  436. });