mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-24 18:09:13 -04:00
Feat/webpack (#2998)
* First commit * Removed govuk for webpack. Modernized javascript importing. Removed dead js * Fixed tests, a few styling bugs * Fixed some table errors and regenerated backstop ref images * Updated tests for coverage * Changes from carlo suggestions
This commit is contained in:
40
app/assets/javascripts/moduleRegistry.js
Normal file
40
app/assets/javascripts/moduleRegistry.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const modules = new Map();
|
||||
|
||||
export function registerModule(name, ModuleClass) {
|
||||
modules.set(name, ModuleClass);
|
||||
window.NotifyModules = window.NotifyModules || {};
|
||||
window.NotifyModules[name] = ModuleClass;
|
||||
}
|
||||
|
||||
export function initModules() {
|
||||
const moduleElements = document.querySelectorAll('[data-module]');
|
||||
|
||||
moduleElements.forEach(element => {
|
||||
const moduleName = element.getAttribute('data-module');
|
||||
const moduleStarted = element.getAttribute('data-module-started');
|
||||
|
||||
if (moduleStarted) return;
|
||||
|
||||
const ModuleClass = modules.get(moduleName);
|
||||
|
||||
if (!ModuleClass) {
|
||||
console.warn(`Module "${moduleName}" not found in registry`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const moduleInstance = new ModuleClass();
|
||||
|
||||
if (moduleInstance.start && typeof moduleInstance.start === 'function') {
|
||||
moduleInstance.start(element);
|
||||
}
|
||||
|
||||
element.setAttribute('data-module-started', 'true');
|
||||
} catch (error) {
|
||||
console.error(`Error initializing module "${moduleName}":`, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
window.NotifyModules = window.NotifyModules || {};
|
||||
window.NotifyModules.start = initModules;
|
||||
Reference in New Issue
Block a user