Files
FireworksApp/staticfiles/vite/assets/manual-report-D-WkDeVJ.js
T

2 lines
13 KiB
JavaScript

const ie=document.querySelector("#manual-report-form");function ae(){if(!ie)return null;const e=document.querySelector("#burst-start"),n=document.querySelector("#burst-panel"),o=document.querySelector("#burst-heading"),s=document.querySelector("#burst-detail"),I=document.querySelector("#burst-waveform"),G=document.querySelector("#burst-preview"),Q=document.querySelector("#burst-resume"),j=document.querySelector("#burst-discard"),ee=document.querySelector("#burst-stop"),F=document.querySelector("#burst-error"),C=new Float32Array(300);let m=0,p="off",f,l,S,c,b,X,z,h;function V(){const t=I.getContext("2d");t.clearRect(0,0,I.width,I.height),t.fillStyle="#ffbd62";const a=I.width/C.length,u=Math.max(.003,...C);for(let i=0;i<C.length;i++){const W=C[(m+i)%C.length],A=Math.max(2,Math.min(I.height,W/u*I.height*.8));t.fillRect(i*a,(I.height-A)/2,Math.max(1,a-1),A)}}function k(){z=null,G.pause(),G.removeAttribute("src"),G.load(),G.hidden=!0,X&&URL.revokeObjectURL(X),X=null}function M(t){p=t,e.hidden=t!=="off",n.hidden=t==="off",Q.hidden=j.hidden=t!=="captured",o.textContent=t==="captured"?"Sound captured":t==="tail"?"Sound detected — finishing clip…":"Listening for the next sound…",s.textContent=t==="listening"?"Keeping the most recent ~10 seconds":t==="captured"?"Preview the clip or send your report.":""}function te(t,a){const u=new ArrayBuffer(44+t.length*2),i=new DataView(u),W=(A,v)=>{for(let K=0;K<v.length;K++)i.setUint8(A+K,v.charCodeAt(K))};W(0,"RIFF"),i.setUint32(4,u.byteLength-8,!0),W(8,"WAVEfmt "),i.setUint32(16,16,!0),i.setUint16(20,1,!0),i.setUint16(22,1,!0),i.setUint32(24,a,!0),i.setUint32(28,a*2,!0),i.setUint16(32,2,!0),i.setUint16(34,16,!0),W(36,"data"),i.setUint32(40,t.length*2,!0);for(let A=0;A<t.length;A++){const v=Math.max(-1,Math.min(1,t[A]));i.setInt16(44+A*2,v<0?v*32768:v*32767,!0)}return new Blob([u],{type:"audio/wav"})}function ne(){return`sound-signal-manual-${new Date().toISOString().replace(/[-:]/g,"").slice(0,15)}.wav`}async function oe(){var t;F.hidden=!0,e.disabled=!0;try{if(!((t=navigator.mediaDevices)!=null&&t.getUserMedia)||!window.AudioWorkletNode)throw new Error("Microphone listening is unavailable in this browser or connection.");f=await navigator.mediaDevices.getUserMedia({audio:{channelCount:1},video:!1}),l=new AudioContext,await l.audioWorklet.addModule(new URL("data:text/javascript;base64,Y2xhc3MgQnVyc3RMaXN0ZW5lclByb2Nlc3NvciBleHRlbmRzIEF1ZGlvV29ya2xldFByb2Nlc3NvciB7CiAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgICBzdXBlcigpOwogICAgICAgIHRoaXMuc2FtcGxlcyA9IG5ldyBGbG9hdDMyQXJyYXkoTWF0aC5jZWlsKHNhbXBsZVJhdGUgKiAxMCkpOwogICAgICAgIHRoaXMucG9zaXRpb24gPSAwOwogICAgICAgIHRoaXMuY291bnQgPSAwOwogICAgICAgIHRoaXMuaW50ZXJ2YWwgPSAwOwogICAgICAgIHRoaXMuZW5lcmd5ID0gMDsKICAgICAgICB0aGlzLnBlYWsgPSAwOwogICAgICAgIHRoaXMuYmFzZWxpbmUgPSAwLjAxOwogICAgICAgIHRoaXMubGFzdFRyaWdnZXIgPSAtc2FtcGxlUmF0ZSAqIDEwOwogICAgICAgIHRoaXMucG9zdFJvbGwgPSAwOwogICAgICAgIHRoaXMuZXZlbnRQb3NpdGlvbiA9IDA7CiAgICAgICAgdGhpcy5ldmVudENvdW50ID0gMDsKICAgICAgICB0aGlzLmZyYW1lID0gMDsKICAgICAgICB0aGlzLm1vZGUgPSAibGlzdGVuaW5nIjsKICAgICAgICB0aGlzLnBvcnQub25tZXNzYWdlID0gKHsgZGF0YSB9KSA9PiB7CiAgICAgICAgICAgIGlmIChkYXRhID09PSAic25hcHNob3QiKSB0aGlzLnNlbmRDbGlwKHRoaXMuY291bnQpOwogICAgICAgICAgICBpZiAoZGF0YSA9PT0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nVlOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgY2hhbm5lbC5sZW5ndGg7IGkrKykgewogICAgICAgICAgICBjb25zdCB2YWx1ZSA9IGNoYW5uZWxbaV07CiAgICAgICAgICAgIHRoaXMuc2FtcGxlc1t0aGlzLnBvc2l0aW9uXSA9IHZhbHVlOwogICAgICAgICAgICB0aGlzLnBvc2l0aW9uID0gKHRoaXMucG9zaXRpb24gKyAxKSAlIHRoaXMuc2FtcGxlcy5sZW5ndGg7CiAgICAgICAgICAgIHRoaXMuY291bnQgPSBNYXRoLm1pbih0aGlzLmNvdW50ICsgMSwgdGhpcy5zYW1wbGVzLmxlbmd0aCk7CiAgICAgICAgICAgIHRoaXMuZW5lcmd5ICs9IHZhbHVlICogdmFsdWU7CiAgICAgICAgICAgIHRoaXMucGVhayA9IE1hdGgubWF4KHRoaXMucGVhaywgTWF0aC5hYnModmFsdWUpKTsKICAgICAgICAgICAgdGhpcy5pbnRlcnZhbCsrOwogICAgICAgICAgICB0aGlzLmZyYW1lKys7CiAgICAgICAgICAgIGlmICh0aGlzLm1vZGUgPT09ICJ0YWlsIikgewogICAgICAgICAgICAgICAgaWYgKC0tdGhpcy5wb3N0Um9sbCA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHRoaXMubW9kZSA9ICJjYXB0dXJlZCI7CiAgICAgICAgICAgICAgICAgICAgdGhpcy5zZW5kQ2xpcChNYXRoLm1pbih0aGlzLmNvdW50LCBNYXRoLnJvdW5kKHNhbXBsZVJhdGUgKiAxMCkpKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodGhpcy5pbnRlcnZhbCA+PSBNYXRoLnJvdW5kKHNhbXBsZVJhdGUgLyAzMCkpIHsKICAgICAgICAgICAgICAgIGNvbnN0IHJtcyA9IE1hdGguc3FydCh0aGlzLmVuZXJneSAvIHRoaXMuaW50ZXJ2YWwpOwogICAgICAgICAgICAgICAgdGhpcy5wb3J0LnBvc3RNZXNzYWdlKHsgdHlwZTogImxldmVsIiwgdmFsdWU6IE1hdGgubWF4KHJtcywgdGhpcy5wZWFrICogMC41KSB9KTsKICAgICAgICAgICAgICAgIGlmICh0aGlzLm1vZGUgPT09ICJsaXN0ZW5pbmciICYmIHRoaXMuZnJhbWUgPiBzYW1wbGVSYXRlICYmIHRoaXMuZnJhbWUgLSB0aGlzLmxhc3RUcmlnZ2VyID4gc2FtcGxlUmF0ZSAqIDMKICAgICAgICAgICAgICAgICAgICAmJiBybXMgPiBNYXRoLm1heCgwLjA3NSwgdGhpcy5iYXNlbGluZSAqIDUpICYmIHRoaXMucGVhayA+IDAuMTYpIHsKICAgICAgICAgICAgICAgICAgICB0aGlzLm1vZGUgPSAidGFpbCI7CiAgICAgICAgICAgICAgICAgICAgdGhpcy5wb3N0Um9sbCA9IE1hdGgucm91bmQoc2FtcGxlUmF0ZSAqIDIpOwogICAgICAgICAgICAgICAgICAgIHRoaXMubGFzdFRyaWdnZXIgPSB0aGlzLmZyYW1lOwogICAgICAgICAgICAgICAgICAgIHRoaXMucG9ydC5wb3N0TWVzc2FnZSh7IHR5cGU6ICJ0cmlnZ2VyIiB9KTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAodGhpcy5tb2RlID09PSAibGlzdGVuaW5nIikgewogICAgICAgICAgICAgICAgICAgIHRoaXMuYmFzZWxpbmUgPSB0aGlzLmJhc2VsaW5lICogMC45OCArIE1hdGgubWluKHJtcywgdGhpcy5iYXNlbGluZSAqIDIpICogMC4wMjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHRoaXMuaW50ZXJ2YWwgPSAwOwogICAgICAgICAgICAgICAgdGhpcy5lbmVyZ3kgPSAwOwogICAgICAgICAgICAgICAgdGhpcy5wZWFrID0gMDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KfQoKcmVnaXN0ZXJQcm9jZXNzb3IoImJ1cnN0LWxpc3RlbmVyIiwgQnVyc3RMaXN0ZW5lclByb2Nlc3Nvcik7Cg==",import.meta.url)),S=l.createMediaStreamSource(f),c=new AudioWorkletNode(l,"burst-listener"),b=l.createGain(),b.gain.value=0,c.port.onmessage=({data:a})=>{if(a.type==="level"&&(p==="listening"||p==="tail"))C[m]=a.value,m=(m+1)%C.length,V();else if(a.type==="trigger"&&p==="listening")M("tail");else if(a.type==="clip"){const u=new File([te(a.samples,a.sampleRate)],ne(),{type:"audio/wav"});h?(h(u),h=null):p==="tail"&&(k(),z=u,X=URL.createObjectURL(u),G.src=X,G.hidden=!1,M("captured"))}},S.connect(c),c.connect(b).connect(l.destination),await l.resume(),C.fill(0),m=0,V(),M("listening")}catch(a){await Y(),F.textContent=a.name==="NotAllowedError"?"Microphone permission was denied. Manual reporting is still available.":a.message||"Microphone capture failed.",F.hidden=!1}finally{e.disabled=!1}}async function Y(){h&&(h(null),h=null),k(),c==null||c.disconnect(),S==null||S.disconnect(),b==null||b.disconnect(),f==null||f.getTracks().forEach(t=>t.stop()),await(l==null?void 0:l.close()),f=l=S=c=b=null,C.fill(0),m=0,M("off")}function q(){k(),C.fill(0),m=0,V(),c==null||c.port.postMessage("reset"),c&&M("listening")}return e.addEventListener("click",oe),ee.addEventListener("click",Y),j.addEventListener("click",q),Q.addEventListener("click",q),window.addEventListener("pagehide",Y),document.addEventListener("visibilitychange",()=>{document.hidden&&Y()}),{get active(){return p!=="off"},async clipForSubmission(){return z||(p==="off"||!c?null:new Promise(t=>{h=t,c.port.postMessage("snapshot")}))},submissionSucceeded:q}}const g=window.BOOMCAST_I18N||{},w=document.querySelector("#manual-report-form"),Z=document.querySelector("#manual-submit"),d=document.querySelector("#manual-occurrences"),$=document.querySelector("#manual-decrease"),_=document.querySelector("#manual-increase"),ce=document.querySelector("#manual-submission"),ge=document.querySelector("#manual-latitude"),se=document.querySelector("#manual-longitude"),E=document.querySelector("#manual-direction-degrees"),le=document.querySelector("#manual-direction-accuracy"),T=()=>document.querySelector("#manual-capture-direction"),r=()=>document.querySelector("#manual-direction-status"),O=document.querySelector("#manual-compass"),H=document.querySelector("#manual-compass-rose"),re=document.querySelector("#manual-compass-heading");let R,D,y=null;const B=ae(),P=e=>Number.isFinite(e),U=e=>["N","NE","E","SE","S","SW","W","NW"][Math.floor((e+22.5)%360/45)],L=e=>Number(e.toFixed(1)).toString(),x=e=>{const n=Number.parseInt(e,10);d.value=String(Math.min(1e3,Math.max(1,Number.isFinite(n)?n:1))),$.disabled=d.value==="1",_.disabled=d.value==="1000"};x(d.value);$.addEventListener("click",()=>x(Number(d.value)-1));_.addEventListener("click",()=>x(Number(d.value)+1));d.addEventListener("change",()=>x(d.value));const N=()=>{R&&window.removeEventListener("deviceorientation",R),window.clearTimeout(D),R=null,D=null,O.hidden=!0,y=null},Ie=e=>{const n=-e;if(y===null)y=n,H.style.transition="none",H.style.transform=`rotate(${y}deg)`,H.offsetWidth,H.style.transition="";else{const o=(n-y+540)%360-180;y+=o,H.style.transform=`rotate(${y}deg)`}re.textContent=`${L(e)}° · ${U(e)}`,O.setAttribute("aria-label",`${g.compassDirection||"Compass direction"} ${L(e)}° ${U(e)}`),O.hidden=!1},Ce=(e,n)=>{const o=n===null?g.accuracyUnavailable||"Accuracy unavailable":(g.accuracy||"Accuracy")+": ±"+L(n)+"°",s=r();s&&(s.innerHTML="<strong>"+U(e)+" · "+L(e)+"°</strong><br>"+o);const I=T();I&&(I.textContent=g.stopCapture||"Stop measuring direction")},ue=()=>{r()&&(N(),r().textContent=g.pointPhone||"Point the top of your phone toward the sound — direction will update live.",R=e=>{const n="webkitCompassHeading"in e,o=P(e.webkitCompassHeading)?e.webkitCompassHeading:!n&&e.absolute&&P(e.alpha)?(360-e.alpha)%360:null;if(o===null||o<0||o>=360)return;const s=P(e.webkitCompassAccuracy)&&e.webkitCompassAccuracy>=0?e.webkitCompassAccuracy:null;E.value=o,le.value=s===null?"":s,Ce(o,s),Ie(o),window.clearTimeout(D)},window.addEventListener("deviceorientation",R),D=window.setTimeout(()=>{N();const e=T(),n=r();e&&(e.textContent=g.captureDirection||"Start measuring direction"),n&&(n.textContent=g.compassUnavailable||"Compass unavailable on this device.")},15e3))},Ae=async()=>{if(R){N(),T().textContent=g.captureAgain||"Measure again";return}if(!window.isSecureContext){r().textContent=g.secureCompass||"Compass capture requires a secure HTTPS connection.";return}if(typeof window.DeviceOrientationEvent>"u"){r().textContent=g.compassUnavailable||"Compass unavailable on this device.";return}if(typeof DeviceOrientationEvent.requestPermission=="function"){r().textContent=g.compassPermission||"Requesting compass permission…";try{if(await DeviceOrientationEvent.requestPermission(!0)!=="granted"){r()&&(r().textContent=g.compassDenied||"Compass permission denied.");return}}catch{r()&&(r().textContent=g.compassDenied||"Compass permission denied.");return}}ue()},J=async()=>{if(x(d.value),N(),Z.disabled=!0,B!=null&&B.active){try{const e=await B.clipForSubmission(),n=new FormData(w);e&&n.append("audio",e);const o=await fetch(w.action,{method:"POST",body:n,credentials:"same-origin"}),s=await o.text();document.querySelector("#manual-submission").outerHTML=s,o.ok&&B.submissionSucceeded()}catch{document.querySelector("#manual-submission").textContent="Could not send the report. Please try again."}finally{Z.disabled=!1}return}window.htmx?htmx.ajax("POST",w.action,{source:w,target:"#manual-submission",swap:"outerHTML"}):w.submit()};w.addEventListener("click",e=>{e.target.closest("#manual-capture-direction")&&Ae(),e.target.closest(".manual-direction-panel__close")&&N()});document.body.addEventListener("htmx:afterSwap",e=>{var o;if(((o=e.detail.target)==null?void 0:o.id)!=="manual-direction-panel")return;const n=document.querySelector("#manual-direction-summary");if(n&&E.value!==""){const s=Number(E.value);n.textContent=`${U(s)} · ${L(s)}°`}});Z.addEventListener("click",()=>{if(!Z.disabled){if(Z.disabled=!0,!navigator.geolocation){J();return}ce.textContent=g.requestingLocation||"Requesting your optional location…",navigator.geolocation.getCurrentPosition(e=>{ge.value=e.coords.latitude,se.value=e.coords.longitude,J()},()=>J(),{enableHighAccuracy:!1,maximumAge:3e5,timeout:1e4})}});document.body.addEventListener("htmx:afterRequest",e=>{e.detail.elt===w&&(Z.disabled=!1)});