Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed Mar 11, 2024
1 parent ac2633f commit 3c3499b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
25 changes: 17 additions & 8 deletions tua-bsl.esm.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ function noticeRequiredTargetElement(targetElement) {
}
return true;
}
function preventEventDefault(event) {
if (!event.cancelable)
return;
event.preventDefault();
/**
* Get global function that calls preventDefault
*/
function getPreventEventDefault() {
if ('__BSL_PREVENT_DEFAULT__' in window) {
return window.__BSL_PREVENT_DEFAULT__;
}
window.__BSL_PREVENT_DEFAULT__ = function (event) {
if (!event.cancelable)
return;
event.preventDefault();
};
return window.__BSL_PREVENT_DEFAULT__;
}

const initialLockState = {
Expand Down Expand Up @@ -96,7 +105,7 @@ function handleScroll(event, targetElement, initialClientPos) {
const isOnBottom = clientY < 0 && scrollTop + clientHeight + 1 >= scrollHeight;
if ((isVertical && (isOnTop || isOnBottom)) ||
(!isVertical && (isOnLeft || isOnRight))) {
return preventEventDefault(event);
return getPreventEventDefault()(event);
}
}
event.stopPropagation();
Expand Down Expand Up @@ -172,7 +181,7 @@ function lock(targetElement, options) {
});
}
if (!lockState.documentListenerAdded) {
document.addEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.addEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = true;
}
}
Expand Down Expand Up @@ -209,7 +218,7 @@ function unlock(targetElement, options) {
});
}
if (lockState.documentListenerAdded) {
document.removeEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.removeEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = false;
}
}
Expand All @@ -234,7 +243,7 @@ function clearBodyLocks(options) {
}
}
if (lockState.documentListenerAdded) {
document.removeEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.removeEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tua-bsl.esm.browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions tua-bsl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,18 @@ function noticeRequiredTargetElement(targetElement) {
}
return true;
}
function preventEventDefault(event) {
if (!event.cancelable) return;
event.preventDefault();
/**
* Get global function that calls preventDefault
*/
function getPreventEventDefault() {
if ('__BSL_PREVENT_DEFAULT__' in window) {
return window.__BSL_PREVENT_DEFAULT__;
}
window.__BSL_PREVENT_DEFAULT__ = function (event) {
if (!event.cancelable) return;
event.preventDefault();
};
return window.__BSL_PREVENT_DEFAULT__;
}

var initialLockState = {
Expand Down Expand Up @@ -95,7 +104,7 @@ function handleScroll(event, targetElement, initialClientPos) {
var isOnRight = clientX < 0 && scrollLeft + clientWidth + 1 >= scrollWidth;
var isOnBottom = clientY < 0 && scrollTop + clientHeight + 1 >= scrollHeight;
if (isVertical && (isOnTop || isOnBottom) || !isVertical && (isOnLeft || isOnRight)) {
return preventEventDefault(event);
return getPreventEventDefault()(event);
}
}
event.stopPropagation();
Expand Down Expand Up @@ -178,7 +187,7 @@ function lock(targetElement, options) {
});
}
if (!lockState.documentListenerAdded) {
document.addEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.addEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = true;
}
} else if (lockState.lockedNum <= 0) {
Expand Down Expand Up @@ -209,7 +218,7 @@ function unlock(targetElement, options) {
});
}
if (lockState.documentListenerAdded) {
document.removeEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.removeEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = false;
}
}
Expand All @@ -232,7 +241,7 @@ function clearBodyLocks(options) {
}
}
if (lockState.documentListenerAdded) {
document.removeEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.removeEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = false;
}
}
Expand Down
23 changes: 16 additions & 7 deletions tua-bsl.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@
}
return true;
}
function preventEventDefault(event) {
if (!event.cancelable) return;
event.preventDefault();
/**
* Get global function that calls preventDefault
*/
function getPreventEventDefault() {
if ('__BSL_PREVENT_DEFAULT__' in window) {
return window.__BSL_PREVENT_DEFAULT__;
}
window.__BSL_PREVENT_DEFAULT__ = function (event) {
if (!event.cancelable) return;
event.preventDefault();
};
return window.__BSL_PREVENT_DEFAULT__;
}

var initialLockState = {
Expand Down Expand Up @@ -99,7 +108,7 @@
var isOnRight = clientX < 0 && scrollLeft + clientWidth + 1 >= scrollWidth;
var isOnBottom = clientY < 0 && scrollTop + clientHeight + 1 >= scrollHeight;
if (isVertical && (isOnTop || isOnBottom) || !isVertical && (isOnLeft || isOnRight)) {
return preventEventDefault(event);
return getPreventEventDefault()(event);
}
}
event.stopPropagation();
Expand Down Expand Up @@ -182,7 +191,7 @@
});
}
if (!lockState.documentListenerAdded) {
document.addEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.addEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = true;
}
} else if (lockState.lockedNum <= 0) {
Expand Down Expand Up @@ -213,7 +222,7 @@
});
}
if (lockState.documentListenerAdded) {
document.removeEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.removeEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = false;
}
}
Expand All @@ -236,7 +245,7 @@
}
}
if (lockState.documentListenerAdded) {
document.removeEventListener('touchmove', preventEventDefault, eventListenerOptions);
document.removeEventListener('touchmove', getPreventEventDefault(), eventListenerOptions);
lockState.documentListenerAdded = false;
}
}
Expand Down
Loading

0 comments on commit 3c3499b

Please sign in to comment.