index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { __extends, __read, __rest, __spreadArray } from "tslib";
  2. import { deepMix, isString, isElement, assign, get } from '@antv/util';
  3. import { GUI } from '../../core';
  4. import { deepAssign } from '../../util';
  5. import { CLASS_NAME, POPTIP_ID, POPTIP_STYLE } from './constant';
  6. import { getPositionXY, getSingleTonElement } from './utils';
  7. // 到处方法,可以外部使用
  8. export { getPositionXY } from './utils';
  9. var Poptip = /** @class */ (function (_super) {
  10. __extends(Poptip, _super);
  11. function Poptip(options) {
  12. var _this = _super.call(this, deepMix({ style: { id: POPTIP_ID } }, Poptip.defaultOptions, options)) || this;
  13. /** 显影控制 */
  14. _this.visibility = 'visible';
  15. /** 所有绑定的目标对象 */
  16. _this.map = new Map();
  17. /** 节点样式 */
  18. _this.domStyles = '';
  19. _this.initShape();
  20. _this.render(_this.attributes, _this);
  21. return _this;
  22. }
  23. Object.defineProperty(Poptip.prototype, "visible", {
  24. get: function () {
  25. return this.visibility === 'visible';
  26. },
  27. enumerable: false,
  28. configurable: true
  29. });
  30. Poptip.prototype.render = function (attributes, container) {
  31. this.visibility = this.style.visibility;
  32. this.updatePoptipElement();
  33. };
  34. /**
  35. * poptip 组件更新
  36. */
  37. Poptip.prototype.update = function (cfg) {
  38. this.attr(deepMix({}, this.style, cfg));
  39. this.render(this.attributes, this);
  40. };
  41. /**
  42. * 绑定元素
  43. */
  44. Poptip.prototype.bind = function (element, callback) {
  45. var _this = this;
  46. if (!element)
  47. return;
  48. var defaultText = this.style.text;
  49. var onmousemove = function (e) {
  50. var target = element;
  51. var options = _this.style;
  52. var text = defaultText;
  53. if (callback) {
  54. var _a = typeof callback === 'function' ? callback.call(null, e) : callback, html = _a.html, ele = _a.target, restOptions = __rest(_a, ["html", "target"]);
  55. options = assign({}, _this.style, restOptions);
  56. if (ele || ele === false)
  57. target = ele;
  58. if (typeof html === 'string')
  59. text = html;
  60. }
  61. var position = options.position, arrowPointAtCenter = options.arrowPointAtCenter, follow = options.follow, offset = options.offset;
  62. if (target) {
  63. var _b = e, clientX = _b.clientX, clientY = _b.clientY;
  64. var _c = __read(getPositionXY(clientX, clientY, target, position, arrowPointAtCenter, follow), 2), x = _c[0], y = _c[1];
  65. _this.showTip(x, y, { text: text, position: position, offset: offset });
  66. }
  67. else {
  68. // 没有移动到指定的目标 关闭弹框
  69. _this.hideTip();
  70. }
  71. };
  72. var onmouseleave = function () {
  73. _this.hideTip();
  74. };
  75. element.addEventListener('mousemove', onmousemove);
  76. element.addEventListener('mouseleave', onmouseleave);
  77. // 存储监听
  78. this.map.set(element, [onmousemove, onmouseleave]);
  79. };
  80. Poptip.prototype.unbind = function (element) {
  81. if (this.map.has(element)) {
  82. var _a = __read(this.map.get(element) || [], 2), listener1 = _a[0], listener2 = _a[1];
  83. listener1 && element.removeEventListener('mousemove', listener1);
  84. listener2 && element.removeEventListener('mouseleave', listener2);
  85. this.map.delete(element);
  86. }
  87. };
  88. /**
  89. * 清空容器内容
  90. */
  91. Poptip.prototype.clear = function () {
  92. this.container.innerHTML = '';
  93. };
  94. /**
  95. * 清除
  96. */
  97. Poptip.prototype.destroy = function () {
  98. var _this = this;
  99. var _a;
  100. __spreadArray([], __read(this.map.keys()), false).forEach(function (ele) { return _this.unbind(ele); });
  101. (_a = this.container) === null || _a === void 0 ? void 0 : _a.remove();
  102. _super.prototype.destroy.call(this);
  103. };
  104. /**
  105. * 显示 + 改变位置
  106. * @param x 可选 改变位置 x 方向
  107. * @param y 可选 改变位置 y 方向
  108. * @param text 文本变化
  109. */
  110. Poptip.prototype.showTip = function (x, y, options) {
  111. var text = get(options, 'text');
  112. if (text && typeof text !== 'string')
  113. return;
  114. this.applyStyles();
  115. // 不传入 不希望改变 x y
  116. if (x && y && options) {
  117. var offset = options.offset, position = options.position;
  118. position && this.container.setAttribute('data-position', position);
  119. this.setOffsetPosition(x, y, offset);
  120. if (typeof text === 'string') {
  121. // do something
  122. var textElement = this.container.querySelector(".".concat(CLASS_NAME.TEXT));
  123. if (textElement) {
  124. textElement.innerHTML = text;
  125. }
  126. }
  127. this.visibility = 'visible';
  128. this.container.style.visibility = 'visible';
  129. }
  130. };
  131. /**
  132. * 隐藏
  133. */
  134. Poptip.prototype.hideTip = function () {
  135. this.visibility = 'hidden';
  136. this.container.style.visibility = 'hidden';
  137. };
  138. /**
  139. * 获取内部容器 HTMLElement
  140. * @returns this.element:HTMLElement;
  141. */
  142. Poptip.prototype.getContainer = function () {
  143. return this.container;
  144. };
  145. Poptip.prototype.getClassName = function () {
  146. var containerClassName = this.style.containerClassName;
  147. return "".concat(CLASS_NAME.CONTAINER).concat(containerClassName ? " ".concat(containerClassName) : '');
  148. };
  149. /**
  150. * 初始化容器
  151. */
  152. Poptip.prototype.initShape = function () {
  153. var _this = this;
  154. var id = this.style.id;
  155. this.container = getSingleTonElement(id);
  156. this.container.className = this.getClassName();
  157. // 盒子添加交互
  158. this.container.addEventListener('mousemove', function () { return _this.showTip(); });
  159. this.container.addEventListener('mouseleave', function () { return _this.hideTip(); });
  160. };
  161. /**
  162. * 更新 HTML 上的内容
  163. */
  164. Poptip.prototype.updatePoptipElement = function () {
  165. var container = this.container;
  166. this.clear();
  167. var _a = this.style, id = _a.id, template = _a.template, text = _a.text;
  168. this.container.setAttribute('id', id);
  169. this.container.className = this.getClassName();
  170. // 增加 arrow 元素
  171. var arrowNode = "<span class=\"".concat(CLASS_NAME.ARROW, "\"></span>");
  172. container.innerHTML = arrowNode;
  173. // 置入 text 模版
  174. if (isString(template)) {
  175. container.innerHTML += template;
  176. }
  177. else if (template && isElement(template)) {
  178. container.appendChild(template);
  179. }
  180. // 置入 text
  181. if (text) {
  182. container.getElementsByClassName(CLASS_NAME.TEXT)[0].textContent = text;
  183. }
  184. this.applyStyles();
  185. this.container.style.visibility = this.visibility;
  186. };
  187. /**
  188. * 应用样式表
  189. */
  190. Poptip.prototype.applyStyles = function () {
  191. var styles = deepAssign({}, POPTIP_STYLE, this.style.domStyles);
  192. var styleStr = Object.entries(styles).reduce(function (r, _a) {
  193. var _b = __read(_a, 2), key = _b[0], value = _b[1];
  194. var styleStr = Object.entries(value).reduce(function (r, _a) {
  195. var _b = __read(_a, 2), k = _b[0], v = _b[1];
  196. return "".concat(r).concat(k, ": ").concat(v, ";");
  197. }, '');
  198. return "".concat(r).concat(key, "{").concat(styleStr, "}");
  199. }, '');
  200. if (this.domStyles !== styleStr) {
  201. this.domStyles = styleStr;
  202. var styleDOM = this.container.querySelector('style');
  203. if (styleDOM)
  204. this.container.removeChild(styleDOM);
  205. styleDOM = document.createElement('style');
  206. styleDOM.innerHTML = styleStr;
  207. this.container.appendChild(styleDOM);
  208. }
  209. };
  210. /**
  211. * 将相对于指针的偏移量生效到dom元素上
  212. * @param x 盒子相对于页面 x 的位置
  213. * @param y 盒子相对于页面 y 的位置
  214. */
  215. Poptip.prototype.setOffsetPosition = function (x, y, offset) {
  216. if (offset === void 0) { offset = this.style.offset; }
  217. var _a = __read(offset, 2), _b = _a[0], offsetX = _b === void 0 ? 0 : _b, _c = _a[1], offsetY = _c === void 0 ? 0 : _c;
  218. this.container.style.left = "".concat(x + offsetX, "px");
  219. this.container.style.top = "".concat(y + offsetY, "px");
  220. };
  221. Poptip.tag = 'poptip';
  222. Poptip.defaultOptions = {
  223. style: {
  224. x: 0,
  225. y: 0,
  226. width: 0,
  227. height: 0,
  228. target: null,
  229. visibility: 'hidden',
  230. text: '',
  231. position: 'top',
  232. follow: false,
  233. offset: [0, 0],
  234. domStyles: POPTIP_STYLE,
  235. template: "<div class=\"".concat(CLASS_NAME.TEXT, "\"></div>"),
  236. },
  237. };
  238. return Poptip;
  239. }(GUI));
  240. export { Poptip };
  241. //# sourceMappingURL=index.js.map