utils.js 2.1 KB

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