handleScroll.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var alwaysContainsScroll = function (node) {
  2. // textarea will always _contain_ scroll inside self. It only can be hidden
  3. return node.tagName === 'TEXTAREA';
  4. };
  5. var elementCanBeScrolled = function (node, overflow) {
  6. var styles = window.getComputedStyle(node);
  7. return (
  8. // not-not-scrollable
  9. styles[overflow] !== 'hidden' &&
  10. // contains scroll inside self
  11. !(styles.overflowY === styles.overflowX && !alwaysContainsScroll(node) && styles[overflow] === 'visible'));
  12. };
  13. var elementCouldBeVScrolled = function (node) { return elementCanBeScrolled(node, 'overflowY'); };
  14. var elementCouldBeHScrolled = function (node) { return elementCanBeScrolled(node, 'overflowX'); };
  15. export var locationCouldBeScrolled = function (axis, node) {
  16. var current = node;
  17. do {
  18. // Skip over shadow root
  19. if (typeof ShadowRoot !== 'undefined' && current instanceof ShadowRoot) {
  20. current = current.host;
  21. }
  22. var isScrollable = elementCouldBeScrolled(axis, current);
  23. if (isScrollable) {
  24. var _a = getScrollVariables(axis, current), s = _a[1], d = _a[2];
  25. if (s > d) {
  26. return true;
  27. }
  28. }
  29. current = current.parentNode;
  30. } while (current && current !== document.body);
  31. return false;
  32. };
  33. var getVScrollVariables = function (_a) {
  34. var scrollTop = _a.scrollTop, scrollHeight = _a.scrollHeight, clientHeight = _a.clientHeight;
  35. return [
  36. scrollTop,
  37. scrollHeight,
  38. clientHeight,
  39. ];
  40. };
  41. var getHScrollVariables = function (_a) {
  42. var scrollLeft = _a.scrollLeft, scrollWidth = _a.scrollWidth, clientWidth = _a.clientWidth;
  43. return [
  44. scrollLeft,
  45. scrollWidth,
  46. clientWidth,
  47. ];
  48. };
  49. var elementCouldBeScrolled = function (axis, node) {
  50. return axis === 'v' ? elementCouldBeVScrolled(node) : elementCouldBeHScrolled(node);
  51. };
  52. var getScrollVariables = function (axis, node) {
  53. return axis === 'v' ? getVScrollVariables(node) : getHScrollVariables(node);
  54. };
  55. var getDirectionFactor = function (axis, direction) {
  56. /**
  57. * If the element's direction is rtl (right-to-left), then scrollLeft is 0 when the scrollbar is at its rightmost position,
  58. * and then increasingly negative as you scroll towards the end of the content.
  59. * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft
  60. */
  61. return axis === 'h' && direction === 'rtl' ? -1 : 1;
  62. };
  63. export var handleScroll = function (axis, endTarget, event, sourceDelta, noOverscroll) {
  64. var directionFactor = getDirectionFactor(axis, window.getComputedStyle(endTarget).direction);
  65. var delta = directionFactor * sourceDelta;
  66. // find scrollable target
  67. var target = event.target;
  68. var targetInLock = endTarget.contains(target);
  69. var shouldCancelScroll = false;
  70. var isDeltaPositive = delta > 0;
  71. var availableScroll = 0;
  72. var availableScrollTop = 0;
  73. do {
  74. var _a = getScrollVariables(axis, target), position = _a[0], scroll_1 = _a[1], capacity = _a[2];
  75. var elementScroll = scroll_1 - capacity - directionFactor * position;
  76. if (position || elementScroll) {
  77. if (elementCouldBeScrolled(axis, target)) {
  78. availableScroll += elementScroll;
  79. availableScrollTop += position;
  80. }
  81. }
  82. target = target.parentNode;
  83. } while (
  84. // portaled content
  85. (!targetInLock && target !== document.body) ||
  86. // self content
  87. (targetInLock && (endTarget.contains(target) || endTarget === target)));
  88. if (isDeltaPositive && ((noOverscroll && availableScroll === 0) || (!noOverscroll && delta > availableScroll))) {
  89. shouldCancelScroll = true;
  90. }
  91. else if (!isDeltaPositive &&
  92. ((noOverscroll && availableScrollTop === 0) || (!noOverscroll && -delta > availableScrollTop))) {
  93. shouldCancelScroll = true;
  94. }
  95. return shouldCancelScroll;
  96. };