{"version":3,"file":"batch_update_controller-BHGQgwW1.js","sources":["../../../app/javascript/controllers/audience/batch_update_controller.js"],"sourcesContent":["import { post, get } from '@rails/request.js'\n\nimport ModalController from '../modal_controller'\n\nimport { useNumberHelpers } from '../mixins/useNumberHelpers'\n\nexport default class extends ModalController {\n static values = {\n url: String,\n }\n\n static targets = [\n 'pane',\n 'detailPaneContainer',\n 'singularProfilePane',\n 'name',\n 'submitButton',\n 'modificationsContainer',\n 'propertyForm',\n 'propertyFormSkeleton',\n 'overrideCheckbox',\n 'stateModificationContainer',\n 'stateOptionsButton',\n ]\n\n initialize() {\n this.abortChanges = this.abortChanges.bind(this)\n super.initialize()\n }\n\n connect() {\n useNumberHelpers(this)\n super.connect()\n }\n\n async create({ currentTarget }) {\n currentTarget.disabled = true\n currentTarget.querySelector('[data-text]').innerText = currentTarget.dataset.disableWith\n\n await post(this.urlValue, {\n responseKind: 'turbo-stream',\n body: {\n update: {\n ...this.activePropertyPayload,\n contact_ids_to_operate_on: this.contactIdsToOperateOn,\n state: this.stateValue,\n },\n },\n })\n\n currentTarget.disabled = false\n\n this.reset()\n this.close()\n\n this.dispatch('confirmed', {\n target: this.element,\n })\n }\n\n onSelectionChange({ detail }) {\n const { count, contactIdsToOperateOn } = detail\n\n this.contactIdsToOperateOn = contactIdsToOperateOn\n\n if (count <= 1) {\n return this.reset()\n }\n\n const content = this.numberWithDelimiter(count)\n\n this.nameTarget.innerText = t.audience.batch_update.title.replace(\n '%{count}',\n content\n )\n\n this.titleTarget.innerText = t.audience.batch_update.modal.title.replace(\n '%{count}',\n content\n )\n\n this.submitButtonTarget.querySelector('[data-text]').innerText = t.audience.batch_update.submit.replace('%{count}', content)\n\n this.paneTarget.classList.remove('hidden')\n this.singularProfilePaneTarget.classList.add('hidden')\n\n this.detailPaneContainerTarget.classList.remove('hidden')\n this.replaceListAndTagsWithSkeletons()\n\n get(this.urlValue, {\n responseKind: 'turbo-stream',\n query: {\n count: content,\n contact_ids_to_operate_on: contactIdsToOperateOn,\n }\n })\n }\n\n requestConfirmation({ detail, srcElement }) {\n this.stateValue = null\n\n this.activePropertyPayload = detail\n this.activePropertyFormElement = srcElement\n\n this.abortAllChanges()\n\n const content = srcElement.cloneNode(true)\n this.transform(content)\n\n if(['date', 'birthday'].includes(this.activePropertyPayload.kind)) {\n content.querySelector('[data-content]').innerHTML = ''\n\n if(Object.values(this.activePropertyPayload.value).some(value => value.length === 0)) {\n return this.openAltViewOfDateProperty(content, srcElement)\n }\n\n const li = document.createElement('span')\n li.innerText = `${t.date.month_names[parseInt(this.activePropertyPayload.value.month)]}, ${parseInt(this.activePropertyPayload.value.day)} ${this.activePropertyPayload.value.year}`\n content.querySelector('[data-content]').appendChild(li)\n }\n\n this.modificationsContainerTarget.classList.remove('flex-col')\n this.modificationsContainerTarget.replaceWith(content)\n\n this.modificationsContainerTarget.classList.remove('hidden')\n this.stateModificationContainerTarget.classList.add('hidden')\n\n this.open()\n }\n\n onDialogClose() {\n this.modificationsContainerTarget.innerHTML = ''\n\n if(this.activePropertyFormElement) {\n this.dispatch('cancelled', {\n target: this.activePropertyFormElement,\n })\n }\n\n if(this.stateValue) {\n this.stateOptionsButtonTarget.click()\n this.stateValue = null\n }\n\n this.activePropertyFormElement = null\n }\n\n updatePathValue({ detail }) {\n this.urlValue = detail.batchUpdateUrl\n }\n\n changeState({ currentTarget }) {\n const { state } = currentTarget.dataset\n\n this.stateModificationContainerTarget.querySelector('p').textContent = t.audience.batch_update.modal.state[state]\n\n this.stateModificationContainerTarget.classList.remove('hidden')\n this.modificationsContainerTarget.classList.add('hidden')\n\n this.stateValue = state\n this.open()\n }\n\n // private\n\n transform(content) {\n const batchClearAllCheckbox = content.querySelector('input[type=\"checkbox\"][data-audience--batch-update-target=\"overrideCheckbox\"]')\n\n content.querySelectorAll('input:not(input[type=\"hidden\"])').forEach((input) => {\n if(input.type === 'checkbox') {\n return input.classList.add('pointer-events-none')\n }\n\n if(input.hasAttribute('data-search')) {\n return input.remove()\n }\n\n const span = document.createElement('span')\n const overrideCheckbox = ['date', 'birthday'].includes(this.activePropertyPayload.kind) ? input.nextElementSibling : batchClearAllCheckbox\n\n input.classList.add('hidden')\n input.insertAdjacentElement('afterend', span)\n\n if(overrideCheckbox?.checked && input.value.length === 0) {\n span.classList.add('line-through', 'acts-as-placeholder')\n span.innerText = ['date', 'birthday'].includes(this.activePropertyPayload.kind) ? input.placeholder : t.properties.fields.multiple\n } else {\n span.innerText = input.value || input.placeholder\n\n if(input.nextElementSibling.hasAttribute('data-spacer')) {\n input.nextElementSibling.remove()\n }\n }\n })\n\n content\n .querySelectorAll('.dropdown-container, input[type=\"hidden\"], input[type=\"checkbox\"][data-audience--batch-update-target=\"overrideCheckbox\"]')\n .forEach(element => element.remove())\n\n this.removeDataAttributes(content)\n\n content\n .querySelectorAll('.property__array_input-group, .default__input_group')\n .forEach((group) => group.classList.add('disabled'))\n\n content.setAttribute(\n 'data-audience--batch-update-target',\n 'modificationsContainer'\n )\n }\n\n openAltViewOfDateProperty(content, srcElement) {\n content.querySelector('[data-content]').classList.add('flex-col')\n\n Object\n .entries(this.activePropertyPayload.value)\n .filter(([key, value]) => {\n let duplicateSrcElement = srcElement.cloneNode(true)\n const input = duplicateSrcElement.querySelector(`input[name=\"date[${key}]\"]`)\n\n return value.length > 0 || input.value.length === 0 && input.nextElementSibling.checked\n })\n .forEach(([key, value]) => {\n let duplicateSrcElement = srcElement.cloneNode(true)\n\n this.removeDataAttributes(duplicateSrcElement)\n\n duplicateSrcElement.querySelector('x-property[data-name]').textContent += t.properties.fields.date_parts[key]\n duplicateSrcElement.querySelector('[data-container]').innerText = value.length === 0 ? t.properties.fields.date_placeholders[key] : parseInt(value)\n\n if(value.length === 0) {\n duplicateSrcElement.querySelector('[data-container]').classList.add('line-through', 'acts-as-placeholder')\n }\n\n content.querySelector('[data-content]').appendChild(duplicateSrcElement)\n })\n\n this.modificationsContainerTarget.innerHTML = content.querySelector('[data-content]').innerHTML\n this.modificationsContainerTarget.classList.add('flex-col')\n\n this.open()\n }\n\n reset() {\n this.singularProfilePaneTarget.classList.remove('hidden')\n this.paneTarget.classList.add('hidden')\n\n this.propertyFormTargets.forEach(this.abortChanges)\n this.replaceListAndTagsWithSkeletons()\n }\n\n hidePane() {\n this.singularProfilePaneTarget.classList.remove('hidden')\n this.paneTarget.classList.add('hidden')\n }\n\n replaceListAndTagsWithSkeletons() {\n this.propertyFormSkeletonTargets.forEach((skeleton) => {\n if(['list', 'tags'].includes(skeleton.dataset.kind) && ['list', 'tags'].includes(skeleton.previousElementSibling.dataset.kind)) {\n skeleton.classList.remove('hidden')\n skeleton.previousElementSibling.remove()\n }\n })\n }\n\n // Ensures that only one property form is active at a time\n // When there's a pending change and the user clicks on another property\n // The property's form shows up, this is to prevent these cases from happening.\n abortAllChanges() {\n this.propertyFormTargets\n .filter((target) => target !== this.activePropertyFormElement)\n .forEach(this.abortChanges)\n\n this.dispatch('aborted', {\n target: document,\n })\n }\n\n abortChanges(target) {\n this.dispatch('aborted', {\n target,\n })\n }\n\n removeDataAttributes(node) {\n if (node.nodeType === 1) {\n Array.from(node.attributes).forEach((attr) => {\n if (attr.name.startsWith('data-controller') || attr.name.endsWith('-target')) {\n node.removeAttribute(attr.name)\n }\n })\n }\n\n if (node.tagName === 'FORM') {\n node.removeAttribute('action')\n node.removeAttribute('method')\n node.removeAttribute('id')\n }\n\n node.childNodes.forEach((child) => this.removeDataAttributes(child))\n }\n}\n"],"names":["batch_update_controller","ModalController","useNumberHelpers","currentTarget","post","detail","count","contactIdsToOperateOn","content","get","srcElement","value","li","state","batchClearAllCheckbox","input","span","overrideCheckbox","element","group","key","duplicateSrcElement","skeleton","target","node","attr","child","__publicField"],"mappings":"yTAMe,MAAKA,UAASC,CAAgB,CAmB3C,YAAa,CACX,KAAK,aAAe,KAAK,aAAa,KAAK,IAAI,EAC/C,MAAM,WAAU,CACpB,CAEE,SAAU,CACRC,EAAiB,IAAI,EACrB,MAAM,QAAO,CACjB,CAEE,MAAM,OA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