document.addEventListener('DOMContentLoaded', function() {
console.log('Modules page initializing...');
// Reference to container and filters
const modulesContainer = document.getElementById('modules-container');
const moduleSearch = document.getElementById('module-search');
const stageFilter = document.getElementById('stage-filter');
const componentFilter = document.getElementById('component-filter');
const templateFilter = document.getElementById('template-filter');
// Data structures
let allModules = [];
let stageNames = {};
let componentNames = {};
let templateUsage = {};
let processedTemplates = [];
// Functions
function initializeData() {
console.log('Initializing data...');
// Check if data is available
if (typeof moduleData === 'undefined') {
console.error('Error: moduleData is undefined');
modulesContainer.innerHTML = '
Contains ${stageModules.length} module${stageModules.length !== 1 ? 's' : ''}
`;
// Create toggle button
const toggleBtn = document.createElement('button');
toggleBtn.className = 'toggle-btn';
toggleBtn.innerHTML = '+';
toggleBtn.setAttribute('aria-label', 'Toggle stage content');
stageHeader.appendChild(stageHeaderContent);
stageHeader.appendChild(toggleBtn);
stageSection.appendChild(stageHeader);
// Create stage body
const stageBody = document.createElement('div');
stageBody.className = 'stage-body';
stageBody.style.display = 'none';
// Group modules by component
const modulesByComponent = {};
stageModules.forEach(module => {
// Use just the component name without duplicating stage info
const cleanComponentName = module.componentName.replace(module.stageName, '').trim();
const displayComponentName = cleanComponentName || module.componentName;
if (!modulesByComponent[displayComponentName]) {
modulesByComponent[displayComponentName] = [];
}
modulesByComponent[displayComponentName].push(module);
});
// Sort components
const sortedComponents = Object.keys(modulesByComponent).sort();
// Create components container
const componentsContainer = document.createElement('div');
componentsContainer.className = 'components';
// Create component sections
sortedComponents.forEach(componentName => {
const componentModules = modulesByComponent[componentName];
// Create a component group heading with count
const componentHeading = document.createElement('h3');
componentHeading.className = 'component-group-heading';
// Create main text
const headingText = document.createTextNode(componentName);
componentHeading.appendChild(headingText);
// Add count in parentheses
const moduleCount = componentModules.length;
const countSpan = document.createElement('span');
countSpan.className = 'component-module-count';
countSpan.textContent = ` (${moduleCount} module${moduleCount !== 1 ? 's' : ''})`;
componentHeading.appendChild(countSpan);
componentsContainer.appendChild(componentHeading);
// Add modules to component section
componentModules.forEach(module => {
const moduleCard = createModuleCard(module);
componentsContainer.appendChild(moduleCard);
});
});
stageBody.appendChild(componentsContainer);
stageSection.appendChild(stageBody);
// Add toggle functionality
stageHeaderContent.addEventListener('click', function() {
if (stageBody.style.display === 'none') {
stageBody.style.display = 'block';
toggleBtn.innerHTML = '−';
} else {
stageBody.style.display = 'none';
toggleBtn.innerHTML = '+';
}
});
toggleBtn.addEventListener('click', function(e) {
e.stopPropagation();
if (stageBody.style.display === 'none') {
stageBody.style.display = 'block';
toggleBtn.innerHTML = '−';
} else {
stageBody.style.display = 'none';
toggleBtn.innerHTML = '+';
}
});
modulesContainer.appendChild(stageSection);
});
console.log('Modules rendering complete');
}
function createModuleCard(module) {
const card = document.createElement('div');
card.className = 'component-card';
card.dataset.id = module.id;
// Format templates list
const templatesList = module.usedInTemplates.length > 0
? module.usedInTemplates.join(', ')
: 'Not used in any template';
// Create card content with structure similar to builder component
card.innerHTML = `
${module.stageName}
${module.componentName}
Used in templates: ${templatesList}
`;
// Make the header clickable to toggle content visibility
const header = card.querySelector('.component-header');
const body = card.querySelector('.component-body');
// Add toggle button to the header (already positioned by CSS flexbox)
const toggleBtn = document.createElement('button');
toggleBtn.className = 'toggle-btn';
toggleBtn.innerHTML = '+';
toggleBtn.setAttribute('aria-label', 'Toggle module content');
header.appendChild(toggleBtn);
// Start with content hidden
body.style.display = 'none';
// Toggle functionality
function toggleContent() {
if (body.style.display === 'none') {
body.style.display = 'block';
toggleBtn.innerHTML = '−';
card.classList.add('expanded');
} else {
body.style.display = 'none';
toggleBtn.innerHTML = '+';
card.classList.remove('expanded');
}
}
header.addEventListener('click', toggleContent);
return card;
}
// Initialize page
function init() {
console.log('Initializing modules page...');
// Load data
if (!initializeData()) {
console.error('Failed to initialize data');
return;
}
// Populate filters
populateFilters();
// Render modules
renderModules();
// Add event listeners to filters
moduleSearch.addEventListener('input', renderModules);
stageFilter.addEventListener('change', renderModules);
componentFilter.addEventListener('change', renderModules);
templateFilter.addEventListener('change', renderModules);
console.log('Modules page initialized with', allModules.length, 'modules');
}
// Start initialization
init();
});