|
| 1 | +// the selection box thing... |
| 2 | + |
| 3 | +let selectDragActive = false; |
| 4 | +const startPosition = { x: 0, y: 0 }; |
| 5 | +const endPosition = { x: 0, y: 0 }; |
| 6 | + |
| 7 | +window.addEventListener("mousemove", (event) => { |
| 8 | + if (event.buttons & 1) { |
| 9 | + // Starting a new drag |
| 10 | + if (!selectDragActive) { |
| 11 | + if (didClickWindow(event.target)) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + selectDragActive = true; |
| 16 | + startPosition.x = event.clientX; |
| 17 | + startPosition.y = event.clientY; |
| 18 | + } |
| 19 | + |
| 20 | + endPosition.x = event.clientX; |
| 21 | + endPosition.y = event.clientY; |
| 22 | + |
| 23 | + doSelectionBoxLogic(getSanePositions()); |
| 24 | + } else { |
| 25 | + selectDragActive = false; |
| 26 | + } |
| 27 | + displaySelectionBox(); |
| 28 | +}); |
| 29 | + |
| 30 | +window.addEventListener("mouseup", () => { |
| 31 | + if (selectDragActive) { |
| 32 | + selectDragActive = false; |
| 33 | + doSelectionBoxLogic(getSanePositions()); |
| 34 | + displaySelectionBox(); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +const didClickWindow = (element) => { |
| 39 | + return !!findParentOfClass(element, "draggable"); |
| 40 | +}; |
| 41 | + |
| 42 | +const findParentOfClass = (element, classname) => { |
| 43 | + if (!element) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + if (!element.parentElement) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + const classList = element.classList; |
| 52 | + if (!classList.contains(classname)) { |
| 53 | + return findParentOfClass(element.parentElement, classname); |
| 54 | + } |
| 55 | + |
| 56 | + return element; |
| 57 | +}; |
| 58 | + |
| 59 | +const getSanePositions = () => { |
| 60 | + // organize |
| 61 | + const { x: sX, y: sY } = startPosition; |
| 62 | + const { x: eX, y: eY } = endPosition; |
| 63 | + |
| 64 | + const width = Math.abs(sX - eX); |
| 65 | + const height = Math.abs(sY - eY); |
| 66 | + |
| 67 | + // Determine top left of selected box |
| 68 | + const x = sX < eX ? sX : eX; |
| 69 | + const y = sY < eY ? sY : eY; |
| 70 | + |
| 71 | + return { x, y, width, height }; |
| 72 | +}; |
| 73 | + |
| 74 | +const containedInSelection = (x, y, width, height, positions) => { |
| 75 | + const rightEdge = x + width; |
| 76 | + const bottomEdge = y + height; |
| 77 | + |
| 78 | + const containerRightEdge = positions.x + positions.width; |
| 79 | + const containerBottomEdge = positions.y + positions.height; |
| 80 | + |
| 81 | + const xContained = (x >= positions.x || rightEdge >= positions.x) && !(x >= containerRightEdge); |
| 82 | + |
| 83 | + const yContained = (y >= positions.y || bottomEdge >= positions.y) && !(y >= containerBottomEdge); |
| 84 | + |
| 85 | + return xContained && yContained; |
| 86 | +}; |
| 87 | + |
| 88 | +const doSelectionBoxLogic = (boxPositions) => { |
| 89 | + const desktopItems = Array.from(document.querySelectorAll(".desktop > div")); |
| 90 | + desktopItems.forEach((element) => { |
| 91 | + const bounds = element.getBoundingClientRect(); |
| 92 | + const contained = containedInSelection(bounds.x, bounds.y, bounds.width, bounds.height, boxPositions); |
| 93 | + |
| 94 | + if (contained) { |
| 95 | + element.classList.add("selected"); |
| 96 | + } else { |
| 97 | + element.classList.remove("selected"); |
| 98 | + } |
| 99 | + }); |
| 100 | +}; |
| 101 | + |
| 102 | +const displaySelectionBox = () => { |
| 103 | + const selectionBox = document.querySelector(".selection-box"); |
| 104 | + |
| 105 | + if (!selectDragActive) { |
| 106 | + selectionBox.style.visibility = "hidden"; |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const { x, y, width, height } = getSanePositions(); |
| 111 | + |
| 112 | + // apply to css |
| 113 | + selectionBox.style.visibility = "visible"; |
| 114 | + selectionBox.style.width = `${width}px`; |
| 115 | + selectionBox.style.height = `${height}px`; |
| 116 | + selectionBox.style.left = `${x}px`; |
| 117 | + selectionBox.style.top = `${y}px`; |
| 118 | + // selectionBox.style.left = `${100}px`; |
| 119 | +}; |
| 120 | + |
| 121 | +const desktopClickHandler = (event) => { |
| 122 | + const positions = { |
| 123 | + x: event.pageX, |
| 124 | + y: event.pageY, |
| 125 | + width: 1, |
| 126 | + height: 1, |
| 127 | + }; |
| 128 | + |
| 129 | + doSelectionBoxLogic(positions); |
| 130 | +}; |
| 131 | + |
| 132 | +window.addEventListener("mousedown", desktopClickHandler); |
| 133 | + |
| 134 | +// Doubleclick |
| 135 | + |
| 136 | +const desktopDoubleclickHandler = (event) => { |
| 137 | + const target = event.target; |
| 138 | + |
| 139 | + if (!target) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + const parent = findParentOfClass(target, "icon"); |
| 144 | + const configItem = DESKTOP_CONFIG[parent.id]; |
| 145 | + |
| 146 | + if (!configItem) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + configItem?.clickHandler(); |
| 151 | +}; |
| 152 | + |
| 153 | +window.addEventListener("load", () => { |
| 154 | + const desktopIcons = document.querySelectorAll(".desktop > .icon"); |
| 155 | + |
| 156 | + desktopIcons.forEach((icon) => { |
| 157 | + icon.addEventListener("dblclick", desktopDoubleclickHandler); |
| 158 | + }); |
| 159 | +}); |
0 commit comments