/**
* SmartVue Widget Loader with Client Config Support
* This loader fetches the current version and loads the appropriate files
*
* Usage:
*
* OR
*
*/
(function() {
'use strict';
// Get the current script tag
const currentScript = document.currentScript;
// Get the base URL from the loader script
const loaderSrc = currentScript && currentScript.src;
const baseUrl = loaderSrc ? loaderSrc.substring(0, loaderSrc.lastIndexOf('/')) : '';
// Determine client name from data attribute or URL parameter
let clientName = null;
// Check data attribute first
if (currentScript && currentScript.dataset.client) {
clientName = currentScript.dataset.client;
}
// Check URL parameter as fallback
if (!clientName && loaderSrc) {
try {
const url = new URL(loaderSrc);
clientName = url.searchParams.get('client');
} catch (e) {
// Invalid URL, skip
}
}
// Helper: copy data attributes from loader script to a new script tag
function copyDataAttributes(targetScript, excludeClient) {
if (currentScript) {
Array.from(currentScript.attributes).forEach(function(attr) {
if (attr.name.startsWith('data-') && (!excludeClient || attr.name !== 'data-client')) {
targetScript.setAttribute(attr.name, attr.value);
}
});
}
}
// Check the host page URL for a ?v= version override (for staging/testing)
var versionOverride = null;
try {
versionOverride = new URL(window.location.href).searchParams.get('v');
} catch (e) {
// Ignore — older browsers without URL support
}
if (versionOverride) {
// Load a specific staged version directly, skip version.json entirely
console.log('SmartVue: Loading staged version ' + versionOverride);
var stagedScript = document.createElement('script');
stagedScript.src = baseUrl + '/smartvue.' + versionOverride + '.js';
stagedScript.async = true;
copyDataAttributes(stagedScript, false);
document.head.appendChild(stagedScript);
} else {
// Normal flow: fetch version info and load the appropriate files
fetch(baseUrl + '/version.json?_=' + Date.now())
.then(function(response) { return response.json(); })
.then(function(versionData) {
if (clientName && versionData.clients && versionData.clients[clientName]) {
// Load client-specific config (which will load the widget)
console.log('SmartVue: Loading client config for ' + clientName);
var clientConfig = versionData.clients[clientName];
var script = document.createElement('script');
script.src = baseUrl + '/' + clientConfig.path;
script.async = true;
copyDataAttributes(script, true);
document.head.appendChild(script);
} else {
// Load main widget directly (no client config)
console.log('SmartVue: Loading main widget');
var script = document.createElement('script');
script.src = baseUrl + '/' + versionData.widget.filename;
script.async = true;
copyDataAttributes(script, false);
document.head.appendChild(script);
}
})
.catch(function(error) {
console.error('SmartVue: Failed to load version info', error);
// Fallback to main file if version fetch fails
var script = document.createElement('script');
script.src = baseUrl + '/smartvue.js';
script.async = true;
copyDataAttributes(script, false);
document.head.appendChild(script);
});
}
})();