simulate.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. (function($) {
  2. /* General Utils
  3. ----------------------------------------------------------------------------------------------------------------------*/
  4. $.simulateByPoint = function(type, options) {
  5. var docEl = $(document);
  6. var point = options.point;
  7. var clientX, clientY;
  8. var node;
  9. if (point) {
  10. clientX = point.left - docEl.scrollLeft();
  11. clientY = point.top - docEl.scrollTop();
  12. node = document.elementFromPoint(clientX, clientY);
  13. $(node).simulate(type, options);
  14. }
  15. };
  16. /* Touch
  17. ----------------------------------------------------------------------------------------------------------------------*/
  18. var origSimulateEvent = $.simulate.prototype.simulateEvent;
  19. var touchUID = Date.now();
  20. $.simulate.prototype.simulateEvent = function(elem, type, options) {
  21. if (/^touch/.test(type)) {
  22. return this.simulateTouchEvent(elem, type, options);
  23. }
  24. else {
  25. return origSimulateEvent.apply(this, arguments);
  26. }
  27. };
  28. $.simulate.prototype.simulateTouchEvent = function(elem, type, options) {
  29. // http://stackoverflow.com/a/29019278/96342
  30. var event = document.createEvent('Event');
  31. event.initEvent(type, true, true); // cancelable, bubbleable
  32. event.touches = [{
  33. target: elem,
  34. identifier: touchUID++,
  35. pageX: options.clientX,
  36. pageY: options.clientY,
  37. screenX: options.clientX,
  38. screenY: options.clientY,
  39. clientX: options.clientX,
  40. clientY: options.clientY
  41. }];
  42. this.dispatchEvent(elem, type, event, options);
  43. };
  44. $.simulateTouchClick = function(elem) {
  45. var $elem = $(elem);
  46. var clientCoords = {
  47. clientX: $elem.offset().left + $elem.outerWidth() / 2,
  48. clientY: $elem.offset().top + $elem.outerHeight() / 2
  49. };
  50. $elem.simulate('touchstart', clientCoords);
  51. $elem.simulate('touchend', clientCoords);
  52. $elem.simulate('mousemove', clientCoords);
  53. $elem.simulate('mousedown', clientCoords);
  54. $elem.simulate('mouseup', clientCoords);
  55. $elem.simulate('click', clientCoords);
  56. };
  57. /* Drag-n-drop
  58. ----------------------------------------------------------------------------------------------------------------------*/
  59. var DEBUG_DELAY = 500;
  60. var DEBUG_MIN_DURATION = 2000;
  61. var DEBUG_MIN_MOVES = 100;
  62. var DRAG_DEFAULTS = {
  63. point: null, // the start point
  64. localPoint: { left: '50%', top: '50%' },
  65. end: null, // can be a point or an el
  66. localEndPoint: { left: '50%', top: '50%' },
  67. dx: 0,
  68. dy: 0,
  69. moves: 5,
  70. duration: 100 // ms
  71. };
  72. var dragStackCnt = 0;
  73. $.simulate.prototype.simulateDrag = function() {
  74. var targetNode = this.target;
  75. var targetEl = $(targetNode);
  76. var options = $.extend({}, DRAG_DEFAULTS, this.options);
  77. var dx = options.dx;
  78. var dy = options.dy;
  79. var duration = options.duration;
  80. var moves = options.moves;
  81. var startPoint;
  82. var endEl;
  83. var endPoint;
  84. var localPoint;
  85. var offset;
  86. // compute start point
  87. if (options.point) {
  88. startPoint = options.point;
  89. }
  90. else {
  91. localPoint = normalizeElPoint(options.localPoint, targetEl);
  92. offset = targetEl.offset();
  93. startPoint = {
  94. left: offset.left + localPoint.left,
  95. top: offset.top + localPoint.top
  96. };
  97. }
  98. // compute end point
  99. if (options.end) {
  100. if (isPoint(options.end)) {
  101. endPoint = options.end;
  102. }
  103. else { // assume options.end is an element
  104. endEl = $(options.end);
  105. localPoint = normalizeElPoint(options.localEndPoint, endEl);
  106. offset = endEl.offset();
  107. endPoint = {
  108. left: offset.left + localPoint.left,
  109. top: offset.top + localPoint.top
  110. };
  111. }
  112. }
  113. if (endPoint) {
  114. dx = endPoint.left - startPoint.left;
  115. dy = endPoint.top - startPoint.top;
  116. }
  117. moves = Math.max(moves, options.debug ? DEBUG_MIN_MOVES : 1);
  118. duration = Math.max(duration, options.debug ? DEBUG_MIN_DURATION : 10);
  119. simulateDrag(
  120. this,
  121. targetNode,
  122. startPoint,
  123. dx,
  124. dy,
  125. moves,
  126. duration,
  127. options
  128. );
  129. };
  130. function simulateDrag(self, targetNode, startPoint, dx, dy, moveCnt, duration, options) {
  131. var debug = options.debug;
  132. var isTouch = options.isTouch;
  133. var docNode = targetNode.ownerDocument;
  134. var docEl = $(docNode);
  135. var waitTime = duration / moveCnt;
  136. var moveIndex = 0;
  137. var clientCoords;
  138. var intervalId;
  139. var dotEl;
  140. var dragId;
  141. if (debug) {
  142. dotEl = $('<div>')
  143. .css({
  144. position: 'absolute',
  145. zIndex: 99999,
  146. border: '5px solid red',
  147. borderRadius: '5px',
  148. margin: '-5px 0 0 -5px'
  149. })
  150. .appendTo('body');
  151. }
  152. function updateCoords() {
  153. var progress = moveIndex / moveCnt;
  154. var left = startPoint.left + dx * progress;
  155. var top = startPoint.top + dy * progress;
  156. clientCoords = {
  157. clientX: left - docEl.scrollLeft(),
  158. clientY: top - docEl.scrollTop()
  159. };
  160. if (debug) {
  161. dotEl.css({ left: left, top: top });
  162. }
  163. }
  164. function startDrag() {
  165. updateCoords();
  166. dragId = ++dragStackCnt;
  167. // simulate a drag-start only if another drag isn't already happening
  168. if (dragStackCnt === 1) {
  169. self.simulateEvent(targetNode, isTouch ? 'touchstart' : 'mousedown', clientCoords);
  170. }
  171. var delay = options.delay || 0;
  172. if (debug) {
  173. delay = Math.max(delay, DEBUG_DELAY);
  174. }
  175. if (delay) {
  176. setTimeout(function() {
  177. startMoving();
  178. }, delay);
  179. }
  180. else {
  181. startMoving();
  182. }
  183. }
  184. function startMoving() {
  185. intervalId = setInterval(tick, waitTime);
  186. }
  187. function tick() { // called one interval after start
  188. moveIndex++;
  189. updateCoords(); // update clientCoords before mousemove
  190. self.simulateEvent(docNode, isTouch ? 'touchmove' : 'mousemove', clientCoords);
  191. if (moveIndex >= moveCnt) {
  192. stopMoving();
  193. }
  194. }
  195. function stopMoving() {
  196. clearInterval(intervalId);
  197. if (debug) {
  198. setTimeout(function() {
  199. dotEl.remove(); // do this before calling stopDrag/callback. don't want dot picked up by elementFromPoint
  200. stopDrag();
  201. }, DEBUG_DELAY);
  202. }
  203. else {
  204. stopDrag();
  205. }
  206. }
  207. function stopDrag() { // progress at 1, coords already up to date at this point
  208. (options.onBeforeRelease || function() {})();
  209. // only simulate a drop if the current drag is still the active one.
  210. // otherwise, this means another drag has begun via onBeforeRelease.
  211. if (dragId === dragStackCnt) {
  212. if ($.contains(docNode, targetNode)) {
  213. self.simulateEvent(targetNode, isTouch ? 'touchend' : 'mouseup', clientCoords);
  214. self.simulateEvent(targetNode, 'click', clientCoords);
  215. }
  216. else {
  217. self.simulateEvent(docNode, isTouch ? 'touchend' : 'mouseup', clientCoords);
  218. }
  219. }
  220. dragStackCnt--;
  221. (options.onRelease || options.callback || function() {})(); // TODO: deprecate "callback" ?
  222. }
  223. startDrag();
  224. }
  225. function normalizeElPoint(point, el) {
  226. var left = point.left;
  227. var top = point.top;
  228. if (/%$/.test(left)) {
  229. left = parseInt(left) / 100 * el.outerWidth();
  230. }
  231. if (/%$/.test(top)) {
  232. top = parseInt(top) / 100 * el.outerHeight();
  233. }
  234. return { left: left, top: top };
  235. }
  236. function isPoint(input) {
  237. return typeof input === 'object' && // `in` operator only works on objects
  238. 'left' in input && 'top' in input;
  239. }
  240. })(jQuery);