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;
|
||||
|
||||
Reference in New Issue
Block a user