14 changed files with 10367 additions and 131 deletions
@ -1 +1,157 @@ |
|||||
window.initBlock=function(e="",n="",o){document.querySelectorAll(n).forEach((e=>o(e)))},function(){let e;function n(n){const o=document.querySelector("body > main").scrollHeight;if(e===o)return;e=o,window.parent.postMessage("resize:"+JSON.stringify({height:e}),"*")}n(),new ResizeObserver(n).observe(document.body)}(); |
window.initBlock = initBlock; |
||||
|
|
||||
|
let template; |
||||
|
let data = {}; |
||||
|
let reload; |
||||
|
|
||||
|
// Blocks Initialization.
|
||||
|
function initBlock(blockName = '', selector = '', cb) { |
||||
|
reload = function () { |
||||
|
document.querySelectorAll(selector).forEach((el) => cb(el)); |
||||
|
}; |
||||
|
|
||||
|
reload(); |
||||
|
} |
||||
|
|
||||
|
// Scrollbars / Frame resizes notifications.
|
||||
|
(function () { |
||||
|
let height; |
||||
|
|
||||
|
handleHeightChange(); // Initial frame's height setup.
|
||||
|
setupResizeListener(); // Listen to frame's height changes.
|
||||
|
|
||||
|
///
|
||||
|
|
||||
|
function setupResizeListener() { |
||||
|
const resizeObserver = new ResizeObserver(handleHeightChange); |
||||
|
resizeObserver.observe(document.body); |
||||
|
} |
||||
|
|
||||
|
function handleHeightChange() { |
||||
|
const updatedHeight = getCurrentHeight(); |
||||
|
|
||||
|
if (height === updatedHeight) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
const RESIZE_CODE = 'resize:'; |
||||
|
height = updatedHeight; |
||||
|
window.parent.postMessage(RESIZE_CODE + JSON.stringify({height}), '*'); |
||||
|
} |
||||
|
|
||||
|
function getCurrentHeight() { |
||||
|
return document.querySelector('#hbs-container').scrollHeight; |
||||
|
} |
||||
|
})(); |
||||
|
|
||||
|
// Data Updates Listeners.
|
||||
|
(function () { |
||||
|
loadDataOptions(); |
||||
|
listenToDataOptionsUpdates(); |
||||
|
|
||||
|
function listenToDataOptionsUpdates() { |
||||
|
window.addEventListener('message', function (event) { |
||||
|
const message = event.data; |
||||
|
const prefix = 'dataUpdate:'; |
||||
|
|
||||
|
if (typeof message !== "string" || !message.startsWith(prefix)) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
data = JSON.parse(message.substring(prefix.length)); |
||||
|
updateBlock({data}); |
||||
|
} catch (e) { |
||||
|
console.log('Error parsing incoming data.', e); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
function getQueryParams() { |
||||
|
const urlParams = new URLSearchParams(window.location.search); |
||||
|
const params = {}; |
||||
|
|
||||
|
for (const [key, value] of urlParams) { |
||||
|
params[key] = value; |
||||
|
} |
||||
|
|
||||
|
return params; |
||||
|
} |
||||
|
|
||||
|
function loadDataOptions() { |
||||
|
const queryParameters = new URLSearchParams({name: getQueryParams().data || 'default'}); |
||||
|
fetch(`/data?${queryParameters}`) |
||||
|
.then((response) => response.json()) |
||||
|
.then((response) => { |
||||
|
data = response.data; // Update state.
|
||||
|
updateBlock({data}); |
||||
|
}); |
||||
|
} |
||||
|
})(); |
||||
|
|
||||
|
// Listen to Template updates.
|
||||
|
(function () { |
||||
|
initSocket(); |
||||
|
|
||||
|
function initSocket() { |
||||
|
const socket = window.io.connect(); |
||||
|
|
||||
|
socket.on('error', function (err) { |
||||
|
console.log(err); |
||||
|
}); |
||||
|
|
||||
|
// socket.on('connect', function () {
|
||||
|
// console.log('user connected', socket.id)
|
||||
|
// });
|
||||
|
|
||||
|
socket.on('templateUpdate', function (args) { |
||||
|
updateBlock({template: args.template}); |
||||
|
}); |
||||
|
} |
||||
|
})(); |
||||
|
|
||||
|
function updateBlock(args = {}) { |
||||
|
if (args.template) { |
||||
|
template = args.template; // Update state.
|
||||
|
} |
||||
|
|
||||
|
if (args.data) { |
||||
|
data = args.data; // Update state.
|
||||
|
} |
||||
|
|
||||
|
if (!template) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
renderBlock(template, data || {}, document.getElementById("hbs-container")); |
||||
|
} |
||||
|
|
||||
|
function renderBlock(templateHbs, jsonData, target) { |
||||
|
const template = Handlebars.compile(templateHbs); |
||||
|
|
||||
|
/** |
||||
|
* Handlebars Helpers |
||||
|
*/ |
||||
|
Handlebars.registerHelper('esc_attr', function (attr) { |
||||
|
return attr; |
||||
|
}); |
||||
|
|
||||
|
Handlebars.registerHelper('esc_url', function (attr) { |
||||
|
return attr; |
||||
|
}); |
||||
|
|
||||
|
Handlebars.registerHelper('esc_html', function (attr) { |
||||
|
return attr; |
||||
|
}); |
||||
|
|
||||
|
Handlebars.registerHelper('base_url', function () { |
||||
|
return '/'; |
||||
|
}); |
||||
|
|
||||
|
target.innerHTML = template(jsonData); |
||||
|
|
||||
|
if (reload) { |
||||
|
reload(); |
||||
|
} |
||||
|
} |
||||
|
//# sourceMappingURL=frame-index.min.js.map
|
||||
|
|||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue