« get me outta code hell

client: refactor point-over-element into utility - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-01-09 13:14:03 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-09 13:21:51 -0400
commit708698289ab1a544c2ce066ade7171ff87108f82 (patch)
treedbdd262ffc5bba5b13f6be8a8ef5a617e2f6c4be /src
parent907f81c3ddad14e028b1b5aaea8c9cd8d6b2c2df (diff)
client: refactor point-over-element into utility
Diffstat (limited to 'src')
-rw-r--r--src/static/client3.js32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/static/client3.js b/src/static/client3.js
index 6174e5c4..35d9ab69 100644
--- a/src/static/client3.js
+++ b/src/static/client3.js
@@ -102,6 +102,14 @@ function cssProp(el, ...args) {
   }
 }
 
+// Curry-style, so multiple points can more conveniently be tested at once.
+function pointIsOverAnyOf(elements) {
+  return (clientX, clientY) => {
+    const element = document.elementFromPoint(clientX, clientY);
+    return elements.some(el => el.contains(element));
+  };
+}
+
 // TODO: These should pro8a8ly access some shared urlSpec path. We'd need to
 // separ8te the tooling around that into common-shared code too.
 const getLinkHref = (type, directory) => rebase(`${type}/${directory}`);
@@ -767,10 +775,12 @@ function handleTooltipHoverableTouchEnded(hoverable, domEvent) {
 
   // Don't proceed if none of the (just-ended) touches ended over the
   // hoverable.
+
+  const pointIsOverThisHoverable = pointIsOverAnyOf([hoverable]);
+
   const anyTouchEndedOverHoverable =
-    touches.some(touch =>
-      hoverable.contains(
-        document.elementFromPoint(touch.clientX, touch.clientY)));
+    touches.some(({clientX, clientY}) =>
+      pointIsOverThisHoverable(clientX, clientY));
 
   if (!anyTouchEndedOverHoverable) {
     return;
@@ -990,6 +1000,11 @@ function addHoverableTooltipPageListeners() {
     });
   });
 
+  const getHoverablesAndTooltips = () => [
+    ...Array.from(state.registeredHoverables.keys()),
+    ...Array.from(state.registeredTooltips.keys()),
+  ];
+
   document.body.addEventListener('touchend', domEvent => {
     const hoverables = Array.from(state.registeredHoverables.keys());
     const tooltips = Array.from(state.registeredTooltips.keys());
@@ -1006,13 +1021,12 @@ function addHoverableTooltipPageListeners() {
 
     if (empty(touches)) return;
 
+    const pointIsOverHoverableOrTooltip =
+      pointIsOverAnyOf(getHoverablesAndTooltips());
+
     const anyTouchOverAnyHoverableOrTooltip =
-      touches.some(({clientX, clientY}) => {
-        const element = document.elementFromPoint(clientX, clientY);
-        if (hoverables.some(el => el.contains(element))) return true;
-        if (tooltips.some(el => el.contains(element))) return true;
-        return false;
-      });
+      touches.some(({clientX, clientY}) =>
+        pointIsOverHoverableOrTooltip(clientX, clientY));
 
     if (!anyTouchOverAnyHoverableOrTooltip) {
       hideCurrentlyShownTooltip();