Files
FireworksApp/staticfiles/vite/assets/manual-report-4E-uuYx3.js
T

2 lines
16 KiB
JavaScript

const pe=document.querySelector("#manual-report-form"),g=window.BOOMCAST_I18N||{};function be(){if(!pe)return null;const e=document.querySelector("#burst-start"),t=e.closest(".burst-listener"),n=e.closest(".manual-report__supporting"),i=document.querySelector("#burst-state"),C=document.querySelector("#burst-panel"),y=document.querySelector("#burst-heading"),L=document.querySelector("#burst-detail"),w=document.querySelector("#burst-waveform"),Z=document.querySelector("#burst-preview"),ie=document.querySelector("#burst-resume"),se=document.querySelector("#burst-discard"),Ce=document.querySelector("#burst-stop"),_=document.querySelector("#burst-error"),I=new Float32Array(300);let f=0,G="off",M,d,X,c,S,N,k,v;function $(){const o=w.getContext("2d");o.clearRect(0,0,w.width,w.height),o.fillStyle="#ffbd62";const l=w.width/I.length,m=Math.max(.003,...I);for(let s=0;s<I.length;s++){const D=I[(f+s)%I.length],p=Math.max(2,Math.min(w.height,D/m*w.height*.8));o.fillRect(s*l,(w.height-p)/2,Math.max(1,l-1),p)}}function ee(){k=null,Z.pause(),Z.removeAttribute("src"),Z.load(),Z.hidden=!0,N&&URL.revokeObjectURL(N),N=null}function x(o){G=o,t&&(t.dataset.state=o),n&&(n.dataset.state=o),i&&(i.textContent=o==="listening"?g.listeningNextBang||"Listening for the next bang":o==="tail"?g.soundDetected||"Sound detected":o==="captured"?g.soundCaptured||"Sound captured":g.notListening||"Not listening"),e.hidden=o!=="off",C.hidden=o==="off",ie.hidden=se.hidden=o!=="captured",y.textContent=o==="captured"?g.soundCaptured||"Sound captured":o==="tail"?g.soundDetectedFinishing||"Sound detected — finishing clip…":g.listeningNextSound||"Listening for the next sound…",L.textContent=o==="listening"?g.keepingRecentAudio||"Keeping the most recent ~10 seconds":o==="captured"?g.previewClipOrSend||"Preview the clip or send your report.":""}function Ie(o,l){const m=new ArrayBuffer(44+o.length*2),s=new DataView(m),D=(p,W)=>{for(let V=0;V<W.length;V++)s.setUint8(p+V,W.charCodeAt(V))};D(0,"RIFF"),s.setUint32(4,m.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,o.length*2,!0);for(let p=0;p<o.length;p++){const W=Math.max(-1,Math.min(1,o[p]));s.setInt16(44+p*2,W<0?W*32768:W*32767,!0)}return new Blob([m],{type:"audio/wav"})}function Ae(){return`sound-signal-manual-${new Date().toISOString().replace(/[-:]/g,"").slice(0,15)}.wav`}async function me(){var o;_.hidden=!0,e.disabled=!0,t&&(t.dataset.state="starting"),n&&(n.dataset.state="starting"),i&&(i.textContent=g.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.");M=await navigator.mediaDevices.getUserMedia({audio:{channelCount:1},video:!1}),d=new AudioContext,await d.audioWorklet.addModule(new URL("data:text/javascript;base64,Y2xhc3MgQnVyc3RMaXN0ZW5lclByb2Nlc3NvciBleHRlbmRzIEF1ZGlvV29ya2xldFByb2Nlc3NvciB7CiAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgICBzdXBlcigpOwogICAgICAgIHRoaXMuc2FtcGxlcyA9IG5ldyBGbG9hdDMyQXJyYXkoTWF0aC5jZWlsKHNhbXBsZVJhdGUgKiAxMCkpOwogICAgICAgIHRoaXMucG9zaXRpb24gPSAwOwogICAgICAgIHRoaXMuY291bnQgPSAwOwogICAgICAgIHRoaXMuaW50ZXJ2YWwgPSAwOwogICAgICAgIHRoaXMuZW5lcmd5ID0gMDsKICAgICAgICB0aGlzLnBlYWsgPSAwOwogICAgICAgIHRoaXMuYmFzZWxpbmUgPSAwLjAxOwogICAgICAgIHRoaXMubGFzdFRyaWdnZXIgPSAtc2FtcGxlUmF0ZSAqIDEwOwogICAgICAgIHRoaXMucG9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GFydCArIGkpICUgdGhpcy5zYW1wbGVzLmxlbmd0aF07CiAgICAgICAgdGhpcy5wb3J0LnBvc3RNZXNzYWdlKHsgdHlwZTogImNsaXAiLCBzYW1wbGVzOiBjbGlwLCBzYW1wbGVSYXRlIH0sIFtjbGlwLmJ1ZmZlcl0pOwogICAgfQoKICAgIHByb2Nlc3MoaW5wdXRzKSB7CiAgICAgICAgY29uc3QgY2hhbm5lbCA9IGlucHV0c1swXT8uWzBdOwogICAgICAgIGlmICghY2hhbm5lbCB8fCB0aGlzLm1vZGUgPT09ICJjYXB0dXJlZCIpIHJldHVybiB0cnVlOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgY2hhbm5lbC5sZW5ndGg7IGkrKykgewogICAgICAgICAgICBjb25zdCB2YWx1ZSA9IGNoYW5uZWxbaV07CiAgICAgICAgICAgIHRoaXMuc2FtcGxlc1t0aGlzLnBvc2l0aW9uXSA9IHZhbHVlOwogICAgICAgICAgICB0aGlzLnBvc2l0aW9uID0gKHRoaXMucG9zaXRpb24gKyAxKSAlIHRoaXMuc2FtcGxlcy5sZW5ndGg7CiAgICAgICAgICAgIHRoaXMuY291bnQgPSBNYXRoLm1pbih0aGlzLmNvdW50ICsgMSwgdGhpcy5zYW1wbGVzLmxlbmd0aCk7CiAgICAgICAgICAgIHRoaXMuZW5lcmd5ICs9IHZhbHVlICogdmFsdWU7CiAgICAgICAgICAgIHRoaXMucGVhayA9IE1hdGgubWF4KHRoaXMucGVhaywgTWF0aC5hYnModmFsdWUpKTsKICAgICAgICAgICAgdGhpcy5pbnRlcnZhbCsrOwogICAgICAgICAgICB0aGlzLmZyYW1lKys7CiAgICAgICAgICAgIGlmICh0aGlzLm1vZGUgPT09ICJ0YWlsIikgewogICAgICAgICAgICAgICAgaWYgKC0tdGhpcy5wb3N0Um9sbCA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHRoaXMubW9kZSA9ICJjYXB0dXJlZCI7CiAgICAgICAgICAgICAgICAgICAgdGhpcy5zZW5kQ2xpcChNYXRoLm1pbih0aGlzLmNvdW50LCBNYXRoLnJvdW5kKHNhbXBsZVJhdGUgKiAxMCkpKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodGhpcy5pbnRlcnZhbCA+PSBNYXRoLnJvdW5kKHNhbXBsZVJhdGUgLyAzMCkpIHsKICAgICAgICAgICAgICAgIGNvbnN0IHJtcyA9IE1hdGguc3FydCh0aGlzLmVuZXJneSAvIHRoaXMuaW50ZXJ2YWwpOwogICAgICAgICAgICAgICAgdGhpcy5wb3J0LnBvc3RNZXNzYWdlKHsgdHlwZTogImxldmVsIiwgdmFsdWU6IE1hdGgubWF4KHJtcywgdGhpcy5wZWFrICogMC41KSB9KTsKICAgICAgICAgICAgICAgIGlmICh0aGlzLm1vZGUgPT09ICJsaXN0ZW5pbmciICYmIHRoaXMuZnJhbWUgPiBzYW1wbGVSYXRlICYmIHRoaXMuZnJhbWUgLSB0aGlzLmxhc3RUcmlnZ2VyID4gc2FtcGxlUmF0ZSAqIDMKICAgICAgICAgICAgICAgICAgICAmJiBybXMgPiBNYXRoLm1heCgwLjA3NSwgdGhpcy5iYXNlbGluZSAqIDUpICYmIHRoaXMucGVhayA+IDAuMTYpIHsKICAgICAgICAgICAgICAgICAgICB0aGlzLm1vZGUgPSAidGFpbCI7CiAgICAgICAgICAgICAgICAgICAgdGhpcy5wb3N0Um9sbCA9IE1hdGgucm91bmQoc2FtcGxlUmF0ZSAqIDIpOwogICAgICAgICAgICAgICAgICAgIHRoaXMubGFzdFRyaWdnZXIgPSB0aGlzLmZyYW1lOwogICAgICAgICAgICAgICAgICAgIHRoaXMucG9ydC5wb3N0TWVzc2FnZSh7IHR5cGU6ICJ0cmlnZ2VyIiB9KTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAodGhpcy5tb2RlID09PSAibGlzdGVuaW5nIikgewogICAgICAgICAgICAgICAgICAgIHRoaXMuYmFzZWxpbmUgPSB0aGlzLmJhc2VsaW5lICogMC45OCArIE1hdGgubWluKHJtcywgdGhpcy5iYXNlbGluZSAqIDIpICogMC4wMjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHRoaXMuaW50ZXJ2YWwgPSAwOwogICAgICAgICAgICAgICAgdGhpcy5lbmVyZ3kgPSAwOwogICAgICAgICAgICAgICAgdGhpcy5wZWFrID0gMDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KfQoKcmVnaXN0ZXJQcm9jZXNzb3IoImJ1cnN0LWxpc3RlbmVyIiwgQnVyc3RMaXN0ZW5lclByb2Nlc3Nvcik7Cg==",import.meta.url)),X=d.createMediaStreamSource(M),c=new AudioWorkletNode(d,"burst-listener"),S=d.createGain(),S.gain.value=0,c.port.onmessage=({data:l})=>{if(l.type==="level"&&(G==="listening"||G==="tail"))I[f]=l.value,f=(f+1)%I.length,$();else if(l.type==="trigger"&&G==="listening")x("tail");else if(l.type==="clip"){const m=new File([Ie(l.samples,l.sampleRate)],Ae(),{type:"audio/wav"});v?(v(m),v=null):G==="tail"&&(ee(),k=m,N=URL.createObjectURL(m),Z.src=N,Z.hidden=!1,x("captured"))}},X.connect(c),c.connect(S).connect(d.destination),await d.resume(),I.fill(0),f=0,$(),x("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(),S==null||S.disconnect(),M==null||M.getTracks().forEach(o=>o.stop()),await(d==null?void 0:d.close()),M=d=X=c=S=null,I.fill(0),f=0,x("off")}function te(){ee(),I.fill(0),f=0,$(),c==null||c.port.postMessage("reset"),c&&x("listening")}return e.addEventListener("click",me),Ce.addEventListener("click",P),se.addEventListener("click",te),ie.addEventListener("click",te),window.addEventListener("pagehide",P),document.addEventListener("visibilitychange",()=>{document.hidden&&P()}),{get active(){return G!=="off"},async clipForSubmission(){return k||(G==="off"||!c?null:new Promise(o=>{v=o,c.port.postMessage("snapshot")}))},submissionSucceeded:te}}const a=window.BOOMCAST_I18N||{},A=document.querySelector("#manual-report-form"),K=document.querySelector("#manual-submit"),F=document.querySelector("#manual-submit-label"),b=document.querySelector("#manual-occurrences"),le=document.querySelector("#manual-decrease"),ge=document.querySelector("#manual-increase"),he=document.querySelector("#manual-latitude"),ye=document.querySelector("#manual-longitude"),O=document.querySelector("#manual-direction-degrees"),we=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"),J=document.querySelector("#manual-compass-home"),z=document.querySelector("#manual-compass-rose"),fe=document.querySelector("#manual-compass-heading"),E=document.querySelector("#manual-report-time"),re=document.querySelector("#report-clock-hours"),ce=document.querySelector("#report-clock-minutes");let B,T,R=null;const Y=be(),ne=e=>Number.isFinite(e),ae=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)],Q=e=>Number(e.toFixed(1)).toString(),ue=()=>{var C,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=(C=t.find(L=>L.type==="hour"))==null?void 0:C.value,i=(y=t.find(L=>L.type==="minute"))==null?void 0:y.value;!n||!i||!re||!ce||(re.textContent=n,ce.textContent=i,E.setAttribute("aria-label",`${n}:${i}`))};ue();E&&window.setInterval(ue,6e4);const Ge=(F==null?void 0:F.textContent)||"I heard fireworks",h=(e,t="")=>{if(A.dataset.state=e,K.disabled=e==="submitting",F&&(F.textContent=e==="submitting"?a.sendingReport||"Sending…":Ge),t){const n=document.querySelector("#manual-submission");n.className="manual-report__submission",n.textContent=t}},de=(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",a.reportFailed||"Could not send the report. Please try again."),!1)},q=e=>{const t=Number.parseInt(e,10);b.value=String(Math.min(1e3,Math.max(1,Number.isFinite(t)?t:1))),le.disabled=b.value==="1",ge.disabled=b.value==="1000"};q(b.value);le.addEventListener("click",()=>q(Number(b.value)-1));ge.addEventListener("click",()=>q(Number(b.value)+1));b.addEventListener("change",()=>q(b.value));const U=()=>{B&&window.removeEventListener("deviceorientation",B),window.clearTimeout(T),B=null,T=null,H.hidden=!0,R=null},Se=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)`}fe.textContent=`${Q(e)}° · ${ae(e)}`,H.setAttribute("aria-label",`${a.compassDirection||"Compass direction"} ${Q(e)}° ${ae(e)}`),H.hidden=!1},ve=(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 C=document.createElement("span");C.textContent=a.accuracy||"Accuracy";const y=document.createElement("strong");y.textContent=`±${Q(t)}°`,n.replaceChildren(C,y)}const i=j();i&&(i.textContent=a.stopCapture||"Stop measuring direction")},Re=()=>{u()&&(U(),r()&&(r().dataset.state="measuring"),u().textContent=a.pointPhone||"Point the top of your phone toward the sound — direction will update live.",B=e=>{const t="webkitCompassHeading"in e,n=ne(e.webkitCompassHeading)?e.webkitCompassHeading:!t&&e.absolute&&ne(e.alpha)?(360-e.alpha)%360:null;if(n===null||n<0||n>=360)return;const i=ne(e.webkitCompassAccuracy)&&e.webkitCompassAccuracy>=0?e.webkitCompassAccuracy:null;O.value=n,we.value=i===null?"":i,ve(n,i),Se(n),window.clearTimeout(T)},window.addEventListener("deviceorientation",B),T=window.setTimeout(()=>{U();const e=j(),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))},Ze=async()=>{var e;if(B){U(),j().textContent=a.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=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}}Re()},oe=async()=>{if(q(b.value),U(),h("submitting",a.sendingReport||"Sending…"),Y!=null&&Y.active){try{const e=await Y.clipForSubmission(),t=new FormData(A);e&&t.append("audio",e);const n=await fetch(A.action,{method:"POST",body:t,credentials:"same-origin"}),i=await n.text();de(i,n.ok)&&Y.submissionSucceeded()}catch{h("error",a.reportFailed||"Could not send the report. Please try again.")}return}if(window.htmx)try{htmx.ajax("POST",A.action,{source:A,target:"#manual-submission",swap:"outerHTML"})}catch{h("error",a.reportFailed||"Could not send the report. Please try again.")}else A.submit()};A.addEventListener("click",e=>{e.target.closest("#manual-capture-direction")&&Ze(),e.target.closest(".manual-direction-panel__close")&&(U(),J&&J.append(H))});document.body.addEventListener("htmx:beforeSwap",e=>{var t;((t=e.detail.target)==null?void 0:t.id)==="manual-direction-panel"&&J&&J.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 C=Number(O.value);t.textContent=`${ae(C)} · ${Q(C)}°`,r().dataset.state="captured"}});K.addEventListener("click",()=>{if(!K.disabled){if(h("submitting",a.requestingLocation||"Requesting your optional location…"),!navigator.geolocation){oe();return}navigator.geolocation.getCurrentPosition(e=>{he.value=e.coords.latitude,ye.value=e.coords.longitude,oe()},()=>oe(),{enableHighAccuracy:!1,maximumAge:3e5,timeout:1e4})}});A.addEventListener("submit",e=>{e.preventDefault(),K.disabled||K.click()});document.body.addEventListener("htmx:afterRequest",e=>{if(e.detail.elt!==A)return;const t=e.detail.xhr;t?t.status>=400?de(t.responseText,!1):h("submitted"):h("error",a.reportFailed||"Could not send the report. Please try again.")});document.body.addEventListener("htmx:sendError",e=>{e.detail.elt===A&&h("error",a.reportFailed||"Could not send the report. Please try again.")});