{"version":3,"file":"templates_controller-DA6XNYnf.js","sources":["../../../app/javascript/controllers/compose/templates_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 technologies: Array,\n url: String,\n query: String\n }\n\n static targets = [\"template\", \"skeletonsList\", \"list\"]\n\n initialize() {\n super.initialize()\n this.activeIndex = 0\n }\n\n removeInvisibilityClasses() {\n this.removeClass(this.element,\"invisible\")\n this.element.classList.add(\"h-[20rem]\")\n this.element.setAttribute('data-visible', '')\n }\n\n show(e) {\n const detail = e.type === \"chat:templates:show\" ? e.detail.detail : e.detail\n\n this.removeInvisibilityClasses()\n\n if(detail !== this.queryValue) {\n clearTimeout(this.searchTimeout)\n\n this.queryValue = detail\n this.search()\n }\n }\n\n hide() {\n super.hide(...this.templateTargets)\n\n this.addClass(this.element,\"invisible\")\n this.element.classList.remove(\"h-[20rem]\")\n\n this.element.removeAttribute('data-visible')\n\n this.nextTick(() => {\n super.show(...this.templateTargets)\n })\n }\n\n onTemplateSelection({ currentTarget: templateElement }) {\n this.dispatchSelectedEvent(templateElement, {\n method: 'click'\n })\n }\n\n onTechnologyChange({ detail }) {\n this.technologiesValue = [detail.technology.id]\n this.search()\n }\n\n toggle(event) {\n if(event.detail) {\n this.removeInvisibilityClasses()\n } else {\n this.hide()\n }\n }\n\n close() {\n if(this.visible) {\n this.hide()\n\n this.dispatch('closed-by-escape')\n }\n }\n\n onArrowUp(e) {\n if(this.invisible) return;\n\n e.preventDefault()\n e.stopPropagation()\n\n this.activeIndex -= 1\n this.updateHighlightedElement({ scrollIntoView: true })\n }\n\n onArrowDown(e) {\n if(this.invisible) return;\n\n e.preventDefault()\n e.stopPropagation()\n\n this.activeIndex += 1\n this.updateHighlightedElement({ scrollIntoView: true })\n }\n\n onTemplateHover({ currentTarget }) {\n if(this.invisible) return\n\n this.activeIndex = this.templateTargets.indexOf(currentTarget)\n this.updateHighlightedElement({ scrollIntoView: false })\n }\n\n onEnter(e) {\n if(this.invisible) return;\n\n e.preventDefault()\n e.stopPropagation()\n\n const template = this.templateTargets[this.activeIndex]\n\n if(template) {\n this.dispatchSelectedEvent(template, {\n method: 'enter'\n })\n }\n }\n\n // private\n\n dispatchSelectedEvent(template, { method }) {\n const payload = JSON.parse(template.dataset.payload)\n\n this.dispatch(\"selected\",\n {\n detail: {\n ...payload,\n selectionMethod: method,\n coupon: payload.coupon ? JSON.parse(payload.coupon) : null,\n element: template\n }\n }\n )\n }\n\n async search() {\n this.searchTimeout = setTimeout(async () => {\n if(this.queryValue) {\n super.hide(this.listTarget)\n super.show(this.skeletonsListTarget)\n }\n\n await get(this.urlValue, {\n responseKind: 'turbo-stream',\n query: {\n technologies: this.technologiesValue.join(','),\n search: this.queryValue.trim().replace(/\\u200C/g, \"\")\n }\n })\n\n super.hide(this.skeletonsListTarget)\n super.show(this.listTarget)\n\n this.activeIndex = 0\n }, 300)\n }\n\n updateHighlightedElement({ scrollIntoView }) {\n if(this.activeIndex < 0) {\n this.activeIndex = this.templateTargets.length - 1\n } else if(this.activeIndex >= this.templateTargets.length) {\n this.activeIndex = 0\n }\n\n this.templateTargets.forEach((element, index) => {\n if(index === this.activeIndex) {\n this.addClass(element, ...this.activeClasses)\n\n element.setAttribute('data-active', '')\n\n if(scrollIntoView) {\n element.scrollIntoView(false)\n }\n } else {\n this.removeClass(element, ...this.activeClasses)\n this.element.removeAttribute('data-active')\n }\n })\n }\n\n get visible() {\n return this.element.hasAttribute('data-visible')\n }\n\n get invisible() {\n return !this.visible\n }\n\n get activeClasses() {\n return ['bg-night-5', 'border-l-tiger']\n }\n}\n"],"names":["templates_controller","ApplicationController","detail","templateElement","event","currentTarget","template","method","payload","get","scrollIntoView","element","index","__publicField"],"mappings":"+PAIe,MAAKA,UAASC,CAAsB,CASjD,YAAa,CACX,MAAM,WAAU,EAChB,KAAK,YAAc,CACvB,CAEE,2BAA4B,CAC1B,KAAK,YAAY,KAAK,QAAQ,WAAW,EACz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