* 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
62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
|
|
|
|
/* global checkUserLoggedIn */
|
|
|
|
function removeExistingCSRF() {
|
|
var csrfTokenMeta = document.querySelector("meta[name='csrf-token']");
|
|
var csrfParamMeta = document.querySelector("meta[name='csrf-param']");
|
|
if (csrfTokenMeta && csrfParamMeta) {
|
|
csrfTokenMeta.parentNode.removeChild(csrfTokenMeta);
|
|
csrfParamMeta.parentNode.removeChild(csrfParamMeta);
|
|
}
|
|
}
|
|
|
|
function fetchBaseData() {
|
|
var xmlhttp;
|
|
if (window.XMLHttpRequest) {
|
|
xmlhttp = new XMLHttpRequest();
|
|
} else {
|
|
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
|
|
}
|
|
xmlhttp.onreadystatechange = () => {
|
|
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
|
|
// Assigning CSRF
|
|
var json = JSON.parse(xmlhttp.responseText);
|
|
if (json.token) {
|
|
removeExistingCSRF();
|
|
}
|
|
var newCsrfParamMeta = document.createElement('meta');
|
|
newCsrfParamMeta.name = 'csrf-param';
|
|
newCsrfParamMeta.content = json.param;
|
|
document.getElementsByTagName('head')[0].appendChild(newCsrfParamMeta);
|
|
var newCsrfTokenMeta = document.createElement('meta');
|
|
newCsrfTokenMeta.name = 'csrf-token';
|
|
newCsrfTokenMeta.content = json.token;
|
|
document.getElementsByTagName('head')[0].appendChild(newCsrfTokenMeta);
|
|
document.getElementsByTagName('body')[0].dataset.loaded = 'true';
|
|
|
|
// Assigning Broadcast
|
|
if (json.broadcast) {
|
|
document.body.dataset.broadcast = json.broadcast;
|
|
}
|
|
|
|
// Assigning User
|
|
if (checkUserLoggedIn()) {
|
|
document.getElementsByTagName('body')[0].dataset.user = json.user;
|
|
browserStoreCache('set', json.user);
|
|
setTimeout(() => {
|
|
if (typeof ga === 'function') {
|
|
ga('set', 'userId', JSON.parse(json.user).id);
|
|
}
|
|
}, 400);
|
|
}
|
|
}
|
|
};
|
|
|
|
xmlhttp.open('GET', '/async_info/base_data', true);
|
|
xmlhttp.send();
|
|
}
|
|
|
|
function initializeBodyData() {
|
|
fetchBaseData();
|
|
}
|