util.js 6.2 KB

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