{"version":3,"file":"loader-GsOtWJeL.js","sources":["../../../app/frontend/javascript/components/errors.js","../../../app/frontend/javascript/components/loader.js"],"sourcesContent":["export default function reportError (error) {\n if(typeof(Rollbar) != 'undefined') {\n Rollbar.error(error.message)\n }\n console.error(error, error.stack)\n}\n","// The purpose of this object is to run all the required javascripts against given DOM content, that\n// would be consistent accross the application. It registeres two types of hooks, functional - that\n// are to be executed once per content (e.g create selectize elements, initialize other libraries)\n// and visuals that are to be executed every time new content is displayed (e.g. resize elements\n// according to other element sizes).\n//\n// To register new hook, one must call Loader.functional(fn(content, resolve, reject)) or similar\n// Loader.visual function. fn is to perform some action that calls resolve or reject when completed\n// (similarly to regular promises).\n//\n// When adding dynamic content to the page, one need to run loader against it. The main goal is to\n// ensure that all javascript has run before the content is displayed.\n//\n// new Loader(myNewContent).all().then -> myNewContent.display()\n//\n// Note that visual hooks might require some extra setup in which it might be usefull to run those\n// hooks separatly, e.g equalizer only works with element that are visible to browser (no parent\n// being display: none):\n//\n// loader = new Loader(myNewContent)\n// loader.functional()\n// .then (content) ->\n// content.style.setProperty('height', 0)\n// content.style.setProperty('display', 'block')\n// loader.visual()\n// .then (content) ->\n// slideDown(content)\n\nimport reportError from \"@/javascript/components/errors\"\n\nexport default class Loader {\n static functional (fn) {\n this.hooks.functional.push(fn)\n }\n\n static visual (fn) {\n this.hooks.visual.push(fn)\n }\n\n static promises = []\n\n static hooks = {\n functional: [],\n visual: []\n }\n\n constructor (content, loadingClass = 'loading') {\n this.content = content\n this.loadingClass = loadingClass\n }\n\n getHooks (modes) {\n const result = []\n Object.entries(this.constructor.hooks).forEach((entry) => {\n const [key, hooks] = entry\n if(modes.includes(key)) {\n result.push(...hooks)\n }\n })\n return result\n }\n\n start (opts = {}) {\n this.running = true\n if(this.loadingClass) {\n this.content.classList.add(this.loadingClass)\n }\n const hooks = this.getHooks(opts.modes)\n\n return Promise.all(\n hooks.map((hook) => {\n return ((hook) => {\n return Promise.race([\n new Promise((resolve, reject) => hook(this.content, resolve, reject)),\n new Promise((resolve) => {\n const func = () => {\n if(this.running) {\n console.error('Loader timed out', hook)\n }\n resolve()\n }\n return setTimeout(func, opts.timeout || 2000)\n })\n ])\n })(hook)\n })\n )\n .catch((e) => reportError(e))\n .then(() => this.running = false)\n .then(() => {\n if(this.loadingClass) {\n this.content.classList.remove(this.loadingClass)\n }\n })\n .then(() => this.content)\n }\n\n all () {\n return this.start({modes: ['functional', 'visual']})\n }\n\n functional () {\n return this.start({modes: ['functional']})\n }\n visual () {\n return this.start({modes: ['visual']})\n }\n}\n\ndocument.addEventListener(\"turbo:before-stream-render\", ((event) => {\n const fallbackToDefaultActions = event.detail.render\n\n event.detail.render = async function (streamElement) {\n fallbackToDefaultActions(streamElement)\n const target = document.getElementById(streamElement.target)\n if(target) {\n await new Loader(target).all()\n }\n }\n}))\n\n/* This is split into two methods because there are loading hooks that access\n * document, which means the new content needs to be present in the DOM.\n *\n * If this is changed then this code can be done in a single event listener as:\n *\n * document.addEventListener(\"turbo:before-frame-render\", ((event) => {\n * event.preventDefault();\n *\n * new Loader(event.detail.newFrame).all()\n * .then(() => {\n * event.detail.resume();\n * });\n * }))\n */\ndocument.addEventListener(\"turbo:before-frame-render\", ((event) => {\n event.detail.newFrame.childNodes.forEach(function(child) {\n child.classList.add('loading')\n })\n}))\n\ndocument.addEventListener(\"turbo:frame-render\", ((event) => {\n event.target.childNodes.forEach(function(child) {\n new Loader(child).all()\n })\n}))\n\ndocument.addEventListener('resized', ((event) => {\n new Loader(event.target).visual()\n}))\n"],"names":["reportError","error","Loader","fn","content","loadingClass","modes","result","entry","key","hooks","opts","hook","resolve","reject","e","__publicField","event","fallbackToDefaultActions","streamElement","target","child"],"mappings":"oKAAe,SAASA,EAAaC,EAAO,CACvC,OAAO,QAAY,KACpB,QAAQ,MAAMA,EAAM,OAAO,EAE7B,QAAQ,MAAMA,EAAOA,EAAM,KAAK,CAClC,CCyBe,MAAMC,CAAO,CAC1B,OAAO,WAAYC,EAAI,CACrB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