events.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. var test = require('tape');
  3. var events = require('./lib/events');
  4. var dragula = require('..');
  5. test('.start() emits "cloned" for copies', function (t) {
  6. var div = document.createElement('div');
  7. var item = document.createElement('div');
  8. var drake = dragula([div], { copy: true });
  9. div.appendChild(item);
  10. document.body.appendChild(div);
  11. drake.on('cloned', cloned);
  12. drake.start(item);
  13. t.plan(3);
  14. t.end();
  15. function cloned (copy, original, type) {
  16. if (type === 'copy') {
  17. t.notEqual(copy, item, 'copy is not a reference to item');
  18. t.equal(copy.nodeType, item.nodeType, 'copy of original is provided');
  19. t.equal(original, item, 'original item is provided');
  20. }
  21. }
  22. });
  23. test('.start() emits "drag" for items', function (t) {
  24. var div = document.createElement('div');
  25. var item = document.createElement('div');
  26. var drake = dragula([div]);
  27. div.appendChild(item);
  28. document.body.appendChild(div);
  29. drake.on('drag', drag);
  30. drake.start(item);
  31. t.plan(2);
  32. t.end();
  33. function drag (original, container) {
  34. t.equal(original, item, 'item is a reference to moving target');
  35. t.equal(container, div, 'container matches expected div');
  36. }
  37. });
  38. test('.end() emits "cancel" when not moved', function (t) {
  39. var div = document.createElement('div');
  40. var item = document.createElement('div');
  41. var drake = dragula([div]);
  42. div.appendChild(item);
  43. document.body.appendChild(div);
  44. drake.on('dragend', dragend);
  45. drake.on('cancel', cancel);
  46. events.raise(item, 'mousedown', { which: 1 });
  47. events.raise(item, 'mousemove', { which: 1 });
  48. drake.end();
  49. t.plan(3);
  50. t.end();
  51. function dragend (original) {
  52. t.equal(original, item, 'item is a reference to moving target');
  53. }
  54. function cancel (original, container) {
  55. t.equal(original, item, 'item is a reference to moving target');
  56. t.equal(container, div, 'container matches expected div');
  57. }
  58. });
  59. test('.end() emits "drop" when moved', function (t) {
  60. var div = document.createElement('div');
  61. var div2 = document.createElement('div');
  62. var item = document.createElement('div');
  63. var drake = dragula([div, div2]);
  64. div.appendChild(item);
  65. document.body.appendChild(div);
  66. document.body.appendChild(div2);
  67. drake.on('dragend', dragend);
  68. drake.on('drop', drop);
  69. events.raise(item, 'mousedown', { which: 1 });
  70. events.raise(item, 'mousemove', { which: 1 });
  71. div2.appendChild(item);
  72. drake.end();
  73. t.plan(4);
  74. t.end();
  75. function dragend (original) {
  76. t.equal(original, item, 'item is a reference to moving target');
  77. }
  78. function drop (original, target, container) {
  79. t.equal(original, item, 'item is a reference to moving target');
  80. t.equal(target, div2, 'target matches expected div');
  81. t.equal(container, div, 'container matches expected div');
  82. }
  83. });
  84. test('.remove() emits "remove" for items', function (t) {
  85. var div = document.createElement('div');
  86. var item = document.createElement('div');
  87. var drake = dragula([div]);
  88. div.appendChild(item);
  89. document.body.appendChild(div);
  90. drake.on('dragend', dragend);
  91. drake.on('remove', remove);
  92. events.raise(item, 'mousedown', { which: 1 });
  93. events.raise(item, 'mousemove', { which: 1 });
  94. drake.remove();
  95. t.plan(3);
  96. t.end();
  97. function dragend (original) {
  98. t.equal(original, item, 'item is a reference to moving target');
  99. }
  100. function remove (original, container) {
  101. t.equal(original, item, 'item is a reference to moving target');
  102. t.equal(container, div, 'container matches expected div');
  103. }
  104. });
  105. test('.remove() emits "cancel" for copies', function (t) {
  106. var div = document.createElement('div');
  107. var item = document.createElement('div');
  108. var drake = dragula([div], { copy: true });
  109. div.appendChild(item);
  110. document.body.appendChild(div);
  111. drake.on('dragend', dragend);
  112. drake.on('cancel', cancel);
  113. events.raise(item, 'mousedown', { which: 1 });
  114. events.raise(item, 'mousemove', { which: 1 });
  115. drake.remove();
  116. t.plan(4);
  117. t.end();
  118. function dragend () {
  119. t.pass('dragend got invoked');
  120. }
  121. function cancel (copy, container) {
  122. t.notEqual(copy, item, 'copy is not a reference to item');
  123. t.equal(copy.nodeType, item.nodeType, 'item is a copy of item');
  124. t.equal(container, null, 'container matches expectation');
  125. }
  126. });
  127. test('.cancel() emits "cancel" when not moved', function (t) {
  128. var div = document.createElement('div');
  129. var item = document.createElement('div');
  130. var drake = dragula([div]);
  131. div.appendChild(item);
  132. document.body.appendChild(div);
  133. drake.on('dragend', dragend);
  134. drake.on('cancel', cancel);
  135. events.raise(item, 'mousedown', { which: 1 });
  136. events.raise(item, 'mousemove', { which: 1 });
  137. drake.cancel();
  138. t.plan(3);
  139. t.end();
  140. function dragend (original) {
  141. t.equal(original, item, 'item is a reference to moving target');
  142. }
  143. function cancel (original, container) {
  144. t.equal(original, item, 'item is a reference to moving target');
  145. t.equal(container, div, 'container matches expected div');
  146. }
  147. });
  148. test('.cancel() emits "drop" when not reverted', function (t) {
  149. var div = document.createElement('div');
  150. var div2 = document.createElement('div');
  151. var item = document.createElement('div');
  152. var drake = dragula([div]);
  153. div.appendChild(item);
  154. document.body.appendChild(div);
  155. document.body.appendChild(div2);
  156. drake.on('dragend', dragend);
  157. drake.on('drop', drop);
  158. events.raise(item, 'mousedown', { which: 1 });
  159. events.raise(item, 'mousemove', { which: 1 });
  160. div2.appendChild(item);
  161. drake.cancel();
  162. t.plan(4);
  163. t.end();
  164. function dragend (original) {
  165. t.equal(original, item, 'item is a reference to moving target');
  166. }
  167. function drop (original, parent, container) {
  168. t.equal(original, item, 'item is a reference to moving target');
  169. t.equal(parent, div2, 'parent matches expected div');
  170. t.equal(container, div, 'container matches expected div');
  171. }
  172. });
  173. test('.cancel() emits "cancel" when reverts', function (t) {
  174. var div = document.createElement('div');
  175. var div2 = document.createElement('div');
  176. var item = document.createElement('div');
  177. var drake = dragula([div], { revertOnSpill: true });
  178. div.appendChild(item);
  179. document.body.appendChild(div);
  180. document.body.appendChild(div2);
  181. drake.on('dragend', dragend);
  182. drake.on('cancel', cancel);
  183. events.raise(item, 'mousedown', { which: 1 });
  184. events.raise(item, 'mousemove', { which: 1 });
  185. div2.appendChild(item);
  186. drake.cancel();
  187. t.plan(3);
  188. t.end();
  189. function dragend (original) {
  190. t.equal(original, item, 'item is a reference to moving target');
  191. }
  192. function cancel (original, container) {
  193. t.equal(original, item, 'item is a reference to moving target');
  194. t.equal(container, div, 'container matches expected div');
  195. }
  196. });
  197. test('mousedown emits "cloned" for mirrors', function (t) {
  198. var div = document.createElement('div');
  199. var item = document.createElement('div');
  200. var drake = dragula([div]);
  201. div.appendChild(item);
  202. document.body.appendChild(div);
  203. drake.on('cloned', cloned);
  204. events.raise(item, 'mousedown', { which: 1 });
  205. events.raise(item, 'mousemove', { which: 1 });
  206. t.plan(3);
  207. t.end();
  208. function cloned (copy, original, type) {
  209. if (type === 'mirror') {
  210. t.notEqual(copy, item, 'mirror is not a reference to item');
  211. t.equal(copy.nodeType, item.nodeType, 'mirror of original is provided');
  212. t.equal(original, item, 'original item is provided');
  213. }
  214. }
  215. });
  216. test('mousedown emits "cloned" for copies', function (t) {
  217. var div = document.createElement('div');
  218. var item = document.createElement('div');
  219. var drake = dragula([div], { copy: true });
  220. div.appendChild(item);
  221. document.body.appendChild(div);
  222. drake.on('cloned', cloned);
  223. events.raise(item, 'mousedown', { which: 1 });
  224. events.raise(item, 'mousemove', { which: 1 });
  225. t.plan(3);
  226. t.end();
  227. function cloned (copy, original, type) {
  228. if (type === 'copy') {
  229. t.notEqual(copy, item, 'copy is not a reference to item');
  230. t.equal(copy.nodeType, item.nodeType, 'copy of original is provided');
  231. t.equal(original, item, 'original item is provided');
  232. }
  233. }
  234. });
  235. test('mousedown emits "drag" for items', function (t) {
  236. var div = document.createElement('div');
  237. var item = document.createElement('div');
  238. var drake = dragula([div]);
  239. div.appendChild(item);
  240. document.body.appendChild(div);
  241. drake.on('drag', drag);
  242. events.raise(item, 'mousedown', { which: 1 });
  243. events.raise(item, 'mousemove', { which: 1 });
  244. t.plan(2);
  245. t.end();
  246. function drag (original, container) {
  247. t.equal(original, item, 'item is a reference to moving target');
  248. t.equal(container, div, 'container matches expected div');
  249. }
  250. });