Files
FireworksApp/staticfiles/vite/assets/manual-report-44fwUW4y.js
2026-10-10 16:03:14 +02:00

2 lines
17 KiB
JavaScript

const we=document.querySelector("#manual-report-form"),g=window.BOOMCAST_I18N||{};function fe(){if(!we)return null;const e=document.querySelector("#burst-start"),t=e.closest(".burst-listener"),n=e.closest(".manual-report__supporting"),i=document.querySelector("#burst-state"),I=document.querySelector("#burst-panel"),y=document.querySelector("#burst-heading"),L=document.querySelector("#burst-detail"),w=document.querySelector("#burst-waveform"),Z=document.querySelector("#burst-preview"),se=document.querySelector("#burst-resume"),re=document.querySelector("#burst-discard"),pe=document.querySelector("#burst-stop"),_=document.querySelector("#burst-error"),A=new Float32Array(300);let f=0,S="off",M,d,X,c,G,x,k,v;function $(){const a=w.getContext("2d");a.clearRect(0,0,w.width,w.height),a.fillStyle="#ffbd62";const l=w.width/A.length,p=Math.max(.003,...A);for(let s=0;s<A.length;s++){const D=A[(f+s)%A.length],b=Math.max(2,Math.min(w.height,D/p*w.height*.8));a.fillRect(s*l,(w.height-b)/2,Math.max(1,l-1),b)}}function ee(){k=null,Z.pause(),Z.removeAttribute("src"),Z.load(),Z.hidden=!0,x&&URL.revokeObjectURL(x),x=null}function N(a){S=a,t&&(t.dataset.state=a),n&&(n.dataset.state=a),i&&(i.textContent=a==="listening"?g.listeningNextBang||"Listening for the next bang":a==="tail"?g.soundDetected||"Sound detected":a==="captured"?g.soundCaptured||"Sound captured":g.notListening||"Not listening"),e.hidden=a!=="off",I.hidden=a==="off",se.hidden=re.hidden=a!=="captured",y.textContent=a==="captured"?g.soundCaptured||"Sound captured":a==="tail"?g.soundDetectedFinishing||"Sound detected — finishing clip…":g.listeningNextSound||"Listening for the next sound…",L.textContent=a==="listening"?g.keepingRecentAudio||"Keeping the most recent ~10 seconds":a==="captured"?g.previewClipOrSend||"Preview the clip or send your report.":""}function be(a,l){const p=new ArrayBuffer(44+a.length*2),s=new DataView(p),D=(b,W)=>{for(let V=0;V<W.length;V++)s.setUint8(b+V,W.charCodeAt(V))};D(0,"RIFF"),s.setUint32(4,p.byteLength-8,!0),D(8,"WAVEfmt "),s.setUint32(16,16,!0),s.setUint16(20,1,!0),s.setUint16(22,1,!0),s.setUint32(24,l,!0),s.setUint32(28,l*2,!0),s.setUint16(32,2,!0),s.setUint16(34,16,!0),D(36,"data"),s.setUint32(40,a.length*2,!0);for(let b=0;b<a.length;b++){const W=Math.max(-1,Math.min(1,a[b]));s.setInt16(44+b*2,W<0?W*32768:W*32767,!0)}return new Blob([p],{type:"audio/wav"})}function he(){return`sound-signal-manual-${new Date().toISOString().replace(/[-:]/g,"").slice(0,15)}.wav`}async function ye(){var a;_.hidden=!0,e.disabled=!0,t&&(t.dataset.state="starting"),n&&(n.dataset.state="starting"),i&&(i.textContent=g.startingMicrophone||"Starting microphone…");try{if(!((a=navigator.mediaDevices)!=null&&a.getUserMedia)||!window.AudioWorkletNode)throw new Error("Microphone listening is unavailable in this browser or connection.");M=await navigator.mediaDevices.getUserMedia({audio:{channelCount:1},video:!1}),d=new AudioContext,await d.audioWorklet.addModule(new URL("data:text/javascript;base64,Y2xhc3MgQnVyc3RMaXN0ZW5lclByb2Nlc3NvciBleHRlbmRzIEF1ZGlvV29ya2xldFByb2Nlc3NvciB7CiAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgICBzdXBlcigpOwogICAgICAgIHRoaXMuc2FtcGxlcyA9IG5ldyBGbG9hdDMyQXJyYXkoTWF0aC5jZWlsKHNhbXBsZVJhdGUgKiAxMCkpOwogICAgICAgIHRoaXMucG9zaXRpb24gPSAwOwogICAgICAgIHRoaXMuY291bnQgPSAwOwogICAgICAgIHRoaXMuaW50ZXJ2YWwgPSAwOwogICAgICAgIHRoaXMuZW5lcmd5ID0gMDsKICAgICAgICB0aGlzLnBlYWsgPSAwOwogICAgICAgIHRoaXMuYmFzZWxpbmUgPSAwLjAxOwogICAgICAgIHRoaXMubGFzdFRyaWdnZXIgPSAtc2FtcGxlUmF0ZSAqIDEwOwogICAgICAgIH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GVzWyhzdGFydCArIGkpICUgdGhpcy5zYW1wbGVzLmxlbmd0aF07CiAgICAgICAgdGhpcy5wb3J0LnBvc3RNZXNzYWdlKHsgdHlwZTogImNsaXAiLCBzYW1wbGVzOiBjbGlwLCBzYW1wbGVSYXRlIH0sIFtjbGlwLmJ1ZmZlcl0pOwogICAgfQoKICAgIHByb2Nlc3MoaW5wdXRzKSB7CiAgICAgICAgY29uc3QgY2hhbm5lbCA9IGlucHV0c1swXT8uWzBdOwogICAgICAgIGlmICghY2hhbm5lbCB8fCB0aGlzLm1vZGUgPT09ICJjYXB0dXJlZCIpIHJldHVybiB0cnVlOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgY2hhbm5lbC5sZW5ndGg7IGkrKykgewogICAgICAgICAgICBjb25zdCB2YWx1ZSA9IGNoYW5uZWxbaV07CiAgICAgICAgICAgIHRoaXMuc2FtcGxlc1t0aGlzLnBvc2l0aW9uXSA9IHZhbHVlOwogICAgICAgICAgICB0aGlzLnBvc2l0aW9uID0gKHRoaXMucG9zaXRpb24gKyAxKSAlIHRoaXMuc2FtcGxlcy5sZW5ndGg7CiAgICAgICAgICAgIHRoaXMuY291bnQgPSBNYXRoLm1pbih0aGlzLmNvdW50ICsgMSwgdGhpcy5zYW1wbGVzLmxlbmd0aCk7CiAgICAgICAgICAgIHRoaXMuZW5lcmd5ICs9IHZhbHVlICogdmFsdWU7CiAgICAgICAgICAgIHRoaXMucGVhayA9IE1hdGgubWF4KHRoaXMucGVhaywgTWF0aC5hYnModmFsdWUpKTsKICAgICAgICAgICAgdGhpcy5pbnRlcnZhbCsrOwogICAgICAgICAgICB0aGlzLmZyYW1lKys7CiAgICAgICAgICAgIGlmICh0aGlzLm1vZGUgPT09ICJ0YWlsIikgewogICAgICAgICAgICAgICAgaWYgKC0tdGhpcy5wb3N0Um9sbCA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHRoaXMubW9kZSA9ICJjYXB0dXJlZCI7CiAgICAgICAgICAgICAgICAgICAgdGhpcy5zZW5kQ2xpcChNYXRoLm1pbih0aGlzLmNvdW50LCBNYXRoLnJvdW5kKHNhbXBsZVJhdGUgKiAxMCkpKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodGhpcy5pbnRlcnZhbCA+PSBNYXRoLnJvdW5kKHNhbXBsZVJhdGUgLyAzMCkpIHsKICAgICAgICAgICAgICAgIGNvbnN0IHJtcyA9IE1hdGguc3FydCh0aGlzLmVuZXJneSAvIHRoaXMuaW50ZXJ2YWwpOwogICAgICAgICAgICAgICAgdGhpcy5wb3J0LnBvc3RNZXNzYWdlKHsgdHlwZTogImxldmVsIiwgdmFsdWU6IE1hdGgubWF4KHJtcywgdGhpcy5wZWFrICogMC41KSB9KTsKICAgICAgICAgICAgICAgIGlmICh0aGlzLm1vZGUgPT09ICJsaXN0ZW5pbmciICYmIHRoaXMuZnJhbWUgPiBzYW1wbGVSYXRlICYmIHRoaXMuZnJhbWUgLSB0aGlzLmxhc3RUcmlnZ2VyID4gc2FtcGxlUmF0ZSAqIDMKICAgICAgICAgICAgICAgICAgICAmJiBybXMgPiBNYXRoLm1heCgwLjA3NSwgdGhpcy5iYXNlbGluZSAqIDUpICYmIHRoaXMucGVhayA+IDAuMTYpIHsKICAgICAgICAgICAgICAgICAgICB0aGlzLm1vZGUgPSAidGFpbCI7CiAgICAgICAgICAgICAgICAgICAgdGhpcy5wb3N0Um9sbCA9IE1hdGgucm91bmQoc2FtcGxlUmF0ZSAqIDIpOwogICAgICAgICAgICAgICAgICAgIHRoaXMubGFzdFRyaWdnZXIgPSB0aGlzLmZyYW1lOwogICAgICAgICAgICAgICAgICAgIHRoaXMucG9ydC5wb3N0TWVzc2FnZSh7IHR5cGU6ICJ0cmlnZ2VyIiB9KTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAodGhpcy5tb2RlID09PSAibGlzdGVuaW5nIikgewogICAgICAgICAgICAgICAgICAgIHRoaXMuYmFzZWxpbmUgPSB0aGlzLmJhc2VsaW5lICogMC45OCArIE1hdGgubWluKHJtcywgdGhpcy5iYXNlbGluZSAqIDIpICogMC4wMjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHRoaXMuaW50ZXJ2YWwgPSAwOwogICAgICAgICAgICAgICAgdGhpcy5lbmVyZ3kgPSAwOwogICAgICAgICAgICAgICAgdGhpcy5wZWFrID0gMDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KfQoKcmVnaXN0ZXJQcm9jZXNzb3IoImJ1cnN0LWxpc3RlbmVyIiwgQnVyc3RMaXN0ZW5lclByb2Nlc3Nvcik7Cg==",import.meta.url)),X=d.createMediaStreamSource(M),c=new AudioWorkletNode(d,"burst-listener"),G=d.createGain(),G.gain.value=0,c.port.onmessage=({data:l})=>{if(l.type==="level"&&(S==="listening"||S==="tail"))A[f]=l.value,f=(f+1)%A.length,$();else if(l.type==="trigger"&&S==="listening")N("tail");else if(l.type==="clip"){const p=new File([be(l.samples,l.sampleRate)],he(),{type:"audio/wav"});v?(v(p),v=null):S==="tail"&&(ee(),k=p,x=URL.createObjectURL(p),Z.src=x,Z.hidden=!1,N("captured"))}},X.connect(c),c.connect(G).connect(d.destination),await d.resume(),A.fill(0),f=0,$(),N("listening")}catch(l){await P(),t&&(t.dataset.state="error"),n&&(n.dataset.state="error"),i&&(i.textContent=g.microphoneUnavailable||"Microphone unavailable"),_.textContent=l.name==="NotAllowedError"?g.microphoneDenied||"Microphone permission was denied. Manual reporting is still available.":g.microphoneFailed||"Microphone capture failed.",_.hidden=!1}finally{e.disabled=!1}}async function P(){v&&(v(null),v=null),ee(),c==null||c.disconnect(),X==null||X.disconnect(),G==null||G.disconnect(),M==null||M.getTracks().forEach(a=>a.stop()),await(d==null?void 0:d.close()),M=d=X=c=G=null,A.fill(0),f=0,N("off")}function te(){ee(),A.fill(0),f=0,$(),c==null||c.port.postMessage("reset"),c&&N("listening")}return e.addEventListener("click",ye),pe.addEventListener("click",P),re.addEventListener("click",te),se.addEventListener("click",te),window.addEventListener("pagehide",P),document.addEventListener("visibilitychange",()=>{document.hidden&&P()}),{get active(){return S!=="off"},async clipForSubmission(){return k||(S==="off"||!c?null:new Promise(a=>{v=a,c.port.postMessage("snapshot")}))},submissionSucceeded:te}}const o=window.BOOMCAST_I18N||{},m=document.querySelector("#manual-report-form"),K=document.querySelector("#manual-submit"),F=document.querySelector("#manual-submit-label"),C=document.querySelector("#manual-occurrences"),de=document.querySelector("#manual-decrease"),Ce=document.querySelector("#manual-increase"),Se=document.querySelector("#manual-latitude"),Ge=document.querySelector("#manual-longitude"),O=document.querySelector("#manual-direction-degrees"),ve=document.querySelector("#manual-direction-accuracy"),j=()=>document.querySelector("#manual-capture-direction"),u=()=>document.querySelector("#manual-direction-status"),r=()=>document.querySelector("#manual-direction-panel"),H=document.querySelector("#manual-compass"),T=document.querySelector("#manual-compass-home"),z=document.querySelector("#manual-compass-rose"),Re=document.querySelector("#manual-compass-heading"),E=document.querySelector("#manual-report-time"),ce=document.querySelector("#report-clock-hours"),le=document.querySelector("#report-clock-minutes"),Ze=document.querySelector("#manual-occurred-at"),ne=document.querySelector("#manual-use-now"),ge=document.querySelector("#manual-burst-status"),ue=document.querySelector("#manual-burst-duration");let B,J,R=null;const Y=fe(),oe=e=>Number.isFinite(e),ie=e=>[o.north||"North",o.northeast||"Northeast",o.east||"East",o.southeast||"Southeast",o.south||"South",o.southwest||"Southwest",o.west||"West",o.northwest||"Northwest"][Math.floor((e+22.5)%360/45)],Q=e=>Number(e.toFixed(1)).toString(),Ie=()=>{var I,y;if(!E)return;const e=new Date;E.dateTime=e.toISOString();const t=new Intl.DateTimeFormat(document.documentElement.lang||void 0,{hour:"2-digit",minute:"2-digit",hourCycle:"h23"}).formatToParts(e),n=(I=t.find(L=>L.type==="hour"))==null?void 0:I.value,i=(y=t.find(L=>L.type==="minute"))==null?void 0:y.value;!n||!i||!ce||!le||(ce.textContent=n,le.textContent=i,E.setAttribute("aria-label",`${n}:${i}`))};Ie();E&&window.setInterval(Ie,6e4);const Me=(F==null?void 0:F.textContent)||"I heard fireworks";ne==null||ne.addEventListener("click",()=>{const e=new Date,t=new Date(e.getTime()-e.getTimezoneOffset()*6e4);Ze.value=t.toISOString().slice(0,16)});const h=(e,t="")=>{if(m.dataset.state=e,K.disabled=e==="submitting",F&&(F.textContent=e==="submitting"?o.sendingReport||"Sending…":Me),t){const n=document.querySelector("#manual-submission");n.className="manual-report__submission",n.textContent=t}},Ae=(e,t)=>{const n=new DOMParser().parseFromString(e,"text/html").querySelector("#manual-submission");return n?(document.querySelector("#manual-submission").replaceWith(n),h(t?"submitted":"error"),t):(h("error",o.reportFailed||"Could not send the report. Please try again."),!1)},me=()=>{const e=Number(C.value)>=3;ge&&(ge.textContent=e?o.burstEvent||"Burst Event":o.ordinaryReport||"Ordinary report"),ue&&(ue.hidden=!e)},U=e=>{const t=Number.parseInt(e,10);C.value=String(Math.min(1e3,Math.max(1,Number.isFinite(t)?t:1))),de.disabled=C.value==="1",Ce.disabled=C.value==="1000",me()};U(C.value);de.addEventListener("click",()=>U(Number(C.value)-1));Ce.addEventListener("click",()=>U(Number(C.value)+1));C.addEventListener("change",()=>U(C.value));C.addEventListener("input",me);const q=()=>{B&&window.removeEventListener("deviceorientation",B),window.clearTimeout(J),B=null,J=null,H.hidden=!0,R=null},Xe=e=>{const t=-e;if(R===null)R=t,z.style.transition="none",z.style.transform=`rotate(${R}deg)`,z.offsetWidth,z.style.transition="";else{const n=(t-R+540)%360-180;R+=n,z.style.transform=`rotate(${R}deg)`}Re.textContent=`${Q(e)}° · ${ie(e)}`,H.setAttribute("aria-label",`${o.compassDirection||"Compass direction"} ${Q(e)}° ${ie(e)}`),H.hidden=!1},We=(e,t)=>{const n=u();if(n)if(n.classList.toggle("manual-direction-panel__status--accuracy",t!==null),t===null)n.textContent=o.accuracyUnavailable||"Accuracy unavailable";else{const I=document.createElement("span");I.textContent=o.accuracy||"Accuracy";const y=document.createElement("strong");y.textContent=`±${Q(t)}°`,n.replaceChildren(I,y)}const i=j();i&&(i.textContent=o.stopCapture||"Stop measuring direction")},Be=()=>{u()&&(q(),r()&&(r().dataset.state="measuring"),u().textContent=o.pointPhone||"Point the top of your phone toward the sound — direction will update live.",B=e=>{const t="webkitCompassHeading"in e,n=oe(e.webkitCompassHeading)?e.webkitCompassHeading:!t&&e.absolute&&oe(e.alpha)?(360-e.alpha)%360:null;if(n===null||n<0||n>=360)return;const i=oe(e.webkitCompassAccuracy)&&e.webkitCompassAccuracy>=0?e.webkitCompassAccuracy:null;O.value=n,ve.value=i===null?"":i,We(n,i),Xe(n),window.clearTimeout(J)},window.addEventListener("deviceorientation",B),J=window.setTimeout(()=>{q();const e=j(),t=u();e&&(e.textContent=o.captureDirection||"Start measuring direction"),t&&(t.textContent=o.compassUnavailable||"Compass unavailable on this device."),r()&&(r().dataset.state="unavailable")},15e3))},He=async()=>{var e;if(B){q(),j().textContent=o.captureAgain||"Measure again",r()&&(r().dataset.state=O.value?"captured":"ready");return}if((e=u())==null||e.classList.remove("manual-direction-panel__status--accuracy"),!window.isSecureContext){u().textContent=o.secureCompass||"Compass capture requires a secure HTTPS connection.",r()&&(r().dataset.state="unavailable");return}if(typeof window.DeviceOrientationEvent>"u"){u().textContent=o.compassUnavailable||"Compass unavailable on this device.",r()&&(r().dataset.state="unavailable");return}if(typeof DeviceOrientationEvent.requestPermission=="function"){u().textContent=o.compassPermission||"Requesting compass permission…";try{if(await DeviceOrientationEvent.requestPermission(!0)!=="granted"){u()&&(u().textContent=o.compassDenied||"Compass permission denied."),r()&&(r().dataset.state="unavailable");return}}catch{u()&&(u().textContent=o.compassDenied||"Compass permission denied."),r()&&(r().dataset.state="unavailable");return}}Be()},ae=async()=>{if(U(C.value),q(),h("submitting",o.sendingReport||"Sending…"),Y!=null&&Y.active){try{const e=await Y.clipForSubmission(),t=new FormData(m);e&&t.append("audio",e);const n=await fetch(m.action,{method:"POST",body:t,credentials:"same-origin"}),i=await n.text();Ae(i,n.ok)&&Y.submissionSucceeded()}catch{h("error",o.reportFailed||"Could not send the report. Please try again.")}return}if(window.htmx)try{htmx.ajax("POST",m.action,{source:m,target:"#manual-submission",swap:"outerHTML"})}catch{h("error",o.reportFailed||"Could not send the report. Please try again.")}else m.submit()};m.addEventListener("click",e=>{e.target.closest("#manual-capture-direction")&&He(),e.target.closest(".manual-direction-panel__close")&&(q(),T&&T.append(H))});document.body.addEventListener("htmx:beforeSwap",e=>{var t;((t=e.detail.target)==null?void 0:t.id)==="manual-direction-panel"&&T&&T.append(H)});document.body.addEventListener("htmx:afterSwap",e=>{var n,i;if(((n=e.detail.target)==null?void 0:n.id)!=="manual-direction-panel")return;j()&&((i=r().querySelector(".manual-direction-panel__copy"))==null||i.after(H));const t=document.querySelector("#manual-direction-summary");if(t&&O.value!==""){const I=Number(O.value);t.textContent=`${ie(I)} · ${Q(I)}°`,r().dataset.state="captured"}});K.addEventListener("click",()=>{if(!K.disabled){if(h("submitting",o.requestingLocation||"Requesting your optional location…"),!navigator.geolocation){ae();return}navigator.geolocation.getCurrentPosition(e=>{Se.value=e.coords.latitude,Ge.value=e.coords.longitude,ae()},()=>ae(),{enableHighAccuracy:!1,maximumAge:3e5,timeout:1e4})}});m.addEventListener("submit",e=>{e.preventDefault(),K.disabled||K.click()});document.body.addEventListener("htmx:afterRequest",e=>{if(e.detail.elt!==m)return;const t=e.detail.xhr;t?t.status>=400?Ae(t.responseText,!1):h("submitted"):h("error",o.reportFailed||"Could not send the report. Please try again.")});document.body.addEventListener("htmx:sendError",e=>{e.detail.elt===m&&h("error",o.reportFailed||"Could not send the report. Please try again.")});