{"version":3,"file":"tags_controller-B2WXhi9y.js","sources":["../../../app/javascript/controllers/property/tags_controller.js"],"sourcesContent":["import {get, post, destroy} from \"@rails/request.js\";\n\nimport PropertyController from \"../contact/property_controller\"\n\nimport { useBatchUpdateNotifiers } from '../mixins/useBatchUpdateNotifiers'\n\nexport default class extends PropertyController {\n static values = {\n labelingUrl: String,\n url: String,\n autofocus: Boolean,\n searchUrl: String,\n key: { type: String, default: \"label\" }\n }\n\n static targets = [\"persisted\", \"item\", \"itemPill\", \"research\", \"deleted\", \"menu\", \"button\"]\n\n initialize() {\n this.placeholderElement = this.element.querySelector(\"x-placeholder\")?.cloneNode(true)\n super.initialize();\n }\n\n connect() {\n super.connect();\n\n if (this.autofocusValue) {\n this.inputTarget.focus()\n }\n\n useBatchUpdateNotifiers(this, {\n onBatchUpdateCancellation: () => {\n if(this.blockedAnchor) {\n return this.blockedAnchor.click()\n }\n\n this.inputTarget.focus()\n },\n onBatchUpdateAbortion: () => this.clearAndHide()\n })\n }\n\n saveChanges(e) {\n if(e.target.nodeName === 'DIALOG' || e.target.getAttribute('data-audience--contact-target') === 'checkbox' || this.isBlockedAnchor(e)) return\n\n const modalContainer = document.querySelector(\"[data-controller='labels--modal'] [data-labels--modal-target='container']\")\n\n if (modalContainer) {\n if (this.withinBoundaries(e) || (this.withinBoundaries(e) && modalContainer?.contains(e.target))) return\n\n if (this.containerTarget.contains(e.target) === false && !this.menuTarget.contains(e.target) && !modalContainer.contains(e.target)) {\n this.onClickOutside(e)\n }\n } else {\n if (this.withinBoundaries(e)) return\n\n if (this.containerTarget.contains(e.target) === false) {\n this.onClickOutside(e)\n }\n }\n }\n\n search() {\n this.clearDebounce()\n\n if (this.inputTarget.value.trim().length === 0) {\n this.menuTarget.classList.add(\"hidden\")\n } else {\n this.debounce(async () => {\n await get(this.searchUrlValue, {\n responseKind: \"turbo-stream\",\n query: {\n search: this.inputTarget.value\n }\n })\n }, 300)\n }\n }\n\n save({currentTarget: label}) {\n const options = {\n responseKind: \"turbo-stream\",\n body: {\n label_id: label.dataset.value,\n mode: this.getMode(),\n label: { name: label.innerText }\n }\n }\n\n const url = this.persistedValue ? this.labelingUrlValue : this.urlValue\n\n post(url, options)\n }\n\n saveNewTag({currentTarget: newTag}) {\n const options = {\n responseKind: \"turbo-stream\",\n body: {\n label: {name: newTag.dataset.value},\n mode: this.getMode()\n }\n }\n\n const url = this.persistedValue ? this.labelingUrlValue : this.urlValue\n\n post(url, options)\n }\n\n persistedTargetConnected() {\n this.persistedValue = true\n this.labelingUrlValue = this.persistedTarget.dataset.url\n\n this.element.querySelector('[data-placeholder]').classList.remove(...this.unsetClasses)\n\n this.persistedTarget.remove()\n }\n\n submitOnEnter() {\n if(this.inputTarget.value.length === 0) {\n if(this.itemsChanged && this.inBatchUpdateMode()) {\n this.requestBatchUpdateConfirmation()\n } else {\n this.clearAndHide()\n }\n\n return\n }\n\n if (this.element.querySelector(\"[data-new]\")) {\n this.element.querySelector(\"[data-new]\").click()\n return\n } else if (this.element.querySelectorAll(\"[data-existing]\").length > 0) {\n this.element.querySelectorAll(\"[data-existing]\")[0].click()\n return\n }\n\n this.clearDebounce()\n this.onInputEnter()\n }\n\n removeItem({ currentTarget }, identifier = '[data-property--tags-target=\"item\"]') {\n const item = currentTarget.closest(identifier)\n\n if(item.hasAttribute('data-added')) {\n return item.remove()\n }\n\n if(item.tagName === 'FORM' && !this.inBatchUpdateMode()) {\n item.requestSubmit()\n }\n\n if(this.inBatchUpdateMode()) {\n item.toggleAttribute('data-removed')\n this.focus()\n } else {\n this.itemPillTargets.find(pill => pill.dataset.id === item.dataset.id)?.remove()\n item.remove()\n }\n }\n\n setLabelToDestroy({currentTarget}) {\n this.elementId = currentTarget.dataset.id\n }\n\n deletedTargetConnected() {\n this.persistedValue = false\n this.element.querySelector('[data-placeholder]').classList.add(...this.unsetClasses)\n\n if(this.hasDeleteTarget) {\n this.deletedTarget.remove()\n }\n }\n\n researchTargetConnected() {\n if(this.containerTarget.classList.contains(\"hidden\")) return\n\n if(\n this.element.querySelectorAll(\"[data-new]\").length === 0\n || this.element.querySelectorAll(\"[data-existing]\").length === 0\n ) {\n this.focus()\n this.search()\n }\n }\n\n onClickOutside(e) {\n if(this.inBatchUpdateMode() && this.itemsChanged) {\n if(e.target.tagName === 'A' || e.target.closest('a')) {\n e.preventDefault()\n e.stopPropagation()\n\n this.blockedAnchor = e.target.closest('a') || e.target\n }\n\n return this.requestBatchUpdateConfirmation()\n }\n\n if(this.hasButtonTarget) {\n this.buttonTarget.classList.remove(\"hidden\")\n }\n\n this.containerTarget.classList.add(\"hidden\")\n this.inputTarget.value = \"\"\n\n if(this.suggestions.length === 0) {\n this.menuTarget.classList.add(\"hidden\")\n }\n }\n\n clearAndHide() {\n if(this.inBatchUpdateMode()) {\n this.itemTargets.filter(item => item.hasAttribute('data-added')).forEach(item => item.remove())\n this.itemTargets.filter(item => item.hasAttribute('data-removed')).forEach(item => item.removeAttribute('data-removed'))\n }\n\n this.clearDebounce()\n this.inputTarget.value = \"\"\n\n this.containerTarget.classList.add(\"hidden\")\n this.menuTarget.classList.add(\"hidden\")\n\n if(this.hasButtonTarget) {\n this.buttonTarget.classList.remove(\"hidden\")\n }\n\n this.blurLine()\n }\n\n async onInputEnter() {\n const options = {\n responseKind: \"turbo-stream\",\n body: {\n label: { name: this.inputTarget.value },\n mode: this.getMode()\n }\n }\n\n const url = this.persistedValue ? this.labelingUrlValue : this.urlValue\n\n await post(url, options)\n this.refocus()\n }\n\n sync() {\n if(this.inputTarget.value.length === 0) {\n clearTimeout(this.searchTimeout)\n\n this.menuTarget.classList.add(\"hidden\")\n document.querySelector(\"[data-controller='labels--modal'] [data-labels--modal-target='container']\")?.remove()\n }\n }\n\n itemTargetConnected() {\n if(this.isVisible(this.menuTarget)) {\n this.refocus()\n }\n\n if(this.element.querySelector('[data-placeholder]')) {\n this.element.querySelector('[data-placeholder]').classList.remove(...this.unsetClasses)\n }\n }\n\n refocus() {\n this.inputTarget.value = \"\"\n this.inputTarget.focus()\n\n this.search()\n\n this.menuTarget.classList.add('hidden')\n }\n\n itemTargetDisconnected() {\n this.inputTarget.focus()\n this.menuTarget.classList.add(\"hidden\")\n\n if(this.itemTargets.length === 0) {\n this.deletedTargetConnected()\n }\n }\n\n get itemsChanged() {\n return this.itemTargets.filter(item => item.hasAttribute('data-removed') || item.hasAttribute('data-added')).length > 0\n }\n\n get suggestions() {\n return this.element.querySelectorAll(\"[data-suggestion='true']\")\n }\n\n get invisibleClass() {\n return \"hidden\"\n }\n\n getValueForBatch() {\n return {\n add_to: this.itemTargets.filter(item => item.hasAttribute('data-added')).map(item => item.innerText.trim()).join(', '),\n remove_from: this.itemTargets.filter(item => item.hasAttribute('data-removed')).map(item => item.innerText.trim()).join(', '),\n }\n }\n}\n"],"names":["TagsController","PropertyController","_a","useBatchUpdateNotifiers","modalContainer","get","label","options","url","post","newTag","currentTarget","identifier","item","pill","__publicField"],"mappings":"mUAMe,MAAKA,UAASC,CAAmB,CAW9C,YAAa,OACX,KAAK,oBAAqBC,EAAA,KAAK,QAAQ,cAAc,eAAe,IAA1C,YAAAA,EAA6C,UAAU,IACjF,MAAM,WAAY,CACtB,CAEE,SAAU,CACR,MAAM,QAAS,EAEX,KAAK,gBACP,KAAK,YAAY,MAAK,EAGxBC,EAAwB,KAAM,CAC5B,0BAA2B,IAAM,CAC/B,GAAG,KAAK,cACN,OAAO,KAAK,cAAc,MAAK,EAGjC,KAAK,YAAY,MAAK,CACvB,EACD,sBAAuB,IAAM,KAAK,aAAY,CAC/C,CAAA,CACL,CAEE,YAAY,EAAG,CACb,GAAG,EAAE,OAAO,WAAa,UAAY,EAAE,OAAO,aAAa,+BAA+B,IAAM,YAAc,KAAK,gBAAgB,CAAC,EAAG,OAEvI,MAAMC,EAAiB,SAAS,cAAc,2EAA2E,EAEzH,GAAIA,EAAgB,CAClB,GAAI,KAAK,iBAAiB,CAAC,GAAM,KAAK,iBAAiB,CAAC,IAAKA,GAAA,MAAAA,EAAgB,SAAS,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