Skip to content

Commit f39e176

Browse files
committed
Display active tab in URL hash #101
And allow to activate an alternative default tab from URL Signed-off-by: Thomas Druez <[email protected]>
1 parent 1bca5b1 commit f39e176

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

scancodeio/static/main.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,32 @@ function setupCloseModalButtons() {
6363
function setupTabs() {
6464
const $tabLinks = getAll('.tabs a');
6565

66+
function activateTab($tabLink) {
67+
const activeLink = document.querySelector('.tabs .is-active');
68+
const activeTabContent = document.querySelector('.tab-content.is-active');
69+
const targetId = $tabLink.dataset.target;
70+
const targetTabContent = document.getElementById(targetId);
71+
72+
activeLink.classList.remove('is-active');
73+
$tabLink.parentNode.classList.add('is-active');
74+
if (activeTabContent) activeTabContent.classList.remove('is-active');
75+
if (targetTabContent) targetTabContent.classList.add('is-active');
76+
77+
// Set the active tab in the URL hash. The "tab-" prefix is removed to avoid
78+
// un-wanted scrolling to the related "id" element
79+
document.location.hash = targetId.replace('tab-', '');
80+
}
81+
82+
// Activate the related tab if hash is present in URL
83+
if (document.location.hash !== "") {
84+
let tabName = document.location.hash.slice(1);
85+
let tabLink = document.querySelector(`a[data-target="tab-${tabName}"]`);
86+
if (tabLink) activateTab(tabLink);
87+
}
88+
6689
$tabLinks.forEach(function ($el) {
67-
$el.addEventListener('click', function (event) {
68-
const activeLink = document.querySelector('.tabs .is-active');
69-
const activeTabContent = document.querySelector('.tab-content.is-active');
70-
const target_id = $el.dataset.target;
71-
const targetTabContent = document.getElementById(target_id);
72-
73-
activeLink.classList.remove('is-active');
74-
$el.parentNode.classList.add('is-active');
75-
if (activeTabContent) activeTabContent.classList.remove('is-active');
76-
if (targetTabContent) targetTabContent.classList.add('is-active');
90+
$el.addEventListener('click', function () {
91+
activateTab($el)
7792
});
7893
});
7994
}

0 commit comments

Comments
 (0)