{"version":3,"file":"lazy-partial_controller-JEXouxmB.js","sources":["../../../app/frontend/javascript/controllers/main/lazy-partial_controller.js"],"sourcesContent":["import { Controller } from '@hotwired/stimulus';\nimport CurrentUser from '@frontend/app/features/CurrentUser';\nimport { get } from '@rails/request.js';\nimport { runWhenDomLoaded } from '@frontend/utils/dom';\n\n/*\n * Useful to asynchronously load dynamic content.\n * Use cases:\n * - loading dynamic parts on a cached page, e.g. the article page\n * - load paginated content, e.g. article teasers.\n *\n * Build to be attached to placeholder tags (e.g. a `
`) or ``-tags.\n *\n * == Examples\n *\n *
// The div will be replaced with the dynamically loaded content\n *
Load more\n *\n * */\nexport default class extends Controller {\n static values = {\n path: String,\n skipLoading: Boolean,\n requiredRoles: Array,\n };\n static targets = ['container'];\n\n connect() {\n this.element.addEventListener(\n 'lazy-partial:reload',\n this.reload.bind(this)\n );\n const userRoles = CurrentUser.roles();\n const requiresRole = this.requiredRolesValue.length > 0;\n const userHasRequiredRole =\n this.requiredRolesValue.filter((role) => userRoles.includes(role))\n .length > 0;\n const skipLoading =\n this.skipLoadingValue || (requiresRole && !userHasRequiredRole);\n\n if (false === skipLoading) {\n runWhenDomLoaded(this.load.bind(this, true));\n }\n\n if (this.requiredRolesValue.length > 0 && false === userHasRequiredRole) {\n this.element.remove();\n }\n }\n\n reload(event) {\n event.preventDefault();\n\n // debugger;\n let path = undefined;\n if (event.params) {\n path = event.params.path;\n }\n const { lazyPartialPath } = event.currentTarget.dataset;\n if (lazyPartialPath || path) {\n this.pathValue = lazyPartialPath || path;\n }\n\n this.load(false);\n }\n\n scrollToAnchor(parentElement) {\n const targetElementID = window.location.hash;\n const targetElement =\n targetElementID.length > 0\n ? parentElement.querySelector(targetElementID)\n : false;\n\n if (targetElement) {\n targetElement.scrollIntoView();\n }\n }\n\n async load(scrollToAnchor = false) {\n const response = await get(this.pathValue);\n if (response.ok) {\n const html = await response.text;\n const parentElement = this.hasContainerTarget\n ? this.containerTarget\n : this.element;\n\n if (this.hasContainerTarget) {\n this.containerTarget.innerHTML = html;\n } else {\n this.element.outerHTML = html;\n }\n\n // If the current url references a valid anchor, we attemp to scroll to it. Useful to deep-link to parts\n // of dynamically loaded content, e.g. a comment in the comment system.\n if (scrollToAnchor) {\n this.scrollToAnchor(parentElement);\n }\n }\n }\n}\n\nexport function reload(targetEl) {\n targetEl.dispatchEvent(\n new CustomEvent('lazy-partial:reload', { bubbles: true })\n );\n}\n"],"names":["LazyPartialController","Controller","userRoles","CurrentUser","requiresRole","userHasRequiredRole","role","runWhenDomLoaded","event","path","lazyPartialPath","parentElement","targetElementID","targetElement","scrollToAnchor","response","get","html","__publicField","reload","targetEl"],"mappings":"+UAmBe,MAAKA,UAASC,CAAW,CAQtC,SAAU,CACR,KAAK,QAAQ,iBACX,sBACA,KAAK,OAAO,KAAK,IAAI,CAC3B,EACI,MAAMC,EAAYC,EAAY,QACxBC,EAAe,KAAK,mBAAmB,OAAS,EAChDC,EACJ,KAAK,mBAAmB,OAAQC,GAASJ,EAAU,SAASI,CAAI,CAAC,EAC9D,OAAS,GAEZ,KAAK,kBAAqBF,GAAgB,CAACC,KAEzC,IACFE,EAAiB,KAAK,KAAK,KAAK,KAAM,EAAI,CAAC,EAGzC,KAAK,mBAAmB,OAAS,GAAeF,IAAV,IACxC,KAAK,QAAQ,QAEhB,CAED,OAAOG,EAAO,CACZA,EAAM,eAAc,EAGpB,IAAIC,EACAD,EAAM,SACRC,EAAOD,EAAM,OAAO,MAEtB,KAAM,CAAE,gBAAAE,CAAiB,EAAGF,EAAM,cAAc,SAC5CE,GAAmBD,KACrB,KAAK,UAAYC,GAAmBD,GAGtC,KAAK,KAAK,EAAK,CAChB,CAED,eAAeE,EAAe,CAC5B,MAAMC,EAAkB,OAAO,SAAS,KAClCC,EACJD,EAAgB,OAAS,EACrBD,EAAc,cAAcC,CAAe,EAC3C,GAEFC,GACFA,EAAc,eAAc,CAE/B,CAED,MAAM,KAAKC,EAAiB,GAAO,CACjC,MAAMC,EAAW,MAAMC,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