naive-upload
Version:
<p align="left"> <a href="https://www.npmjs.org/package/naive-upload"> <img src="https://img.shields.io/npm/v/naive-upload.svg"> </a> <a href="https://bundlephobia.com/package/naive-upload@latest"> <img src="https://img.shields.io/bundl
83 lines (82 loc) • 3.77 kB
JavaScript
var m = Object.defineProperty;
var E = (h, s, e) => s in h ? m(h, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[s] = e;
var t = (h, s, e) => (E(h, typeof s != "symbol" ? s + "" : s, e), e);
class l {
constructor() {
t(this, "containerEl");
t(this, "el");
t(this, "key", -1);
t(this, "els");
t(this, "isMobile", !1);
t(this, "flag", !1);
t(this, "x", 0);
t(this, "y", 0);
t(this, "currentX", 0);
t(this, "currentY", 0);
t(this, "offsetX", 0);
t(this, "offsetY", 0);
t(this, "scrollX", 0);
t(this, "scrollY", 0);
t(this, "transX", 0);
t(this, "transY", 0);
t(this, "lastTransX", 0);
t(this, "lastTransY", 0);
t(this, "zIndex", "");
t(this, "position", "");
t(this, "transform", "");
t(this, "transLate", 1);
t(this, "restoreError", [20, 10]);
t(this, "mouseMove");
t(this, "scroll");
}
moving(s, e) {
this.currentX = s, this.currentY = e, this.checkRestore() ? (this.transX = 0, this.transY = 0, this.el.style.transform = this.transform) : (this.transX = this.currentX - this.x + this.scrollX + this.offsetX, this.transY = this.currentY - this.y + this.scrollY + this.offsetY, (Math.abs(this.transX - this.lastTransX) >= this.transLate || Math.abs(this.transY - this.lastTransY) >= this.transLate) && (this.el.style.transform = `translate(${this.transX}px, ${this.transY}px)`)), this.lastTransX = this.transX, this.lastTransY = this.transY;
}
checkRestore() {
return l.equalError(this.x, this.currentX, this.restoreError[0]) && l.equalError(this.y, this.currentY, this.restoreError[1]);
}
static equalError(s, e, r) {
return e + r >= s && e - r <= s;
}
static getInstance(s, e, r, n) {
let i = new l();
return i.els = e, i.el = e.get(r), i.key = r, i.containerEl = s, i.isMobile = n, i;
}
start(s, e, r) {
this.flag = !0, this.currentX = s, this.currentY = e, this.save(), this.el.style.zIndex = "999";
let n = !1;
this.mouseMove = (i) => {
if (!this.flag || n)
return;
const a = this.isMobile ? i.targetTouches[0].clientX : i.clientX, f = this.isMobile ? i.targetTouches[0].clientY : i.clientY;
if (i.preventDefault(), n = !0, this.moving(a, f), r && this.els) {
let c = !1;
this.els.forEach((o, u) => {
if (u == this.key)
return;
const X = this.el.offsetTop + this.transY, Y = this.el.offsetLeft + this.transX;
X > o.offsetTop && X < o.offsetTop + o.offsetHeight && Y > o.offsetLeft && Y < o.offsetLeft + o.offsetWidth && (r(u, a, f), c = !0);
}), c || r(-1, a, f);
}
n = !1;
}, this.isMobile ? this.containerEl.addEventListener("touchmove", this.mouseMove) : this.containerEl.addEventListener("mousemove", this.mouseMove), this.scroll = (i) => {
this.scrollX = this.containerEl.scrollLeft, this.scrollY = this.containerEl.scrollTop;
}, this.containerEl.addEventListener("scroll", this.scroll);
}
offset(s, e) {
this.offsetX = s, this.offsetY = e;
}
save() {
this.x = this.currentX, this.y = this.currentY, this.zIndex = this.el.style.zIndex, this.position = this.el.style.position, this.transform = this.el.style.transform;
}
restore() {
this.flag = !1, this.x = this.currentX, this.y = this.currentY, this.el.style.zIndex = this.zIndex, this.el.style.position = this.position, this.el.style.transform = this.transform;
}
end(s) {
s && this.restore(), this.isMobile ? this.containerEl.removeEventListener("touchmove", this.mouseMove) : this.containerEl.removeEventListener("mousemove", this.mouseMove), this.containerEl.removeEventListener("scroll", this.scroll);
}
}
export {
l as default
};
//# sourceMappingURL=DraggingHelper.js.map