From e2ff41dc89bfc1877cd7425c387808ceea7432d6 Mon Sep 17 00:00:00 2001 From: sokumon Date: Tue, 9 Dec 2025 01:59:47 +0530 Subject: [PATCH] fix: enhance the sidebar which is generated --- .../auto_generate_sidebar_from_modules.py | 54 +++++++++++++++---- frappe/public/js/frappe/ui/sidebar/sidebar.js | 4 +- .../js/frappe/ui/sidebar/sidebar_item.js | 4 +- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/frappe/patches/v16_0/auto_generate_sidebar_from_modules.py b/frappe/patches/v16_0/auto_generate_sidebar_from_modules.py index 008c7a1b6b..3f78ffeb86 100644 --- a/frappe/patches/v16_0/auto_generate_sidebar_from_modules.py +++ b/frappe/patches/v16_0/auto_generate_sidebar_from_modules.py @@ -18,7 +18,6 @@ def execute(): sidebar.header_icon = "hammer" sidebars.append(sidebar) sidebar.save() - return sidebars def get_module_info(module_name): @@ -32,30 +31,63 @@ def get_module_info(module_name): filters.append({"istable": 0}) module_info[entity] = frappe.get_all(entity, filters=filters, pluck="name") + # if module info has no workspaces, then move doctypes to the front + if not module_info.get("Workspace"): + module_info = { + "DocType": module_info.get("DocType"), + "Workspace": module_info.get("Workspace"), + "Report": module_info.get("Report"), + "Dashboard": module_info.get("Dashboard"), + "Page": module_info.get("Page"), + } return module_info def create_sidebar_items(module_info): sidebar_items = [] + idx = 1 + + section_entities = {"report": "Reports", "dashboard": "Dashboards", "page": "Pages"} + for entity, items in module_info.items(): - if entity.lower() == "report": - section_break = frappe.new_doc("Workspace Sidebar Item") - section_break.update( - { - "type": "Section Break", - } - ) + section_break_added = False + entity_lower = entity.lower() + + if entity_lower in section_entities: + if entity_lower == "report": + section_break = add_section_breaks("Reports", idx) + elif entity_lower in ("dashboard", "page") and len(items) > 1: + section_break = add_section_breaks(section_entities[entity_lower], idx) + section_break_added = True sidebar_items.append(section_break) + idx += 1 + for item in items: - item_info = {"label": item, "type": "Link", "link_type": entity, "link_to": item} - if entity.lower() == "workspace": + print(entity, item) + item_info = {"label": item, "type": "Link", "link_type": entity, "link_to": item, "idx": idx} + + if entity_lower == "report": + item_info["child"] = 1 + + if entity_lower == "workspace": item_info["icon"] = "home" - if entity.lower() == "doctype" and "settings" in item.lower(): + if entity_lower == "doctype" and "settings" in item.lower(): item_info["icon"] = "settings" + if section_break_added: + item_info["child"] = 1 + sidebar_item = frappe.new_doc("Workspace Sidebar Item") sidebar_item.update(item_info) sidebar_items.append(sidebar_item) + idx += 1 + return sidebar_items + + +def add_section_breaks(label, idx): + section_break = frappe.new_doc("Workspace Sidebar Item") + section_break.update({"label": label, "type": "Section Break", "idx": idx}) + return section_break diff --git a/frappe/public/js/frappe/ui/sidebar/sidebar.js b/frappe/public/js/frappe/ui/sidebar/sidebar.js index a101dfddf4..5d7106de6f 100644 --- a/frappe/public/js/frappe/ui/sidebar/sidebar.js +++ b/frappe/public/js/frappe/ui/sidebar/sidebar.js @@ -161,11 +161,11 @@ frappe.ui.Sidebar = class Sidebar { let match = false; const that = this; $(".item-anchor").each(function () { - let href = $(this).attr("href")?.split("?")[0]; + let href = decodeURIComponent($(this).attr("href")?.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 = new RegExp(`^${href}(?:/|$)`).test(path); + const isActive = href === path; if (href && isActive) { match = true; if (that.active_item) that.active_item.removeClass("active-sidebar"); diff --git a/frappe/public/js/frappe/ui/sidebar/sidebar_item.js b/frappe/public/js/frappe/ui/sidebar/sidebar_item.js index bb88ec5f4a..d67d435fba 100644 --- a/frappe/public/js/frappe/ui/sidebar/sidebar_item.js +++ b/frappe/public/js/frappe/ui/sidebar/sidebar_item.js @@ -54,7 +54,9 @@ frappe.ui.sidebar_item.TypeLink = class SidebarItem { }); } } - return path; + if (path) { + return encodeURI(path); + } } prepare() {} make() {