Skip to content

Commit 977236e

Browse files
committed
opencodelaw: merge ui js
Signed-off-by: kiranpranay <[email protected]>
1 parent 6dfbf6b commit 977236e

File tree

3 files changed

+68
-53
lines changed

3 files changed

+68
-53
lines changed

index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<!-- Include opencon.js -->
1212
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
1313
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.0.0/js-yaml.min.js"></script>
14-
<script src="js/opencon.js"></script>
15-
<script src="js/main.js"></script>
14+
<script async src="js/opencodelaw.js"></script>
1615
</body>
1716
</html>

js/main.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

js/opencon.js renamed to js/opencodelaw.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,73 @@ fetch("specs/opencon.yaml")
187187
}
188188
});
189189
})
190+
.then(() => {
191+
// ui script
192+
193+
const subMenus = document.querySelectorAll(".sub-menu");
194+
const navLinks = document.querySelectorAll(".nav-a");
195+
196+
console.log(subMenus);
197+
198+
// Function to toggle visibility of sub-menus
199+
function toggleSubMenu(event) {
200+
const subMenu = event.currentTarget.querySelector(".sub-nav");
201+
subMenu.classList.toggle("nav-hide");
202+
}
203+
204+
// Function to prevent sub-menu from closing on click inside
205+
function preventSubMenuClose(event) {
206+
event.stopPropagation();
207+
}
208+
209+
// Add event listeners to each sub-menu parent
210+
subMenus.forEach((subMenu) => {
211+
subMenu.addEventListener("click", toggleSubMenu);
212+
subMenu
213+
.querySelector(".nav-section")
214+
.addEventListener("click", preventSubMenuClose);
215+
});
216+
217+
// Highlight the active menu based on content scroll
218+
function updateActiveMenu() {
219+
const currentPosition = window.scrollY;
220+
navLinks.forEach((link) => {
221+
const section = document.querySelector(link.getAttribute("href"));
222+
if (
223+
section.offsetTop <= currentPosition + 100 && // Adding an offset to improve accuracy
224+
section.offsetTop + section.offsetHeight > currentPosition + 100
225+
) {
226+
link.classList.add("active");
227+
} else {
228+
link.classList.remove("active");
229+
}
230+
});
231+
}
232+
233+
// Update "active" class when a menu item is clicked
234+
navLinks.forEach((link) => {
235+
link.addEventListener("click", (event) => {
236+
navLinks.forEach((link) => link.classList.remove("active"));
237+
link.classList.add("active");
238+
const target = document.querySelector(link.getAttribute("href"));
239+
240+
// Scroll to the target element with smooth scrolling
241+
target.scrollIntoView({ behavior: "smooth" });
242+
243+
// Close open sub-menus when a menu item is clicked
244+
subMenus.forEach((subMenu) => {
245+
const subNav = subMenu.querySelector(".nav-section");
246+
if (!subNav.contains(target)) {
247+
subMenu.querySelector(".sub-nav").classList.add("nav-hide");
248+
}
249+
});
250+
});
251+
});
252+
253+
// Update active menu on scroll
254+
window.addEventListener("scroll", updateActiveMenu);
255+
updateActiveMenu(); // Initial update on page load
256+
})
190257
.catch((error) => {
191258
console.error("Error loading YAML data:", error);
192259
});

0 commit comments

Comments
 (0)