Fixed bug where swipe events was added more than once and other screens were not working

This commit is contained in:
Bofh 2021-01-04 01:14:42 +01:00
parent 58265980cb
commit d8da16a0ef
1 changed files with 55 additions and 18 deletions

View File

@ -60,25 +60,60 @@ function api_send_message() {
} }
var fediloveUI = { var fediloveUI = {
registerSwipeOnElementEvents: {
elements: [],
vars: {
startX: null, startY: null, dist: null,
threshold: 30, allowedTime: 200,
elapsedTime: 0, startTime: null
},
touchstart: function(e) {
const vars = fediloveUI.registerSwipeOnElementEvents.vars;
const touchobj = e.changedTouches[0];
vars.dist = 0; vars.startX = touchobj.pageX; vars.startY = touchobj.pageY;
vars.startTime = new Date().getTime(); e.preventDefault();
},
touchmove: function(e) { e.preventDefault() },
touchend: function(e) {
if (fediloveUI.registerSwipeOnElementEvents.touchendCallback != null) {
const vars = fediloveUI.registerSwipeOnElementEvents.vars;
const touchobj = e.changedTouches[0];
vars.dist = touchobj.pageX - vars.startX;
vars.elapsedTime = new Date().getTime() - vars.startTime;
if (Math.abs(vars.dist) < 20) { return e.preventDefault(); }
const swiperightBol = (vars.elapsedTime <= vars.allowedTime && vars.dist >= vars.threshold);
fediloveUI.registerSwipeOnElementEvents.touchendCallback(swiperightBol);
e.preventDefault();
}
},
touchendCallback: null
},
unregisterSwipeOnElement: function(selector) {
const element = document.querySelector(selector);
if (element === null || element === undefined) return;
fediloveUI.registerSwipeOnElementEvents.elements = fediloveUI.registerSwipeOnElementEvents.elements.filter(
function(item) { return item !== selector });
element.removeEventListener('touchstart', fediloveUI.registerSwipeOnElementEvents.touchstart);
element.removeEventListener('touchmove', fediloveUI.registerSwipeOnElementEvents.touchmove);
element.removeEventListener('touchend', fediloveUI.registerSwipeOnElementEvents.touchend);
},
registerSwipeOnElement: function(selector, cbackFunc) { registerSwipeOnElement: function(selector, cbackFunc) {
var touchsurface; const element = document.querySelector(selector);
if (typeof selector == "string") if (element === null || element === undefined) return;
touchsurface = document.querySelector(selector);
else if (typeof selector == "object") if (fediloveUI.registerSwipeOnElementEvents.elements.includes(selector))
touchsurface = selector; return true;
else return true;
var startX,startY,dist,threshold = 30,allowedTime = 200,elapsedTime,startTime; fediloveUI.registerSwipeOnElementEvents.elements.push(selector);
touchsurface.addEventListener('touchstart', function(e){ fediloveUI.registerSwipeOnElementEvents.touchendCallback = cbackFunc;
const touchobj = e.changedTouches[0]; dist = 0; startX = touchobj.pageX; startY = touchobj.pageY; element.addEventListener('touchstart', fediloveUI.registerSwipeOnElementEvents.touchstart, false);
startTime = new Date().getTime(); e.preventDefault(); element.addEventListener('touchmove', fediloveUI.registerSwipeOnElementEvents.touchmove, false);
}, false); element.addEventListener('touchend', fediloveUI.registerSwipeOnElementEvents.touchend, false);
touchsurface.addEventListener('touchmove', function(e){ e.preventDefault() }, false);
touchsurface.addEventListener('touchend', function(e){
const touchobj = e.changedTouches[0]; dist = touchobj.pageX - startX; elapsedTime = new Date().getTime() - startTime;
if (Math.abs(dist) < 20) { return e.preventDefault(); }
const swiperightBol = (elapsedTime <= allowedTime && dist >= threshold);
cbackFunc(swiperightBol); e.preventDefault();
}, false);
}, },
meetAccountImageDirection: function(direction) { meetAccountImageDirection: function(direction) {
if (!window.location.pathname.startsWith('/accounts') || if (!window.location.pathname.startsWith('/accounts') ||
@ -314,6 +349,8 @@ function fedilove_customization() {
document.querySelector('#chat-compose-global').style = 'visibility: collapse'; document.querySelector('#chat-compose-global').style = 'visibility: collapse';
document.querySelector('#chat-party-hide').style = 'display: none !important'; document.querySelector('#chat-party-hide').style = 'display: none !important';
document.querySelector('nav#main-nav > ul.main-nav-ul').style = ''; document.querySelector('nav#main-nav > ul.main-nav-ul').style = '';
fediloveUI.unregisterSwipeOnElement('main.infinite-scroll-page');
fediloveUI.unregisterSwipeOnElement('#meet-navigation #anim-swipe');
fediloveData.currentAccountIsEmpty = false; fediloveData.currentAccountIsEmpty = false;
$('nav#main-nav li.main-nav-li svg').each(function() { $('nav#main-nav li.main-nav-li svg').each(function() {