From 61dd6dcce1e92c7184d8f657a9e6d5faefdd37a3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Ruffert?= Date: Wed, 12 Nov 2014 17:33:10 +0100 Subject: [PATCH] fix(js): consider 0 value for `pageX`, `pageY` * remove error message `Uncaught TypeError: Cannot read property '0' of undefined` if mouse position X/Y is 0 * add `get_cursor_position(e, xy)` method If you start dragging the slider and move the mouse position X/Y until it is `0`, it is counted as falsey which means `||` in [line 49](https://github.com/zurb/foundation/blob/master/js/foundation/foundat ion.slider.js#L49) and in [line 55](https://github.com/zurb/foundation/blob/master/js/foundation/foundat ion.slider.js#L55) keeps going until it gets to `e.originalEvent.touches[0].clientX/Y` where it raises an error if it is no touch device. I changed it to explicitly checking that the values are `undefined`. --- js/foundation/foundation.slider.js | 31 +++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/js/foundation/foundation.slider.js b/js/foundation/foundation.slider.js index e0807fccb..1b3252684 100644 --- a/js/foundation/foundation.slider.js +++ b/js/foundation/foundation.slider.js @@ -46,16 +46,9 @@ if (!e.pageY) { scroll_offset = window.scrollY; } - self.calculate_position(self.cache.active, (e.pageY || - e.originalEvent.clientY || - e.originalEvent.touches[0].clientY || - e.currentPoint.y) - + scroll_offset); + self.calculate_position(self.cache.active, self.get_cursor_position(e, 'y') + scroll_offset); } else { - self.calculate_position(self.cache.active, e.pageX || - e.originalEvent.clientX || - e.originalEvent.touches[0].clientX || - e.currentPoint.x); + self.calculate_position(self.cache.active, self.get_cursor_position(e, 'x')); } } }) @@ -72,6 +65,26 @@ }, 300)); }, + get_cursor_position : function(e, xy) { + var pageXY = 'page' + xy.toUpperCase(), + clientXY = 'client' + xy.toUpperCase(), + position; + + if (typeof e[pageXY] !== 'undefined') { + position = e[pageXY]; + } + else if (typeof e.originalEvent[clientXY] !== 'undefined') { + position = e.originalEvent[clientXY]; + } + else if (e.originalEvent.touches && e.originalEvent.touches[0] && typeof e.originalEvent.touches[0][clientXY] !== 'undefined') { + position = e.originalEvent.touches[0][clientXY]; + } + else if(e.currentPoint && typeof e.currentPoint[xy] !== 'undefined') { + position = e.currentPoint[xy]; + } + return position; + }, + set_active_slider : function($handle) { this.cache.active = $handle; }, -- 2.47.2