]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Further refactor Touch to create concise initialization entrypoint 9601/head
authorKevin Ball <kmball11@gmail.com>
Wed, 4 Jan 2017 22:02:32 +0000 (14:02 -0800)
committerKevin Ball <kmball11@gmail.com>
Wed, 4 Jan 2017 22:02:32 +0000 (14:02 -0800)
js/foundation.util.touch.js

index 9104b1589cac2064c27c9a0b27f0853d9c15c7d8..44bc22fa9d36db422db6e4bc6be4c6d8ed2078a6 100644 (file)
@@ -2,6 +2,9 @@
 //**Work inspired by multiple jquery swipe plugins**
 //**Done by Yohai Ararat ***************************
 //**************************************************
+
+class Touch {}
+
 (function($) {
 
   var startPosX,
@@ -59,7 +62,7 @@
     this.removeEventListener('touchstart', onTouchStart);
   }
 
-  class spotSwipe {
+  class SpotSwipe {
     constructor($) {
       this.version = '1.0.0';
       this.enabled = 'ontouchstart' in document.documentElement;
       });
     }
   }
-  $.spotSwipe = new spotSwipe($);
-})(jQuery);
-/****************************************************
- * Method for adding psuedo drag events to elements *
- ***************************************************/
-!function($){
-  $.fn.addTouch = function(){
-    this.each(function(i,el){
-      $(el).bind('touchstart touchmove touchend touchcancel',function(){
-        //we pass the original event object because the jQuery event
-        //object is normalized to w3c specs and does not provide the TouchList
-        handleTouch(event);
-      });
-    });
-
-    var handleTouch = function(event){
-      var touches = event.changedTouches,
-          first = touches[0],
-          eventTypes = {
-            touchstart: 'mousedown',
-            touchmove: 'mousemove',
-            touchend: 'mouseup'
-          },
-          type = eventTypes[event.type],
-          simulatedEvent
-        ;
-
-      if('MouseEvent' in window && typeof window.MouseEvent === 'function') {
-        simulatedEvent = new window.MouseEvent(type, {
-          'bubbles': true,
-          'cancelable': true,
-          'screenX': first.screenX,
-          'screenY': first.screenY,
-          'clientX': first.clientX,
-          'clientY': first.clientY
+
+  /****************************************************
+   * As far as I can tell, both setupSpotSwipe and    *
+   * setupTouchHandler should be idempotent,          *
+   * because they directly replace functions &        *
+   * values, and do not add event handlers directly.  *
+   ****************************************************/
+
+  Touch.setupSpotSwipe = function($) {
+    $.spotSwipe = new SpotSwipe($);
+  };
+
+  /****************************************************
+   * Method for adding psuedo drag events to elements *
+   ***************************************************/
+  Touch.setupTouchHandler = function($) {
+    $.fn.addTouch = function(){
+      this.each(function(i,el){
+        $(el).bind('touchstart touchmove touchend touchcancel',function(){
+          //we pass the original event object because the jQuery event
+          //object is normalized to w3c specs and does not provide the TouchList
+          handleTouch(event);
         });
-      } else {
-        simulatedEvent = document.createEvent('MouseEvent');
-        simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
-      }
-      first.target.dispatchEvent(simulatedEvent);
+      });
+
+      var handleTouch = function(event){
+        var touches = event.changedTouches,
+            first = touches[0],
+            eventTypes = {
+              touchstart: 'mousedown',
+              touchmove: 'mousemove',
+              touchend: 'mouseup'
+            },
+            type = eventTypes[event.type],
+            simulatedEvent
+          ;
+
+        if('MouseEvent' in window && typeof window.MouseEvent === 'function') {
+          simulatedEvent = new window.MouseEvent(type, {
+            'bubbles': true,
+            'cancelable': true,
+            'screenX': first.screenX,
+            'screenY': first.screenY,
+            'clientX': first.clientX,
+            'clientY': first.clientY
+          });
+        } else {
+          simulatedEvent = document.createEvent('MouseEvent');
+          simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
+        }
+        first.target.dispatchEvent(simulatedEvent);
+      };
     };
   };
-}(jQuery);
+
+  Touch.init = function($) {
+    Touch.setupSpotSwipe($);
+    Touch.setupTouchHandler($);
+  };
+
+
+  // TODO: When we fully modularize, we should remove this and call it explicitly
+  // after import
+  Touch.init($);
+
+})(jQuery);
+
+//export default Touch