no more losing the window

Signed-off-by: Victor Seiji Hariki <victorseijih@gmail.com>
This commit is contained in:
Victor Seiji Hariki 2022-11-30 18:44:51 -03:00
parent e84ea7a8f4
commit a63228a552
No known key found for this signature in database
GPG key ID: F369E3EA50A0DEEE

View file

@ -1,10 +1,21 @@
function makeDraggable(element) {
const startbb = element.getBoundingClientRect();
let dragging = false;
let offset = {x: 0, y: 0};
element.style.top = startbb.y + "px";
element.style.left = startbb.x + "px";
const margin = 10;
const fixPos = () => {
const dbb = element.getBoundingClientRect();
if (dbb.left < margin) element.style.left = margin + "px";
else if (dbb.right > window.innerWidth - margin)
element.style.left =
dbb.left + (window.innerWidth - margin - dbb.right) + "px";
if (dbb.top < margin) element.style.top = margin + "px";
else if (dbb.bottom > window.innerHeight - margin)
element.style.top =
dbb.top + (window.innerHeight - margin - dbb.bottom) + "px";
};
mouse.listen.window.btn.left.onpaintstart.on((evn) => {
if (
@ -20,14 +31,22 @@ function makeDraggable(element) {
mouse.listen.window.btn.left.onpaint.on((evn) => {
if (dragging) {
element.style.right = null;
element.style.bottom = null;
element.style.top = evn.y - offset.y + "px";
element.style.left = evn.x - offset.x + "px";
fixPos();
}
});
mouse.listen.window.btn.left.onpaintend.on((evn) => {
dragging = false;
});
window.addEventListener("resize", () => {
fixPos();
});
}
document.querySelectorAll(".floating-window").forEach((w) => {