{"version":3,"file":"item_controller-CVDWqe66.js","sources":["../../../app/javascript/controllers/artifact/entity/item_controller.js"],"sourcesContent":["import { get } from '@rails/request.js'\n\nimport PropertyController from './property_controller'\n\nexport default class extends PropertyController {\n static values = {\n searchUrl: String,\n format: { type: String, default: 'json' },\n }\n\n static targets = [\n 'popover',\n 'results',\n 'resultTemplate',\n 'option',\n 'amount',\n 'idInput',\n 'trigger',\n ]\n\n initialize() {\n this.search = this.search.bind(this)\n super.initialize()\n }\n\n connect() {\n this.element.querySelectorAll('input').forEach((input) => this.enable(input))\n super.connect()\n }\n\n debounceSearch() {\n if(this.inputTarget.value) {\n this.debounce(this.search)\n } else {\n this.clearDebounce()\n this.hidePopover()\n }\n }\n\n async search() {\n const response = await get(this.searchUrlValue, {\n responseKind: this.formatValue,\n query: {\n append: this.popoverTarget.id,\n q: this.inputTarget.value,\n },\n })\n\n if(this.formatValue === 'json') {\n this.products = await response.json\n\n this.optionTargets.forEach(option => option.remove())\n\n this.products.forEach(product => {\n const item = this.resultTemplateTarget.cloneNode(true)\n item.classList.remove('hidden')\n\n item.innerText = item.dataset.name = product.name\n item.dataset.id = product.id\n\n item.setAttribute('data-artifact--entity--item-target', 'option')\n\n this.resultsTarget.appendChild(item)\n })\n }\n\n this.showPopover()\n }\n\n edit() {\n super.edit()\n\n if(this.valueValue) {\n this.showPopover()\n }\n }\n\n showPopover() {\n this.dispatch('enable', {\n target: this.inputSection,\n })\n\n this.popoverTarget.showPopover()\n }\n\n hidePopover() {\n this.dispatch('disable', {\n target: this.inputSection,\n })\n\n this.popoverTarget.hidePopover()\n }\n\n restore() {\n super.restore()\n this.hidePopover()\n }\n\n setProduct({ currentTarget }) {\n const { id, name } = currentTarget.dataset\n this.placeholderButtonTarget.textContent = this.inputTarget.value = name\n\n this.addClass(this.placeholderButtonTarget, 'font-medium')\n this.removeClass(this.placeholderButtonTarget, 'text-night-60')\n\n this.optionTargets.forEach((option) => {\n if(option === currentTarget) {\n option.classList.add('bg-lavender-light')\n } else {\n option.classList.remove('bg-lavender-light')\n }\n })\n\n if(this.hasAmountTarget) {\n const amount = this.products.find(product => product.id === id).amount\n\n this.dispatch('set', {\n target: this.amountTarget,\n detail: amount,\n })\n }\n\n this.idInputTarget.value = id\n\n this.dispatch('set', {\n detail: {\n id,\n name,\n }\n })\n\n this.save()\n }\n\n save() {\n this.popoverTarget.hidePopover()\n super.save()\n }\n\n onClickOutside({ target }) {\n if(this.element.contains(target) || this.isInvisible(this.inputSectionTarget)) return\n\n this.hidePopover()\n this.clearDebounce()\n\n this.restore()\n }\n\n get isPopoverVisible() {\n const computedStyle = window.getComputedStyle(this.popoverTarget);\n\n return computedStyle.display !== 'none';\n }\n}\n"],"names":["ItemController","PropertyController","input","response","get","option","product","item","currentTarget","id","name","amount","target","__publicField"],"mappings":"kQAIe,MAAKA,UAASC,CAAmB,CAgB9C,YAAa,CACX,KAAK,OAAS,KAAK,OAAO,KAAK,IAAI,EACnC,MAAM,WAAU,CACpB,CAEE,SAAU,CACR,KAAK,QAAQ,iBAAiB,OAAO,EAAE,QAASC,GAAU,KAAK,OAAOA,CAAK,CAAC,EAC5E,MAAM,QAAO,CACjB,CAEE,gBAAiB,CACZ,KAAK,YAAY,MAClB,KAAK,SAAS,KAAK,MAAM,GAEzB,KAAK,cAAa,EAClB,KAAK,YAAW,EAEtB,CAEE,MAAM,QAAS,CACb,MAAMC,EAAW,MAAMC,EAAI,KAAK,eAAgB,CAC9C,aAAc,KAAK,YACnB,MAAO,CACL,OAAQ,KAAK,cAAc,GAC3B,EAAG,KAAK,YAAY,KACrB,CACF,CAAA,EAEE,KAAK,cAAgB,SACtB,KAAK,SAAW,MAAMD,EAAS,KAE/B,KAAK,cAAc,QAAQE,GAAUA,EAAO,OAAQ,CAAA,EAEpD,KAAK,SAAS,QAAQC,GAAW,CAC/B,MAAMC,EAAO,KAAK,qBAAqB,UAAU,EAAI,EACrDA,EAAK,UAAU,OAAO,QAAQ,EAE9BA,EAAK,UAAYA,EAAK,Q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