UNPKG

drift-zoom

Version:

Easily add "zoom on hover" functionality to your site's images. Lightweight, no-dependency JavaScript.

110 lines (91 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _throwIfMissing = _interopRequireDefault(require("./util/throwIfMissing")); var _dom = require("./util/dom"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var BoundingBox = /*#__PURE__*/ function () { function BoundingBox(options) { _classCallCheck(this, BoundingBox); this.isShowing = false; var _options$namespace = options.namespace, namespace = _options$namespace === void 0 ? null : _options$namespace, _options$zoomFactor = options.zoomFactor, zoomFactor = _options$zoomFactor === void 0 ? (0, _throwIfMissing.default)() : _options$zoomFactor, _options$containerEl = options.containerEl, containerEl = _options$containerEl === void 0 ? (0, _throwIfMissing.default)() : _options$containerEl; this.settings = { namespace: namespace, zoomFactor: zoomFactor, containerEl: containerEl }; this.openClasses = this._buildClasses("open"); this._buildElement(); } _createClass(BoundingBox, [{ key: "_buildClasses", value: function _buildClasses(suffix) { var classes = ["drift-".concat(suffix)]; var ns = this.settings.namespace; if (ns) { classes.push("".concat(ns, "-").concat(suffix)); } return classes; } }, { key: "_buildElement", value: function _buildElement() { this.el = document.createElement("div"); (0, _dom.addClasses)(this.el, this._buildClasses("bounding-box")); } }, { key: "show", value: function show(zoomPaneWidth, zoomPaneHeight) { this.isShowing = true; this.settings.containerEl.appendChild(this.el); var style = this.el.style; style.width = "".concat(Math.round(zoomPaneWidth / this.settings.zoomFactor), "px"); style.height = "".concat(Math.round(zoomPaneHeight / this.settings.zoomFactor), "px"); (0, _dom.addClasses)(this.el, this.openClasses); } }, { key: "hide", value: function hide() { if (this.isShowing) { this.settings.containerEl.removeChild(this.el); } this.isShowing = false; (0, _dom.removeClasses)(this.el, this.openClasses); } }, { key: "setPosition", value: function setPosition(percentageOffsetX, percentageOffsetY, triggerRect) { var pageXOffset = window.pageXOffset; var pageYOffset = window.pageYOffset; var inlineLeft = triggerRect.left + percentageOffsetX * triggerRect.width - this.el.clientWidth / 2 + pageXOffset; var inlineTop = triggerRect.top + percentageOffsetY * triggerRect.height - this.el.clientHeight / 2 + pageYOffset; if (inlineLeft < triggerRect.left + pageXOffset) { inlineLeft = triggerRect.left + pageXOffset; } else if (inlineLeft + this.el.clientWidth > triggerRect.left + triggerRect.width + pageXOffset) { inlineLeft = triggerRect.left + triggerRect.width - this.el.clientWidth + pageXOffset; } if (inlineTop < triggerRect.top + pageYOffset) { inlineTop = triggerRect.top + pageYOffset; } else if (inlineTop + this.el.clientHeight > triggerRect.top + triggerRect.height + pageYOffset) { inlineTop = triggerRect.top + triggerRect.height - this.el.clientHeight + pageYOffset; } this.el.style.left = "".concat(inlineLeft, "px"); this.el.style.top = "".concat(inlineTop, "px"); } }]); return BoundingBox; }(); exports.default = BoundingBox; //# sourceMappingURL=BoundingBox.js.map