//**Work inspired by multiple jquery swipe plugins**
//**Done by Yohai Ararat ***************************
//**************************************************
-(function($) {
- $.spotSwipe = {
- version: '1.0.0',
- enabled: 'ontouchstart' in document.documentElement,
- preventDefault: false,
- moveThreshold: 75,
- timeThreshold: 200
- };
+import $ from 'jquery';
- var startPosX,
- startPosY,
- startTime,
- elapsedTime,
- isMoving = false;
+var Touch = {};
- function onTouchEnd() {
- // alert(this);
- this.removeEventListener('touchmove', onTouchMove);
- this.removeEventListener('touchend', onTouchEnd);
+var startPosX,
+ startPosY,
+ startTime,
+ elapsedTime,
isMoving = false;
- }
- function onTouchMove(e) {
- if ($.spotSwipe.preventDefault) { e.preventDefault(); }
- if(isMoving) {
- var x = e.touches[0].pageX;
- var y = e.touches[0].pageY;
- var dx = startPosX - x;
- var dy = startPosY - y;
- var dir;
- elapsedTime = new Date().getTime() - startTime;
- if(Math.abs(dx) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
- dir = dx > 0 ? 'left' : 'right';
- }
- // else if(Math.abs(dy) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
- // dir = dy > 0 ? 'down' : 'up';
- // }
- if(dir) {
- e.preventDefault();
- onTouchEnd.call(this);
- $(this).trigger('swipe', dir).trigger(`swipe${dir}`);
- }
+function onTouchEnd() {
+ // alert(this);
+ this.removeEventListener('touchmove', onTouchMove);
+ this.removeEventListener('touchend', onTouchEnd);
+ isMoving = false;
+}
+
+function onTouchMove(e) {
+ if ($.spotSwipe.preventDefault) { e.preventDefault(); }
+ if(isMoving) {
+ var x = e.touches[0].pageX;
+ var y = e.touches[0].pageY;
+ var dx = startPosX - x;
+ var dy = startPosY - y;
+ var dir;
+ elapsedTime = new Date().getTime() - startTime;
+ if(Math.abs(dx) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
+ dir = dx > 0 ? 'left' : 'right';
}
- }
-
- function onTouchStart(e) {
- if (e.touches.length == 1) {
- startPosX = e.touches[0].pageX;
- startPosY = e.touches[0].pageY;
- isMoving = true;
- startTime = new Date().getTime();
- this.addEventListener('touchmove', onTouchMove, false);
- this.addEventListener('touchend', onTouchEnd, false);
+ // else if(Math.abs(dy) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
+ // dir = dy > 0 ? 'down' : 'up';
+ // }
+ if(dir) {
+ e.preventDefault();
+ onTouchEnd.call(this);
+ $(this).trigger('swipe', dir).trigger(`swipe${dir}`);
}
}
-
- function init() {
- this.addEventListener && this.addEventListener('touchstart', onTouchStart, false);
+}
+
+function onTouchStart(e) {
+ if (e.touches.length == 1) {
+ startPosX = e.touches[0].pageX;
+ startPosY = e.touches[0].pageY;
+ isMoving = true;
+ startTime = new Date().getTime();
+ this.addEventListener('touchmove', onTouchMove, false);
+ this.addEventListener('touchend', onTouchEnd, false);
}
+}
+
+function init() {
+ this.addEventListener && this.addEventListener('touchstart', onTouchStart, false);
+}
+
+function teardown() {
+ this.removeEventListener('touchstart', onTouchStart);
+}
+
+class SpotSwipe {
+ constructor($) {
+ this.version = '1.0.0';
+ this.enabled = 'ontouchstart' in document.documentElement;
+ this.preventDefault = false;
+ this.moveThreshold = 75;
+ this.timeThreshold = 200;
+ this.$ = $;
+ this._init();
+ }
+
+ _init() {
+ var $ = this.$;
+ $.event.special.swipe = { setup: init };
- function teardown() {
- this.removeEventListener('touchstart', onTouchStart);
+ $.each(['left', 'up', 'down', 'right'], function () {
+ $.event.special[`swipe${this}`] = { setup: function(){
+ $(this).on('swipe', $.noop);
+ } };
+ });
}
+}
- $.event.special.swipe = { setup: init };
+/****************************************************
+ * 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($);
+};
- $.each(['left', 'up', 'down', 'right'], function () {
- $.event.special[`swipe${this}`] = { setup: function(){
- $(this).on('swipe', $.noop);
- } };
- });
-})(jQuery);
/****************************************************
- * Method for adding psuedo drag events to elements *
+ * Method for adding pseudo drag events to elements *
***************************************************/
-!function($){
+Touch.setupTouchHandler = function($) {
$.fn.addTouch = function(){
this.each(function(i,el){
$(el).bind('touchstart touchmove touchend touchcancel',function(){