Get rid of jQuery for active menu selection

This commit is contained in:
emeric
2023-01-06 20:48:59 +01:00
parent 467a53f234
commit 25c36f0a3e
+15 -2
View File
@@ -337,7 +337,6 @@ handlePathChange(Wt::WStackedWidget& stack, bool isAdmin)
LMS_LOG(UI, DEBUG) << "Internal path changed to '" << wApp->internalPath() << "'"; 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) for (const auto& view : views)
{ {
if (wApp->internalPathMatches(view.path)) if (wApp->internalPathMatches(view.path))
@@ -348,6 +347,8 @@ handlePathChange(Wt::WStackedWidget& stack, bool isAdmin)
stack.setCurrentIndex(view.index); stack.setCurrentIndex(view.index);
if (view.title) if (view.title)
LmsApp->setTitle(*view.title); LmsApp->setTitle(*view.title);
LmsApp->doJavaScript(LmsApp->javaScriptClass() + ".updateActiveNav('" + view.path +"')");
return; return;
} }
} }
@@ -396,9 +397,21 @@ LmsApplication::createHome()
_coverResource = std::make_shared<CoverResource>(); _coverResource = std::make_shared<CoverResource>();
declareJavaScriptFunction("onLoadCover", "function(id) { id.className += \" Lms-cover-loaded\"}"); 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::WTemplate>(Wt::WString::tr("Lms.main.template")))}; Wt::WTemplate* main {root()->addWidget(std::make_unique<Wt::WTemplate>(Wt::WString::tr("Lms.main.template")))};
main->addFunction("tr", &Wt::WTemplate::Functions::tr); main->addFunction("tr", &Wt::WTemplate::Functions::tr);
Template* navbar {main->bindNew<Template>("navbar", Wt::WString::tr("Lms.main.template.navbar"))}; Template* navbar {main->bindNew<Template>("navbar", Wt::WString::tr("Lms.main.template.navbar"))};