diff --git a/karma.conf.js b/karma.conf.js index 4e19963..29f9a7f 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -14,13 +14,14 @@ module.exports = function(config) { 'lib/jquery/jquery.js', 'lib/jquery-ui/ui/jquery-ui.js', - // for jquery-simulate-ext + // for jquery simulate 'lib/jquery-simulate-ext/libs/bililiteRange.js', 'lib/jquery-simulate-ext/libs/jquery.simulate.js', 'lib/jquery-simulate-ext/src/jquery.simulate.ext.js', 'lib/jquery-simulate-ext/src/jquery.simulate.drag-n-drop.js', 'lib/jquery-simulate-ext/src/jquery.simulate.key-sequence.js', 'lib/jquery-simulate-ext/src/jquery.simulate.key-combo.js', + 'tests/lib/jquery-simulate-hacks.js', // needs to be last 'lib/jquery-mockjax/jquery.mockjax.js', 'lib/jasmine-jquery/lib/jasmine-jquery.js', diff --git a/tests/lib/jquery-simulate-hacks.js b/tests/lib/jquery-simulate-hacks.js new file mode 100644 index 0000000..c006874 --- /dev/null +++ b/tests/lib/jquery-simulate-hacks.js @@ -0,0 +1,31 @@ +(function($) { + + /* + Addresses a shortcoming with jquery-simulate[-ext] where a click event doesn't + provide pageX/pageY coordinates. Should be the centered coordinates of the element. + This file needs to be loaded after jquery-simulate and jquery-simulate-ext. + */ + + var originalMouseEvent = $.simulate.prototype.mouseEvent; + + $.simulate.prototype.mouseEvent = function(type, options) { + if (type === 'click' && options.pageX === undefined && options.pageY === undefined) { + $.extend(options, findCenterPageXY(this.target)); + } + // after this, jquery-simulate-ext will calculate screenX/screenY + // and jquery-simulate will calculate clientX/clientY... + return originalMouseEvent.apply(this, [ type, options ]); + }; + + function findCenterPageXY( elem ) { + var offset, + $elem = $(elem), + offset = $elem.offset(); + + return { + pageX: Math.round(offset.left + $elem.outerWidth() / 2), + pageY: Math.round(offset.top + $elem.outerHeight() / 2) + }; + } + +})(jQuery); \ No newline at end of file