manual submission redesign phase D
This commit is contained in:
@@ -3,16 +3,17 @@
|
||||
|
||||
const manualForm = document.querySelector("#manual-report-form");
|
||||
const manualSubmit = document.querySelector("#manual-submit");
|
||||
const submitLabel = document.querySelector("#manual-submit-label");
|
||||
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 compass = document.querySelector("#manual-compass");
|
||||
const compassRose = document.querySelector("#manual-compass-rose");
|
||||
const compassHeading = document.querySelector("#manual-compass-heading");
|
||||
@@ -24,6 +25,29 @@
|
||||
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 idleSubmitLabel = submitLabel?.textContent || "I heard fireworks";
|
||||
|
||||
const setSubmissionState = (next, message = "") => {
|
||||
manualForm.dataset.state = next;
|
||||
manualSubmit.disabled = next === "submitting";
|
||||
if (submitLabel) submitLabel.textContent = next === "submitting" ? (t.sendingReport || "Sending…") : idleSubmitLabel;
|
||||
if (message) {
|
||||
const status = document.querySelector("#manual-submission");
|
||||
status.className = "manual-report__submission";
|
||||
status.textContent = message;
|
||||
}
|
||||
};
|
||||
|
||||
const showResponse = (html, successful) => {
|
||||
const parsed = new DOMParser().parseFromString(html, "text/html").querySelector("#manual-submission");
|
||||
if (!parsed) {
|
||||
setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again.");
|
||||
return false;
|
||||
}
|
||||
document.querySelector("#manual-submission").replaceWith(parsed);
|
||||
setSubmissionState(successful ? "submitted" : "error");
|
||||
return successful;
|
||||
};
|
||||
|
||||
const setCount = (value) => {
|
||||
const count = Number.parseInt(value, 10);
|
||||
@@ -75,6 +99,7 @@
|
||||
const listenForCompassReading = () => {
|
||||
if (!directionStatus()) return;
|
||||
stopCompassReading();
|
||||
if (directionPanel()) directionPanel().dataset.state = "measuring";
|
||||
directionStatus().textContent = (t.pointPhone || "Point the top of your phone toward the sound — direction will update live.");
|
||||
orientationListener = (event) => {
|
||||
const isWebKitCompass = "webkitCompassHeading" in event;
|
||||
@@ -101,6 +126,7 @@
|
||||
const status = directionStatus();
|
||||
if (button) button.textContent = (t.captureDirection || "Start measuring direction");
|
||||
if (status) status.textContent = (t.compassUnavailable || "Compass unavailable on this device.");
|
||||
if (directionPanel()) directionPanel().dataset.state = "unavailable";
|
||||
}, 15000);
|
||||
};
|
||||
|
||||
@@ -108,14 +134,17 @@
|
||||
if (orientationListener) {
|
||||
stopCompassReading();
|
||||
captureDirectionButton().textContent = (t.captureAgain || "Measure again");
|
||||
if (directionPanel()) directionPanel().dataset.state = directionDegrees.value ? "captured" : "ready";
|
||||
return;
|
||||
}
|
||||
if (!window.isSecureContext) {
|
||||
directionStatus().textContent = (t.secureCompass || "Compass capture requires a secure HTTPS connection.");
|
||||
if (directionPanel()) directionPanel().dataset.state = "unavailable";
|
||||
return;
|
||||
}
|
||||
if (typeof window.DeviceOrientationEvent === "undefined") {
|
||||
directionStatus().textContent = (t.compassUnavailable || "Compass unavailable on this device.");
|
||||
if (directionPanel()) directionPanel().dataset.state = "unavailable";
|
||||
return;
|
||||
}
|
||||
if (typeof DeviceOrientationEvent.requestPermission === "function") {
|
||||
@@ -123,10 +152,12 @@
|
||||
try {
|
||||
if (await DeviceOrientationEvent.requestPermission(true) !== "granted") {
|
||||
if (directionStatus()) directionStatus().textContent = (t.compassDenied || "Compass permission denied.");
|
||||
if (directionPanel()) directionPanel().dataset.state = "unavailable";
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
if (directionStatus()) directionStatus().textContent = (t.compassDenied || "Compass permission denied.");
|
||||
if (directionPanel()) directionPanel().dataset.state = "unavailable";
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -136,7 +167,7 @@
|
||||
const submitManualReport = async () => {
|
||||
setCount(occurrences.value);
|
||||
stopCompassReading();
|
||||
manualSubmit.disabled = true;
|
||||
setSubmissionState("submitting", t.sendingReport || "Sending…");
|
||||
if (burstListener?.active) {
|
||||
try {
|
||||
const audio = await burstListener.clipForSubmission();
|
||||
@@ -144,21 +175,22 @@
|
||||
if (audio) data.append("audio", audio);
|
||||
const response = await fetch(manualForm.action, { method: "POST", body: data, credentials: "same-origin" });
|
||||
const html = await response.text();
|
||||
document.querySelector("#manual-submission").outerHTML = html;
|
||||
if (response.ok) burstListener.submissionSucceeded();
|
||||
if (showResponse(html, response.ok)) burstListener.submissionSucceeded();
|
||||
} catch (error) {
|
||||
document.querySelector("#manual-submission").textContent = "Could not send the report. Please try again.";
|
||||
} finally {
|
||||
manualSubmit.disabled = false;
|
||||
setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (window.htmx) {
|
||||
htmx.ajax("POST", manualForm.action, {
|
||||
source: manualForm,
|
||||
target: "#manual-submission",
|
||||
swap: "outerHTML",
|
||||
});
|
||||
try {
|
||||
htmx.ajax("POST", manualForm.action, {
|
||||
source: manualForm,
|
||||
target: "#manual-submission",
|
||||
swap: "outerHTML",
|
||||
});
|
||||
} catch (error) {
|
||||
setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again.");
|
||||
}
|
||||
} else {
|
||||
manualForm.submit();
|
||||
}
|
||||
@@ -174,16 +206,16 @@
|
||||
if (summary && directionDegrees.value !== "") {
|
||||
const heading = Number(directionDegrees.value);
|
||||
summary.textContent = `${cardinalDirection(heading)} · ${displayDegrees(heading)}°`;
|
||||
directionPanel().dataset.state = "captured";
|
||||
}
|
||||
});
|
||||
manualSubmit.addEventListener("click", () => {
|
||||
if (manualSubmit.disabled) return;
|
||||
manualSubmit.disabled = true;
|
||||
setSubmissionState("submitting", t.requestingLocation || "Requesting your optional location…");
|
||||
if (!navigator.geolocation) {
|
||||
submitManualReport();
|
||||
return;
|
||||
}
|
||||
manualStatus.textContent = (t.requestingLocation || "Requesting your optional location…");
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(position) => {
|
||||
latitude.value = position.coords.latitude;
|
||||
@@ -194,7 +226,22 @@
|
||||
{ enableHighAccuracy: false, maximumAge: 300000, timeout: 10000 },
|
||||
);
|
||||
});
|
||||
manualForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
if (!manualSubmit.disabled) manualSubmit.click();
|
||||
});
|
||||
document.body.addEventListener("htmx:afterRequest", (event) => {
|
||||
if (event.detail.elt === manualForm) manualSubmit.disabled = false;
|
||||
if (event.detail.elt !== manualForm) return;
|
||||
const response = event.detail.xhr;
|
||||
if (!response) {
|
||||
setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again.");
|
||||
} else if (response.status >= 400) {
|
||||
showResponse(response.responseText, false);
|
||||
} else {
|
||||
setSubmissionState("submitted");
|
||||
}
|
||||
});
|
||||
document.body.addEventListener("htmx:sendError", (event) => {
|
||||
if (event.detail.elt === manualForm) setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again.");
|
||||
});
|
||||
|
||||
|
||||
@@ -44,6 +44,9 @@
|
||||
border-radius: 1rem;
|
||||
background: #1a1739;
|
||||
}
|
||||
.manual-direction-panel[data-state="measuring"] { border-color: #a27cff; }
|
||||
.manual-direction-panel[data-state="captured"] { border-color: #68dba1; }
|
||||
.manual-direction-panel[data-state="unavailable"] { border-color: #d38b7a; }
|
||||
|
||||
.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; }
|
||||
@@ -56,6 +59,16 @@
|
||||
min-height: 4.5rem;
|
||||
}
|
||||
|
||||
.manual-report__submit:disabled { cursor: wait; opacity: .72; }
|
||||
.manual-submission { display: grid; justify-items: center; gap: .5rem; max-width: 36rem; margin-top: 1rem; padding: 1.3rem; border: 1px solid #4caa82; border-radius: 1rem; color: #eafff2; text-align: center; background: #10352e; }
|
||||
.manual-submission__icon { display: grid; width: 3.5rem; height: 3.5rem; place-items: center; border: 2px solid #6ce4a8; border-radius: 50%; color: #eafff2; font-size: 2rem; background: #226c51; }
|
||||
.manual-submission strong { font-size: 1.35rem; }
|
||||
.manual-submission p { margin: 0; line-height: 1.45; }
|
||||
.manual-submission__summary { margin-top: .35rem !important; color: #b2e8cf; font-size: .9rem; }
|
||||
.manual-submission--error { border-color: #d37c6a; color: #fff0e8; background: #3a1c2b; }
|
||||
.manual-submission--error .manual-submission__icon { border-color: #ffac8c; background: #9b493a; }
|
||||
.manual-report__submission:empty { display: none; }
|
||||
|
||||
.manual-report button:focus-visible, .manual-report input:focus-visible { outline: 3px solid #c9b4ff; outline-offset: 3px; }
|
||||
|
||||
.manual-report__supporting {
|
||||
@@ -85,6 +98,10 @@
|
||||
: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-submission { border-color: #54a67b; color: #164d34; background: #e8fff2; }
|
||||
:root[data-theme="light"] .manual-submission__icon { color: #fff; }
|
||||
:root[data-theme="light"] .manual-submission__summary { color: #2b6b48; }
|
||||
:root[data-theme="light"] .manual-submission--error { border-color: #ca7d65; color: #743520; background: #fff0e8; }
|
||||
|
||||
:root[data-theme="light"] .manual-report__supporting {
|
||||
border-color: #d9b4d8;
|
||||
|
||||
Reference in New Issue
Block a user