docbrown/app/assets/javascripts/initializers/initializeBroadcast.js
Vaidehi Joshi defab11a4d
Provide default crayon styles when creating banners (#8361) [deploy]
* Provide default crayon styles when creating banners

* Import crayons styles into /internal pages.
* Allow admins to use crayons banner styles when creating broadcasts.
* Preview banner styles with crayons.

* Use constant and helper for broadcast banner styles

* Add VALID_BANNER_STYLES frozen constant to Broadcast class.
* Add banner_style helper for determining banner style class.
2020-06-09 11:26:14 -07:00

36 lines
895 B
JavaScript

/* eslint-disable camelcase */
/**
* 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, banner_class } = data;
const el = document.getElementById('active-broadcast');
if (el.firstElementChild) {
return; // Only append HTML once, on first render.
}
const bannerDiv = `<div class='broadcast-data ${
banner_class || ''
}'>${html}</div>`;
el.insertAdjacentHTML('afterbegin', bannerDiv);
}
/* eslint-enable camelcase */