From: Kevin Ball Date: Wed, 4 Jan 2017 22:02:32 +0000 (-0800) Subject: Further refactor Touch to create concise initialization entrypoint X-Git-Tag: v6.4.0-rc1~70^2 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F9601%2Fhead;p=thirdparty%2Ffoundation%2Ffoundation-sites.git Further refactor Touch to create concise initialization entrypoint --- diff --git a/js/foundation.util.touch.js b/js/foundation.util.touch.js index 9104b1589..44bc22fa9 100644 --- a/js/foundation.util.touch.js +++ b/js/foundation.util.touch.js @@ -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; @@ -81,47 +84,71 @@ }); } } - $.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