{"version":3,"file":"audio_controller-BRdlOqRA.js","sources":["../../../app/javascript/controllers/message/audio_controller.js"],"sourcesContent":["import interact from \"interactjs\"\n\nimport ApplicationController from \"../application_controller\"\n\nexport default class extends ApplicationController {\n static values = {\n duration: { type: Number, default: 0 }\n }\n\n static targets = [\"playButton\", \"pauseButton\", \"audio\", \"progress\", \"durationLabel\", \"pointer\", \"bar\", \"input\"]\n\n initialize() {\n if(this.durationValue) {\n this.durationLabelTarget.innerText = this.formatDuration(this.durationValue)\n }\n }\n\n connect() {\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 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\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\n this.delayed(() => {\n this.audioTarget.currentTime = 0\n this.durationValue = this.audioTarget.duration\n this.durationLabelTarget.innerText = this.formatDuration(this.audioTarget.duration * 1000)\n }, 1000)\n\n } else {\n this.durationValue = duration\n this.durationLabelTarget.innerText = this.formatDuration(duration * 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.dispatch('play', {\n target: document.documentElement,\n detail: { audio: this.audioTarget }\n })\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({ type, detail }) {\n if(type === 'click' || type !== 'click' && detail.audio !== this.audioTarget) {\n this.audioTarget.pause()\n }\n }\n}\n"],"names":["audio_controller","ApplicationController","interact","event","newX","maxX","percentage","newTime","currentTime","duration","totalSeconds","minutes","seconds","type","detail","__publicField"],"mappings":"sQAIe,MAAKA,UAASC,CAAsB,CAOjD,YAAa,CACR,KAAK,gBACN,KAAK,oBAAoB,UAAY,KAAK,eAAe,KAAK,aAAa,EAEjF,CAEE,SAAU,CACR,KAAK,Y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