overleaf/services/web/frontend/js/features/bookmarkable-tab/index.js
Jakob Ackermann ba460a6755 Merge pull request #5133 from overleaf/as-bookmarkable-tab
De-Angularise bookmarkable-tabset

GitOrigin-RevId: fc80f0c42fded9b0856dc7c62de2c87e66b5d85c
2021-09-22 08:03:35 +00:00

21 lines
581 B
JavaScript

function bookmarkableTab(tabEl) {
tabEl.addEventListener('click', () => {
window.location.hash = tabEl.getAttribute('href')
})
}
function handleHashChange() {
const hash = window.location.hash
if (!hash) return
// Find the bookmarkable tab that links to the hash
const $tabEl = $(`[data-ol-bookmarkable-tab][href="${hash}"]`)
if (!$tabEl) return
// Select the tab via Bootstrap
$tabEl.tab('show')
}
document.querySelectorAll('[data-ol-bookmarkable-tab]').forEach(bookmarkableTab)
window.addEventListener('hashchange', handleHashChange)
handleHashChange()