html.js 4.4 KB

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