76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
(function() {
|
|
var manifestPromise = null;
|
|
var loadedScripts = Object.create(null);
|
|
|
|
function overlayBasePath() {
|
|
return (window.APP_BASE || '/') + 'overlay/';
|
|
}
|
|
|
|
function fetchManifest() {
|
|
return fetch(overlayBasePath() + 'manifest.json', { cache: 'no-store' })
|
|
.then(function(response) {
|
|
if (response.status === 404) {
|
|
return null;
|
|
}
|
|
if (!response.ok) {
|
|
throw new Error('overlay manifest request failed');
|
|
}
|
|
return response.json();
|
|
})
|
|
.catch(function() {
|
|
return null;
|
|
});
|
|
}
|
|
|
|
function loadScript(path) {
|
|
if (loadedScripts[path]) {
|
|
return loadedScripts[path];
|
|
}
|
|
loadedScripts[path] = new Promise(function(resolve, reject) {
|
|
var script = document.createElement('script');
|
|
script.src = overlayBasePath() + path.replace(/^\/+/, '');
|
|
script.async = false;
|
|
script.onload = function() { resolve(); };
|
|
script.onerror = function() { reject(new Error('overlay script load failed')); };
|
|
document.head.appendChild(script);
|
|
}).catch(function() {
|
|
return null;
|
|
});
|
|
return loadedScripts[path];
|
|
}
|
|
|
|
function load() {
|
|
if (manifestPromise) {
|
|
return manifestPromise;
|
|
}
|
|
manifestPromise = fetchManifest().then(function(manifest) {
|
|
if (!(manifest && manifest.slots && window.CrankUiSlots)) {
|
|
return null;
|
|
}
|
|
var tasks = Object.entries(manifest.slots)
|
|
.filter(function(entry) {
|
|
return window.CrankUiSlots.isAllowed(entry[0]) && typeof entry[1] === 'string';
|
|
})
|
|
.map(function(entry) {
|
|
return loadScript(entry[1]);
|
|
});
|
|
return Promise.all(tasks).then(function() {
|
|
return manifest;
|
|
});
|
|
});
|
|
return manifestPromise;
|
|
}
|
|
|
|
async function render(root, context) {
|
|
await load();
|
|
if (window.CrankUiSlots) {
|
|
window.CrankUiSlots.renderAll(root, context);
|
|
}
|
|
}
|
|
|
|
window.CrankOverlay = {
|
|
load: load,
|
|
render: render,
|
|
};
|
|
}());
|