]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
fix(js): consider 0 value for `pageX`, `pageY` 6025/head
authorAndré Ruffert <andre@andreruffert.com>
Wed, 12 Nov 2014 16:33:10 +0000 (17:33 +0100)
committerAndré Ruffert <andre@andreruffert.com>
Wed, 12 Nov 2014 16:33:10 +0000 (17:33 +0100)
* 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

index e0807fccbe450f5920eebe63c85d4a24199ee20d..1b325268403a8f2bbf371fc272d0eff505fc073a 100644 (file)
               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'));
             }
           }
         })
         }, 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;
     },