Przeglądaj źródła

hack to get pageX/pageY working for jquery-simulate click

Adam Shaw 11 lat temu
rodzic
commit
ec8615e526
2 zmienionych plików z 33 dodań i 1 usunięć
  1. 2 1
      karma.conf.js
  2. 31 0
      tests/lib/jquery-simulate-hacks.js

+ 2 - 1
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',

+ 31 - 0
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);