1a35103abc3ec837ea365efd434e6c822976de360d698829ad38d0a663915eb7cf82d7ffc1158332c203ffa6f955d76f1829825a2c0d420545aed6d85a2647 9.6 KB

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