-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraggable_modals.js
More file actions
76 lines (61 loc) · 2.71 KB
/
draggable_modals.js
File metadata and controls
76 lines (61 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function makeWindowsDraggableAndResizable() {
let activeWindow = null;
let action = null;
let offsetX, offsetY, startX, startY, startWidth, startHeight;
function onMouseDown(e) {
const target = e.target;
const draggableWindow = target.closest('.player-window');
if (!draggableWindow) return;
if (typeof setActivePlayer === 'function') {
setActivePlayer(draggableWindow.id);
}
if (target.classList.contains('resize-handle')) {
action = 'resize';
} else if (target.closest('.modal-header-draggable')) {
action = 'drag';
} else {
action = null;
}
if (action) {
e.preventDefault();
activeWindow = draggableWindow;
if (action === 'drag') {
offsetX = e.clientX - activeWindow.getBoundingClientRect().left;
offsetY = e.clientY - activeWindow.getBoundingClientRect().top;
} else if (action === 'resize') {
startX = e.clientX;
startY = e.clientY;
startWidth = parseInt(document.defaultView.getComputedStyle(activeWindow).width, 10);
startHeight = parseInt(document.defaultView.getComputedStyle(activeWindow).height, 10);
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
}
}
function onMouseMove(e) {
if (!activeWindow) return;
if (action === 'drag') {
let x = e.clientX - offsetX;
let y = e.clientY - offsetY;
const container = document.getElementById('app-container');
const containerRect = container.getBoundingClientRect();
const windowRect = activeWindow.getBoundingClientRect();
x = Math.max(containerRect.left, Math.min(x, containerRect.right - windowRect.width));
y = Math.max(containerRect.top, Math.min(y, containerRect.bottom - windowRect.height));
activeWindow.style.left = x + 'px';
activeWindow.style.top = y + 'px';
} else if (action === 'resize') {
const newWidth = startWidth + (e.clientX - startX);
const newHeight = startHeight + (e.clientY - startY);
activeWindow.style.width = Math.max(400, newWidth) + 'px';
activeWindow.style.height = Math.max(300, newHeight) + 'px';
}
}
function onMouseUp() {
activeWindow = null;
action = null;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
}
document.addEventListener('mousedown', onMouseDown);
}