{"version":3,"file":"events_controller-BodZvOeb.js","sources":["../../../app/javascript/controllers/conversations/events_controller.js"],"sourcesContent":["import { get } from '@rails/request.js'\n\nimport ApplicationController from '../application_controller'\n\nexport default class extends ApplicationController {\n static values = {\n url: String,\n }\n\n static targets = [\n 'paginationFrame',\n 'previousPaginationFrame',\n 'event',\n 'scroll',\n 'page',\n 'timestamp',\n 'unifyTimestamp',\n ]\n\n initialize() {\n this.loadedPages = []\n\n this.scrollToBottom()\n this.onScroll = this.onScroll.bind(this)\n\n super.initialize()\n }\n\n connect() {\n this.element.addEventListener('scroll', this.onScroll)\n super.connect()\n }\n\n disconnect() {\n this.element.removeEventListener('scroll', this.onScroll)\n super.disconnect()\n }\n\n scrollToBottom() {\n this.manualScrolling = true\n\n this.element.scroll({\n top: this.element.scrollHeight,\n behavior: this.messageSent ? \"smooth\" : \"instant\",\n })\n\n this.manualScrolling = false\n }\n\n onScroll() {\n if(this.manualScrolling) return\n\n this.oldBottomScrollPosition = this.bottomScrollPosition\n\n if(this.hasPaginationFrameTarget && this.element.scrollTop < 200 && !this.fetchingNewPage && !this.hasScrollTarget) {\n this.fetchingNewPage = true\n\n this.paginationFrameTarget.classList.remove('hidden')\n this.paginationFrameTarget.classList.add('bottom-5', 'left-5', 'absolute')\n }\n\n const bottomScrollPosition = Math.abs(this.element.scrollTop - (this.element.scrollHeight - this.element.clientHeight))\n\n if(this.hasPreviousPaginationFrameTarget && bottomScrollPosition < 400 && !this.hasScrollTarget) {\n this.previousPaginationFrameTarget.classList.remove('hidden')\n this.previousPaginationFrameTarget.classList.add('bottom-5', 'left-5', 'absolute')\n }\n }\n\n paginationFrameTargetConnected(target) {\n this.fetchingNewPage = false\n\n if(this.hasScrollTarget) {\n this.manualScrolling = true\n\n const value = this.scrollTarget.dataset.value\n this.scrollEventIntoView({ detail: value })\n\n this.nextTick(() => {\n this.manualScrolling = false\n })\n } else if(this.oldScrollHeight) {\n this.element.scroll({\n top: this.element.scrollHeight - this.oldScrollHeight,\n behavior: 'instant',\n })\n }\n\n this.oldScrollHeight = this.element.scrollHeight\n\n const url = new URL(target.getAttribute('src'))\n url.searchParams.set('loaded', this.loadedPages.join(','))\n\n target.setAttribute('src', url.toString())\n\n this.nextTick(() => {\n const page = parseInt(target.dataset.page)\n\n if(this.loadedPages.includes(page)) {\n target.remove()\n }\n })\n }\n\n previousPaginationFrameTargetConnected(target) {\n this.paginationFrameTargetConnected(target)\n }\n\n scrollEventIntoView({ detail: eventId }) {\n const elementId = `conversation_${eventId}`\n const eventElement = this.eventTargets.find((element) => element.id === elementId)\n\n if(eventElement) {\n this.manualScrolling = true\n\n eventElement.classList.replace('text-night-60', 'text-night')\n eventElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' })\n\n return this.nextTick(() => {\n this.manualScrolling = false\n\n if(this.hasScrollTarget) {\n this.scrollTarget.remove()\n }\n\n eventElement.classList.replace('text-night', 'text-night-60')\n }, 500)\n } else if(eventId.includes('message')) {\n return this.dispatch('focus-campaign-message', {\n detail: eventId\n })\n }\n\n const recordId = eventId.split('_').pop()\n const url = this.urlValue.replace(':event', recordId)\n\n get(url, {\n responseKind: 'turbo-stream',\n query: {\n loaded: this.loadedPages.join(',')\n }\n })\n }\n\n pageTargetConnected(target) {\n this.loadedPages = Array.from(new Set([...this.loadedPages, parseInt(target.dataset.value)]))\n target.remove()\n\n this.paginationFrameTargets.forEach((frame) => {\n const url = new URL(frame.getAttribute('src'))\n url.searchParams.set('loaded', this.loadedPages.join(','))\n\n frame.setAttribute('src', url.toString())\n })\n\n this.previousPaginationFrameTargets.forEach((frame) => {\n const url = new URL(frame.getAttribute('src'))\n url.searchParams.set('loaded', this.loadedPages.join(','))\n\n frame.setAttribute('src', url.toString())\n })\n }\n\n unifyTimestampTargetConnected() {\n this.unifyTimestampTarget.remove()\n this.removeDuplicateTimestamps()\n }\n\n removeDuplicateTimestamps() {\n let timestamps = this.timestampTargets\n\n timestamps.forEach((timestamp) => {\n timestamps\n .filter(element => element.id === timestamp.id && element !== timestamp)\n .forEach(element => element.remove())\n\n timestamps = timestamps.filter(element => element !== timestamp)\n })\n }\n\n eventTargetConnected(target) {\n if(target.hasAttribute('data-scroll-into-view') && this.oldBottomScrollPosition < 10) {\n target.scrollIntoView({ behavior: 'smooth', block: 'nearest' })\n }\n }\n\n get bottomScrollPosition() {\n return Math.abs(this.element.scrollTop - (this.element.scrollHeight - this.element.clientHeight))\n }\n}\n"],"names":["events_controller","ApplicationController","bottomScrollPosition","target","value","url","page","eventId","elementId","eventElement","element","recordId","get","frame","timestamps","timestamp","__publicField"],"mappings":"0PAIe,MAAKA,UAASC,CAAsB,CAejD,YAAa,CACX,KAAK,YAAc,CAAA,EAEnB,KAAK,eAAc,EACnB,KAAK,SAAW,KAAK,SAAS,KAAK,IAAI,EAEvC,MAAM,WAAU,CACpB,CAEE,SAAU,CACR,KAAK,QAAQ,iBAAiB,SAAU,KAAK,QAAQ,EACrD,MAAM,QAAO,CACjB,CAEE,YAAa,CACX,KAAK,QAAQ,oBAAoB,SAAU,KAAK,QAAQ,EACxD,MAAM,WAAU,CACpB,CAEE,gBAAiB,CACf,KAAK,gBAAkB,GAEvB,KAAK,QAAQ,OAAO,CAClB,IAAK,KAAK,QAAQ,aAClB,SAAU,KAAK,YAAc,SAAW,SACzC,CAAA,EAED,KAAK,gBAAkB,EAC3B,CAEE,UAAW,CACT,GAAG,KAAK,gBAAiB,OAEzB,KAAK,wBAA0B,KAAK,qBAEjC,KAAK,0BAA4B,KAAK,QAAQ,UAAY,KAAO,CAAC,KAAK,iBAAmB,CAAC,KAAK,kBACjG,KAAK,gBAAkB,GAEvB,KAAK,sBAAsB,UAAU,OAAO,QAAQ,EACpD,KAAK,sBAAsB,UAAU,IAAI,WAAY,SAAU,UAAU,GAG3E,MAAMC,EAAuB,KAAK,IAAI,KAAK,QAAQ,WAAa,KAAK,QAAQ,aAAe,KAAK,QAAQ,aAAa,EAEnH,KAAK,kCAAoCA,EAAuB,KAAO,CAAC,KAAK,kBAC9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