Created
September 10, 2012 02:55
-
-
Save mkuklis/3688564 to your computer and use it in GitHub Desktop.
zepto.simulate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
var matchers = { | |
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/, | |
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/ | |
}; | |
var defaults = { | |
x: 0, | |
y: 0, | |
button: 0, | |
detail: 0, | |
ctrlKey: false, | |
altKey: false, | |
shiftKey: false, | |
metaKey: false, | |
bubbles: true, | |
cancelable: true | |
}; | |
function findEventType(eventName, matchers) { | |
for (var name in matchers) { | |
if (matchers[name].test(eventName)) { | |
return name; | |
} | |
} | |
} | |
$.fn.simulate = function (eventName, options) { | |
var event; | |
var o = $.extend(defaults, options || {}); | |
var eventType = findEventType(eventName, matchers); | |
if (!eventType) { | |
throw new SyntaxError('Event not supported'); | |
} | |
event = document.createEvent(eventType); | |
return this.each(function () { | |
if (eventType == 'html') { | |
event.initEvent(eventName, o.bubbles, o.cancelable); | |
} | |
else { | |
event.initMouseEvent( | |
eventName, | |
o.bubbles, | |
o.cancelable, | |
document.defaultView, | |
o.detail, | |
o.x, | |
o.y, | |
o.x, | |
o.y, | |
o.ctrlKey, | |
o.altKey, | |
o.shiftKey, | |
o.metaKey, | |
o.button, | |
this); | |
} | |
this.dispatchEvent(event); | |
}); | |
} | |
})(Zepto); | |
// example | |
$(document).simulate('mousemove', {x: 200, y: 200}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment