{"version":3,"file":"line_controller-BRvaJXAJ.js","sources":["../../../app/javascript/controllers/audience/line_controller.js"],"sourcesContent":["import { computePosition, autoUpdate, shift } from \"@floating-ui/dom\"\n\nimport ApplicationController from '../application_controller'\n\nexport default class extends ApplicationController {\n static targets = ['active', 'hover']\n\n initialize() {\n super.initialize()\n\n this.onMouseMove = this.onMouseMove.bind(this)\n }\n\n connect() {\n super.connect()\n\n if(this.hasActiveTarget) {\n super.hide(this.activeTarget)\n }\n\n if(this.hasHoverTarget) {\n super.hide(this.hoverTarget)\n }\n\n document.addEventListener('mousemove', this.onMouseMove)\n }\n\n disconnect() {\n document.removeEventListener('mousemove', this.onMouseMove)\n super.disconnect()\n }\n\n onMouseMove(event) {\n window.mouseX = event.clientX\n window.mouseY = event.clientY\n }\n\n moveLineToTheElement({ detail }) {\n const elementToPositionTo = detail.element\n const elementHeight = elementToPositionTo.clientHeight\n\n this.activeElement = elementToPositionTo\n\n if(this.activeCleanup) {\n this.activeCleanup()\n }\n\n this.activeTarget.style.height = `${elementHeight}px`\n this.activeCleanup = this.float(this.activeTarget, {\n anchor: elementToPositionTo,\n })\n\n this.activeTarget.classList.remove('hidden')\n }\n\n hoverLineToTheElement({ detail }) {\n this.hover({ srcElement: detail.element })\n }\n\n hover({ srcElement: elementToHoverTo }) {\n const elementHeight = elementToHoverTo.clientHeight\n\n if(this.hoverCleanup) {\n this.hoverCleanup()\n }\n\n this.hoverTarget.style.height = `${elementHeight}px`\n this.hoverCleanup = this.float(this.hoverTarget, {\n anchor: elementToHoverTo,\n })\n\n this.hoverTarget.classList.remove('hidden')\n }\n\n blur({ detail }) {\n const { element } = detail\n\n if(this.activeElement !== element) {\n return\n }\n\n if(this.activeCleanup) {\n this.activeCleanup()\n }\n\n this.activeTarget.classList.add('hidden')\n }\n\n unhover() {\n if(this.hoverCleanup) {\n this.hoverCleanup()\n }\n\n this.hoverTarget.classList.add('hidden')\n }\n\n hide({ target }) {\n this.nextTick(() => {\n if(this.withinLine(target) && this.noActiveLine) {\n return this.manuallyHoverLine(target)\n }\n\n if(!this.withinLine(target)) {\n if(this.hasHoverTarget) {\n super.hide(this.hoverTarget)\n }\n\n if(this.hasActiveTarget) {\n super.hide(this.activeTarget)\n }\n\n if(this.activeCleanup) {\n this.activeCleanup()\n }\n\n if(this.hoverCleanup) {\n this.hoverCleanup()\n }\n\n this.activeElement = null\n }\n })\n }\n\n focus({ currentTarget }) {\n this.moveLineToTheElement({ detail: { element: currentTarget } })\n }\n\n disconnectFromActiveElement({ detail }) {\n const { id } = detail\n\n if(this.activeElement && this.activeElement.dataset.lineId === id) {\n this.hide({ target: this.activeElement })\n\n const newElement = document.querySelector(`[data-line-id=\"${id}\"]`)\n\n if(!newElement) return\n\n if(newElement.querySelector('[data-error]')) {\n this.nextTick(() => {\n this.moveLineToTheElement({ detail: { element: newElement }})\n })\n } else {\n this.hover({\n srcElement: document.querySelector(`[data-line-id=\"${id}\"]`)\n })\n }\n }\n }\n\n // private\n\n float(element, { anchor }) {\n return autoUpdate(anchor, element, () => {\n if(anchor === this.activeElement && !document.contains(anchor)) {\n this.activeTarget.classList.add('hidden')\n this.activeCleanup()\n\n const newElement = document.querySelector(`[data-line-id=\"${anchor.dataset.lineId}\"]`)\n\n if(!newElement) return\n\n if(newElement.querySelector('[data-error]')) {\n this.moveLineToTheElement({ detail: { element: newElement }})\n this.hover({ srcElement: newElement })\n } else {\n this.hover({\n srcElement: document.querySelector(`[data-line-id=\"${anchor.dataset.lineId}\"]`)\n })\n }\n\n return\n }\n\n computePosition(anchor, element, {\n placement: 'left',\n middleware: [\n shift(),\n ]\n }).then(({x, y}) => {\n Object.assign(element.style, {\n top: `${y}px`,\n });\n });\n })\n }\n\n manuallyHoverLine(target) {\n // If clicked within a line that is not active, hover it. This happens when a line's input is focused,\n // and the user clicks within the line that causes the input to lose focus, hence removing the active line.\n // In that case we need to hover the element again manually to invoke a mouseenter event.\n\n super.hide(this.activeTarget)\n\n if(this.activeCleanup) {\n this.activeCleanup()\n }\n\n this.hover({ srcElement: target.closest('.line') || target })\n }\n\n withinLine(target) {\n return target.closest('.line') || target.classList.contains('line')\n }\n\n get noActiveLine() {\n return !document.querySelector('[data-line-active]')\n }\n}\n"],"names":["line_controller","ApplicationController","event","detail","elementToPositionTo","elementHeight","elementToHoverTo","element","target","currentTarget","id","newElement","anchor","autoUpdate","computePosition","shift","x","y","__publicField"],"mappings":"kRAIe,MAAKA,UAASC,CAAsB,CAGjD,YAAa,CACX,MAAM,WAAU,EAEhB,KAAK,YAAc,KAAK,YAAY,KAAK,IAAI,CACjD,CAEE,SAAU,CACR,MAAM,QAAO,EAEV,KAAK,iBACN,MAAM,KAAK,KAAK,YAAY,EAG3B,KAAK,gBACN,MAAM,KAAK,KAAK,WAAW,EAG7B,SAAS,iBAAiB,YAAa,KAAK,WAAW,CAC3D,CAEE,YAAa,CACX,SAAS,oBAAoB,YAAa,KAAK,WAAW,EAC1D,MAAM,WAAU,CACpB,CAEE,YAAYC,EAAO,CACjB,OAAO,OAASA,EAAM,QACtB,OAAO,OAASA,EAAM,OAC1B,CAEE,qBAAqB,CAAE,OAAAC,GAAU,CAC/B,MAAMC,EAAsBD,EAAO,QAC7BE,EAAgBD,EAAoB,aAE1C,KAAK,cAAgBA,EAElB,KAAK,eACN,KAAK,cAAa,EAGpB,KAAK,aAAa,MAAM,OAAS,GAAGC,CAAa,KACjD,KAAK,cAAgB,KAAK,MAAM,KAAK,aAAc,CACjD,OAAQD,CACT,CAAA,EAED,KAAK,aAAa,UAAU,OAAO,QAAQ,CAC/C,CAEE,sBAAsB,CAAE,OAAAD,GAAU,CAChC,KAAK,MAAM,CAAE,WAAYA,EAAO,OAAS,CAAA,CAC7C,CAEE,MAAM,CAAE,WAAYG,GAAoB,CACtC,MAAMD,EAAgBC,EAAiB,aAEpC,KAAK,cACN,KAAK,aAAY,EAGnB,KAAK,YAAY,MAAM,OAAS,GAAGD,CAAa,KAChD,KAAK,aAAe,KAAK,MAAM,KAAK,YAAa,CAC/C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