From 5c1222d9120442faf789d31624c8c8d1810e0950 Mon Sep 17 00:00:00 2001 From: Yash Dave Date: Mon, 6 Jul 2020 00:50:49 +0530 Subject: [PATCH] fix lint errors in initializeSwipeGestures (#8972) Co-authored-by: Nick Taylor 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; } }