Commit 7a49b8cc authored by Akari Labs's avatar Akari Labs

improve clipping logic

parent 34aca9b3
......@@ -54,6 +54,7 @@ function KWindow(attr, children) {
var container;
function makeDraggable(win, header, handle) {
var cX, cY, dX, dY;
function cleanup() {
......@@ -74,50 +75,70 @@ function makeDraggable(win, header, handle) {
updateClientY(e);
}
function updateDelta(e) {
dX = cX - e.clientX;
dY = cY - e.clientY;
function getDelta(e) {
return { x: cX - e.clientX, y: cY - e.clientY };
}
(header || win).onmousedown = e => {
e.preventDefault();
updateClient(e);
document.onmousemove = e => {
e.preventDefault();
updateDelta(e);
const newX = win.offsetLeft - dX;
const newY = win.offsetTop - dY;
if (!(newX < 0 || newX + win.offsetWidth > window.innerWidth)) {
win.style.left = (win.offsetLeft - dX) + 'px';
updateClientX(e);
}
if (!(newY < 0 || newY + win.offsetHeight > window.innerHeight)) {
win.style.top = (win.offsetTop - dY) + 'px';
updateClientY(e);
}
const minX = 0, minY = 0,
maxX = window.innerWidth - win.offsetWidth,
maxY = window.innerHeight - win.offsetHeight;
const delta = getDelta(e);
const newX = win.offsetLeft - delta.x;
const newY = win.offsetTop - delta.y;
win.style.left = Math.min(Math.max(newX, minX), maxX) + 'px';
win.style.top = Math.min(Math.max(newY, minY), maxY) + 'px';
if (newX >= minX && newX <= maxX) updateClientX(e);
if (newY >= minY && newY <= maxY) updateClientY(e);
};
document.onmouseup = cleanup;
};
if (handle) handle.onmousedown = e => {
e.preventDefault();
updateClient(e);
document.onmousemove = e => {
e.preventDefault();
updateDelta(e);
const newW = win.offsetWidth - dX;
const newH = win.offsetHeight - dY;
if (!(newW < 64)) {
win.style.width = (win.offsetWidth - dX) + 'px';
updateClientX(e);
}
if (!(newH < 64)) {
win.style.height = (win.offsetHeight - dY) + 'px';
updateClientY(e);
}
const minW = 64, minH = 64;
const delta = getDelta(e);
const newW = win.offsetWidth - delta.x;
const newH = win.offsetHeight - delta.y;
win.style.width = Math.max(newW, minW) + 'px';
win.style.height = Math.max(newH, minH) + 'px';
if (newW >= minW) updateClientX(e);
if (newH >= minH) updateClientY(e);
};
document.onmouseup = cleanup;
};
}
module.exports = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment