|
| 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 | + |
| 12 | +;(function( $, undefined ) { |
| 13 | + |
| 14 | +var rkeyEvent = /^key/, |
| 15 | + rmouseEvent = /^(?:mouse|contextmenu)|click/; |
| 16 | + |
| 17 | +$.fn.simulate = function( type, options ) { |
| 18 | + return this.each(function() { |
| 19 | + new $.simulate( this, type, options ); |
| 20 | + }); |
| 21 | +}; |
| 22 | + |
| 23 | +$.simulate = function( elem, type, options ) { |
| 24 | + var method = $.camelCase( "simulate-" + type ); |
| 25 | + |
| 26 | + this.target = elem; |
| 27 | + this.options = options; |
| 28 | + |
| 29 | + if ( this[ method ] ) { |
| 30 | + this[ method ](); |
| 31 | + } else { |
| 32 | + this.simulateEvent( elem, type, options ); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +$.extend( $.simulate.prototype, { |
| 37 | + simulateEvent: function( elem, type, options ) { |
| 38 | + var event = this.createEvent( type, options ); |
| 39 | + this.dispatchEvent( elem, type, event, options ); |
| 40 | + }, |
| 41 | + |
| 42 | + createEvent: function( type, options ) { |
| 43 | + if ( rkeyEvent.test( type ) ) { |
| 44 | + return this.keyEvent( type, options ); |
| 45 | + } |
| 46 | + |
| 47 | + if ( rmouseEvent.test( type ) ) { |
| 48 | + return this.mouseEvent( type, options ); |
| 49 | + } |
| 50 | + }, |
| 51 | + |
| 52 | + mouseEvent: function( type, options ) { |
| 53 | + var event, eventDoc, doc, body; |
| 54 | + options = $.extend({ |
| 55 | + bubbles: true, |
| 56 | + cancelable: (type !== "mousemove"), |
| 57 | + view: window, |
| 58 | + detail: 0, |
| 59 | + screenX: 0, |
| 60 | + screenY: 0, |
| 61 | + // TODO: default clientX/Y to a position within the target element |
| 62 | + clientX: 1, |
| 63 | + clientY: 1, |
| 64 | + ctrlKey: false, |
| 65 | + altKey: false, |
| 66 | + shiftKey: false, |
| 67 | + metaKey: false, |
| 68 | + button: 0, |
| 69 | + relatedTarget: undefined |
| 70 | + }, options ); |
| 71 | + |
| 72 | + if ( document.createEvent ) { |
| 73 | + event = document.createEvent( "MouseEvents" ); |
| 74 | + event.initMouseEvent( type, options.bubbles, options.cancelable, |
| 75 | + options.view, options.detail, |
| 76 | + options.screenX, options.screenY, options.clientX, options.clientY, |
| 77 | + options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, |
| 78 | + options.button, options.relatedTarget || document.body.parentNode ); |
| 79 | + |
| 80 | + // IE 9+ creates events with pageX and pageY set to 0. |
| 81 | + // Trying to modify the properties throws an error, |
| 82 | + // so we define getters to return the correct values. |
| 83 | + if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) { |
| 84 | + eventDoc = event.relatedTarget.ownerDocument || document; |
| 85 | + doc = eventDoc.documentElement; |
| 86 | + body = eventDoc.body; |
| 87 | + |
| 88 | + Object.defineProperty( event, "pageX", { |
| 89 | + get: function() { |
| 90 | + return options.clientX + |
| 91 | + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - |
| 92 | + ( doc && doc.clientLeft || body && body.clientLeft || 0 ); |
| 93 | + } |
| 94 | + }); |
| 95 | + Object.defineProperty( event, "pageY", { |
| 96 | + get: function() { |
| 97 | + return options.clientY + |
| 98 | + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - |
| 99 | + ( doc && doc.clientTop || body && body.clientTop || 0 ); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + } else if ( document.createEventObject ) { |
| 104 | + event = document.createEventObject(); |
| 105 | + $.extend( event, options ); |
| 106 | + // TODO: what is this mapping for? |
| 107 | + event.button = { 0:1, 1:4, 2:2 }[ event.button ] || event.button; |
| 108 | + } |
| 109 | + |
| 110 | + return event; |
| 111 | + }, |
| 112 | + |
| 113 | + keyEvent: function( type, options ) { |
| 114 | + var event; |
| 115 | + options = $.extend({ |
| 116 | + bubbles: true, |
| 117 | + cancelable: true, |
| 118 | + view: window, |
| 119 | + ctrlKey: false, |
| 120 | + altKey: false, |
| 121 | + shiftKey: false, |
| 122 | + metaKey: false, |
| 123 | + keyCode: 0, |
| 124 | + charCode: undefined |
| 125 | + }, options ); |
| 126 | + |
| 127 | + if ( document.createEvent ) { |
| 128 | + try { |
| 129 | + event = document.createEvent( "KeyEvents" ); |
| 130 | + event.initKeyEvent( type, options.bubbles, options.cancelable, options.view, |
| 131 | + options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, |
| 132 | + options.keyCode, options.charCode ); |
| 133 | + // TODO: what is this supporting? |
| 134 | + } catch( err ) { |
| 135 | + event = document.createEvent( "Events" ); |
| 136 | + event.initEvent( type, options.bubbles, options.cancelable ); |
| 137 | + $.extend( event, { |
| 138 | + view: options.view, |
| 139 | + ctrlKey: options.ctrlKey, |
| 140 | + altKey: options.altKey, |
| 141 | + shiftKey: options.shiftKey, |
| 142 | + metaKey: options.metaKey, |
| 143 | + keyCode: options.keyCode, |
| 144 | + charCode: options.charCode |
| 145 | + }); |
| 146 | + } |
| 147 | + } else if ( document.createEventObject ) { |
| 148 | + event = document.createEventObject(); |
| 149 | + $.extend( event, options ); |
| 150 | + } |
| 151 | + |
| 152 | + // TODO: can we hook into core's logic? |
| 153 | + if ( $.ui.ie || (({}).toString.call( window.opera ) === "[object Opera]") ) { |
| 154 | + // TODO: is charCode ever <0 ? Can we just use charCode || keyCode? |
| 155 | + event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode; |
| 156 | + event.charCode = undefined; |
| 157 | + } |
| 158 | + |
| 159 | + return event; |
| 160 | + }, |
| 161 | + |
| 162 | + // TODO: does this need type? Can't we just check event.type? |
| 163 | + dispatchEvent: function( elem, type, event ) { |
| 164 | + if ( elem.dispatchEvent ) { |
| 165 | + elem.dispatchEvent( event ); |
| 166 | + } else if ( elem.fireEvent ) { |
| 167 | + elem.fireEvent( "on" + type, event ); |
| 168 | + } |
| 169 | + }, |
| 170 | + |
| 171 | + simulateFocus: function() { |
| 172 | + var focusinEvent, |
| 173 | + triggered = false, |
| 174 | + element = $( this.target ); |
| 175 | + |
| 176 | + function trigger() { |
| 177 | + triggered = true; |
| 178 | + } |
| 179 | + |
| 180 | + element.bind( "focus", trigger ); |
| 181 | + element[ 0 ].focus(); |
| 182 | + |
| 183 | + if ( !triggered ) { |
| 184 | + focusinEvent = $.Event( "focusin" ); |
| 185 | + focusinEvent.preventDefault(); |
| 186 | + element.trigger( focusinEvent ); |
| 187 | + element.triggerHandler( "focus" ); |
| 188 | + } |
| 189 | + element.unbind( "focus", trigger ); |
| 190 | + }, |
| 191 | + |
| 192 | + simulateBlur: function() { |
| 193 | + var focusoutEvent, |
| 194 | + triggered = false, |
| 195 | + element = $( this.target ); |
| 196 | + |
| 197 | + function trigger() { |
| 198 | + triggered = true; |
| 199 | + } |
| 200 | + |
| 201 | + element.bind( "blur", trigger ); |
| 202 | + element[ 0 ].blur(); |
| 203 | + |
| 204 | + // blur events are async in IE |
| 205 | + setTimeout(function() { |
| 206 | + // IE won't let the blur occur if the window is inactive |
| 207 | + if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) { |
| 208 | + element[ 0 ].ownerDocument.body.focus(); |
| 209 | + } |
| 210 | + |
| 211 | + // Firefox won't trigger events if the window is inactive |
| 212 | + // IE doesn't trigger events if we had to manually focus the body |
| 213 | + if ( !triggered ) { |
| 214 | + focusoutEvent = $.Event( "focusout" ); |
| 215 | + focusoutEvent.preventDefault(); |
| 216 | + element.trigger( focusoutEvent ); |
| 217 | + element.triggerHandler( "blur" ); |
| 218 | + } |
| 219 | + element.unbind( "blur", trigger ); |
| 220 | + }, 1 ); |
| 221 | + } |
| 222 | +}); |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | +/** complex events **/ |
| 227 | + |
| 228 | +function findCenter( elem ) { |
| 229 | + var offset, |
| 230 | + document = $( elem.ownerDocument ); |
| 231 | + elem = $( elem ); |
| 232 | + offset = elem.offset(); |
| 233 | + |
| 234 | + return { |
| 235 | + x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(), |
| 236 | + y: offset.top + elem.outerHeight() / 2 - document.scrollTop() |
| 237 | + }; |
| 238 | +} |
| 239 | + |
| 240 | +$.extend( $.simulate.prototype, { |
| 241 | + simulateDrag: function() { |
| 242 | + var target = this.target, |
| 243 | + options = this.options, |
| 244 | + center = findCenter( target ), |
| 245 | + x = Math.floor( center.x ), |
| 246 | + y = Math.floor( center.y ), |
| 247 | + dx = options.dx || 0, |
| 248 | + dy = options.dy || 0, |
| 249 | + coord = { clientX: x, clientY: y }; |
| 250 | + this.simulateEvent( target, "mousedown", coord ); |
| 251 | + coord = { clientX: x + 1, clientY: y + 1 }; |
| 252 | + this.simulateEvent( document, "mousemove", coord ); |
| 253 | + coord = { clientX: x + dx, clientY: y + dy }; |
| 254 | + this.simulateEvent( document, "mousemove", coord ); |
| 255 | + this.simulateEvent( document, "mousemove", coord ); |
| 256 | + this.simulateEvent( target, "mouseup", coord ); |
| 257 | + this.simulateEvent( target, "click", coord ); |
| 258 | + } |
| 259 | +}); |
| 260 | + |
| 261 | +})( jQuery ); |
0 commit comments