html.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _common = require("../../util/common");
  5. var _base = _interopRequireDefault(require("./base"));
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  7. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  8. function getOffsetFromAlign(alignX, alignY, width, height) {
  9. var result = [];
  10. if (alignX === 'left' && alignY === 'top') {
  11. result[0] = 0;
  12. result[1] = 0;
  13. } else if (alignX === 'right' && alignY === 'top') {
  14. result[0] = -width;
  15. result[1] = 0;
  16. } else if (alignX === 'left' && alignY === 'bottom') {
  17. result[0] = 0;
  18. result[1] = Math.floor(-height);
  19. } else if (alignX === 'right' && alignY === 'bottom') {
  20. result[0] = Math.floor(-width);
  21. result[1] = Math.floor(-height);
  22. } else if (alignX === 'right' && alignY === 'middle') {
  23. result[0] = Math.floor(-width);
  24. result[1] = Math.floor(-height / 2);
  25. } else if (alignX === 'left' && alignY === 'middle') {
  26. result[0] = 0;
  27. result[1] = Math.floor(-height / 2);
  28. } else if (alignX === 'center' && alignY === 'bottom') {
  29. result[0] = Math.floor(-width / 2);
  30. result[1] = Math.floor(-height);
  31. } else if (alignX === 'center' && alignY === 'top') {
  32. result[0] = Math.floor(-width / 2);
  33. result[1] = 0;
  34. } else {
  35. result[0] = Math.floor(-width / 2);
  36. result[1] = Math.floor(-height / 2);
  37. }
  38. return result;
  39. }
  40. function modifyCSS(DOM, CSS) {
  41. for (var key in CSS) {
  42. if (CSS.hasOwnProperty(key)) {
  43. DOM.style[key] = CSS[key];
  44. }
  45. }
  46. return DOM;
  47. }
  48. function createDom(str) {
  49. var container = document.createElement('div');
  50. str = str.replace(/(^\s*)|(\s*$)/g, '');
  51. container.innerHTML = '' + str;
  52. return container.childNodes[0];
  53. }
  54. var Html = /*#__PURE__*/function (_GuideBase) {
  55. _inheritsLoose(Html, _GuideBase);
  56. function Html() {
  57. return _GuideBase.apply(this, arguments) || this;
  58. }
  59. var _proto = Html.prototype;
  60. _proto._initDefaultCfg = function _initDefaultCfg() {
  61. this.type = 'html';
  62. /**
  63. * dom position
  64. * @type {Object | Array}
  65. */
  66. this.position = null;
  67. /**
  68. * alignment for horizontal direction,can be 'left','center','right'
  69. * @type {String}
  70. */
  71. this.alignX = 'center';
  72. /**
  73. * alignment for vertical direction,can be 'top', 'middle', 'bottom'
  74. * @type {String}
  75. */
  76. this.alignY = 'middle';
  77. /**
  78. * offset for horizontal direction
  79. * @type {Number}
  80. */
  81. this.offsetX = null;
  82. /**
  83. * offset for vertical direction
  84. * @type {Number}
  85. */
  86. this.offsetY = null;
  87. /**
  88. * the html string
  89. *@type {String | Function}
  90. */
  91. this.html = null;
  92. } // override paint
  93. ;
  94. _proto.render = function render(coord, container) {
  95. var self = this;
  96. var position = self.parsePoint(coord, self.position);
  97. if (!position) {
  98. return;
  99. }
  100. var myNode = createDom(self.html);
  101. myNode = modifyCSS(myNode, {
  102. position: 'absolute',
  103. top: Math.floor(position.y) + 'px',
  104. left: Math.floor(position.x) + 'px',
  105. visibility: 'hidden'
  106. });
  107. var canvasDom = container.get('canvas').get('el');
  108. var parentNode = canvasDom.parentNode;
  109. parentNode = modifyCSS(parentNode, {
  110. position: 'relative'
  111. });
  112. var wrapperNode = createDom('<div class="guideWapper" style="position: absolute;top: 0; left: 0;"></div>');
  113. parentNode.appendChild(wrapperNode);
  114. wrapperNode.appendChild(myNode);
  115. var canvasOffsetTop = canvasDom.offsetTop;
  116. var canvasOffsetLeft = canvasDom.offsetLeft;
  117. var alignX = self.alignX,
  118. alignY = self.alignY,
  119. offsetX = self.offsetX,
  120. offsetY = self.offsetY;
  121. var width = (0, _common.getWidth)(myNode);
  122. var height = (0, _common.getHeight)(myNode);
  123. var newOffset = getOffsetFromAlign(alignX, alignY, width, height);
  124. position.x = position.x + newOffset[0] + canvasOffsetLeft;
  125. position.y = position.y + newOffset[1] + canvasOffsetTop;
  126. if (offsetX) {
  127. position.x += offsetX;
  128. }
  129. if (offsetY) {
  130. position.y += offsetY;
  131. }
  132. modifyCSS(myNode, {
  133. top: Math.floor(position.y) + 'px',
  134. left: Math.floor(position.x) + 'px',
  135. visibility: 'visible'
  136. });
  137. self.element = wrapperNode;
  138. };
  139. _proto.remove = function remove() {
  140. var element = this.element;
  141. element && element.parentNode && element.parentNode.removeChild(element);
  142. };
  143. return Html;
  144. }(_base["default"]);
  145. _base["default"].Html = Html;
  146. var _default = Html;
  147. exports["default"] = _default;