126 lines
6.1 KiB
JavaScript
126 lines
6.1 KiB
JavaScript
|
|
const manualForm = document.querySelector("#manual-report-form");
|
|
const manualSubmit = document.querySelector("#manual-submit");
|
|
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 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;
|
|
|
|
const isUsableNumber = (value) => Number.isFinite(value);
|
|
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 stopCompassReading = () => {
|
|
if (orientationListener) window.removeEventListener("deviceorientation", orientationListener);
|
|
window.clearTimeout(compassUnavailableTimeout);
|
|
orientationListener = null;
|
|
compassUnavailableTimeout = null;
|
|
};
|
|
|
|
const showCapturedDirection = (heading, accuracy) => {
|
|
const accuracyText = accuracy === null ? "Accuracy unavailable" : "Accuracy: ±" + displayDegrees(accuracy) + "°";
|
|
directionStatus.innerHTML = "<strong>" + displayDegrees(heading) + "° · " + cardinalDirection(heading) + "</strong><br>" + accuracyText;
|
|
captureDirectionButton.textContent = "Stop direction capture";
|
|
};
|
|
|
|
const listenForCompassReading = () => {
|
|
stopCompassReading();
|
|
directionStatus.textContent = "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)
|
|
? event.webkitCompassHeading
|
|
: !isWebKitCompass && event.absolute && isUsableNumber(event.alpha)
|
|
? (360 - event.alpha) % 360
|
|
: null;
|
|
if (heading === null || heading < 0 || heading >= 360) return;
|
|
|
|
const accuracy = isUsableNumber(event.webkitCompassAccuracy) && event.webkitCompassAccuracy >= 0
|
|
? event.webkitCompassAccuracy
|
|
: null;
|
|
directionDegrees.value = heading;
|
|
directionAccuracy.value = accuracy === null ? "" : accuracy;
|
|
showCapturedDirection(heading, accuracy);
|
|
window.clearTimeout(compassUnavailableTimeout);
|
|
};
|
|
window.addEventListener("deviceorientation", orientationListener);
|
|
compassUnavailableTimeout = window.setTimeout(() => {
|
|
stopCompassReading();
|
|
captureDirectionButton.textContent = "Capture direction";
|
|
directionStatus.textContent = "Compass unavailable on this device.";
|
|
}, 15000);
|
|
};
|
|
|
|
const captureDirection = async () => {
|
|
if (orientationListener) {
|
|
stopCompassReading();
|
|
captureDirectionButton.textContent = "Capture again";
|
|
return;
|
|
}
|
|
if (!window.isSecureContext) {
|
|
directionStatus.textContent = "Compass capture requires a secure HTTPS connection.";
|
|
return;
|
|
}
|
|
if (typeof window.DeviceOrientationEvent === "undefined") {
|
|
directionStatus.textContent = "Compass unavailable on this device.";
|
|
return;
|
|
}
|
|
if (typeof DeviceOrientationEvent.requestPermission === "function") {
|
|
directionStatus.textContent = "Requesting compass permission…";
|
|
try {
|
|
if (await DeviceOrientationEvent.requestPermission(true) !== "granted") {
|
|
directionStatus.textContent = "Compass permission denied.";
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
directionStatus.textContent = "Compass permission denied.";
|
|
return;
|
|
}
|
|
}
|
|
listenForCompassReading();
|
|
};
|
|
|
|
const submitManualReport = () => {
|
|
stopCompassReading();
|
|
manualSubmit.disabled = true;
|
|
if (window.htmx) {
|
|
htmx.ajax("POST", manualForm.action, {
|
|
source: manualForm,
|
|
target: "#manual-submission",
|
|
swap: "outerHTML",
|
|
});
|
|
} else {
|
|
manualForm.submit();
|
|
}
|
|
};
|
|
|
|
captureDirectionButton.addEventListener("click", captureDirection);
|
|
manualSubmit.addEventListener("click", () => {
|
|
if (!navigator.geolocation) {
|
|
submitManualReport();
|
|
return;
|
|
}
|
|
manualStatus.textContent = "Requesting your optional location…";
|
|
navigator.geolocation.getCurrentPosition(
|
|
(position) => {
|
|
latitude.value = position.coords.latitude;
|
|
longitude.value = position.coords.longitude;
|
|
submitManualReport();
|
|
},
|
|
() => submitManualReport(),
|
|
{ enableHighAccuracy: false, maximumAge: 300000, timeout: 10000 },
|
|
);
|
|
});
|
|
document.body.addEventListener("htmx:afterRequest", (event) => {
|
|
if (event.detail.elt === manualForm) manualSubmit.disabled = false;
|
|
});
|
|
|