File size: 5,800 Bytes
8b7b267 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
/**
* Sidebar Manager - Handles collapse/expand and mobile behavior
*/
class SidebarManager {
constructor() {
this.sidebar = null;
this.toggleBtn = null;
this.overlay = null;
this.isCollapsed = false;
this.isMobile = window.innerWidth <= 1024;
this.init();
}
init() {
// Wait for DOM to be ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => this.setup());
} else {
this.setup();
}
}
setup() {
this.sidebar = document.getElementById('sidebar-modern') || document.querySelector('.sidebar-modern');
this.toggleBtn = document.getElementById('sidebar-collapse-btn');
this.overlay = document.getElementById('sidebar-overlay-modern') || document.querySelector('.sidebar-overlay-modern');
if (!this.sidebar) {
console.warn('Sidebar not found');
return;
}
// Load saved state
this.loadState();
// Setup event listeners
this.setupEventListeners();
// Handle responsive behavior
this.handleResize();
}
setupEventListeners() {
// Toggle button
if (this.toggleBtn) {
this.toggleBtn.addEventListener('click', () => this.toggle());
}
// Overlay click (mobile)
if (this.overlay) {
this.overlay.addEventListener('click', () => this.close());
}
// Resize handler
window.addEventListener('resize', () => this.handleResize());
// ESC key to close on mobile
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && this.isMobile && this.sidebar.classList.contains('open')) {
this.close();
}
});
// Close sidebar on nav link click (mobile only)
const navLinks = this.sidebar.querySelectorAll('.nav-link-modern');
navLinks.forEach(link => {
link.addEventListener('click', () => {
if (this.isMobile) {
this.close();
}
});
});
// Set active page
this.setActivePage();
}
toggle() {
if (this.isMobile) {
// On mobile, toggle open/close
this.sidebar.classList.toggle('open');
this.overlay?.classList.toggle('active');
} else {
// On desktop, toggle collapsed state
this.isCollapsed = !this.isCollapsed;
this.sidebar.classList.toggle('collapsed');
this.saveState();
// Dispatch event for other components
window.dispatchEvent(new CustomEvent('sidebar-toggle', {
detail: { collapsed: this.isCollapsed }
}));
}
}
open() {
if (this.isMobile) {
this.sidebar.classList.add('open');
this.overlay?.classList.add('active');
document.body.style.overflow = 'hidden';
}
}
close() {
if (this.isMobile) {
this.sidebar.classList.remove('open');
this.overlay?.classList.remove('active');
document.body.style.overflow = '';
}
}
collapse() {
if (!this.isMobile && !this.isCollapsed) {
this.isCollapsed = true;
this.sidebar.classList.add('collapsed');
this.saveState();
}
}
expand() {
if (!this.isMobile && this.isCollapsed) {
this.isCollapsed = false;
this.sidebar.classList.remove('collapsed');
this.saveState();
}
}
handleResize() {
const wasMobile = this.isMobile;
this.isMobile = window.innerWidth <= 1024;
// If switching from mobile to desktop or vice versa
if (wasMobile !== this.isMobile) {
// Clean up mobile state
if (!this.isMobile) {
this.sidebar.classList.remove('open');
this.overlay?.classList.remove('active');
document.body.style.overflow = '';
// Restore collapsed state on desktop
if (this.isCollapsed) {
this.sidebar.classList.add('collapsed');
}
} else {
// On mobile, remove collapsed state
this.sidebar.classList.remove('collapsed');
}
}
}
setActivePage() {
// Get current page from URL
const path = window.location.pathname;
const pageName = this.getPageNameFromPath(path);
if (!pageName) return;
// Remove active class from all links
const navLinks = this.sidebar.querySelectorAll('.nav-link-modern');
navLinks.forEach(link => {
link.classList.remove('active');
link.removeAttribute('aria-current');
});
// Add active class to current page link
const activeLink = this.sidebar.querySelector(`[data-page="${pageName}"]`);
if (activeLink) {
activeLink.classList.add('active');
activeLink.setAttribute('aria-current', 'page');
}
}
getPageNameFromPath(path) {
// Extract page name from path
// e.g., /static/pages/dashboard/index.html -> dashboard
const match = path.match(/\/pages\/([^\/]+)\//);
return match ? match[1] : null;
}
saveState() {
try {
localStorage.setItem('sidebar_collapsed', JSON.stringify(this.isCollapsed));
} catch (error) {
console.warn('Failed to save sidebar state:', error);
}
}
loadState() {
try {
const saved = localStorage.getItem('sidebar_collapsed');
if (saved !== null) {
this.isCollapsed = JSON.parse(saved);
if (this.isCollapsed && !this.isMobile) {
this.sidebar.classList.add('collapsed');
}
}
} catch (error) {
console.warn('Failed to load sidebar state:', error);
}
}
// Public API
getState() {
return {
isCollapsed: this.isCollapsed,
isMobile: this.isMobile,
isOpen: this.sidebar?.classList.contains('open') || false
};
}
}
// Initialize and export
const sidebarManager = new SidebarManager();
// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = sidebarManager;
}
export default sidebarManager;
|