docbrown/app/assets/javascripts/initializers/initializeBroadcast.js
Vaidehi Joshi f04b0a7336
Display site-wide announcements (#8301)
* Serialize broadcast_data within AsyncInfoController::base_data

Adds a `broadcast:` key to our `base_data`, regardless of whether a user is logged in or not.
If a broadcast is not present, passes along a `nil` value.

* Initialize active broadcast (if present) when rendering main page

* Allow admins to preview a broadcast's processed_html when editing

* Add some fixed position styling to active broadcast

* Add system specs around broadcasts on homepage

* Use sanitize in place of html_safe when previewing broadcast

* Do not initialize broadcast if no broadcastData available

* Render default value on broadcast in options_for_select

* JS cleanup

* Unset fixed positioning for static navbar config

Also add some default styling for an active broadcast
2020-06-08 13:52:53 -04:00

31 lines
718 B
JavaScript

/**
* Parses the broadcast object on the document into JSON.
*
* @function broadcastData
*/
function broadcastData() {
const { broadcast = null } = document.body.dataset;
return JSON.parse(broadcast);
}
/**
* Inserts the broadcast's HTML into `active-broadcast` element
* as the first child within the document's body, and only inserts the HTML once.
*
* @function initializeBroadcast
*/
function initializeBroadcast() {
const data = broadcastData();
if (!data) {
return;
}
const { html } = data;
var el = document.getElementById('active-broadcast');
if (el.firstElementChild) {
return;
} // Only append HTML once, on first render.
el.insertAdjacentHTML('afterbegin', html);
}