docbrown/app/javascript/packs/billboard.js
Ben Halpern b0cb5540c0
Add new render mode and template options for billboards (#20282)
* Add new render and template modes for billboards

* Add new fields to /admin

* Adjust test

* Adjust markup in form

* Add tests for script execution

* Add two script test

* Adjust scripts

* Adjust scripts

* Add new test

* Add new explicit tests

* Move JS functionaity around
2023-10-30 14:56:09 -04:00

44 lines
1.3 KiB
JavaScript

import { setupBillboardDropdown } from '../utilities/billboardDropdown';
import {
observeBillboards,
executeBBScripts,
} from './billboardAfterRenderActions';
export async function getBillboard() {
const placeholderElements = document.getElementsByClassName(
'js-billboard-container',
);
const promises = [...placeholderElements].map(generateBillboard);
await Promise.all(promises);
}
async function generateBillboard(element) {
const { asyncUrl } = element.dataset;
if (asyncUrl) {
try {
const response = await window.fetch(`${asyncUrl}`);
const htmlContent = await response.text();
const generatedElement = document.createElement('div');
generatedElement.innerHTML = htmlContent;
element.innerHTML = '';
element.appendChild(generatedElement);
executeBBScripts(element);
setupBillboardDropdown();
// This is called here because the ad is loaded asynchronously.
// The original code is still in the asset pipeline, so is not importable.
// This could be refactored to be importable as we continue that migration.
// eslint-disable-next-line no-undef
observeBillboards();
} catch (error) {
if (!/NetworkError/i.test(error.message)) {
Honeybadger.notify(error);
}
}
}
}
getBillboard();