# UI Structure Usage Script / Guide ## How to Use the Application's UI Structure This document provides step-by-step instructions on how to work with the multi-page architecture, shared components, and layout system. --- ## Part 1: Understanding the Structure ### Architecture Overview The application follows a **modular multi-page architecture**: 1. **Pages** (`/static/pages/`) - Standalone page modules 2. **Shared Components** (`/static/shared/`) - Reusable layouts, utilities, components 3. **Layout Manager** - Dynamically injects header, sidebar, footer 4. **API Client** - Centralized HTTP client with caching 5. **Component System** - Toast, Modal, Table, Chart, Loading ### Key Principles - Each page is self-contained but shares common layouts - Layouts are injected dynamically (not hardcoded in each page) - All API calls go through the centralized API client - Components are reusable across pages - Theme system (dark/light) is managed globally --- ## Part 2: Creating a New Page ### Step-by-Step Process #### Step 1: Create Directory Structure ``` Location: /static/pages/your-page-name/ Files needed: - index.html (required) - your-page-name.css (optional, for page-specific styles) - your-page-name.js (optional, for page-specific logic) ``` #### Step 2: Create the HTML Template **Template Structure:** ```html
Are you sure you want to proceed?
', buttons: [ { text: 'Cancel', class: 'btn-secondary', action: () => modal.close() }, { text: 'Confirm', class: 'btn-primary', action: () => { // Perform action performAction(); modal.close(); } } ] }); modal.show(); // Modal with form const formModal = new Modal({ title: 'Add Item', content: ` `, onClose: () => console.log('Modal closed') }); formModal.show(); ``` ### Loading Component **Purpose:** Show/hide loading states **Usage:** ```javascript import { Loading } from '/static/shared/js/components/loading.js'; // Show loading overlay Loading.show('Loading data...'); // Hide loading Loading.hide(); // Show loading in specific container Loading.showIn('#my-container', 'Loading...'); // Hide loading in container Loading.hideIn('#my-container'); ``` ### Table Component **Purpose:** Data tables with sorting and filtering **Usage:** ```javascript import { DataTable } from '/static/shared/js/components/table.js'; const table = new DataTable('#table-container', { columns: [ { key: 'name', label: 'Name', sortable: true }, { key: 'price', label: 'Price', sortable: true, formatter: (val) => `$${val}` }, { key: 'change', label: 'Change', sortable: true } ], data: marketData, searchable: true, pagination: true, pageSize: 10 }); // Update data table.updateData(newData); // Refresh table.refresh(); ``` ### Chart Component **Purpose:** Chart.js wrapper for data visualization **Usage:** ```javascript import { Chart } from '/static/shared/js/components/chart.js'; const chart = new Chart('#chart-container', { type: 'line', data: { labels: dates, datasets: [{ label: 'Price', data: prices, borderColor: '#8B5CF6' }] }, options: { responsive: true, maintainAspectRatio: false } }); // Update chart data chart.updateData(newData); ``` --- ## Part 4: Common Patterns ### Pattern 1: Page with Data Fetching ```javascript import LayoutManager from '/static/shared/js/core/layout-manager.js'; import { ApiClient } from '/static/shared/js/core/api-client.js'; import { Toast } from '/static/shared/js/components/toast.js'; import { Loading } from '/static/shared/js/components/loading.js'; // Initialize await LayoutManager.init('my-page'); // Fetch and display data async function loadData() { Loading.show('Loading data...'); try { const client = new ApiClient(); const data = await client.get('/api/endpoint'); renderData(data); Toast.success('Data loaded'); } catch (error) { Toast.error('Failed to load: ' + error.message); } finally { Loading.hide(); } } function renderData(data) { const container = document.getElementById('data-container'); container.innerHTML = data.map(item => `${item.description}
Are you sure?
', buttons: [ { text: 'Cancel', action: () => modal.close() }, { text: 'Delete', action: async () => { const client = new ApiClient(); await client.delete(`/api/items/${id}`); table.refresh(); modal.close(); } } ] }); modal.show(); } ``` --- ## Part 5: File Paths Reference ### Absolute Paths (Recommended) ``` /static/shared/js/core/layout-manager.js /static/shared/css/design-system.css /static/pages/dashboard/index.html ``` ### Relative Paths (From Page Directory) ``` ../../shared/js/core/layout-manager.js ../../shared/css/design-system.css ../dashboard/index.html ``` ### Import Statements ```javascript // ES6 Modules (recommended) import LayoutManager from '/static/shared/js/core/layout-manager.js'; import { ApiClient } from '/static/shared/js/core/api-client.js'; // Dynamic imports const { Toast } = await import('/static/shared/js/components/toast.js'); ``` --- ## Part 6: Best Practices Checklist ### ✅ Do's - Always use `LayoutManager.init()` in every page - Use `ApiClient` for all API calls (don't use raw `fetch()`) - Show loading states with `Loading` component - Provide user feedback with `Toast` notifications - Handle errors gracefully with try/catch - Use shared CSS classes from design system - Follow the page template structure - Register new pages in config and sidebar - Use absolute paths for imports - Clean up polling/intervals on page unload ### ❌ Don'ts - Don't hardcode layouts in pages (use LayoutManager) - Don't use raw `fetch()` for API calls - Don't create duplicate components (use shared ones) - Don't forget error handling - Don't use inline styles (use CSS classes) - Don't forget to register pages in navigation - Don't use relative paths that break on different routes - Don't forget to stop polling/intervals --- ## Part 7: Troubleshooting ### Layout Not Showing - Check that `LayoutManager.init()` is called - Verify containers exist: `#sidebar-container`, `#header-container` - Check browser console for errors - Verify file paths are correct ### API Calls Failing - Check that `ApiClient` is imported correctly - Verify endpoint URLs are correct - Check network tab for actual requests - Verify CORS settings if calling external APIs ### Components Not Working - Check that component scripts are imported - Verify component initialization code - Check browser console for errors - Ensure CSS is loaded ### Navigation Not Highlighting - Verify page name matches `data-page` attribute - Check that `LayoutManager.setActivePage()` is called - Verify page is registered in `PAGE_METADATA` --- ## Part 8: Quick Reference ### Required Imports for Every Page ```javascript import LayoutManager from '/static/shared/js/core/layout-manager.js'; await LayoutManager.init('page-name'); ``` ### Common Component Imports ```javascript import { ApiClient } from '/static/shared/js/core/api-client.js'; import { Toast } from '/static/shared/js/components/toast.js'; import { Loading } from '/static/shared/js/components/loading.js'; import { Modal } from '/static/shared/js/components/modal.js'; import { PollingManager } from '/static/shared/js/core/polling-manager.js'; ``` ### Required HTML Structure ```html