utils.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var _this = this;
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.getSingleTonElement = exports.getPositionXY = void 0;
  5. /**
  6. * 计算不同方向的 poptip 在目标盒子上的位置,配置 POPTIP_STYLE 达到需要的 样式效果
  7. * @param position PoptipPosition
  8. * @returns { x: number, y: number }
  9. */
  10. function getPositionXY(clientX, clientY, target, position, arrowPointAtCenter, follow) {
  11. if (arrowPointAtCenter === void 0) { arrowPointAtCenter = false; }
  12. if (follow === void 0) { follow = false; }
  13. if (follow)
  14. return [clientX, clientY];
  15. var _a = target.getBoundingClientRect(), x = _a.x, y = _a.y, width = _a.width, height = _a.height;
  16. switch (position) {
  17. case 'top':
  18. return arrowPointAtCenter ? [x + width / 2, y] : [clientX, y];
  19. case 'left':
  20. return arrowPointAtCenter ? [x, y + height / 2] : [x, clientY];
  21. case 'bottom':
  22. return arrowPointAtCenter ? [x + width / 2, y + height] : [clientX, y + height];
  23. case 'right':
  24. return arrowPointAtCenter ? [x + width, y + height / 2] : [x + width, clientY];
  25. case 'top-right':
  26. case 'right-top':
  27. return [x + width, y];
  28. case 'left-bottom':
  29. case 'bottom-left':
  30. return [x, y + height];
  31. case 'right-bottom':
  32. case 'bottom-right':
  33. return [x + width, y + height];
  34. case 'top-left':
  35. case 'left-top':
  36. default:
  37. return [x, y];
  38. }
  39. }
  40. exports.getPositionXY = getPositionXY;
  41. var getSingleTon = function (fn) {
  42. var instance;
  43. return function () {
  44. var args = [];
  45. for (var _i = 0; _i < arguments.length; _i++) {
  46. args[_i] = arguments[_i];
  47. }
  48. if (!instance)
  49. instance = fn.apply(_this, args);
  50. return instance;
  51. };
  52. };
  53. function createElement(id) {
  54. var div = id && document.getElementById(id);
  55. if (!div) {
  56. div = document.createElement('div');
  57. div.setAttribute('id', id);
  58. document.body.appendChild(div);
  59. }
  60. return div;
  61. }
  62. /**
  63. * 获取全局唯一的 dom 元素
  64. */
  65. function getSingleTonElement(id) {
  66. var element = getSingleTon(createElement)(id);
  67. return element;
  68. }
  69. exports.getSingleTonElement = getSingleTonElement;
  70. //# sourceMappingURL=utils.js.map