{"version":3,"file":"condition_controller-BBsaXH8q.js","sources":["../../../app/javascript/controllers/step/condition_controller.js"],"sourcesContent":["import LineController from \"../journeys/line_controller\"\nimport { get, patch } from \"@rails/request.js\"\nimport { StraightConnector } from \"@jsplumb/browser-ui\"\n\nexport default class extends LineController {\n static values = { url: String, replaceUrl: String }\n static targets = [\n \"root\",\n \"else\",\n \"end\",\n \"elseContainer\",\n \"stepsContainer\",\n \"conditionsSelect\",\n \"branch\",\n \"wait\",\n \"stepName\"\n ]\n\n disableWait() {\n this.waitTarget.classList.add('text-night-40')\n }\n\n initialize() {\n this.kind = \"condition\"\n super.initialize()\n }\n\n connect() {\n this.cloneElseDropzone()\n this.drawRootToElseBranch()\n this.drawElseBranchToEndLine()\n\n this.drawEndLine()\n\n if (!this.persistedValue) {\n setTimeout(() => {\n this.togglePane({})\n }, 0)\n }\n super.connect()\n }\n\n disconnect() {\n this.instance.deleteConnection(this.elseToEndLine)\n this.instance.deleteConnection(this.endLine)\n this.instance.deleteConnection(this.rootToElseLine)\n\n super.disconnect()\n }\n\n invalidate({ dataset }) {\n if (dataset.target !== this.element.id) return\n\n this.element.setAttribute(\"data-invalid\", \"true\")\n this.invalid = true\n this.triggerTarget.classList.remove(\"halo--active\")\n this.triggerTarget.classList.add(\"halo--error\")\n }\n\n repositionLines() {\n this.instance.repaintEverything()\n }\n\n onWindowSizeChange() {\n this.instance.repaintEverything()\n }\n\n redraw({ detail }) {\n setTimeout(() => this.instance.repaintEverything(), 0)\n\n if (detail.target !== this.element.id) return\n\n if (detail.child) {\n this.childValue = detail.child\n }\n\n if (detail.branch.includes(\"else\")) {\n this.instance.deleteConnection(this.elseToEndLine)\n this.instance.deleteConnection(this.rootToElseLine)\n this.drawRootToElseBranch(document.getElementById(detail.replaceWith))\n this.elseTarget.classList.add(\"hidden\")\n } else if (detail.branch.includes(\"branchless\")) {\n this.instance.deleteConnection(this.endLine)\n this.childValue = detail.replaceWith\n this.drawEndLine()\n }\n\n this.instance.repaintEverything()\n setTimeout(() => this.instance.repaintEverything(), 0)\n }\n\n redrawEndLineIfNeeded({ detail }) {\n setTimeout(() => this.instance.repaintEverything(), 0)\n\n if (detail.target === this.element.id) {\n switch (detail.branch) {\n case \"else\":\n this.allowDroppingToElseBranch()\n break\n default:\n this.childValue = detail.child\n }\n }\n\n if (detail.child === this.element.id) {\n this.parentTarget.value = detail.parent\n this.branchInputTarget.value = detail.branch\n }\n\n if (detail.parent === this.element.id) {\n this.childValue = detail.child\n if (\n this.element.contains(document.getElementById(detail.child)) === false\n ) {\n this.instance.deleteConnection(this.endLine)\n this.drawEndLine()\n }\n }\n }\n\n sync({ detail }) {\n if (detail.stepId !== this.element.id) return\n\n this.triggerTarget.disabled = false\n this.triggerTarget.classList.remove(\"halo--active\")\n\n this.persistedValue = detail.commit\n this.invalid = false\n\n this.stepNameTarget.textContent = detail.stepName || t.journeys.new.condition\n\n this.triggerTarget.classList.remove(\"halo--error\")\n this.element.removeAttribute(\"data-invalid\")\n\n this.dispatch(\"became-valid\", {\n target: document.documentElement,\n })\n\n this.branchTargets.forEach((branch) => {\n this.dispatch(\"branch:became-valid\", {\n target: branch,\n })\n })\n\n patch(this.replaceUrlValue, {\n responseKind: \"turbo-stream\",\n body: new FormData(this.element.closest(\"form\")),\n })\n\n this.branchesInsidePane.forEach((branchInsidePane) => {\n const eventName = branchInsidePane.classList.contains(\"hidden\") ? \"remove\" : \"persist\"\n this.dispatch(eventName, {\n target: branchInsidePane\n })\n })\n }\n\n remove({ detail }) {\n if (detail !== this.element.id) return\n\n this.dropzone.remove()\n this.element.remove()\n\n if (this.hasRemovedInputTarget && document.getElementById('drawing_container')) {\n this.removedInputTarget.disabled = false\n document.getElementById(\"drawing_container\").appendChild(this.removedInputTarget)\n }\n }\n\n abortChanges({ detail }) {\n if (detail !== this.element.id) return\n this.triggerTarget.classList.remove(\"halo--active\")\n if (!this.persistedValue) {\n this.element.remove()\n this.dropzone.remove()\n return\n }\n\n this.branchesInsidePane.forEach((branchInsidePane) => {\n this.dispatch(\"show\", {\n target: branchInsidePane\n })\n })\n }\n\n childValueChanged() {\n this.dropzone.setAttribute(\"data-child\", this.childValue)\n }\n\n // private\n\n allowDroppingToElseBranch() {\n this.instance.deleteConnection(this.elseToEndLine)\n this.instance.deleteConnection(this.rootToElseLine)\n\n if (this.elseBranchEmpty) {\n this.elseContainerTarget.appendChild(this.elseBranchDropzone)\n this.elseTarget.classList.remove(\"hidden\")\n this.drawElseBranchToEndLine()\n }\n\n this.drawRootToElseBranch()\n\n setTimeout(() => this.instance.repaintEverything(), 0)\n }\n\n drawRootToElseBranch(end = this.elseContainerTarget) {\n this.rootToElseLine = this.draw(this.rootTarget, end, {\n fromOptions: this.blankEndpoint(\"Right\"),\n toOptions: this.blankEndpoint(\"Top\"),\n })\n }\n\n drawElseBranchToEndLine() {\n this.elseToEndLine = this.draw(this.elseContainerTarget, this.endTarget, {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(\"Right\"),\n })\n }\n\n cloneElseDropzone() {\n this.elseBranchDropzone =\n this.elseContainerTarget.firstElementChild.nextElementSibling.cloneNode(\n true\n )\n\n this.elseBranchDropzone.classList.remove(\"hidden\")\n }\n\n drawEndLine() {\n if (this.isLastStep) {\n if(this.element.closest('[data-kind=\"question\"]')) {\n const ancestor = this.getFirstAncestorOfType(['[data-kind=\"question\"]', '[data-kind=\"condition\"]'])\n\n if(ancestor.dataset.kind === \"question\") {\n this.drawEndToQuestion(ancestor)\n } else if(ancestor.dataset.kind === \"condition\") {\n this.drawEndToCondition(ancestor)\n } else {\n this.drawToEndElement()\n }\n }\n\n return\n }\n\n if (this.childValue) {\n return this.endLine = this.draw(\n this.endTarget,\n document.getElementById(this.childValue),\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(\"Top\"),\n lineOptions: StraightConnector.type,\n }\n )\n }\n\n if(this.element.closest('[data-kind=\"question\"]')) {\n const ancestor = this.getFirstAncestorOfType(['[data-kind=\"question\"]', '[data-kind=\"condition\"]'])\n\n if(ancestor.dataset.kind === \"question\") {\n this.drawEndToQuestion(ancestor)\n } else {\n this.drawEndToCondition(ancestor)\n }\n\n return\n }\n\n if (this.endIdValue && !this.childValue) {\n this.endLine = this.draw(\n this.endTarget,\n document.getElementById(this.endIdValue),\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(),\n lineOptions: this.flowChartLineOptions,\n }\n )\n } else if (this.childValue) {\n this.endLine = this.draw(\n this.endTarget,\n document.getElementById(this.childValue),\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(\"Top\"),\n lineOptions: StraightConnector.type,\n }\n )\n } else {\n this.endLine = this.draw(\n this.endTarget,\n document.getElementById(\"end-connector\"),\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(\"Center\"),\n lineOptions: StraightConnector.type,\n }\n )\n }\n }\n\n drawEndToQuestion(ancestor) {\n const controller = this.application.getControllerForElementAndIdentifier(ancestor, 'automation--question')\n\n this.endLine = this.draw(\n this.endTarget,\n controller.endConnectorTarget,\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(),\n lineOptions: this.flowChartLineOptions,\n }\n )\n }\n\n drawEndToCondition(conditionElement) {\n console.log(conditionElement)\n const controller = this.application.getControllerForElementAndIdentifier(conditionElement, 'step--condition')\n\n this.endLine = this.draw(\n this.endTarget,\n controller.endTarget,\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(),\n }\n )\n }\n\n drawToEndElement() {\n if (this.isNestedInElseBranch) {\n this.endLine = this.draw(\n this.endTarget,\n document.getElementById(this.childValue || this.endIdValue),\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(),\n }\n )\n } else {\n this.endLine = this.draw(\n this.endTarget,\n document.getElementById(this.childValue || \"end-connector\"),\n {\n fromOptions: this.blankEndpoint(\"Bottom\"),\n toOptions: this.blankEndpoint(),\n lineOptions: StraightConnector.type,\n }\n )\n }\n }\n\n get isNestedInElseBranch() {\n return this.element.closest(\n \"div [data-step--condition-target='elseContainer']\"\n )\n }\n\n get elseBranchEmpty() {\n return this.elseContainerTarget.querySelectorAll(\"[data-step]\").length === 0\n }\n\n get dropzone() {\n return document.getElementById(`${this.element.id}_dropzone`)\n }\n\n get flowChartLineOptions() {\n const originalOptions = this.flowchartConnectorLineOptions\n\n return {\n ...originalOptions,\n options: {\n ...originalOptions.options,\n cornerRadius:\n this.inBranch && !this.insideEdgeBranch\n ? 0\n : originalOptions.options.cornerRadius,\n },\n }\n }\n\n get branchesInsidePane() {\n return this.element.querySelectorAll('[data-controller*=\"step--condition--pane--branch\"]')\n }\n}\n"],"names":["condition_controller","LineController","dataset","detail","branch","patch","branchInsidePane","eventName","end","ancestor","StraightConnector","controller","conditionElement","originalOptions","__publicField"],"mappings":"qQAIe,MAAKA,UAASC,CAAe,CAc1C,aAAc,CACZ,KAAK,WAAW,UAAU,IAAI,eAAe,CACjD,CAEE,YAAa,CACX,KAAK,KAAO,YACZ,MAAM,WAAU,CACpB,CAEE,SAAU,CACR,KAAK,kBAAiB,EACtB,KAAK,qBAAoB,EACzB,KAAK,wBAAuB,EAE5B,KAAK,YAAW,EAEX,KAAK,gBACR,WAAW,IAAM,CACf,KAAK,WAAW,CAAE,CAAA,CAC1B,EAAS,CAAC,EAEN,MAAM,QAAO,CACjB,CAEE,YAAa,CACX,KAAK,SAAS,iBAAiB,KAAK,aAAa,EACjD,KAAK,SAAS,iBAAiB,KAAK,OAAO,EAC3C,KAAK,SAAS,iBAAiB,KAAK,cAAc,EAElD,MAAM,WAAU,CACpB,CAEE,WAAW,CAAE,QAAAC,GAAW,CAClBA,EAAQ,SAAW,KAAK,QAAQ,KAEpC,KAAK,QAAQ,aAAa,eAAgB,MAAM,EAChD,KAAK,QAAU,GACf,KAAK,cAAc,UAAU,OAAO,cAAc,EAClD,K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