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

2 lines
16 KiB
JavaScript

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