2 * jQuery Simulate v1.0.0 - simulate browser mouse and keyboard events
3 * https://github.com/jquery/jquery-simulate
5 * Copyright 2012 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
12 ;(function( $, undefined ) {
14 var rkeyEvent = /^key/,
15 rmouseEvent = /^(?:mouse|contextmenu)|click/;
17 $.fn.simulate = function( type, options ) {
18 return this.each(function() {
19 new $.simulate( this, type, options );
23 $.simulate = function( elem, type, options ) {
24 var method = $.camelCase( "simulate-" + type );
27 this.options = options;
29 if ( this[ method ] ) {
32 this.simulateEvent( elem, type, options );
36 $.extend( $.simulate, {
70 $.extend( $.simulate.prototype, {
72 simulateEvent: function( elem, type, options ) {
73 var event = this.createEvent( type, options );
74 this.dispatchEvent( elem, type, event, options );
77 createEvent: function( type, options ) {
78 if ( rkeyEvent.test( type ) ) {
79 return this.keyEvent( type, options );
82 if ( rmouseEvent.test( type ) ) {
83 return this.mouseEvent( type, options );
87 mouseEvent: function( type, options ) {
88 var event, eventDoc, doc, body;
91 cancelable: (type !== "mousemove"),
103 relatedTarget: undefined
106 if ( document.createEvent ) {
107 event = document.createEvent( "MouseEvents" );
108 event.initMouseEvent( type, options.bubbles, options.cancelable,
109 options.view, options.detail,
110 options.screenX, options.screenY, options.clientX, options.clientY,
111 options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
112 options.button, options.relatedTarget || document.body.parentNode );
114 // IE 9+ creates events with pageX and pageY set to 0.
115 // Trying to modify the properties throws an error,
116 // so we define getters to return the correct values.
117 if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) {
118 eventDoc = event.relatedTarget.ownerDocument || document;
119 doc = eventDoc.documentElement;
120 body = eventDoc.body;
122 Object.defineProperty( event, "pageX", {
124 return options.clientX +
125 ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
126 ( doc && doc.clientLeft || body && body.clientLeft || 0 );
129 Object.defineProperty( event, "pageY", {
131 return options.clientY +
132 ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
133 ( doc && doc.clientTop || body && body.clientTop || 0 );
137 } else if ( document.createEventObject ) {
138 event = document.createEventObject();
139 $.extend( event, options );
140 // standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
141 // old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
142 // so we actually need to map the standard back to oldIE
147 }[ event.button ] || ( event.button === -1 ? 0 : event.button );
153 keyEvent: function( type, options ) {
167 if ( document.createEvent ) {
169 event = document.createEvent( "KeyEvents" );
170 event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
171 options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
172 options.keyCode, options.charCode );
173 // initKeyEvent throws an exception in WebKit
174 // see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
175 // and also https://bugs.webkit.org/show_bug.cgi?id=13368
176 // fall back to a generic event until we decide to implement initKeyboardEvent
178 event = document.createEvent( "Events" );
179 event.initEvent( type, options.bubbles, options.cancelable );
182 ctrlKey: options.ctrlKey,
183 altKey: options.altKey,
184 shiftKey: options.shiftKey,
185 metaKey: options.metaKey,
186 keyCode: options.keyCode,
187 charCode: options.charCode
190 } else if ( document.createEventObject ) {
191 event = document.createEventObject();
192 $.extend( event, options );
195 if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
196 event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
197 event.charCode = undefined;
203 dispatchEvent: function( elem, type, event ) {
204 if ( elem[ type ] ) {
206 } else if ( elem.dispatchEvent ) {
207 elem.dispatchEvent( event );
208 } else if ( elem.fireEvent ) {
209 elem.fireEvent( "on" + type, event );
213 simulateFocus: function() {
216 element = $( this.target );
222 element.bind( "focus", trigger );
223 element[ 0 ].focus();
226 focusinEvent = $.Event( "focusin" );
227 focusinEvent.preventDefault();
228 element.trigger( focusinEvent );
229 element.triggerHandler( "focus" );
231 element.unbind( "focus", trigger );
234 simulateBlur: function() {
237 element = $( this.target );
243 element.bind( "blur", trigger );
246 // blur events are async in IE
247 setTimeout(function() {
248 // IE won't let the blur occur if the window is inactive
249 if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
250 element[ 0 ].ownerDocument.body.focus();
253 // Firefox won't trigger events if the window is inactive
254 // IE doesn't trigger events if we had to manually focus the body
256 focusoutEvent = $.Event( "focusout" );
257 focusoutEvent.preventDefault();
258 element.trigger( focusoutEvent );
259 element.triggerHandler( "blur" );
261 element.unbind( "blur", trigger );
268 /** complex events **/
270 function findCenter( elem ) {
272 document = $( elem.ownerDocument );
274 offset = elem.offset();
277 x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
278 y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
282 function findCorner( elem ) {
284 document = $( elem.ownerDocument );
286 offset = elem.offset();
289 x: offset.left - document.scrollLeft(),
290 y: offset.top - document.scrollTop()
294 $.extend( $.simulate.prototype, {
295 simulateDrag: function() {
297 target = this.target,
298 options = this.options,
299 center = options.handle === "corner" ? findCorner( target ) : findCenter( target ),
300 x = Math.floor( center.x ),
301 y = Math.floor( center.y ),
302 coord = { clientX: x, clientY: y },
303 dx = options.dx || ( options.x !== undefined ? options.x - x : 0 ),
304 dy = options.dy || ( options.y !== undefined ? options.y - y : 0 ),
305 moves = options.moves || 3;
307 this.simulateEvent( target, "mousedown", coord );
309 for ( ; i < moves ; i++ ) {
314 clientX: Math.round( x ),
315 clientY: Math.round( y )
318 this.simulateEvent( target.ownerDocument, "mousemove", coord );
321 if ( $.contains( document, target ) ) {
322 this.simulateEvent( target, "mouseup", coord );
323 this.simulateEvent( target, "click", coord );
325 this.simulateEvent( document, "mouseup", coord );