html.js 5.4 KB

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