-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop.js
More file actions
168 lines (128 loc) · 3.93 KB
/
Copy pathdesktop.js
File metadata and controls
168 lines (128 loc) · 3.93 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// the selection box thing...
let selectDragActive = false;
let firstClickSafe = false;
const startPosition = { x: 0, y: 0 };
const endPosition = { x: 0, y: 0 };
window.addEventListener("mousemove", (event) => {
if (event.buttons & 1) {
if (!selectDragActive && firstClickSafe) {
if (didClickWindow(event.target)) {
return;
}
selectDragActive = true;
startPosition.x = event.clientX;
startPosition.y = event.clientY;
}
// Starting a new drag
endPosition.x = event.clientX;
endPosition.y = event.clientY;
doSelectionBoxLogic(getSanePositions());
} else {
selectDragActive = false;
}
displaySelectionBox();
});
window.addEventListener("mouseup", () => {
firstClickSafe = true;
if (selectDragActive) {
selectDragActive = false;
doSelectionBoxLogic(getSanePositions());
displaySelectionBox();
}
});
const didClickWindow = (element) => {
return !!findParentOfClass(element, "draggable");
};
const findParentOfClass = (element, classname) => {
if (!element) {
return null;
}
if (!element.parentElement) {
return null;
}
const classList = element.classList;
if (!classList.contains(classname)) {
return findParentOfClass(element.parentElement, classname);
}
return element;
};
const getSanePositions = () => {
// organize
const { x: sX, y: sY } = startPosition;
const { x: eX, y: eY } = endPosition;
const width = Math.abs(sX - eX);
const height = Math.abs(sY - eY);
// Determine top left of selected box
const x = sX < eX ? sX : eX;
const y = sY < eY ? sY : eY;
return { x, y, width, height };
};
const containedInSelection = (x, y, width, height, positions) => {
const rightEdge = x + width;
const bottomEdge = y + height;
const containerRightEdge = positions.x + positions.width;
const containerBottomEdge = positions.y + positions.height;
const xContained = (x >= positions.x || rightEdge >= positions.x) && !(x >= containerRightEdge);
const yContained = (y >= positions.y || bottomEdge >= positions.y) && !(y >= containerBottomEdge);
return xContained && yContained;
};
const doSelectionBoxLogic = (boxPositions, bypassDrag = false) => {
if (!selectDragActive && !bypassDrag) {
return;
}
const desktopItems = Array.from(document.querySelectorAll(".desktop > div"));
desktopItems.forEach((element) => {
const bounds = element.getBoundingClientRect();
const contained = containedInSelection(bounds.x, bounds.y, bounds.width, bounds.height, boxPositions);
if (contained) {
element.classList.add("selected");
} else {
element.classList.remove("selected");
}
});
};
const displaySelectionBox = () => {
const selectionBox = document.querySelector(".selection-box");
if (!selectDragActive) {
selectionBox.style.visibility = "hidden";
return;
}
const { x, y, width, height } = getSanePositions();
// apply to css
selectionBox.style.visibility = "visible";
selectionBox.style.width = `${width}px`;
selectionBox.style.height = `${height}px`;
selectionBox.style.left = `${x}px`;
selectionBox.style.top = `${y}px`;
// selectionBox.style.left = `${100}px`;
};
const desktopClickHandler = (event) => {
firstClickSafe = !didClickWindow(event.target);
const positions = {
x: event.pageX,
y: event.pageY,
width: 1,
height: 1,
};
doSelectionBoxLogic(positions, true);
};
window.addEventListener("mousedown", desktopClickHandler);
// Doubleclick
const desktopDoubleclickHandler = (event) => {
const target = event.target;
if (!target) {
return;
}
const parent = findParentOfClass(target, "icon");
const configItem = DESKTOP_CONFIG[parent.id];
if (!configItem) {
return;
}
configItem?.clickHandler();
};
window.addEventListener("load", () => {
const desktopIcons = document.querySelectorAll(".desktop > .icon");
desktopIcons.forEach((icon) => {
icon.addEventListener("dblclick", desktopDoubleclickHandler);
});
});