UNPKG

@codedoc/core

Version:

Create beautiful modern documentation websites.

92 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToastSwipe = void 0; function isTouch(event) { return window.TouchEvent && event instanceof TouchEvent; } var ToastSwipe = /** @class */ (function () { function ToastSwipe(container, remove) { var _this = this; this.container = container; this.remove = remove; this.anchor = undefined; this.last = undefined; this.direction = undefined; container.addEventListener('mousedown', function (event) { return _this.start(event); }); container.addEventListener('touchstart', function (event) { return _this.start(event); }); document.addEventListener('mousemove', function (event) { return _this.move(event); }); container.addEventListener('touchmove', function (event) { return _this.move(event); }); document.addEventListener('mouseup', function () { return _this.release(); }); container.addEventListener('touchend', function () { return _this.release(); }); } ToastSwipe.prototype.getPos = function (event) { if (isTouch(event)) { return { x: event.touches[0].clientX, y: event.touches[0].clientY }; } else { return { x: event.clientX, y: event.clientY }; } }; ToastSwipe.prototype.start = function (event) { this.anchor = this.getPos(event); this.last = this.anchor; }; ToastSwipe.prototype.move = function (event) { if (this.anchor) { event.stopPropagation(); event.preventDefault(); var pos = this.getPos(event); this.last = pos; var dx = pos.x - this.anchor.x; var dy = pos.y - this.anchor.y; if (!this.direction) { if (Math.sqrt(dx * dx + dy * dy) > 16) { if (Math.abs(dx) > Math.abs(dy)) { this.direction = 'horizontal'; } else { this.direction = 'vertical'; } } } if (this.direction) { this.container.classList.add('moving'); if (this.direction === 'horizontal') { this.container.style.transform = "translateX(" + dx + "px)"; } else { this.container.style.transform = "translateY(" + dy + "px)"; } } } }; ToastSwipe.prototype.release = function () { if (this.anchor && this.last) { this.container.classList.remove('moving'); var dx = this.last.x - this.anchor.x; var dy = this.last.y - this.anchor.y; if (Math.sqrt(dx * dx + dy * dy) > 128) { if (this.direction === 'horizontal') { this.container.style.transform = "translateX(" + 2 * dx + "px)"; } else { this.container.style.transform = "translateY(" + 2 * dy + "px)"; } this.container.style.opacity = '0'; this.remove(); } else { this.container.style.transform = ''; } this.anchor = undefined; this.last = undefined; this.direction = undefined; } }; ToastSwipe.prototype.active = function () { return this.anchor !== undefined; }; return ToastSwipe; }()); exports.ToastSwipe = ToastSwipe; //# sourceMappingURL=swipe.js.map