{"version":3,"file":"template_controller-DwQNKHWq.js","sources":["../../../app/javascript/controllers/compose/template_controller.js"],"sourcesContent":["import ApplicationController from '../application_controller'\n\nexport default class extends ApplicationController {\n static values = {\n inputName: String,\n previewable: { type: Boolean, default: false },\n buttonGroups: Array\n }\n\n static targets = [\n 'buttonsContainer',\n 'buttonGroupTemplate',\n 'buttonTemplate',\n 'group',\n 'compose',\n 'buttonManager'\n ]\n\n insertButtons({ detail: buttonGroups }) {\n buttonGroups.forEach(({ buttons, type}) => {\n if(this.buttonGroupsValue.find(group => group.type === type)) {\n this.buttonGroupsValue = this.buttonGroupsValue.map(group => {\n if(group.type === type) {\n return {\n type,\n buttons: [...group.buttons, ...buttons]\n }\n } else {\n return group\n }\n })\n\n this.insertToCurrentGroup({ buttons, type })\n } else {\n this.buttonGroupsValue = [...this.buttonGroupsValue, { buttons, type }]\n this.insertNewGroup({ buttons, type })\n }\n })\n\n if(this.isPreviewable) {\n this.previewButtons()\n }\n\n this.show(this.buttonsContainerTarget)\n }\n\n updateButtons({ detail: buttonGroups }) {\n this.buttonGroupsValue = buttonGroups\n this.groupTargets.forEach(group => group.remove())\n\n buttonGroups.forEach(({ buttons, type }) => {\n this.insertNewGroup({ buttons, type })\n })\n\n if(this.isPreviewable) {\n this.previewButtons()\n }\n }\n\n loadTemplateButtons({ detail }) {\n this.buttonsContainerTarget.innerHTML = ''\n this.buttonGroupsValue = detail.buttons\n\n this.buttonGroupsValue.forEach(({ buttons, type }) => {\n this.insertNewGroup({ buttons, type })\n })\n\n this.insertInputs()\n this.previewButtons()\n }\n\n restoreButtons({ detail: oldGroup }) {\n this.buttonGroupsValue = oldGroup\n\n this.groupTargets.forEach(group => group.remove())\n\n this.buttonGroupsValue.forEach(({ buttons, type }) => {\n this.insertNewGroup({ buttons, type })\n })\n\n this.insertInputs()\n this.previewButtons()\n\n this.show(this.buttonsContainerTarget)\n }\n\n insertNewGroup({ buttons, type, element }) {\n const buttonGroupElement = this.buttonGroupTemplateTarget.cloneNode(true)\n\n buttonGroupElement.setAttribute('data-compose--template-target', 'group')\n buttonGroupElement.setAttribute('data-type', type)\n\n const contentElement = buttonGroupElement.querySelector('[data-content]')\n\n this.insertButtonIntoGroup(buttons, type, contentElement, 0)\n\n this.show(buttonGroupElement)\n this.show(this.buttonsContainerTarget)\n\n this.insertInputs()\n this.buttonsContainerTarget.appendChild(buttonGroupElement)\n }\n\n insertToCurrentGroup({ buttons, type }) {\n const groupElement = this.groupTargets.find(group => group.dataset.type === type)\n const contentElement = groupElement.querySelector('[data-content]')\n\n let index = contentElement.children.length\n\n this.insertButtonIntoGroup(buttons, type, contentElement, index)\n this.insertInputs()\n }\n\n insertButtonIntoGroup(buttons, type, contentElement, index) {\n buttons.forEach((button) => {\n const buttonElement = this.buttonTemplateTargets.find(template => template.dataset.type === type).cloneNode(true)\n\n buttonElement.querySelector('[data-text]').innerText = button.title\n\n buttonElement.setAttribute('data-type', type)\n buttonElement.setAttribute('data-index', index)\n\n buttonElement.removeAttribute('data-compose--template-target')\n\n this.show(buttonElement)\n contentElement.appendChild(buttonElement)\n\n index++\n })\n }\n\n focusGroup({ currentTarget }) {\n this.dispatch('edit', {\n target: this.buttonManagerTarget,\n detail: {\n groups: this.buttonGroupsValue,\n type: currentTarget.dataset.type,\n index: currentTarget.dataset.index,\n }\n })\n }\n\n disable() {\n this.buttonsContainerTarget.classList.add('grayscale', 'cursor-not-allowed', 'pointer-events-none')\n this.buttonsContainerTarget.querySelectorAll('input').forEach(super.disable)\n }\n\n enable() {\n this.buttonsContainerTarget.classList.remove('grayscale', 'cursor-not-allowed', 'pointer-events-none')\n this.buttonsContainerTarget.querySelectorAll('input').forEach(super.enable)\n }\n\n clear() {\n this.buttonsContainerTarget.innerHTML = ''\n this.buttonGroupsValue = []\n\n this.hide(this.buttonsContainerTarget)\n }\n\n displayAttachmentPreview({ detail: attachmentElement }) {\n if(!this.isPreviewable) return\n\n this.attachmentElement = attachmentElement\n\n if(document.querySelector('[data-controller=\"template--preview\"]')) {\n this.dispatch('attachment:preview', {\n target: document.querySelector('[data-controller=\"template--preview\"]'),\n detail: attachmentElement\n })\n }\n }\n\n hideAttachmentPreview() {\n if(!this.isPreviewable) return\n\n if(document.querySelector('[data-controller=\"template--preview\"]')) {\n this.dispatch('attachment:preview', {\n target: document.querySelector('[data-controller=\"template--preview\"]'),\n detail: 0\n })\n }\n }\n\n displayBodyPreview({ detail }) {\n if(!this.isPreviewable) return\n\n this.dispatch('body:preview', {\n target: document.querySelector('[data-controller=\"template--preview\"]'),\n detail\n })\n }\n\n displayFooterPreview({ target }) {\n if(!this.isPreviewable) return\n\n this.dispatch('footer:preview', {\n target: document.querySelector('[data-controller=\"template--preview\"]'),\n detail: target.value\n })\n }\n\n previewButtons() {\n if(this.hasComposeTarget) {\n this.dispatch('buttons:preview', {\n target: this.composeTarget,\n detail: this.buttonGroupsValue\n })\n } else {\n this.dispatch('buttons:preview', {\n target: document.querySelector('[data-controller=\"template--preview\"]'),\n detail: this.buttonGroupsValue\n })\n }\n }\n\n insertInputs() {\n this.element.querySelector('[data-inputs-container]').innerHTML = ''\n\n let index = 1\n\n this.buttonGroupsValue.forEach(({ buttons, type }) => {\n buttons.forEach((button) => {\n const methodName = type.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join('')\n this[`insert${methodName}Inputs`](button, index++)\n })\n })\n }\n\n insertQuickReplyInputs(button, index) {\n ['title', 'value'].forEach(attribute => {\n const name = `${this.inputNameValue}[buttons][][${attribute}]`\n const value = attribute === 'title' ? button.text || button.title : button[attribute]\n\n this.insertInput(name, value)\n })\n\n const type = `${this.inputNameValue}[buttons][][type]`\n this.insertInput(type, 'quick_reply')\n }\n\n insertCopyCodeInputs(button, index) {\n ['text', 'type'].forEach(attribute => {\n const name = `${this.inputNameValue}[buttons][][${attribute}]`\n const value = button[attribute]\n\n this.insertInput(name, value)\n })\n }\n\n insertUrlInputs(button, index) {\n ['title', 'value'].forEach(attribute => {\n const name = `${this.inputNameValue}[buttons][][${attribute}]`\n const value = attribute === 'title' ? button.text || button.title : button[attribute]\n\n this.insertInput(name, value)\n })\n\n const type = `${this.inputNameValue}[buttons][][type]`\n this.insertInput(type, 'url')\n }\n\n insertPhoneInputs(button, index) {\n ['title', 'value'].forEach(attribute => {\n const name = `${this.inputNameValue}[buttons][][${attribute}]`\n const value = attribute === 'title' ? button.text || button.title : button[attribute]\n\n this.insertInput(name, value)\n })\n\n const type = `${this.inputNameValue}[buttons][][type]`\n this.insertInput(type, 'phone')\n }\n\n insertInput(name, value) {\n const input = document.createElement('input')\n\n input.setAttribute('type', 'hidden')\n input.setAttribute('name', name)\n\n input.value = value\n\n this.element.querySelector('[data-inputs-container]').appendChild(input)\n }\n\n get isPreviewable() {\n return this.previewableValue || document.querySelector('[data-controller=\"template--preview\"]')\n }\n}\n"],"names":["template_controller","ApplicationController","buttonGroups","buttons","type","group","detail","oldGroup","element","buttonGroupElement","contentElement","index","button","buttonElement","template","currentTarget","attachmentElement","target","methodName","word","attribute","name","value","input","__publicField"],"mappings":"uNAEe,MAAKA,UAASC,CAAsB,CAgBjD,cAAc,CAAE,OAAQC,GAAgB,CACtCA,EAAa,QAAQ,CAAC,CAAE,QAAAC,EAAS,KAAAC,CAAI,IAAM,CACtC,KAAK,kBAAkB,KAAKC,GAASA,EAAM,OAASD,CAAI,GACzD,KAAK,kBAAoB,KAAK,kBAAkB,IAAIC,GAC/CA,EAAM,OAASD,EACT,CACL,KAAAA,EACA,QAAS,CAAC,GAAGC,EAAM,QAAS,GAAGF,CAAO,CACpD,EAEmBE,CAEV,EAED,KAAK,qBAAqB,CAAE,QAAAF,EAAS,KAAAC,CAAM,CAAA,IAE3C,KAAK,kBAAoB,CAAC,GAAG,KAAK,kBAAmB,CAAE,QAAAD,EAAS,KAAAC,CAAM,CAAA,EACtE,KAAK,eAAe,CAAE,QAAAD,EAAS,KAAAC,CAAM,CAAA,EAExC,CAAA,EAEE,KAAK,eACN,KAAK,eAAc,EAGrB,KAAK,KAAK,KAAK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