manual submission redesign phase B
This commit is contained in:
@@ -3,20 +3,19 @@
|
||||
|
||||
const manualForm = document.querySelector("#manual-report-form");
|
||||
const manualSubmit = document.querySelector("#manual-submit");
|
||||
const occurrences = document.querySelector("#manual-occurrences");
|
||||
const decrease = document.querySelector("#manual-decrease");
|
||||
const increase = document.querySelector("#manual-increase");
|
||||
const manualStatus = document.querySelector("#manual-submission");
|
||||
const latitude = document.querySelector("#manual-latitude");
|
||||
const longitude = document.querySelector("#manual-longitude");
|
||||
const directionDegrees = document.querySelector("#manual-direction-degrees");
|
||||
const directionAccuracy = document.querySelector("#manual-direction-accuracy");
|
||||
const captureDirectionButton = document.querySelector("#manual-capture-direction");
|
||||
const directionStatus = document.querySelector("#manual-direction-status");
|
||||
const directionPanel = document.querySelector("#manual-direction-panel");
|
||||
const captureDirectionButton = () => document.querySelector("#manual-capture-direction");
|
||||
const directionStatus = () => document.querySelector("#manual-direction-status");
|
||||
const compass = document.querySelector("#manual-compass");
|
||||
const compassRose = document.querySelector("#manual-compass-rose");
|
||||
const compassHeading = document.querySelector("#manual-compass-heading");
|
||||
const isMobileDevice = /Android|iPhone|iPad|iPod|IEMobile|Opera Mini/.test(navigator.userAgent)
|
||||
|| (navigator.userAgent.includes("Macintosh") && navigator.maxTouchPoints > 1);
|
||||
if (isMobileDevice) directionPanel.hidden = false;
|
||||
let orientationListener;
|
||||
let compassUnavailableTimeout;
|
||||
let compassRotation = null;
|
||||
@@ -26,6 +25,17 @@
|
||||
const cardinalDirection = (heading) => ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor(((heading + 22.5) % 360) / 45)];
|
||||
const displayDegrees = (value) => Number(value.toFixed(1)).toString();
|
||||
|
||||
const setCount = (value) => {
|
||||
const count = Number.parseInt(value, 10);
|
||||
occurrences.value = String(Math.min(1000, Math.max(1, Number.isFinite(count) ? count : 1)));
|
||||
decrease.disabled = occurrences.value === "1";
|
||||
increase.disabled = occurrences.value === "1000";
|
||||
};
|
||||
setCount(occurrences.value);
|
||||
decrease.addEventListener("click", () => setCount(Number(occurrences.value) - 1));
|
||||
increase.addEventListener("click", () => setCount(Number(occurrences.value) + 1));
|
||||
occurrences.addEventListener("change", () => setCount(occurrences.value));
|
||||
|
||||
const stopCompassReading = () => {
|
||||
if (orientationListener) window.removeEventListener("deviceorientation", orientationListener);
|
||||
window.clearTimeout(compassUnavailableTimeout);
|
||||
@@ -56,13 +66,16 @@
|
||||
|
||||
const showCapturedDirection = (heading, accuracy) => {
|
||||
const accuracyText = accuracy === null ? (t.accuracyUnavailable || "Accuracy unavailable") : (t.accuracy || "Accuracy") + ": ±" + displayDegrees(accuracy) + "°";
|
||||
directionStatus.innerHTML = "<strong>" + displayDegrees(heading) + "° · " + cardinalDirection(heading) + "</strong><br>" + accuracyText;
|
||||
captureDirectionButton.textContent = (t.stopCapture || "Stop direction capture");
|
||||
const status = directionStatus();
|
||||
if (status) status.innerHTML = "<strong>" + cardinalDirection(heading) + " · " + displayDegrees(heading) + "°</strong><br>" + accuracyText;
|
||||
const button = captureDirectionButton();
|
||||
if (button) button.textContent = (t.stopCapture || "Stop measuring direction");
|
||||
};
|
||||
|
||||
const listenForCompassReading = () => {
|
||||
if (!directionStatus()) return;
|
||||
stopCompassReading();
|
||||
directionStatus.textContent = (t.pointPhone || "Point the top of your phone toward the sound — direction will update live.");
|
||||
directionStatus().textContent = (t.pointPhone || "Point the top of your phone toward the sound — direction will update live.");
|
||||
orientationListener = (event) => {
|
||||
const isWebKitCompass = "webkitCompassHeading" in event;
|
||||
const heading = isUsableNumber(event.webkitCompassHeading)
|
||||
@@ -84,34 +97,36 @@
|
||||
window.addEventListener("deviceorientation", orientationListener);
|
||||
compassUnavailableTimeout = window.setTimeout(() => {
|
||||
stopCompassReading();
|
||||
captureDirectionButton.textContent = (t.captureDirection || "Capture direction");
|
||||
directionStatus.textContent = (t.compassUnavailable || "Compass unavailable on this device.");
|
||||
const button = captureDirectionButton();
|
||||
const status = directionStatus();
|
||||
if (button) button.textContent = (t.captureDirection || "Start measuring direction");
|
||||
if (status) status.textContent = (t.compassUnavailable || "Compass unavailable on this device.");
|
||||
}, 15000);
|
||||
};
|
||||
|
||||
const captureDirection = async () => {
|
||||
if (orientationListener) {
|
||||
stopCompassReading();
|
||||
captureDirectionButton.textContent = (t.captureAgain || "Capture again");
|
||||
captureDirectionButton().textContent = (t.captureAgain || "Measure again");
|
||||
return;
|
||||
}
|
||||
if (!window.isSecureContext) {
|
||||
directionStatus.textContent = (t.secureCompass || "Compass capture requires a secure HTTPS connection.");
|
||||
directionStatus().textContent = (t.secureCompass || "Compass capture requires a secure HTTPS connection.");
|
||||
return;
|
||||
}
|
||||
if (typeof window.DeviceOrientationEvent === "undefined") {
|
||||
directionStatus.textContent = (t.compassUnavailable || "Compass unavailable on this device.");
|
||||
directionStatus().textContent = (t.compassUnavailable || "Compass unavailable on this device.");
|
||||
return;
|
||||
}
|
||||
if (typeof DeviceOrientationEvent.requestPermission === "function") {
|
||||
directionStatus.textContent = (t.compassPermission || "Requesting compass permission…");
|
||||
directionStatus().textContent = (t.compassPermission || "Requesting compass permission…");
|
||||
try {
|
||||
if (await DeviceOrientationEvent.requestPermission(true) !== "granted") {
|
||||
directionStatus.textContent = (t.compassDenied || "Compass permission denied.");
|
||||
if (directionStatus()) directionStatus().textContent = (t.compassDenied || "Compass permission denied.");
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
directionStatus.textContent = (t.compassDenied || "Compass permission denied.");
|
||||
if (directionStatus()) directionStatus().textContent = (t.compassDenied || "Compass permission denied.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -119,6 +134,7 @@
|
||||
};
|
||||
|
||||
const submitManualReport = async () => {
|
||||
setCount(occurrences.value);
|
||||
stopCompassReading();
|
||||
manualSubmit.disabled = true;
|
||||
if (burstListener?.active) {
|
||||
@@ -148,8 +164,21 @@
|
||||
}
|
||||
};
|
||||
|
||||
captureDirectionButton.addEventListener("click", captureDirection);
|
||||
manualForm.addEventListener("click", (event) => {
|
||||
if (event.target.closest("#manual-capture-direction")) captureDirection();
|
||||
if (event.target.closest(".manual-direction-panel__close")) stopCompassReading();
|
||||
});
|
||||
document.body.addEventListener("htmx:afterSwap", (event) => {
|
||||
if (event.detail.target?.id !== "manual-direction-panel") return;
|
||||
const summary = document.querySelector("#manual-direction-summary");
|
||||
if (summary && directionDegrees.value !== "") {
|
||||
const heading = Number(directionDegrees.value);
|
||||
summary.textContent = `${cardinalDirection(heading)} · ${displayDegrees(heading)}°`;
|
||||
}
|
||||
});
|
||||
manualSubmit.addEventListener("click", () => {
|
||||
if (manualSubmit.disabled) return;
|
||||
manualSubmit.disabled = true;
|
||||
if (!navigator.geolocation) {
|
||||
submitManualReport();
|
||||
return;
|
||||
|
||||
@@ -16,18 +16,48 @@
|
||||
}
|
||||
|
||||
.manual-report__quantity #manual-occurrences {
|
||||
margin-bottom: 0;
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
min-height: 3.75rem;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
color: #f5f0ff;
|
||||
font-size: clamp(2.25rem, 8vw, 3.5rem);
|
||||
font-weight: 850;
|
||||
text-align: center;
|
||||
background: transparent;
|
||||
appearance: textfield;
|
||||
-moz-appearance: textfield;
|
||||
}
|
||||
|
||||
.manual-report__quantity #manual-occurrences::-webkit-inner-spin-button,
|
||||
.manual-report__quantity #manual-occurrences::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
|
||||
|
||||
.manual-report__stepper { display: grid; grid-template-columns: 3.5rem minmax(0, 1fr) 3.5rem; align-items: center; gap: .5rem; }
|
||||
.manual-report__stepper button { width: 3.5rem; min-height: 3.5rem; border: 1px solid #6d54ac; border-radius: 50%; color: #f5f0ff; font: inherit; font-size: 2rem; background: #302553; cursor: pointer; }
|
||||
.manual-report__stepper button:disabled { opacity: .4; cursor: default; }
|
||||
|
||||
.manual-report__primary .manual-direction-panel {
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
border-radius: 1rem;
|
||||
background: #1a1739;
|
||||
}
|
||||
|
||||
.manual-direction-panel__topline { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
|
||||
.manual-direction-panel__summary { margin: .35rem 0 .8rem; color: #f5f0ff; font-weight: 700; }
|
||||
.manual-direction-panel__open, .manual-direction-panel__close { min-height: 2.75rem; border: 1px solid #8d77d2; border-radius: .7rem; color: #e8dcff; font: inherit; font-weight: 750; background: #302553; cursor: pointer; }
|
||||
.manual-direction-panel__open { padding: .5rem .85rem; }
|
||||
.manual-direction-panel__close { min-width: 2.75rem; font-size: 1.4rem; }
|
||||
|
||||
.manual-report__primary .manual-report__submit {
|
||||
width: 100%;
|
||||
min-height: 4.5rem;
|
||||
}
|
||||
|
||||
.manual-report button:focus-visible, .manual-report input:focus-visible { outline: 3px solid #c9b4ff; outline-offset: 3px; }
|
||||
|
||||
.manual-report__supporting {
|
||||
max-width: 36rem;
|
||||
margin-top: 1.5rem;
|
||||
@@ -50,6 +80,12 @@
|
||||
background: #fff8fb;
|
||||
}
|
||||
|
||||
:root[data-theme="light"] .manual-report__quantity #manual-occurrences { color: #321d42; }
|
||||
:root[data-theme="light"] .manual-report__stepper button,
|
||||
:root[data-theme="light"] .manual-direction-panel__open,
|
||||
:root[data-theme="light"] .manual-direction-panel__close { color: #321d42; border-color: #b68bb5; background: #ead2eb; }
|
||||
:root[data-theme="light"] .manual-direction-panel__summary { color: #321d42; }
|
||||
|
||||
:root[data-theme="light"] .manual-report__supporting {
|
||||
border-color: #d9b4d8;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ test("compass stays hidden until permission and a heading, then rotates and hide
|
||||
<form id="manual-report-form" action="/submit">
|
||||
<input id="manual-latitude"><input id="manual-longitude">
|
||||
<input id="manual-direction-degrees"><input id="manual-direction-accuracy">
|
||||
<button id="manual-decrease" type="button"></button><input id="manual-occurrences" value="1"><button id="manual-increase" type="button"></button>
|
||||
<div id="manual-direction-panel" hidden><button id="manual-capture-direction" type="button"></button><div id="manual-direction-status"></div></div>
|
||||
<button id="manual-submit" type="button"></button>
|
||||
<button id="burst-start" type="button"></button><div id="burst-panel"></div><strong id="burst-heading"></strong>
|
||||
|
||||
Reference in New Issue
Block a user