jquery.simulate.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*!
  2. * jQuery Simulate v@VERSION - simulate browser mouse and keyboard events
  3. * https://github.com/jquery/jquery-simulate
  4. *
  5. * Copyright 2012 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. try {
  106. Object.defineProperty( event, "pageX", {
  107. get: function() {
  108. return options.clientX +
  109. ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
  110. ( doc && doc.clientLeft || body && body.clientLeft || 0 );
  111. }
  112. });
  113. } catch (ex) {
  114. // Fix for PhantomJS 2.1
  115. event.__defineGetter__("pageX", function () {
  116. return options.clientX +
  117. ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
  118. ( doc && doc.clientLeft || body && body.clientLeft || 0 );
  119. });
  120. }
  121. try {
  122. Object.defineProperty( event, "pageY", {
  123. get: function() {
  124. return options.clientY +
  125. ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
  126. ( doc && doc.clientTop || body && body.clientTop || 0 );
  127. }
  128. });
  129. } catch (ex) {
  130. // Fix for PhantomJS 2.1
  131. event.__defineGetter__("pageY", function () {
  132. return options.clientY +
  133. ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
  134. ( doc && doc.clientTop || body && body.clientTop || 0 );
  135. });
  136. }
  137. }
  138. } else if ( document.createEventObject ) {
  139. try {
  140. event = document.createEventObject(options);
  141. } catch (e) {
  142. event = document.createEventObject();
  143. $.extend( event, options );
  144. }
  145. // standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
  146. // old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
  147. // so we actually need to map the standard back to oldIE
  148. event.button = {
  149. 0: 1,
  150. 1: 4,
  151. 2: 2
  152. }[ event.button ] || ( event.button === -1 ? 0 : event.button );
  153. }
  154. return event;
  155. },
  156. keyEvent: function( type, options ) {
  157. var event;
  158. options = $.extend({
  159. bubbles: true,
  160. cancelable: true,
  161. view: window,
  162. ctrlKey: false,
  163. altKey: false,
  164. shiftKey: false,
  165. metaKey: false,
  166. keyCode: 0,
  167. charCode: undefined
  168. }, options );
  169. if ( document.createEvent ) {
  170. try {
  171. event = document.createEvent( "KeyEvents" );
  172. event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
  173. options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
  174. options.keyCode, options.charCode );
  175. // initKeyEvent throws an exception in WebKit
  176. // see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
  177. // and also https://bugs.webkit.org/show_bug.cgi?id=13368
  178. // fall back to a generic event until we decide to implement initKeyboardEvent
  179. } catch( err ) {
  180. event = document.createEvent( "Events" );
  181. event.initEvent( type, options.bubbles, options.cancelable );
  182. $.extend( event, {
  183. view: options.view,
  184. ctrlKey: options.ctrlKey,
  185. altKey: options.altKey,
  186. shiftKey: options.shiftKey,
  187. metaKey: options.metaKey,
  188. keyCode: options.keyCode,
  189. charCode: options.charCode
  190. });
  191. }
  192. } else if ( document.createEventObject ) {
  193. event = document.createEventObject();
  194. $.extend( event, options );
  195. }
  196. if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
  197. event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
  198. event.charCode = undefined;
  199. }
  200. return event;
  201. },
  202. dispatchEvent: function( elem, type, event ) {
  203. if ( elem[ type ] ) {
  204. elem[ type ]();
  205. } else if ( elem.dispatchEvent ) {
  206. elem.dispatchEvent( event );
  207. } else if ( elem.fireEvent ) {
  208. elem.fireEvent( "on" + type, event );
  209. }
  210. },
  211. simulateFocus: function() {
  212. var focusinEvent,
  213. triggered = false,
  214. element = $( this.target );
  215. function trigger() {
  216. triggered = true;
  217. }
  218. element.bind( "focus", trigger );
  219. element[ 0 ].focus();
  220. if ( !triggered ) {
  221. focusinEvent = $.Event( "focusin" );
  222. focusinEvent.preventDefault();
  223. element.trigger( focusinEvent );
  224. element.triggerHandler( "focus" );
  225. }
  226. element.unbind( "focus", trigger );
  227. },
  228. simulateBlur: function() {
  229. var focusoutEvent,
  230. triggered = false,
  231. element = $( this.target );
  232. function trigger() {
  233. triggered = true;
  234. }
  235. element.bind( "blur", trigger );
  236. element[ 0 ].blur();
  237. // blur events are async in IE
  238. setTimeout(function() {
  239. // IE won't let the blur occur if the window is inactive
  240. if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
  241. element[ 0 ].ownerDocument.body.focus();
  242. }
  243. // Firefox won't trigger events if the window is inactive
  244. // IE doesn't trigger events if we had to manually focus the body
  245. if ( !triggered ) {
  246. focusoutEvent = $.Event( "focusout" );
  247. focusoutEvent.preventDefault();
  248. element.trigger( focusoutEvent );
  249. element.triggerHandler( "blur" );
  250. }
  251. element.unbind( "blur", trigger );
  252. }, 1 );
  253. }
  254. });
  255. /** complex events **/
  256. function findCenter( elem ) {
  257. var offset,
  258. document = $( elem.ownerDocument );
  259. elem = $( elem );
  260. offset = elem.offset();
  261. return {
  262. x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
  263. y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
  264. };
  265. }
  266. function findCorner( elem ) {
  267. var offset,
  268. document = $( elem.ownerDocument );
  269. elem = $( elem );
  270. offset = elem.offset();
  271. return {
  272. x: offset.left - document.scrollLeft(),
  273. y: offset.top - document.scrollTop()
  274. };
  275. }
  276. $.extend( $.simulate.prototype, {
  277. simulateDrag: function() {
  278. var i = 0,
  279. target = this.target,
  280. options = this.options,
  281. center = options.handle === "corner" ? findCorner( target ) : findCenter( target ),
  282. x = Math.floor( center.x ),
  283. y = Math.floor( center.y ),
  284. coord = { clientX: x, clientY: y },
  285. dx = options.dx || ( options.x !== undefined ? options.x - x : 0 ),
  286. dy = options.dy || ( options.y !== undefined ? options.y - y : 0 ),
  287. moves = options.moves || 3;
  288. this.simulateEvent( target, "mousedown", coord );
  289. for ( ; i < moves ; i++ ) {
  290. x += dx / moves;
  291. y += dy / moves;
  292. coord = {
  293. clientX: Math.round( x ),
  294. clientY: Math.round( y )
  295. };
  296. this.simulateEvent( target.ownerDocument, "mousemove", coord );
  297. }
  298. if ( $.contains( document, target ) ) {
  299. this.simulateEvent( target, "mouseup", coord );
  300. this.simulateEvent( target, "click", coord );
  301. } else {
  302. this.simulateEvent( document, "mouseup", coord );
  303. }
  304. }
  305. });
  306. })( jQuery );