From f2fe9efbf8427add1856ac3d9855267ff76a0ef1 Mon Sep 17 00:00:00 2001 From: sokumon Date: Sun, 11 Jan 2026 15:39:47 +0530 Subject: [PATCH] fix: show correct active element in sidebar --- frappe/public/js/frappe/ui/sidebar/sidebar.js | 19 ++++++++++++------- frappe/public/js/frappe/utils/utils.js | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/frappe/public/js/frappe/ui/sidebar/sidebar.js b/frappe/public/js/frappe/ui/sidebar/sidebar.js index a0f37c5657..4dbee57dbb 100644 --- a/frappe/public/js/frappe/ui/sidebar/sidebar.js +++ b/frappe/public/js/frappe/ui/sidebar/sidebar.js @@ -162,11 +162,16 @@ frappe.ui.Sidebar = class Sidebar { let match = false; const that = this; $(".item-anchor").each(function () { - let href = decodeURIComponent($(this).attr("href")?.split("?")[0]); + let href = decodeURIComponent($(this).attr("href")?.split("?")[0].split("#")[0]); + const path = decodeURIComponent(window.location.pathname); - // Match only if path equals href or starts with it followed by "/" or end of string - const isActive = href === path; + // ensure no trailing slash mismatch + const clean_href = href.replace(/\/$/, ""); + const clean_path = path.replace(/\/$/, ""); + + const isActive = clean_path === clean_href || clean_path.startsWith(clean_href + "/"); + if (href && isActive) { match = true; if (that.active_item) that.active_item.removeClass("active-sidebar"); @@ -423,7 +428,7 @@ frappe.ui.Sidebar = class Sidebar { default: entity_name = route[1]; } - let sidebars = this.get_correct_workspace_sidebars(entity_name); + let sidebars = this.get_workspace_sidebars(entity_name); this.preffered_sidebars = sidebars; let module = router?.meta?.module; if (this.sidebar_title && sidebars.includes(this.sidebar_title)) { @@ -497,9 +502,9 @@ frappe.ui.Sidebar = class Sidebar { if (matches) return; let workspace_title; if (route.length == 2) { - workspace_title = this.get_correct_workspace_sidebars(route[1]); + workspace_title = this.get_workspace_sidebars(route[1]); } else { - workspace_title = this.get_correct_workspace_sidebars(route[0]); + workspace_title = this.get_workspace_sidebars(route[0]); } let module_name = workspace_title[0]; if (module_name) { @@ -507,7 +512,7 @@ frappe.ui.Sidebar = class Sidebar { } } - get_correct_workspace_sidebars(link_to) { + get_workspace_sidebars(link_to) { let sidebars = []; Object.entries(this.all_sidebar_items).forEach(([name, sidebar]) => { const { items, label } = sidebar; diff --git a/frappe/public/js/frappe/utils/utils.js b/frappe/public/js/frappe/utils/utils.js index 1a1f5dfe58..1263740ef2 100644 --- a/frappe/public/js/frappe/utils/utils.js +++ b/frappe/public/js/frappe/utils/utils.js @@ -1472,7 +1472,7 @@ Object.assign(frappe.utils, { let doctype_slug = frappe.router.slug(item.doctype); if (frappe.model.is_single(item.doctype)) { - route = doctype_slug; + route = `${doctype_slug}/${item.doctype}`; } else { switch (item.doc_view) { case "List":