From 75855b1ddaae181592c0e4dd679a604ad38033c7 Mon Sep 17 00:00:00 2001 From: sokumon Date: Thu, 4 Dec 2025 02:49:46 +0530 Subject: [PATCH] feat: add support for condition in menu --- frappe/public/js/frappe/ui/menu.js | 9 ++++- .../js/frappe/ui/sidebar/sidebar_header.js | 36 +++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/frappe/public/js/frappe/ui/menu.js b/frappe/public/js/frappe/ui/menu.js index 93d846feaf..330095832f 100644 --- a/frappe/public/js/frappe/ui/menu.js +++ b/frappe/public/js/frappe/ui/menu.js @@ -14,7 +14,14 @@ frappe.ui.menu = class ContextMenu { this.template.empty(); this.menu_items.forEach((f) => { - this.add_menu_item(f); + f.condition = + f.condition || + function () { + return true; + }; + if (f.condition()) { + this.add_menu_item(f); + } }); // if (!$.contains(document.body, this.template[0])) { diff --git a/frappe/public/js/frappe/ui/sidebar/sidebar_header.js b/frappe/public/js/frappe/ui/sidebar/sidebar_header.js index ae19db7ef6..84f0b5d3ed 100644 --- a/frappe/public/js/frappe/ui/sidebar/sidebar_header.js +++ b/frappe/public/js/frappe/ui/sidebar/sidebar_header.js @@ -5,13 +5,16 @@ frappe.ui.SidebarHeader = class SidebarHeader { this.drop_down_expanded = false; this.title = this.sidebar.sidebar_title; const me = this; - this.fetch; + this.sibling_workspaces = this.fetch_sibling_workspaces(); this.dropdown_items = [ { name: "workspaces", label: "Workspaces", icon: "wallpaper", - items: this.fetch_sibling_workspaces(), + condition: function () { + return me.sibling_workspaces.length > 0; + }, + items: this.sibling_workspaces, }, { name: "desktop", @@ -46,18 +49,23 @@ frappe.ui.SidebarHeader = class SidebarHeader { } fetch_sibling_workspaces() { let sibling_workspaces = []; - let workspaces = frappe.current_app.workspaces; - workspaces.splice(workspaces.indexOf(this.title), 1); - workspaces.forEach((w) => { - let item = { - name: w.toLowerCase(), - label: w, - icon: "wallpaper", - url: frappe.utils.generate_route({ type: "Workspace", route: w.toLowerCase() }), - }; - sibling_workspaces.push(item); - }); - return sibling_workspaces; + if (frappe.current_app) { + let workspaces = [...frappe.current_app.workspaces]; + workspaces.splice(workspaces.indexOf(this.title), 1); + workspaces.forEach((w) => { + let item = { + name: w.toLowerCase(), + label: w, + icon: "wallpaper", + url: frappe.utils.generate_route({ + type: "Workspace", + route: w.toLowerCase(), + }), + }; + sibling_workspaces.push(item); + }); + return sibling_workspaces; + } } make() { $(".sidebar-header").remove();