|
| 1 | +// Sidebar collapse functionality for Cyclus documentation |
| 2 | +(function() { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + const COLLAPSE_THRESHOLD = 0.55; // Auto-collapse when window width <= 55% of screen width |
| 6 | + const STORAGE_KEY = 'cyclus-sidebar-collapsed'; |
| 7 | + |
| 8 | + let sidebar = null; |
| 9 | + let toggleButton = null; |
| 10 | + |
| 11 | + function init() { |
| 12 | + if (document.readyState === 'loading') { |
| 13 | + document.addEventListener('DOMContentLoaded', setupSidebarToggle); |
| 14 | + } else { |
| 15 | + setupSidebarToggle(); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + function setupSidebarToggle() { |
| 20 | + // Find the sidebar element |
| 21 | + sidebar = document.querySelector('.sphinxsidebar'); |
| 22 | + if (!sidebar) { |
| 23 | + console.warn('Sidebar element not found'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Create toggle button |
| 28 | + toggleButton = document.createElement('button'); |
| 29 | + toggleButton.id = 'sidebar-toggle-btn'; |
| 30 | + toggleButton.className = 'sidebar-toggle-button'; |
| 31 | + toggleButton.setAttribute('aria-label', 'Toggle sidebar'); |
| 32 | + toggleButton.setAttribute('title', 'Collapse sidebar'); |
| 33 | + toggleButton.innerHTML = '◀'; |
| 34 | + |
| 35 | + // Initially place button in sidebar (top-right) |
| 36 | + sidebar.style.position = 'relative'; |
| 37 | + sidebar.appendChild(toggleButton); |
| 38 | + |
| 39 | + // Check initial state |
| 40 | + const wasCollapsed = localStorage.getItem(STORAGE_KEY) === 'true'; |
| 41 | + const shouldAutoCollapse = checkAutoCollapse(); |
| 42 | + |
| 43 | + if (wasCollapsed || shouldAutoCollapse) { |
| 44 | + collapseSidebar(true); |
| 45 | + } |
| 46 | + |
| 47 | + // Button click handler |
| 48 | + toggleButton.addEventListener('click', function() { |
| 49 | + const isCollapsed = sidebar.style.display === 'none'; |
| 50 | + if (isCollapsed) { |
| 51 | + expandSidebar(); |
| 52 | + } else { |
| 53 | + collapseSidebar(false); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + // Window resize handler for auto-collapse and button repositioning |
| 58 | + let resizeTimeout; |
| 59 | + window.addEventListener('resize', function() { |
| 60 | + clearTimeout(resizeTimeout); |
| 61 | + resizeTimeout = setTimeout(function() { |
| 62 | + const shouldCollapse = checkAutoCollapse(); |
| 63 | + const isCollapsed = sidebar.style.display === 'none'; |
| 64 | + |
| 65 | + // If sidebar is collapsed, update button position and bodywrapper width |
| 66 | + if (isCollapsed && toggleButton.classList.contains('sidebar-toggle-collapsed')) { |
| 67 | + // Recalculate bodywrapper width to match navbar |
| 68 | + const bodyWrapper = document.querySelector('.bodywrapper'); |
| 69 | + if (bodyWrapper) { |
| 70 | + const relatedNav = document.querySelector('.related'); |
| 71 | + if (relatedNav) { |
| 72 | + const navRect = relatedNav.getBoundingClientRect(); |
| 73 | + const docWrapper = document.querySelector('.documentwrapper'); |
| 74 | + const docLeft = docWrapper ? docWrapper.getBoundingClientRect().left : 0; |
| 75 | + |
| 76 | + const relativeNavLeft = navRect.left - docLeft; |
| 77 | + const navWidth = navRect.right - navRect.left; |
| 78 | + |
| 79 | + bodyWrapper.style.marginLeft = relativeNavLeft + 'px'; |
| 80 | + bodyWrapper.style.width = navWidth + 'px'; |
| 81 | + bodyWrapper.offsetHeight; // Force reflow |
| 82 | + } |
| 83 | + } |
| 84 | + updateCollapsedButtonPosition(); |
| 85 | + } |
| 86 | + |
| 87 | + if (shouldCollapse && !isCollapsed) { |
| 88 | + collapseSidebar(true); |
| 89 | + } else if (!shouldCollapse && isCollapsed && localStorage.getItem(STORAGE_KEY) !== 'true') { |
| 90 | + expandSidebar(); |
| 91 | + } |
| 92 | + }, 150); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + function checkAutoCollapse() { |
| 97 | + const windowWidth = window.innerWidth || document.documentElement.clientWidth; |
| 98 | + const screenWidth = screen.width; |
| 99 | + return windowWidth <= (screenWidth * COLLAPSE_THRESHOLD); |
| 100 | + } |
| 101 | + |
| 102 | + function updateCollapsedButtonPosition(targetTop) { |
| 103 | + // If targetTop not provided, maintain current vertical position |
| 104 | + if (targetTop === undefined) { |
| 105 | + const rect = toggleButton.getBoundingClientRect(); |
| 106 | + targetTop = rect.top; |
| 107 | + |
| 108 | + // Ensure button is below navbar |
| 109 | + const relatedNav = document.querySelector('.related'); |
| 110 | + if (relatedNav) { |
| 111 | + const navBottom = relatedNav.getBoundingClientRect().bottom; |
| 112 | + if (targetTop < navBottom + 10) { |
| 113 | + targetTop = navBottom + 10; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Get bodywrapper's current position |
| 119 | + const bodyWrapper = document.querySelector('.bodywrapper'); |
| 120 | + let targetLeft = '10px'; |
| 121 | + if (bodyWrapper) { |
| 122 | + // Force layout recalculation to get updated position |
| 123 | + bodyWrapper.offsetHeight; |
| 124 | + const bodyRect = bodyWrapper.getBoundingClientRect(); |
| 125 | + targetLeft = (bodyRect.left + 10) + 'px'; // 10px padding from left edge |
| 126 | + } |
| 127 | + |
| 128 | + // Disable transition for instant positioning |
| 129 | + toggleButton.style.transition = 'none'; |
| 130 | + toggleButton.style.top = targetTop + 'px'; |
| 131 | + toggleButton.style.left = targetLeft; |
| 132 | + |
| 133 | + // Re-enable transition after a brief moment (for hover effects) |
| 134 | + setTimeout(function() { |
| 135 | + toggleButton.style.transition = ''; |
| 136 | + }, 10); |
| 137 | + } |
| 138 | + |
| 139 | + function collapseSidebar(isAutoCollapse) { |
| 140 | + if (!sidebar || !toggleButton) return; |
| 141 | + |
| 142 | + // Hide sidebar |
| 143 | + sidebar.style.display = 'none'; |
| 144 | + |
| 145 | + // Move button to body first |
| 146 | + if (sidebar.contains(toggleButton)) { |
| 147 | + document.body.appendChild(toggleButton); |
| 148 | + } |
| 149 | + |
| 150 | + toggleButton.classList.add('sidebar-toggle-collapsed'); |
| 151 | + toggleButton.style.position = 'fixed'; |
| 152 | + |
| 153 | + // Adjust bodywrapper to match navbar width FIRST |
| 154 | + const bodyWrapper = document.querySelector('.bodywrapper'); |
| 155 | + if (bodyWrapper) { |
| 156 | + const relatedNav = document.querySelector('.related'); |
| 157 | + if (relatedNav) { |
| 158 | + const navRect = relatedNav.getBoundingClientRect(); |
| 159 | + const navStyles = window.getComputedStyle(relatedNav); |
| 160 | + const docWrapper = document.querySelector('.documentwrapper'); |
| 161 | + const docLeft = docWrapper ? docWrapper.getBoundingClientRect().left : 0; |
| 162 | + |
| 163 | + // Calculate relative position |
| 164 | + const relativeNavLeft = navRect.left - docLeft; |
| 165 | + const navWidth = navRect.right - navRect.left; |
| 166 | + |
| 167 | + bodyWrapper.style.marginLeft = relativeNavLeft + 'px'; |
| 168 | + bodyWrapper.style.width = navWidth + 'px'; |
| 169 | + bodyWrapper.style.backgroundColor = 'white'; |
| 170 | + // No padding needed - button is fixed and won't scroll with content |
| 171 | + bodyWrapper.classList.add('sidebar-collapsed-body'); |
| 172 | + |
| 173 | + // Force layout recalculation |
| 174 | + bodyWrapper.offsetHeight; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // NOW position button AFTER bodywrapper has been repositioned |
| 179 | + // Get button's current vertical position |
| 180 | + const rect = toggleButton.getBoundingClientRect(); |
| 181 | + let targetTop = rect.top; |
| 182 | + |
| 183 | + // Ensure button is below navbar |
| 184 | + const relatedNav = document.querySelector('.related'); |
| 185 | + if (relatedNav) { |
| 186 | + const navBottom = relatedNav.getBoundingClientRect().bottom; |
| 187 | + if (targetTop < navBottom + 10) { |
| 188 | + targetTop = navBottom + 10; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // Position button at bodywrapper's left edge with a bit of breathing room |
| 193 | + // (Called after bodywrapper is repositioned) |
| 194 | + updateCollapsedButtonPosition(targetTop); |
| 195 | + |
| 196 | + toggleButton.innerHTML = '▶'; |
| 197 | + toggleButton.setAttribute('title', 'Expand sidebar'); |
| 198 | + |
| 199 | + // Store state |
| 200 | + if (!isAutoCollapse) { |
| 201 | + localStorage.setItem(STORAGE_KEY, 'true'); |
| 202 | + } |
| 203 | + |
| 204 | + document.body.classList.add('sidebar-collapsed-body'); |
| 205 | + } |
| 206 | + |
| 207 | + function expandSidebar() { |
| 208 | + if (!sidebar || !toggleButton) return; |
| 209 | + |
| 210 | + // Show sidebar |
| 211 | + sidebar.style.display = ''; |
| 212 | + |
| 213 | + // Move button back to sidebar |
| 214 | + if (document.body.contains(toggleButton)) { |
| 215 | + sidebar.appendChild(toggleButton); |
| 216 | + } |
| 217 | + |
| 218 | + toggleButton.classList.remove('sidebar-toggle-collapsed'); |
| 219 | + toggleButton.style.position = 'absolute'; |
| 220 | + |
| 221 | + // Disable transition for instant positioning |
| 222 | + toggleButton.style.transition = 'none'; |
| 223 | + toggleButton.style.top = '10px'; |
| 224 | + toggleButton.style.right = '10px'; |
| 225 | + toggleButton.style.left = 'auto'; |
| 226 | + toggleButton.innerHTML = '◀'; |
| 227 | + toggleButton.setAttribute('title', 'Collapse sidebar'); |
| 228 | + |
| 229 | + // Re-enable transition after a brief moment (for hover effects) |
| 230 | + setTimeout(function() { |
| 231 | + toggleButton.style.transition = ''; |
| 232 | + }, 10); |
| 233 | + |
| 234 | + // Restore bodywrapper |
| 235 | + const bodyWrapper = document.querySelector('.bodywrapper'); |
| 236 | + if (bodyWrapper) { |
| 237 | + bodyWrapper.style.marginLeft = ''; |
| 238 | + bodyWrapper.style.width = ''; |
| 239 | + bodyWrapper.style.backgroundColor = ''; |
| 240 | + bodyWrapper.classList.remove('sidebar-collapsed-body'); |
| 241 | + } |
| 242 | + |
| 243 | + // Clear state |
| 244 | + localStorage.removeItem(STORAGE_KEY); |
| 245 | + document.body.classList.remove('sidebar-collapsed-body'); |
| 246 | + } |
| 247 | + |
| 248 | + init(); |
| 249 | +})(); |
| 250 | + |
0 commit comments