From 25c36f0a3ea9eac74a53ef1f3f19a816328f4556 Mon Sep 17 00:00:00 2001 From: emeric Date: Fri, 6 Jan 2023 20:48:59 +0100 Subject: [PATCH] Get rid of jQuery for active menu selection --- src/lms/ui/LmsApplication.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lms/ui/LmsApplication.cpp b/src/lms/ui/LmsApplication.cpp index 5437ef94..9aaf02af 100644 --- a/src/lms/ui/LmsApplication.cpp +++ b/src/lms/ui/LmsApplication.cpp @@ -337,7 +337,6 @@ handlePathChange(Wt::WStackedWidget& stack, bool isAdmin) LMS_LOG(UI, DEBUG) << "Internal path changed to '" << wApp->internalPath() << "'"; - LmsApp->doJavaScript(R"($('.navbar-nav a.active').removeClass('active'); $('.navbar-nav a[href="' + location.pathname + '"]').closest('a').addClass('active');)"); for (const auto& view : views) { if (wApp->internalPathMatches(view.path)) @@ -348,6 +347,8 @@ handlePathChange(Wt::WStackedWidget& stack, bool isAdmin) stack.setCurrentIndex(view.index); if (view.title) LmsApp->setTitle(*view.title); + + LmsApp->doJavaScript(LmsApp->javaScriptClass() + ".updateActiveNav('" + view.path +"')"); return; } } @@ -396,9 +397,21 @@ LmsApplication::createHome() _coverResource = std::make_shared(); declareJavaScriptFunction("onLoadCover", "function(id) { id.className += \" Lms-cover-loaded\"}"); + declareJavaScriptFunction("updateActiveNav", +R"(function(current) { + const menuItems = document.querySelectorAll('.nav-item a[href]:not([href=""])'); + for (const menuItem of menuItems) { + if (menuItem.getAttribute("href").indexOf(current) !== -1) { + menuItem.classList.add('active'); + } + else { + menuItem.classList.remove('active'); + } + + } +})"); Wt::WTemplate* main {root()->addWidget(std::make_unique(Wt::WString::tr("Lms.main.template")))}; - main->addFunction("tr", &Wt::WTemplate::Functions::tr); Template* navbar {main->bindNew