diff --git a/app/assets/javascripts/initializers/initializeSwipeGestures.js b/app/assets/javascripts/initializers/initializeSwipeGestures.js index 2dd8d2675..8f3eed296 100644 --- a/app/assets/javascripts/initializers/initializeSwipeGestures.js +++ b/app/assets/javascripts/initializers/initializeSwipeGestures.js @@ -1,61 +1,112 @@ -function initializeSwipeGestures(){ - initializeSwipeGestures.called = true; - swipeState = "middle"; - setTimeout(function(){ +/* global slideSidebar, swipeState: true */ +'use strict'; - // window.onload=function(){ - (function(d){ - var - ce=function(e,n){var a=document.createEvent("CustomEvent");a.initCustomEvent(n,true,true,e.target);e.target.dispatchEvent(a);a=null;return false}, - nm=true,sp={x:0,y:0},ep={x:0,y:0}, - touch={ - touchstart:function(e){sp={x:e.touches[0].pageX,y:e.touches[0].pageY,scrollY:window.scrollY}}, - touchmove:function(e){nm=false;ep={x:e.touches[0].pageX,y:e.touches[0].pageY,scrollY:window.scrollY}}, - touchend:function(e){ - if(nm){ce(e,'fc')} - else { - var x=ep.x-sp.x,xr=Math.abs(x),y=ep.y-sp.y,yr=Math.abs(y),absScroll=Math.abs(sp.scrollY-ep.scrollY); - if(Math.max(xr,yr)>15){ - var shouldScroll = (xr/2)>yr && absScroll < 5; - ce(e,(shouldScroll?(x<0?'swl':'swr'):(y<0?'swu':'swd'))) - } +function slideContent(d) { + var ce = function (e, n) { + var a = document.createEvent('CustomEvent'); + a.initCustomEvent(n, true, true, e.target); + e.target.dispatchEvent(a); + a = null; + return false; + }, + nm = true, + sp = { x: 0, y: 0 }, + ep = { x: 0, y: 0 }, + touch = { + touchstart: function (e) { + sp = { + x: e.touches[0].pageX, + y: e.touches[0].pageY, + scrollY: window.scrollY, }; - nm=true }, - touchcancel:function(e){nm=false} + touchmove: function (e) { + nm = false; + ep = { + x: e.touches[0].pageX, + y: e.touches[0].pageY, + scrollY: window.scrollY, + }; + }, + touchend: function (e) { + if (nm) { + ce(e, 'fc'); + } else { + var x = ep.x - sp.x, + xr = Math.abs(x), + y = ep.y - sp.y, + yr = Math.abs(y), + absScroll = Math.abs(sp.scrollY - ep.scrollY); + if (Math.max(xr, yr) > 15) { + var shouldScroll = xr / 2 > yr && absScroll < 5; + ce( + e, + shouldScroll ? (x < 0 ? 'swl' : 'swr') : y < 0 ? 'swu' : 'swd', + ); + } + } + nm = true; + }, + touchcancel: function (e) { + nm = false; + }, }; - for(var a in touch){d.addEventListener(a,touch[a],false);} - })(document); - var h=function(e){console.log(e.type,e)}; - document.body.addEventListener('swl',handleSwipeLeft,false); - document.body.addEventListener('swr',handleSwipeRight,false); - },50) + for (var a in touch) { + d.addEventListener(a, touch[a], false); + } } +function initializeSwipeGestures() { + initializeSwipeGestures.called = true; + let swipeState = 'middle'; + setTimeout(function () { + // window.onload=function(){ + slideContent(document); + var h = function (e) { + console.log(e.type, e); + }; + document.body.addEventListener( + 'swl', + (e) => { + swipeState = handleSwipeLeft(e, swipeState); + }, + false, + ); + document.body.addEventListener( + 'swr', + (e) => { + swipeState = handleSwipeRight(e, swipeState); + }, + false, + ); + }, 50); +} -function handleSwipeLeft(e){ - if (!document.getElementById("on-page-nav-controls")){ +function handleSwipeLeft(e, swipeState) { + if (!document.getElementById('on-page-nav-controls')) { return; } - if (swipeState == "middle"){ - swipeState = "right" - slideSidebar("right","intoView"); - } - else{ - swipeState = "middle" - slideSidebar("left","outOfView"); + if (swipeState == 'middle') { + swipeState = 'right'; + slideSidebar('right', 'intoView'); + return swipeState; + } else { + swipeState = 'middle'; + slideSidebar('left', 'outOfView'); + return swipeState; } } -function handleSwipeRight(e){ - if (!document.getElementById("on-page-nav-controls")){ +function handleSwipeRight(e, swipeState) { + if (!document.getElementById('on-page-nav-controls')) { return; } - if (swipeState == "middle"){ - swipeState = "left" - slideSidebar("left","intoView"); - } - else{ - swipeState = "middle" - slideSidebar("right","outOfView"); + if (swipeState == 'middle') { + swipeState = 'left'; + slideSidebar('left', 'intoView'); + return swipeState; + } else { + swipeState = 'middle'; + slideSidebar('right', 'outOfView'); + return swipeState; } }