{"version":3,"file":"recorded_voice_controller-DYq4K8eT.js","sources":["../../../app/javascript/controllers/editor/recorded_voice_controller.js"],"sourcesContent":["import {DirectUpload} from \"@rails/activestorage\";\nimport interact from \"interactjs\"\n\nimport ApplicationController from \"../application_controller\"\nimport { useRemove } from \"../mixins/useRemove\"\n\nexport default class extends ApplicationController {\n static values = {\n audioUrl: String,\n duration: Number\n }\n\n static targets = [\"playButton\", \"pauseButton\", \"audio\", \"progress\", \"durationLabel\", \"pointer\", \"bar\", \"input\", \"overlay\", \"overlayActions\"]\n\n initialize() {\n this.durationLabelTarget.innerText = this.formatDuration(this.durationValue)\n this.upload = new DirectUpload(this.inputTarget.files[0], this.inputTarget.dataset.directUploadUrl, this)\n }\n\n connect() {\n this.audioTarget.src = this.audioUrlValue\n this.audioTarget.load()\n\n this.audioTarget.addEventListener(\"loadedmetadata\", this.onAudioMetadataLoaded.bind(this))\n this.audioTarget.addEventListener(\"play\", this.onAudioPlay.bind(this))\n this.audioTarget.addEventListener(\"pause\", this.onAudioPause.bind(this))\n\n useRemove(this, {\n stopEventPropagation: true,\n after: this.dispatchRemoveEvent.bind(this)\n })\n\n this.startUpload()\n\n interact(this.pointerTarget).draggable({\n restrict: {\n restriction: this.barTarget,\n elementRect: { top: 0, left: 0, bottom: 1, right: 1 },\n endOnly: true\n },\n inertia: true,\n modifiers: [\n interact.modifiers.restrictRect({\n restriction: this.barTarget,\n endOnly: true\n })\n ],\n onstart: (event) => {\n event.target.style.zIndex = '9999';\n },\n onmove: (event) => {\n const newX = (parseFloat(event.target.getAttribute('data-x')) || 0) + event.dx;\n const maxX = this.barTarget.clientWidth - event.target.clientWidth;\n\n if(newX < 0 || newX > maxX) return;\n\n this.progressTarget.style.width = `${(newX / maxX) * 100}%`;\n\n event.target.setAttribute('data-x', newX);\n\n const percentage = newX / maxX;\n const newTime = this.audioTarget.duration * percentage;\n this.audioTarget.currentTime = newTime;\n this.durationLabelTarget.innerText = this.formatDuration(newTime * 1000)\n }\n });\n\n this.element.querySelectorAll(\"input\").forEach(input => {\n this.enable(input)\n })\n }\n\n disconnect() {\n this.dispatchRemoveEvent()\n }\n\n onAudioTimeUpdate() {\n const { currentTime, duration } = this.audioTarget\n\n if(this.isPlaying) {\n this.durationLabelTarget.innerText = this.formatDuration(currentTime * 1000)\n this.progressTarget.style.width = `${(currentTime / duration) * 100}%`\n this.progressTarget.style.minWidth = \"8%\"\n\n if (currentTime === duration) {\n this.onAudioEnd()\n }\n }\n }\n\n onAudioMetadataLoaded() {\n const { duration } = this.audioTarget\n\n if (duration === Infinity || isNaN(duration)) {\n this.audioTarget.currentTime = 10000000;\n this.delayed(() => this.audioTarget.currentTime = 0, 1000)\n }\n\n this.audioTarget.addEventListener(\"timeupdate\", this.onAudioTimeUpdate.bind(this))\n }\n\n onAudioPlay() {\n this.show(this.pauseButtonTarget)\n this.hide(this.playButtonTarget)\n\n this.isPlaying = true\n }\n\n onAudioPause() {\n this.show(this.playButtonTarget)\n this.hide(this.pauseButtonTarget)\n\n this.isPlaying = false\n }\n\n onAudioEnd() {\n this.audioTarget.pause()\n\n this.delayed(() => {\n this.show(this.playButtonTarget)\n this.hide(this.pauseButtonTarget)\n\n this.isPlaying = false\n this.audioTarget.currentTime = 0\n\n this.progressTarget.style.removeProperty(\"width\")\n this.progressTarget.style.removeProperty(\"min-width\")\n }, 500)\n }\n\n formatDuration(duration = this.durationValue) {\n const totalSeconds = Math.floor(duration / 1000)\n const minutes = Math.floor(totalSeconds / 60)\n const seconds = Math.floor(totalSeconds % 60)\n\n return `${minutes.toString().padStart(2, \"0\")}:${seconds.toString().padStart(2, \"0\")}`\n }\n\n play() {\n this.audioTarget.play()\n }\n\n pause() {\n this.audioTarget.pause()\n }\n\n startUpload() {\n this.element.setAttribute(\"data-uploading\", \"\")\n\n this.upload.create((error, blob) => {\n if (error) {\n this.element.removeAttribute(\"data-uploading\")\n this.element.setAttribute(\"data-upload-error\", \"\")\n\n this.hide(this.overlayTarget.querySelector('[data-spinner]'))\n this.show(this.overlayActionsTarget)\n } else {\n this.element.removeAttribute(\"data-uploading\")\n this.element.removeAttribute(\"data-upload-error\")\n\n this.hide(this.overlayTarget)\n\n let hiddenField = document.createElement('input')\n hiddenField.type = \"hidden\"\n hiddenField.value = blob.signed_id;\n hiddenField.name = \"message[voice][signed_id]\"\n\n this.element.appendChild(hiddenField)\n }\n })\n }\n\n retryUpload() {\n this.show(this.overlayTarget.querySelector('[data-spinner]'))\n this.hide(this.overlayActionsTarget)\n\n this.startUpload()\n }\n\n dispatchRemoveEvent() {\n this.dispatch(\"remove\", {\n target: document.getElementById(\"editor--voice-tool\")\n })\n }\n\n enable() {\n this.element.removeAttribute('data-disabled')\n super.enable(this.playButtonTarget)\n\n interact(this.pointerTarget).draggable(true)\n }\n\n disable() {\n this.element.setAttribute('data-disabled', '')\n super.disable(this.playButtonTarget)\n\n interact(this.pointerTarget).draggable(false)\n }\n}\n"],"names":["recorded_voice_controller","ApplicationController","DirectUpload","useRemove","interact","event","newX","maxX","percentage","newTime","input","currentTime","duration","totalSeconds","minutes","seconds","error","blob","hiddenField","__publicField"],"mappings":"sWAMe,MAAKA,UAASC,CAAsB,CAQjD,YAAa,CACX,KAAK,oBAAoB,UAAY,KAAK,eAAe,KAAK,aAAa,EAC3E,KAAK,OAAS,IAAIC,EAAa,KAAK,YAAY,MAAM,CAAC,EAAG,KAAK,YAAY,QAAQ,gBAAiB,IAAI,CAC5G,CAEE,SAAU,CACR,KAAK,YAAY,IAAM,KAAK,cAC5B,KAAK,YAAY,KAAI,EAErB,KAAK,YAAY,iBAAiB,iBAAkB,KAAK,sBAAsB,KAAK,IAAI,CAAC,EACzF,KAAK,YAAY,iBAAiB,OAAQ,KAAK,YAAY,KAAK,IAAI,CAAC,EACrE,KAAK,YAAY,iBAAiB,QAAS,KAAK,aAAa,KAAK,IAAI,CAAC,EAEvEC,EAAU,KAAM,CACd,qBAAsB,GACtB,MAAO,KAAK,oBAAoB,KAAK,IAAI,CAC1C,CAAA,EAED,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