Improve touch detection and avoid multiple window launch

Former-commit-id: 04d996e44dc4af81333f358d138e4ea9a4b1df44 [formerly 360e8c5bc25b5c17eb034eb146d2bd8b1a6c1444 [formerly 0a020e019f09b413d15a32c3a87831586f4c2412]]
Former-commit-id: d87ff30a7d112bbdcd99e2b1d86106a79e6a35a3
Former-commit-id: 429c1a6a756d5d5efd35c149b629635a988363ce
This commit is contained in:
Jaifroid 2021-05-15 13:22:21 +01:00
parent 675954d405
commit 1c0da90c2f

View File

@ -4040,48 +4040,48 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett
var kiwixTarget = appstate.target;
var thisWindow = articleWindow;
var thisContainer = articleContainer;
// Establish a variable for tracking long press
var touched = false;
a.addEventListener('touchstart', function () {
if (!params.windowOpener) return;
touched = true;
a.addEventListener('touchstart', function (e) {
if (!params.windowOpener || a.touched) return;
e.stopPropagation();
a.touched = true;
console.log('Touch activation detected:', e.target);
// The link will be clicked if the user long-presses for more than 600ms (if the option is enabled)
setTimeout(function () {
if (!touched) return;
if (!a.touched || a.launched) return;
a.launched = true;
a.click();
}, 600);
}, {
passive: true
});
a.addEventListener('touchend', function () {
touched = false;
}, {
passive: true
a.touched = false;
a.launched = false;
});
// This detects right-click in all browsers (only if the option is enabled)
a.addEventListener('contextmenu', function (e) {
if (!params.windowOpener) return;
e.preventDefault();
e.stopPropagation();
touched = true;
a.launched = true;
a.click();
});
// This detects the middle-click event
a.addEventListener('mousedown', function (e) {
if (e.which === 2 || e.button === 4) {
e.preventDefault();
touched = true;
a.launched = true;
a.click();
}
});
// The main click routine (called by other events above as well)
a.addEventListener('click', function (e) {
// Prevent opening multiple windows
if (a.touched && !a.launched) return;
// Restore original values for this window/tab
appstate.target = kiwixTarget;
articleWindow = thisWindow;
articleContainer = thisContainer;
// This detects Ctrl-click, Command-click, the long-press event, and middle-click
if ((e.ctrlKey || e.metaKey || touched || e.which === 2 || e.button === 4) && params.windowOpener) {
if ((e.ctrlKey || e.metaKey || a.launched || e.which === 2 || e.button === 4) && params.windowOpener) {
// We open the new window immediately so that it is a direct result of user action (click)
// and we'll populate it later - this avoids most popup blockers
articleContainer = window.open('article.html', params.windowOpener === 'tab' ? '_blank' : a.title,
@ -4094,11 +4094,15 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett
// and allow any propagated clicks on other elements to run
return;
}
touched = false;
e.preventDefault();
e.stopPropagation();
var zimUrl = uiUtil.deriveZimUrlFromRelativeUrl(uriComponent, baseUrl);
goToArticle(zimUrl, downloadAttrValue, contentType);
setTimeout(function () {
// By delaying unblocking of the touch event, we prevent multiple touch events launching the same window
a.touched = false;
a.launched = false;
}, 1400);
});
}