index.js 9.4 KB

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