"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); 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); }; } 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; } } var Util = require('../../util/common'); var GuideBase = require('./base'); function getOffsetFromAlign(alignX, alignY, width, height) { var result = []; if (alignX === 'left' && alignY === 'top') { result[0] = 0; result[1] = 0; } else if (alignX === 'right' && alignY === 'top') { result[0] = -width; result[1] = 0; } else if (alignX === 'left' && alignY === 'bottom') { result[0] = 0; result[1] = Math.floor(-height); } else if (alignX === 'right' && alignY === 'bottom') { result[0] = Math.floor(-width); result[1] = Math.floor(-height); } else if (alignX === 'right' && alignY === 'middle') { result[0] = Math.floor(-width); result[1] = Math.floor(-height / 2); } else if (alignX === 'left' && alignY === 'middle') { result[0] = 0; result[1] = Math.floor(-height / 2); } else if (alignX === 'center' && alignY === 'bottom') { result[0] = Math.floor(-width / 2); result[1] = Math.floor(-height); } else if (alignX === 'center' && alignY === 'top') { result[0] = Math.floor(-width / 2); result[1] = 0; } else { result[0] = Math.floor(-width / 2); result[1] = Math.floor(-height / 2); } return result; } function modifyCSS(DOM, CSS) { for (var key in CSS) { if (CSS.hasOwnProperty(key)) { DOM.style[key] = CSS[key]; } } return DOM; } function createDom(str) { var container = document.createElement('div'); str = str.replace(/(^\s*)|(\s*$)/g, ''); container.innerHTML = '' + str; return container.childNodes[0]; } var Html = /*#__PURE__*/function (_GuideBase) { (0, _inheritsLoose2["default"])(Html, _GuideBase); var _super = _createSuper(Html); function Html() { return _GuideBase.apply(this, arguments) || this; } var _proto = Html.prototype; _proto._initDefaultCfg = function _initDefaultCfg() { this.type = 'html'; /** * dom position * @type {Object | Array} */ this.position = null; /** * alignment for horizontal direction,can be 'left','center','right' * @type {String} */ this.alignX = 'center'; /** * alignment for vertical direction,can be 'top', 'middle', 'bottom' * @type {String} */ this.alignY = 'middle'; /** * offset for horizontal direction * @type {Number} */ this.offsetX = null; /** * offset for vertical direction * @type {Number} */ this.offsetY = null; /** * the html string *@type {String | Function} */ this.html = null; } // override paint ; _proto.render = function render(coord, container) { var self = this; var position = self.parsePoint(coord, self.position); if (!position) { return; } var myNode = createDom(self.html); myNode = modifyCSS(myNode, { position: 'absolute', top: Math.floor(position.y) + 'px', left: Math.floor(position.x) + 'px', visibility: 'hidden' }); var canvasDom = container.get('canvas').get('el'); var parentNode = canvasDom.parentNode; parentNode = modifyCSS(parentNode, { position: 'relative' }); var wrapperNode = createDom('
'); parentNode.appendChild(wrapperNode); wrapperNode.appendChild(myNode); var canvasOffsetTop = canvasDom.offsetTop; var canvasOffsetLeft = canvasDom.offsetLeft; var alignX = self.alignX, alignY = self.alignY, offsetX = self.offsetX, offsetY = self.offsetY; var width = Util.getWidth(myNode); var height = Util.getHeight(myNode); var newOffset = getOffsetFromAlign(alignX, alignY, width, height); position.x = position.x + newOffset[0] + canvasOffsetLeft; position.y = position.y + newOffset[1] + canvasOffsetTop; if (offsetX) { position.x += offsetX; } if (offsetY) { position.y += offsetY; } modifyCSS(myNode, { top: Math.floor(position.y) + 'px', left: Math.floor(position.x) + 'px', visibility: 'visible' }); self.element = wrapperNode; }; _proto.remove = function remove() { var element = this.element; element && element.parentNode && element.parentNode.removeChild(element); }; return Html; }(GuideBase); GuideBase.Html = Html; module.exports = Html;