Skip to content

Commit a83161a

Browse files
committed
rebased
1 parent 1f39290 commit a83161a

File tree

1 file changed

+33
-58
lines changed

1 file changed

+33
-58
lines changed

frontend/javascript/page_navigation.js

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,25 @@ export function enableScrollToTop() {
1919
export function setupSidebar() {
2020
const h1Elements = document.querySelectorAll('h1');
2121
const h2Elements = document.querySelectorAll('article h2');
22-
if (h2Elements.length === 0) {
23-
// If there are no h2 headings, do not set up the sidebar
24-
return;
25-
}
26-
const sidebar = document.querySelector('.sidebar');
27-
function appendSidebarItem(textContent, tagName) {
28-
const newItem = document.createElement('a');
29-
newItem.textContent = textContent;
30-
if (tagName === 'h2') {
31-
newItem.classList.add('sidebar-item-h2', 'text-gray-500', 'hover:text-green-400', 'ml-4', 'cursor-pointer');
32-
}
33-
sidebar.appendChild(newItem);
3422

35-
newItem.addEventListener('click', function() {
36-
const allSidebarItems = document.querySelectorAll('.sidebar a');
37-
allSidebarItems.forEach(function(item) {
38-
item.style.color = ''; // Reset color
39-
item.style.fontWeight='';
40-
});
41-
42-
// Change color of clicked item
43-
newItem.style.color = '#34D399';
44-
newItem.style.fontWeight='bold';
45-
46-
const currentVersion = newItem.textContent;
47-
const headings = document.querySelectorAll('h2');
48-
let targetElement = null;
49-
headings.forEach(function(heading) {
50-
if (heading.textContent.trim() === currentVersion.trim()) {
51-
targetElement = heading;
52-
}
53-
});
54-
if (targetElement) {
55-
window.scrollTo(0, targetElement.offsetTop - 100);
56-
}
57-
});
23+
if (h2Elements.length == 0) {
24+
return;
5825
}
5926

60-
const sidebarHeading = document.createElement('div');
61-
sidebarHeading.textContent = 'ON THIS PAGE';
62-
sidebarHeading.classList.add('sidebar-heading');
63-
sidebarHeading.style.color = '#34D399';
64-
sidebarHeading.style.fontSize = '15px';
65-
sidebarHeading.style.fontWeight='bold';
66-
sidebarHeading.style.paddingBottom='15px';
67-
sidebar.appendChild(sidebarHeading);
27+
h1Elements.forEach(function (element) {
28+
appendSidebarItem(element.textContent, 'h1');
29+
});
6830

6931
h2Elements.forEach(function (element) {
7032
appendSidebarItem(element.textContent, 'h2');
7133
});
72-
34+
7335
const h2Items = document.querySelectorAll('.sidebar-item-h2');
7436
h2Items.forEach(item => {
75-
item.style.paddingTop = '5px';
37+
item.style.paddingTop = '10px';
7638
});
7739
}
7840

79-
export function saveAndRestoreNavigationPosition() {
80-
var scrollPosition = sessionStorage.getItem('scrollPosition');
81-
if (scrollPosition !== undefined) {
82-
document.querySelector('#navigation-sidebar').scrollTop = parseInt(scrollPosition);
83-
}
84-
document.querySelector('#navigation-sidebar').addEventListener('click', function() {
85-
var scrollPosition = document.querySelector('#navigation-sidebar').scrollTop;
86-
sessionStorage.setItem('scrollPosition', scrollPosition);
87-
console.log('clicked')
88-
});
89-
};
90-
9141
function appendSidebarItem(textContent, tagName) {
9242
const sidebar = document.querySelector('.sidebar');
9343
const newItem = document.createElement('a');
@@ -101,6 +51,19 @@ function appendSidebarItem(textContent, tagName) {
10151
sidebar.appendChild(newItem);
10252

10353
newItem.addEventListener('click', function() {
54+
const allSidebarItems = document.querySelectorAll('.sidebar a');
55+
allSidebarItems.forEach(function(item) {
56+
item.style.color = ''; // Reset color
57+
item.style.fontWeight='';
58+
});
59+
60+
// Change color of clicked item
61+
if(tagName=='h2'){
62+
newItem.style.color = '#34D399';
63+
newItem.style.fontWeight='bold';
64+
}
65+
66+
10467
const currentVersion = newItem.textContent;
10568
const headings = document.querySelectorAll('h2');
10669
let targetElement = null;
@@ -112,5 +75,17 @@ function appendSidebarItem(textContent, tagName) {
11275
if (targetElement) {
11376
window.scrollTo(0, targetElement.offsetTop - 100);
11477
}
115-
});
78+
});
11679
}
80+
81+
export function saveAndRestoreNavigationPosition() {
82+
var scrollPosition = sessionStorage.getItem('scrollPosition');
83+
if (scrollPosition !== undefined) {
84+
document.querySelector('#navigation-sidebar').scrollTop = parseInt(scrollPosition);
85+
}
86+
document.querySelector('#navigation-sidebar').addEventListener('click', function() {
87+
var scrollPosition = document.querySelector('#navigation-sidebar').scrollTop;
88+
sessionStorage.setItem('scrollPosition', scrollPosition);
89+
console.log('clicked')
90+
});
91+
};

0 commit comments

Comments
 (0)