{"version":3,"file":"jump_pane_controller-Cuhm3mOR.js","sources":["../../../app/javascript/controllers/automation/jump_pane_controller.js"],"sourcesContent":["import { get } from '@rails/request.js'\n\nimport ApplicationController from \"@/controllers/application_controller\"\n\nexport default class extends ApplicationController {\n static values = {\n journey: Object,\n stepSearchUrl: String,\n stepId: String,\n stepName: String,\n sourceName: String,\n }\n\n static targets = [\n 'stepPopover',\n 'stepTrigger',\n 'step',\n 'stepTemplate',\n 'submit',\n 'openModal',\n 'removeButton',\n 'destinationIdInput',\n 'input',\n 'stepPopoverMenu',\n 'captionStepTemplate',\n 'stepNameInput',\n 'content',\n 'notice',\n ]\n\n initialize() {\n this.stepId = this.stepIdValue\n this.stepName = this.stepNameValue\n\n // Use a more structured data representation\n this.ancestry = {}\n this.stepHierarchy = {} // Will hold parent-child relationships\n\n super.initialize()\n }\n\n connect() {\n this.buildAncestryTree(document.getElementById('journeys--editor'), [], 0)\n this.stepTargets.find(item => item.dataset.id.split('_')[1] === this.stepIdValue)?.classList?.add('bg-lavender-light')\n\n super.connect()\n }\n\n buildAncestryTree(element, ancestors = [], indentLevel = 0) {\n if (!element) return\n\n let children = Array.from(document.querySelectorAll(`[data-parent=\"${element.id}\"]`))\n\n // Handle special case for condition elements\n if (element.dataset.kind === 'condition') {\n children = this.prepareConditionChildren(element, children)\n }\n\n // Calculate indent level for children based on element type\n const childIndentLevel = this.calculateChildIndent(element, indentLevel);\n\n // Keep track of ancestry for children\n const currentAncestors = [...ancestors, {\n id: element.id,\n kind: element.dataset.kind\n }]\n\n children.forEach(child => {\n if (!child) return\n\n if (child.dataset.kind === 'else') {\n return this.appendElseStep(childIndentLevel)\n }\n\n if (child.dataset.kind === 'answer' || child.dataset.kind === 'branch') {\n this.appendCaptionStep(child, childIndentLevel)\n this.buildAncestryTree(child, currentAncestors, childIndentLevel)\n return\n }\n\n this.appendRegularStep(child, childIndentLevel)\n this.buildAncestryTree(child, currentAncestors, childIndentLevel)\n });\n }\n\n prepareConditionChildren(element, originalChildren) {\n const branchChildren = originalChildren.filter(child => child.dataset.kind === 'branch')\n const branchlessChild = originalChildren.find(child => child.dataset.branch === 'branchless')\n\n return [\n ...branchChildren,\n {\n dataset: {\n id: `${element.id}_else`,\n kind: 'else',\n }\n },\n element.querySelector('[data-branch=\"else\"]'),\n branchlessChild\n ];\n }\n\n calculateChildIndent(element, currentIndent) {\n if (element.dataset.kind === 'condition' || element.dataset.kind === 'question') {\n return currentIndent + 2; // Add 2em of indentation per level\n }\n return currentIndent\n }\n\n createIndentElement(indentLevel) {\n if (indentLevel <= 0) return null\n\n const indentElement = document.createElement('div')\n\n indentElement.classList.add('flex-none')\n indentElement.setAttribute('data-indent', '')\n indentElement.style.width = `${indentLevel}em`\n\n return indentElement\n }\n\n appendElseStep(indentLevel) {\n const step = this.captionStepTemplateTarget.cloneNode(true)\n\n step.removeAttribute('data-automation--jump-pane-target')\n step.querySelector('[data-name]').innerText = t.informational.else\n\n step.classList.remove('hidden')\n\n const indentElement = this.createIndentElement(indentLevel);\n if (indentElement) {\n step.insertBefore(indentElement, step.firstChild);\n }\n\n this.stepPopoverMenuTarget.appendChild(step);\n }\n\n appendCaptionStep(child, indentLevel) {\n const step = this.captionStepTemplateTarget.cloneNode(true)\n\n step.removeAttribute('data-automation--jump-pane-target')\n step.querySelector('[data-name]').innerText = child.querySelector('[data-description]').innerText\n\n step.classList.remove('hidden')\n\n const indentElement = this.createIndentElement(indentLevel)\n if (indentElement) {\n step.insertBefore(indentElement, step.firstChild)\n }\n\n this.contentTarget.appendChild(step)\n }\n\n appendRegularStep(child, indentLevel) {\n const pane = document.getElementById(`${child.id}_pane`)\n const step = this.stepTemplateTarget.cloneNode(true)\n\n const avatar = pane?.querySelector('[data-avatar]') || child.querySelector('[data-avatar]')\n\n step.removeAttribute('data-automation--jump-pane-target')\n\n step.querySelector('[data-avatar]').innerHTML = ''\n step.querySelector('[data-avatar]').appendChild(avatar.cloneNode(true))\n\n const name = pane\n ? pane.querySelector('[data-step--name-target]')?.innerText\n : child.querySelector('[data-description]').innerText;\n\n step.querySelector('[data-name]').innerText = name;\n step.classList.remove('hidden');\n\n step.setAttribute('data-id', child.id)\n step.setAttribute('data-name', name)\n step.setAttribute('data-automation--jump-pane-target', 'step')\n\n const indentElement = this.createIndentElement(indentLevel)\n if (indentElement) {\n step.insertBefore(indentElement, step.querySelector('[data-avatar]'))\n }\n\n this.contentTarget.appendChild(step)\n }\n\n getIndentLevel(elementId) {\n if (!this.ancestry[elementId]) return 0;\n\n // Count only condition and question ancestors, like in the server-side code\n return this.ancestry[elementId].filter(ancestor =>\n ['Steps::Condition', 'Steps::Question'].includes(ancestor.type)\n ).length * 2\n }\n\n setDestination({ currentTarget }) {\n this.stepId = currentTarget.dataset.id\n this.stepName = currentTarget.dataset.name\n\n this.stepTargets.forEach((item) => {\n if(item === currentTarget) {\n item.classList.add('bg-lavender-light')\n } else {\n item.classList.remove('bg-lavender-light')\n }\n })\n\n const item = currentTarget.cloneNode(true)\n item.querySelector('[data-indent]')?.remove()\n\n this.stepTriggerTarget.innerHTML = item.innerHTML\n this.enable(this.submitTarget)\n\n this.dispatch('hide', {\n target: this.stepPopoverTarget,\n })\n\n if(this.hasNoticeTarget) {\n this.hide(this.noticeTarget)\n }\n\n this.stepTriggerTarget.classList.add('font-medium')\n }\n\n save() {\n this.stepIdValue = this.stepId\n this.stepNameValue = this.stepName\n this.sourceNameValue = this.stepNameInputTarget.value.trim()\n\n this.destinationIdInputTarget.value = this.stepIdValue\n\n this.dispatch('save', {\n target: this.elementStep,\n detail: {\n id: this.stepIdValue,\n name: this.stepNameValue,\n sourceName: this.sourceNameValue,\n }\n })\n\n this.show(this.removeButtonTarget)\n }\n\n onClickOutside({ target }) {\n if(this.isPaneInvisible || this.element.contains(target)) return\n\n if(this.hasUnsavedChanges) {\n return this.openModalTarget.click()\n }\n\n this.abortChanges()\n }\n\n abortChanges() {\n this.dispatch('abort', {\n target: this.elementStep,\n })\n }\n\n remove() {\n this.dispatch('remove', {\n target: this.elementStep,\n })\n }\n\n get elementStep() {\n return document.getElementById(this.element.id.replace('_pane', ''))\n }\n\n get isPaneVisible() {\n return this.element.classList.contains(\"right-0\")\n }\n\n get isPaneInvisible() {\n return this.element.classList.contains(\"-right-full\")\n }\n\n get hasUnsavedChanges() {\n return this.stepIdValue !== this.stepId\n || this.stepNameValue !== this.stepName\n || this.sourceNameValue !== this.stepNameInputTarget.value.trim()\n }\n}\n"],"names":["jump_pane_controller","ApplicationController","_b","_a","item","element","ancestors","indentLevel","children","childIndentLevel","currentAncestors","child","originalChildren","branchChildren","branchlessChild","currentIndent","indentElement","step","pane","avatar","name","elementId","ancestor","currentTarget","target","__publicField"],"mappings":"uNAIe,MAAKA,UAASC,CAAsB,CA0BjD,YAAa,CACX,KAAK,OAAS,KAAK,YACnB,KAAK,SAAW,KAAK,cAGrB,KAAK,SAAW,CAAA,EAChB,KAAK,cAAgB,CAAE,EAEvB,MAAM,WAAU,CACpB,CAEE,SAAU,SACR,KAAK,kBAAkB,SAAS,eAAe,kBAAkB,EAAG,CAAA,EAAI,CAAC,GACzEC,GAAAC,EAAA,KAAK,YAAY,KAAKC,GAAQA,EAAK,QAAQ,GAAG,MAAM,GAAG,EAAE,CAAC,IAAM,KAAK,WAAW,IAAhF,YAAAD,EAAmF,YAAnF,MAAAD,EAA8F,IAAI,qBAElG,MAAM,QAAO,CACjB,CAEE,kBAAkBG,EAASC,EAAY,CAAA,EAAIC,EAAc,EAAG,CAC1D,GAAI,CAACF,EAAS,OAEd,IAAIG,EAAW,MAAM,KAAK,SAAS,iBAAiB,iBAAiBH,EAAQ,EAAE,IAAI,CAAC,EAGhFA,EAAQ,QAAQ,OAAS,cAC3BG,EAAW,KAAK,yBAAyBH,EAASG,CAAQ,GAI5D,MAAMC,EAAmB,KAAK,qBAAqBJ,EAASE,CAAW,EAGjEG,EAAmB,CAAC,GAAGJ,EAAW,CACtC,GAAID,EAAQ,GACZ,KAAMA,EAAQ,QAAQ,IACvB,CAAA,EAEDG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