simulate.js 7.3 KB

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