modular-application / index.html
Darkxx91's picture
"Create a modular HTML application using the Module Pattern with IIFE (Immediately Invoked Function Expressions) to simulate a component-based architecture. Include a service layer, state management using closures, and event-driven communication between modules. Structure: App.init() → ModuleA.init() → ModuleB.init() with dependency injection." - Initial Deployment
4ec0a27 verified
Raw History Blame Contribute Delete
12.2 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modular Application with IIFE</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.module-card {
transition: all 0.3s ease;
}
.module-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.event-log {
max-height: 200px;
overflow-y: auto;
}
.fade-in {
animation: fadeIn 0.5s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<div class="container mx-auto px-4 py-8">
<header class="text-center mb-12">
<h1 class="text-4xl font-bold text-indigo-700 mb-2">Modular Application</h1>
<p class="text-gray-600">Component-based architecture with IIFE modules</p>
</header>
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
<!-- Module A -->
<div id="moduleA" class="module-card bg-white rounded-xl shadow-md p-6">
<div class="flex items-center justify-between mb-4">
<h2 class="text-xl font-semibold text-indigo-600">Module A</h2>
<span class="bg-indigo-100 text-indigo-800 text-xs font-medium px-2.5 py-0.5 rounded-full">State Manager</span>
</div>
<div class="mb-4">
<p class="text-gray-700 mb-2">Current State:</p>
<div id="moduleAState" class="bg-gray-50 p-3 rounded-lg text-sm font-mono"></div>
</div>
<button id="moduleAButton" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white py-2 px-4 rounded-lg transition">
Update State
</button>
</div>
<!-- Module B -->
<div id="moduleB" class="module-card bg-white rounded-xl shadow-md p-6">
<div class="flex items-center justify-between mb-4">
<h2 class="text-xl font-semibold text-green-600">Module B</h2>
<span class="bg-green-100 text-green-800 text-xs font-medium px-2.5 py-0.5 rounded-full">UI Component</span>
</div>
<div class="mb-4">
<p class="text-gray-700 mb-2">Received Data:</p>
<div id="moduleBData" class="bg-gray-50 p-3 rounded-lg text-sm font-mono"></div>
</div>
<button id="moduleBButton" class="w-full bg-green-600 hover:bg-green-700 text-white py-2 px-4 rounded-lg transition">
Request Data
</button>
</div>
<!-- Service Module -->
<div id="serviceModule" class="module-card bg-white rounded-xl shadow-md p-6">
<div class="flex items-center justify-between mb-4">
<h2 class="text-xl font-semibold text-purple-600">Service Module</h2>
<span class="bg-purple-100 text-purple-800 text-xs font-medium px-2.5 py-0.5 rounded-full">Data Service</span>
</div>
<div class="mb-4">
<p class="text-gray-700 mb-2">Service Data:</p>
<div id="serviceData" class="bg-gray-50 p-3 rounded-lg text-sm font-mono"></div>
</div>
<button id="refreshServiceData" class="w-full bg-purple-600 hover:bg-purple-700 text-white py-2 px-4 rounded-lg transition">
Refresh Data
</button>
</div>
</div>
<!-- Event Log -->
<div class="bg-white rounded-xl shadow-md p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
<i class="fas fa-list-alt mr-2 text-blue-500"></i> Event Log
</h2>
<div id="eventLog" class="event-log bg-gray-50 p-4 rounded-lg text-sm font-mono space-y-2"></div>
</div>
</div>
<script>
// Event Bus for module communication
const EventBus = (() => {
const events = {};
const subscribe = (eventName, callback) => {
if (!events[eventName]) {
events[eventName] = [];
}
events[eventName].push(callback);
logEvent(`Subscribed to ${eventName}`);
};
const publish = (eventName, data) => {
if (events[eventName]) {
events[eventName].forEach(callback => {
callback(data);
});
}
logEvent(`Published ${eventName}`, data);
};
return {
subscribe,
publish
};
})();
// Logger utility
const logEvent = (message, data = null) => {
const eventLog = document.getElementById('eventLog');
const logEntry = document.createElement('div');
logEntry.className = 'fade-in border-l-4 border-blue-500 pl-3 py-1';
const timestamp = new Date().toLocaleTimeString();
logEntry.innerHTML = `<span class="text-blue-600">[${timestamp}]</span> ${message}`;
if (data) {
const dataElement = document.createElement('div');
dataElement.className = 'ml-4 mt-1 text-gray-600';
dataElement.textContent = JSON.stringify(data, null, 2);
logEntry.appendChild(dataElement);
}
eventLog.prepend(logEntry);
};
// Service Module - Data Service
const DataService = (() => {
let serviceData = {
users: [
{ id: 1, name: 'John Doe', email: 'john@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane@example.com' }
],
lastUpdated: new Date().toISOString()
};
const getData = () => {
logEvent('DataService: getData called');
return { ...serviceData };
};
const refreshData = () => {
// Simulate API call
setTimeout(() => {
serviceData.lastUpdated = new Date().toISOString();
serviceData.users = serviceData.users.map(user => ({
...user,
lastActive: new Date().toISOString()
}));
EventBus.publish('DATA_REFRESHED', serviceData);
updateServiceUI();
}, 500);
};
const updateServiceUI = () => {
const serviceDataElement = document.getElementById('serviceData');
serviceDataElement.textContent = JSON.stringify(serviceData, null, 2);
};
// Initialize
(() => {
document.getElementById('refreshServiceData').addEventListener('click', refreshData);
updateServiceUI();
logEvent('DataService initialized');
})();
return {
getData,
refreshData
};
})();
// Module A - State Manager
const ModuleA = ((eventBus, dataService) => {
let state = {
counter: 0,
lastUpdate: null
};
const updateState = () => {
state.counter += 1;
state.lastUpdate = new Date().toISOString();
eventBus.publish('STATE_UPDATED', state);
updateUI();
logEvent('ModuleA: State updated');
};
const updateUI = () => {
const stateElement = document.getElementById('moduleAState');
stateElement.textContent = JSON.stringify(state, null, 2);
};
// Initialize
const init = () => {
document.getElementById('moduleAButton').addEventListener('click', updateState);
updateUI();
logEvent('ModuleA initialized with DataService dependency');
// Subscribe to events from ModuleB
eventBus.subscribe('DATA_REQUESTED', () => {
logEvent('ModuleA received DATA_REQUESTED event');
eventBus.publish('DATA_PROVIDED', dataService.getData());
});
};
return {
init,
getState: () => ({ ...state })
};
})(EventBus, DataService);
// Module B - UI Component
const ModuleB = ((eventBus) => {
let receivedData = null;
const requestData = () => {
logEvent('ModuleB requesting data');
eventBus.publish('DATA_REQUESTED');
};
const handleDataProvided = (data) => {
receivedData = data;
updateUI();
logEvent('ModuleB received data');
};
const updateUI = () => {
const dataElement = document.getElementById('moduleBData');
if (receivedData) {
dataElement.textContent = JSON.stringify(receivedData, null, 2);
} else {
dataElement.textContent = 'No data received yet';
}
};
// Initialize
const init = () => {
document.getElementById('moduleBButton').addEventListener('click', requestData);
eventBus.subscribe('DATA_PROVIDED', handleDataProvided);
updateUI();
logEvent('ModuleB initialized with EventBus dependency');
// Also listen to state changes from ModuleA
eventBus.subscribe('STATE_UPDATED', (state) => {
logEvent('ModuleB received STATE_UPDATED event');
document.getElementById('moduleB').classList.add('ring-2', 'ring-indigo-200');
setTimeout(() => {
document.getElementById('moduleB').classList.remove('ring-2', 'ring-indigo-200');
}, 1000);
});
};
return {
init
};
})(EventBus);
// Main App
const App = (() => {
const init = () => {
logEvent('App initializing...');
// Initialize modules with dependencies
ModuleA.init();
ModuleB.init();
logEvent('App initialized successfully');
};
return {
init
};
})();
// Start the application
document.addEventListener('DOMContentLoaded', () => {
App.init();
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Darkxx91/modular-application" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>