|
153 | 153 |
|
154 | 154 | // Generate Portal |
155 | 155 | authWithViya(VIYA, SASLOGIN, PORTAL, SASPORTAL); |
| 156 | + |
| 157 | + // Function to activate a tab based on its ID |
| 158 | + function activateTab(tabId) { |
| 159 | + const tabButton = document.getElementById(tabId); |
| 160 | + if (tabButton) { |
| 161 | + const bsTab = new bootstrap.Tab(tabButton); |
| 162 | + bsTab.show(); |
| 163 | + history.pushState(null, '', '#' + tabId); |
| 164 | + } else { |
| 165 | + console.warn(`Tab button with ID '${tabId}' not found.`); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // This function encapsulates all initialization logic for tabs and deeplinks |
| 170 | + function initializeTabsAndDeeplinks() { |
| 171 | + let attempts = 0; |
| 172 | + const maxAttempts = 20; |
| 173 | + const intervalTime = 1000; |
| 174 | + |
| 175 | + const checkTabsInterval = setInterval(() => { |
| 176 | + const tabButtons = document.querySelectorAll('.nav-tabs .nav-link'); |
| 177 | + |
| 178 | + if (tabButtons.length > 0 && document.readyState === 'complete') { |
| 179 | + console.log(document.readyState); |
| 180 | + clearInterval(checkTabsInterval); |
| 181 | + const initialHash = window.location.hash.substring(1); |
| 182 | + if (initialHash) { |
| 183 | + activateTab(initialHash); |
| 184 | + } else { |
| 185 | + const activeTabButton = document.querySelector('.nav-tabs .nav-link.active'); |
| 186 | + if (activeTabButton) { |
| 187 | + history.replaceState(null, '', '#' + activeTabButton.id); |
| 188 | + } else { |
| 189 | + const firstTabButton = document.querySelector('.nav-tabs .nav-link'); |
| 190 | + if (firstTabButton) { |
| 191 | + activateTab(firstTabButton.id); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + tabButtons.forEach(button => { |
| 197 | + button.addEventListener('shown.bs.tab', function (event) { |
| 198 | + const newTabId = event.target.id; |
| 199 | + history.pushState(null, '', '#' + newTabId); |
| 200 | + }); |
| 201 | + }); |
| 202 | + } else { |
| 203 | + attempts++; |
| 204 | + if (attempts >= maxAttempts) { |
| 205 | + clearInterval(checkTabsInterval); |
| 206 | + console.error('Max attempts reached: Tab elements not found after', maxAttempts * intervalTime / 1000, 'seconds.'); |
| 207 | + } else { |
| 208 | + console.log(`Attempt ${attempts}: Waiting for tab elements...`); |
| 209 | + } |
| 210 | + } |
| 211 | + }, intervalTime); |
| 212 | + } |
| 213 | + |
| 214 | + if(document.readyState === 'loading') { |
| 215 | + document.addEventListener('DOMContentLoaded', initializeTabsAndDeeplinks); |
| 216 | + } else { |
| 217 | + initializeTabsAndDeeplinks(); |
| 218 | + } |
156 | 219 | </script> |
157 | 220 | </body> |
158 | 221 | </html> |
0 commit comments