189 lines
6.0 KiB
JavaScript
189 lines
6.0 KiB
JavaScript
// Module Loader Lite - Versão simplificada para testes standalone
|
|
window.ModuleLoaderLite = (function () {
|
|
|
|
function log(message, ...args) {
|
|
console.log(`🧪 ModuleLoaderLite: ${message}`, ...args);
|
|
}
|
|
|
|
function error(message, ...args) {
|
|
console.error(`❌ ModuleLoaderLite: ${message}`, ...args);
|
|
}
|
|
|
|
function extractModuleMetadata(container) {
|
|
const metadataScript = container.querySelector('#module-metadata');
|
|
if (!metadataScript) {
|
|
log('Nenhum metadata encontrado no módulo');
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const metadata = JSON.parse(metadataScript.textContent);
|
|
log('Metadata extraído:', metadata);
|
|
return metadata;
|
|
} catch (err) {
|
|
error('Erro ao parsear metadata:', err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function loadLocalScript(url) {
|
|
try {
|
|
log(`Carregando script local: ${url}`);
|
|
|
|
const response = await fetch(url);
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
const scriptContent = await response.text();
|
|
|
|
// Criar script element e executar
|
|
const script = document.createElement('script');
|
|
script.textContent = scriptContent;
|
|
script.setAttribute('data-test-script', url);
|
|
document.head.appendChild(script);
|
|
|
|
log(`✅ Script local carregado: ${url}`);
|
|
return true;
|
|
} catch (err) {
|
|
error(`Falha ao carregar script ${url}:`, err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function loadLocalStyle(url) {
|
|
try {
|
|
log(`Carregando CSS local: ${url}`);
|
|
|
|
const link = document.createElement('link');
|
|
link.rel = 'stylesheet';
|
|
link.href = url;
|
|
link.setAttribute('data-test-style', url);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
link.onload = () => {
|
|
log(`✅ CSS local carregado: ${url}`);
|
|
resolve(true);
|
|
};
|
|
link.onerror = () => {
|
|
error(`Falha ao carregar CSS: ${url}`);
|
|
reject(false);
|
|
};
|
|
document.head.appendChild(link);
|
|
});
|
|
} catch (err) {
|
|
error(`Erro ao carregar CSS ${url}:`, err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function initializeLocalModule(containerId) {
|
|
log(`Inicializando módulo local em: ${containerId}`);
|
|
|
|
const container = document.getElementById(containerId);
|
|
if (!container) {
|
|
error(`Container não encontrado: ${containerId}`);
|
|
return false;
|
|
}
|
|
|
|
// Extrair metadata
|
|
const metadata = extractModuleMetadata(container);
|
|
if (!metadata) {
|
|
error('Metadata não encontrado, não é possível inicializar');
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
// Carregar CSS se especificado
|
|
if (metadata.cssUrl) {
|
|
await loadLocalStyle(metadata.cssUrl);
|
|
}
|
|
|
|
// Carregar JavaScript
|
|
if (metadata.jsUrl) {
|
|
const scriptLoaded = await loadLocalScript(metadata.jsUrl);
|
|
if (!scriptLoaded) {
|
|
throw new Error('Falha ao carregar script principal');
|
|
}
|
|
}
|
|
|
|
// Aguardar execução do script
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
|
|
|
// Chamar função de inicialização
|
|
if (metadata.jsFunction) {
|
|
const functionPath = metadata.jsFunction.split('.');
|
|
let func = window;
|
|
|
|
for (const part of functionPath) {
|
|
func = func[part];
|
|
if (!func) {
|
|
throw new Error(`Função ${metadata.jsFunction} não encontrada`);
|
|
}
|
|
}
|
|
|
|
if (typeof func === 'function') {
|
|
log(`Chamando função: ${metadata.jsFunction}`);
|
|
const result = func(containerId);
|
|
|
|
if (result) {
|
|
log(`✅ Módulo ${metadata.moduleId} inicializado localmente`);
|
|
|
|
// Disparar evento
|
|
const event = new CustomEvent('moduleLoadedLocal', {
|
|
detail: {
|
|
moduleId: metadata.moduleId,
|
|
containerId,
|
|
metadata,
|
|
mode: 'standalone'
|
|
}
|
|
});
|
|
document.dispatchEvent(event);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
} catch (err) {
|
|
error(`Erro ao inicializar módulo local:`, err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function loadModuleLocally(containerId, moduleEndpoint) {
|
|
log(`Carregando módulo local em ${containerId} de ${moduleEndpoint}`);
|
|
|
|
try {
|
|
const response = await fetch(moduleEndpoint);
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
const html = await response.text();
|
|
const container = document.getElementById(containerId);
|
|
|
|
if (!container) {
|
|
throw new Error(`Container ${containerId} não encontrado`);
|
|
}
|
|
|
|
// Inserir HTML
|
|
container.innerHTML = html;
|
|
|
|
// Inicializar
|
|
return await initializeLocalModule(containerId);
|
|
|
|
} catch (err) {
|
|
error(`Erro ao carregar módulo local:`, err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// API pública
|
|
return {
|
|
loadModule: loadModuleLocally,
|
|
initializeModule: initializeLocalModule,
|
|
version: '1.0.0-lite'
|
|
};
|
|
})(); |