/* eslint-disable camelcase */ /** * Parses the broadcast object on the document into JSON. * * @function broadcastData * @returns {Object} Returns an object of the parsed broadcast data. */ function broadcastData() { const { broadcast = null } = document.body.dataset; return JSON.parse(broadcast); } /** * Parses the broadcast object on the document into JSON. * * @function camelizedBroadcastKey * @param {string} title The title of the broadcast. * @returns {string} Returns the camelized title appended with "Seen". */ function camelizedBroadcastKey(title) { const camelizedTitle = title.replace(/\W+(.)/g, (match, string) => { return string.toUpperCase(); }); return `${camelizedTitle}Seen`; } /** * A function that finds the close button and adds a click handler to it. * The click handler sets a key in local storage and removes the broadcast * element entirely from the DOM. * * @function addCloseButtonClickHandle * @param {string} title The title of the broadcast. */ function addCloseButtonClickHandle(title) { var closeButton = document.getElementsByClassName( 'close-announcement-button', )[0]; closeButton.onclick = (e) => { localStorage.setItem(camelizedBroadcastKey(title), true); document.getElementById('active-broadcast').remove(); }; } /** * A function to insert the broadcast's HTML into the `active-broadcast` element. * Determines what classes to add to the broadcast element, * and inserts a close button and adds a click handler to it. * * Adds a `.broadcast-visible` class to the broadcastElement to make it display. * * @function initializeBroadcast * @param {string} broadcastElement The HTML element for the broadcast, with a class of `.active-broadcast`. * @param {Object} data An object representing the parsed broadcast data. */ function renderBroadcast(broadcastElement, data) { const { banner_class, html, title } = data; if (banner_class) { const [defaultClass, additionalClass] = banner_class.split(' '); if (additionalClass) { broadcastElement.classList.add(defaultClass, additionalClass); } else { broadcastElement.classList.add(defaultClass); } } const closeButton = ``; broadcastElement.insertAdjacentHTML( 'afterbegin', `