util.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. function isPointsEq() {
  3. var a1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  4. var a2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  5. var isAlignPoint = arguments.length > 2 ? arguments[2] : undefined;
  6. if (isAlignPoint) {
  7. return a1[0] === a2[0];
  8. }
  9. return a1[0] === a2[0] && a1[1] === a2[1];
  10. }
  11. export function getAlignPopupClassName(builtinPlacements, prefixCls, align, isAlignPoint) {
  12. var points = align.points;
  13. var placements = Object.keys(builtinPlacements);
  14. for (var i = 0; i < placements.length; i += 1) {
  15. var _builtinPlacements$pl;
  16. var placement = placements[i];
  17. if (isPointsEq((_builtinPlacements$pl = builtinPlacements[placement]) === null || _builtinPlacements$pl === void 0 ? void 0 : _builtinPlacements$pl.points, points, isAlignPoint)) {
  18. return "".concat(prefixCls, "-placement-").concat(placement);
  19. }
  20. }
  21. return '';
  22. }
  23. /** @deprecated We should not use this if we can refactor all deps */
  24. export function getMotion(prefixCls, motion, animation, transitionName) {
  25. if (motion) {
  26. return motion;
  27. }
  28. if (animation) {
  29. return {
  30. motionName: "".concat(prefixCls, "-").concat(animation)
  31. };
  32. }
  33. if (transitionName) {
  34. return {
  35. motionName: transitionName
  36. };
  37. }
  38. return null;
  39. }
  40. export function getWin(ele) {
  41. return ele.ownerDocument.defaultView;
  42. }
  43. /**
  44. * Get all the scrollable parent elements of the element
  45. * @param ele The element to be detected
  46. * @param areaOnly Only return the parent which will cut visible area
  47. */
  48. export function collectScroller(ele) {
  49. var scrollerList = [];
  50. var current = ele === null || ele === void 0 ? void 0 : ele.parentElement;
  51. var scrollStyle = ['hidden', 'scroll', 'clip', 'auto'];
  52. while (current) {
  53. var _getWin$getComputedSt = getWin(current).getComputedStyle(current),
  54. overflowX = _getWin$getComputedSt.overflowX,
  55. overflowY = _getWin$getComputedSt.overflowY,
  56. overflow = _getWin$getComputedSt.overflow;
  57. if ([overflowX, overflowY, overflow].some(function (o) {
  58. return scrollStyle.includes(o);
  59. })) {
  60. scrollerList.push(current);
  61. }
  62. current = current.parentElement;
  63. }
  64. return scrollerList;
  65. }
  66. export function toNum(num) {
  67. var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  68. return Number.isNaN(num) ? defaultValue : num;
  69. }
  70. function getPxValue(val) {
  71. return toNum(parseFloat(val), 0);
  72. }
  73. /**
  74. *
  75. *
  76. * **************************************
  77. * * Border *
  78. * * ************************** *
  79. * * * * * *
  80. * * B * * S * B *
  81. * * o * * c * o *
  82. * * r * Content * r * r *
  83. * * d * * o * d *
  84. * * e * * l * e *
  85. * * r ******************** l * r *
  86. * * * Scroll * *
  87. * * ************************** *
  88. * * Border *
  89. * **************************************
  90. *
  91. */
  92. /**
  93. * Get visible area of element
  94. */
  95. export function getVisibleArea(initArea, scrollerList) {
  96. var visibleArea = _objectSpread({}, initArea);
  97. (scrollerList || []).forEach(function (ele) {
  98. if (ele instanceof HTMLBodyElement || ele instanceof HTMLHtmlElement) {
  99. return;
  100. }
  101. // Skip if static position which will not affect visible area
  102. var _getWin$getComputedSt2 = getWin(ele).getComputedStyle(ele),
  103. overflow = _getWin$getComputedSt2.overflow,
  104. overflowClipMargin = _getWin$getComputedSt2.overflowClipMargin,
  105. borderTopWidth = _getWin$getComputedSt2.borderTopWidth,
  106. borderBottomWidth = _getWin$getComputedSt2.borderBottomWidth,
  107. borderLeftWidth = _getWin$getComputedSt2.borderLeftWidth,
  108. borderRightWidth = _getWin$getComputedSt2.borderRightWidth;
  109. var eleRect = ele.getBoundingClientRect();
  110. var eleOutHeight = ele.offsetHeight,
  111. eleInnerHeight = ele.clientHeight,
  112. eleOutWidth = ele.offsetWidth,
  113. eleInnerWidth = ele.clientWidth;
  114. var borderTopNum = getPxValue(borderTopWidth);
  115. var borderBottomNum = getPxValue(borderBottomWidth);
  116. var borderLeftNum = getPxValue(borderLeftWidth);
  117. var borderRightNum = getPxValue(borderRightWidth);
  118. var scaleX = toNum(Math.round(eleRect.width / eleOutWidth * 1000) / 1000);
  119. var scaleY = toNum(Math.round(eleRect.height / eleOutHeight * 1000) / 1000);
  120. // Original visible area
  121. var eleScrollWidth = (eleOutWidth - eleInnerWidth - borderLeftNum - borderRightNum) * scaleX;
  122. var eleScrollHeight = (eleOutHeight - eleInnerHeight - borderTopNum - borderBottomNum) * scaleY;
  123. // Cut border size
  124. var scaledBorderTopWidth = borderTopNum * scaleY;
  125. var scaledBorderBottomWidth = borderBottomNum * scaleY;
  126. var scaledBorderLeftWidth = borderLeftNum * scaleX;
  127. var scaledBorderRightWidth = borderRightNum * scaleX;
  128. // Clip margin
  129. var clipMarginWidth = 0;
  130. var clipMarginHeight = 0;
  131. if (overflow === 'clip') {
  132. var clipNum = getPxValue(overflowClipMargin);
  133. clipMarginWidth = clipNum * scaleX;
  134. clipMarginHeight = clipNum * scaleY;
  135. }
  136. // Region
  137. var eleLeft = eleRect.x + scaledBorderLeftWidth - clipMarginWidth;
  138. var eleTop = eleRect.y + scaledBorderTopWidth - clipMarginHeight;
  139. var eleRight = eleLeft + eleRect.width + 2 * clipMarginWidth - scaledBorderLeftWidth - scaledBorderRightWidth - eleScrollWidth;
  140. var eleBottom = eleTop + eleRect.height + 2 * clipMarginHeight - scaledBorderTopWidth - scaledBorderBottomWidth - eleScrollHeight;
  141. visibleArea.left = Math.max(visibleArea.left, eleLeft);
  142. visibleArea.top = Math.max(visibleArea.top, eleTop);
  143. visibleArea.right = Math.min(visibleArea.right, eleRight);
  144. visibleArea.bottom = Math.min(visibleArea.bottom, eleBottom);
  145. });
  146. return visibleArea;
  147. }