jquery.simulate.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*!
  2. * jQuery Simulate v@VERSION - simulate browser mouse and keyboard events
  3. * https://github.com/jquery/jquery-simulate
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * Date: @DATE
  10. */
  11. ;(function( $, undefined ) {
  12. var rkeyEvent = /^key/,
  13. rmouseEvent = /^(?:mouse|contextmenu)|click/;
  14. $.fn.simulate = function( type, options ) {
  15. return this.each(function() {
  16. new $.simulate( this, type, options );
  17. });
  18. };
  19. $.simulate = function( elem, type, options ) {
  20. var method = $.camelCase( "simulate-" + type );
  21. this.target = elem;
  22. this.options = options;
  23. if ( this[ method ] ) {
  24. this[ method ]();
  25. } else {
  26. this.simulateEvent( elem, type, options );
  27. }
  28. };
  29. $.extend( $.simulate, {
  30. keyCode: {
  31. BACKSPACE: 8,
  32. COMMA: 188,
  33. DELETE: 46,
  34. DOWN: 40,
  35. END: 35,
  36. ENTER: 13,
  37. ESCAPE: 27,
  38. HOME: 36,
  39. LEFT: 37,
  40. NUMPAD_ADD: 107,
  41. NUMPAD_DECIMAL: 110,
  42. NUMPAD_DIVIDE: 111,
  43. NUMPAD_ENTER: 108,
  44. NUMPAD_MULTIPLY: 106,
  45. NUMPAD_SUBTRACT: 109,
  46. PAGE_DOWN: 34,
  47. PAGE_UP: 33,
  48. PERIOD: 190,
  49. RIGHT: 39,
  50. SPACE: 32,
  51. TAB: 9,
  52. UP: 38
  53. },
  54. buttonCode: {
  55. LEFT: 0,
  56. MIDDLE: 1,
  57. RIGHT: 2
  58. }
  59. });
  60. $.extend( $.simulate.prototype, {
  61. simulateEvent: function( elem, type, options ) {
  62. var event = this.createEvent( type, options );
  63. this.dispatchEvent( elem, type, event, options );
  64. },
  65. createEvent: function( type, options ) {
  66. if ( rkeyEvent.test( type ) ) {
  67. return this.keyEvent( type, options );
  68. }
  69. if ( rmouseEvent.test( type ) ) {
  70. return this.mouseEvent( type, options );
  71. }
  72. },
  73. mouseEvent: function( type, options ) {
  74. var event, eventDoc, doc, body;
  75. options = $.extend({
  76. bubbles: true,
  77. cancelable: (type !== "mousemove"),
  78. view: window,
  79. detail: 0,
  80. screenX: 0,
  81. screenY: 0,
  82. clientX: 1,
  83. clientY: 1,
  84. ctrlKey: false,
  85. altKey: false,
  86. shiftKey: false,
  87. metaKey: false,
  88. button: 0,
  89. relatedTarget: undefined
  90. }, options );
  91. if ( document.createEvent ) {
  92. event = document.createEvent( "MouseEvents" );
  93. event.initMouseEvent( type, options.bubbles, options.cancelable,
  94. options.view, options.detail,
  95. options.screenX, options.screenY, options.clientX, options.clientY,
  96. options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
  97. options.button, options.relatedTarget || document.body.parentNode );
  98. // IE 9+ creates events with pageX and pageY set to 0.
  99. // Trying to modify the properties throws an error,
  100. // so we define getters to return the correct values.
  101. if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) {
  102. eventDoc = event.relatedTarget.ownerDocument || document;
  103. doc = eventDoc.documentElement;
  104. body = eventDoc.body;
  105. Object.defineProperty( event, "pageX", {
  106. get: function() {
  107. return options.clientX +
  108. ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
  109. ( doc && doc.clientLeft || body && body.clientLeft || 0 );
  110. }
  111. });
  112. Object.defineProperty( event, "pageY", {
  113. get: function() {
  114. return options.clientY +
  115. ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
  116. ( doc && doc.clientTop || body && body.clientTop || 0 );
  117. }
  118. });
  119. }
  120. } else if ( document.createEventObject ) {
  121. event = document.createEventObject();
  122. $.extend( event, options );
  123. // standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
  124. // old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
  125. // so we actually need to map the standard back to oldIE
  126. event.button = {
  127. 0: 1,
  128. 1: 4,
  129. 2: 2
  130. }[ event.button ] || ( event.button === -1 ? 0 : event.button );
  131. }
  132. return event;
  133. },
  134. keyEvent: function( type, options ) {
  135. var event;
  136. options = $.extend({
  137. bubbles: true,
  138. cancelable: true,
  139. view: window,
  140. ctrlKey: false,
  141. altKey: false,
  142. shiftKey: false,
  143. metaKey: false,
  144. keyCode: 0,
  145. charCode: undefined
  146. }, options );
  147. if ( document.createEvent ) {
  148. try {
  149. event = document.createEvent( "KeyEvents" );
  150. event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
  151. options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
  152. options.keyCode, options.charCode );
  153. // initKeyEvent throws an exception in WebKit
  154. // see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
  155. // and also https://bugs.webkit.org/show_bug.cgi?id=13368
  156. // fall back to a generic event until we decide to implement initKeyboardEvent
  157. } catch( err ) {
  158. event = document.createEvent( "Events" );
  159. event.initEvent( type, options.bubbles, options.cancelable );
  160. $.extend( event, {
  161. view: options.view,
  162. ctrlKey: options.ctrlKey,
  163. altKey: options.altKey,
  164. shiftKey: options.shiftKey,
  165. metaKey: options.metaKey,
  166. keyCode: options.keyCode,
  167. charCode: options.charCode
  168. });
  169. }
  170. } else if ( document.createEventObject ) {
  171. event = document.createEventObject();
  172. $.extend( event, options );
  173. }
  174. if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
  175. event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
  176. event.charCode = undefined;
  177. }
  178. return event;
  179. },
  180. dispatchEvent: function( elem, type, event ) {
  181. if ( elem.dispatchEvent ) {
  182. elem.dispatchEvent( event );
  183. } else if ( type === "click" && elem.click && elem.nodeName.toLowerCase() === "input" ) {
  184. elem.click();
  185. } else if ( elem.fireEvent ) {
  186. elem.fireEvent( "on" + type, event );
  187. }
  188. },
  189. simulateFocus: function() {
  190. var focusinEvent,
  191. triggered = false,
  192. element = $( this.target );
  193. function trigger() {
  194. triggered = true;
  195. }
  196. element.bind( "focus", trigger );
  197. element[ 0 ].focus();
  198. if ( !triggered ) {
  199. focusinEvent = $.Event( "focusin" );
  200. focusinEvent.preventDefault();
  201. element.trigger( focusinEvent );
  202. element.triggerHandler( "focus" );
  203. }
  204. element.unbind( "focus", trigger );
  205. },
  206. simulateBlur: function() {
  207. var focusoutEvent,
  208. triggered = false,
  209. element = $( this.target );
  210. function trigger() {
  211. triggered = true;
  212. }
  213. element.bind( "blur", trigger );
  214. element[ 0 ].blur();
  215. // blur events are async in IE
  216. setTimeout(function() {
  217. // IE won't let the blur occur if the window is inactive
  218. if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
  219. element[ 0 ].ownerDocument.body.focus();
  220. }
  221. // Firefox won't trigger events if the window is inactive
  222. // IE doesn't trigger events if we had to manually focus the body
  223. if ( !triggered ) {
  224. focusoutEvent = $.Event( "focusout" );
  225. focusoutEvent.preventDefault();
  226. element.trigger( focusoutEvent );
  227. element.triggerHandler( "blur" );
  228. }
  229. element.unbind( "blur", trigger );
  230. }, 1 );
  231. }
  232. });
  233. /** complex events **/
  234. function findCenter( elem ) {
  235. var offset,
  236. document = $( elem.ownerDocument );
  237. elem = $( elem );
  238. offset = elem.offset();
  239. return {
  240. x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
  241. y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
  242. };
  243. }
  244. function findCorner( elem ) {
  245. var offset,
  246. document = $( elem.ownerDocument );
  247. elem = $( elem );
  248. offset = elem.offset();
  249. return {
  250. x: offset.left - document.scrollLeft(),
  251. y: offset.top - document.scrollTop()
  252. };
  253. }
  254. $.extend( $.simulate.prototype, {
  255. simulateDrag: function() {
  256. var i = 0,
  257. target = this.target,
  258. eventDoc = target.ownerDocument,
  259. options = this.options,
  260. center = options.handle === "corner" ? findCorner( target ) : findCenter( target ),
  261. x = Math.floor( center.x ),
  262. y = Math.floor( center.y ),
  263. coord = { clientX: x, clientY: y },
  264. dx = options.dx || ( options.x !== undefined ? options.x - x : 0 ),
  265. dy = options.dy || ( options.y !== undefined ? options.y - y : 0 ),
  266. moves = options.moves || 3;
  267. this.simulateEvent( target, "mousedown", coord );
  268. for ( ; i < moves ; i++ ) {
  269. x += dx / moves;
  270. y += dy / moves;
  271. coord = {
  272. clientX: Math.round( x ),
  273. clientY: Math.round( y )
  274. };
  275. this.simulateEvent( eventDoc, "mousemove", coord );
  276. }
  277. if ( $.contains( eventDoc, target ) ) {
  278. this.simulateEvent( target, "mouseup", coord );
  279. this.simulateEvent( target, "click", coord );
  280. } else {
  281. this.simulateEvent( eventDoc, "mouseup", coord );
  282. }
  283. }
  284. });
  285. })( jQuery );