Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Document Submenu architecture as pattern

Henriette Darge55dd4b5faef5
diff --git a/lookbook/docs/patterns/07-menu-and-navigation.md.erb b/lookbook/docs/patterns/07-menu-and-navigation.md.erb
new file mode 100644
index 00000000000..ae7d104f3f9
--- /dev/null
+++ b/lookbook/docs/patterns/07-menu-and-navigation.md.erb
@@ -0,0 +1,134 @@
+The left-hand menu has not yet been primerised and is part of ongoing conceptual discussions. There are however some patterns worth noting down.
+
+## Query-based sub menu
+
+For modules, that have a query-based sub menu, we introduced a component called `OpenProject::Common::SubmenuComponent`. For technical details, have a look at the [component preview](../../inspect/OpenProject/Common/submenu/default).
+
+<%= embed OpenProject::Common::SubmenuComponentPreview, :default, panels: %i[source] %>
+
+Modules that use this components are (amongst others):
+
+* WorkPackages
+* Team planners
+* Calendars
+* BIM
+* Gantt
+
+Please note, that this component is (as of now) not used for navigational submenus like in the project settings or the administration menu.
+
+### Data structure
+
+A sub menu consists of a set of global items, as well as collapsible sections with child elements. In order to enforce that structure we defined two Data structures:
+
+* `OpenProject::Menu::MenuGroup`: A group of items, with an optional header.
+```ruby
+MenuGroup = Data.define(:header, :children)
+```
+
+* `OpenProject::Menu::MenuItem`: A concrete Item with all options it supports
+```ruby
+MenuItem = Data.define(:title, :href, :selected, :favored, :icon, :count, :show_enterprise_icon) do
+ def initialize(title:, href:, selected:, favored: false, icon: nil, count: nil, show_enterprise_icon: false)
+ super
+ end
+ end
+```
+
+
+### Data loading
+
+Since we moved away from Angular towards Turbo, we use turbo frames to render the submenu only once they are needed.
+
+There is a generic `Submenu` class that provides a basic setup of how to collect all the data to be displayed in the sub menu. This class is based on the existence of so called `views` which are used to assign a query to a module. The `Submenu` class collects all queries for a given `view` does the following things:
+
+* Highlight the currently selected query
+* Create params for the links of each item
+* Group the queries into collapsible sections
+ * Favourite
+ * Default
+ * Public
+ * Private
+
+All of the modules that implement a query-based submenu need to have an own `menu.rb` file, placed under `app/menus` which inherits from `Submenu`. This is at least necessary to define some basics as the `view` or the `base_path` of that menu. Further, the default queries for that module can be defined there.
+
+This is an excerpt of the base class:
+
+```ruby
+class Submenu
+ def initialize(view_type:, project: nil, params: nil)
+ @view_type = view_type
+ @project = project
+ @params = params
+ end
+
+ def menu_items
+ [
+ OpenProject::Menu::MenuGroup.new(header: I18n.t("js.label_starred_queries"), children: starred_queries),
+ # ....
+ ]
+ end
+
+ def starred_queries
+ base_query
+ .where("starred" => "t")
+ .pluck(:id, :name)
+ .map { |id, name| menu_item(title: name, query_params: query_params(id)) }
+ .sort_by(&:title)
+ end
+
+ # ...
+
+ def base_query
+ base_query ||= Query
+ .visible(User.current)
+ .includes(:project)
+ .joins(:views)
+ .where("views.type" => view_type)
+
+ if project.present?
+ base_query.where("queries.project_id" => project.id)
+ else
+ base_query.where("queries.project_id" => nil)
+ end
+ end
+end
+```
+
+
+## Filter-based sub menu
+
+Not all modules are based on queries with an attached `view` object to show in their submenu. Some use the generic `filters` to provide the elements. For those modules, basically the same architecture as above can be used. The only difference is that you cannot rely on the existing `base_query` of the parent class but have to define your own query. The following modules use such an approach (amongst others):
+
+* Members (example provided below)
+* Project lists
+* Meetings
+* Notifications
+
+```ruby
+module Members
+ class Menu < Submenu
+ attr_reader :project, :params
+
+ def initialize(project: nil, params: nil)
+ super(view_type: nil, project:, params:)
+ end
+
+ def menu_items
+ [
+ OpenProject::Menu::MenuGroup.new(header: I18n.t("members.menu.project_roles"), children: project_roles_entries),
+ # ...
+ ]
+ end
+
+ def project_roles_entries
+ ProjectRole
+ .where(id: MemberRole.where(member_id: @project.members.select(:id)).select(:role_id))
+ .distinct
+ .pluck(:id, :name)
+ .map { |id, name| menu_item(title: name, query_params: { role_id: id }) }
+ end
+
+ # ...
+ end
+end
+```
diff --git a/lookbook/previews/open_project/common/submenu_component_preview.rb b/lookbook/previews/open_project/common/submenu_component_preview.rb
index b64d39f2c7b..87a22e5ea1e 100644
--- a/lookbook/previews/open_project/common/submenu_component_preview.rb
+++ b/lookbook/previews/open_project/common/submenu_component_preview.rb
@@ -3,55 +3,34 @@ module OpenProject
# @logical_path OpenProject/Common
class SubmenuComponentPreview < Lookbook::Preview
# @label Default
+ # @display min_height 450px
def default
- render_with_template(template: "open_project/common/submenu_preview/playground",
- locals: { sidebar_menu_items: menu_items, searchable: false, create_btn_options: nil })
- end
-
- # @label Searchable
- # Searching is currently not working in the lookbook because stimulus controllers are not loaded correctly.
- # It will be fine in production.
- def searchable
- render_with_template(template: "open_project/common/submenu_preview/playground",
- locals: { sidebar_menu_items: menu_items, searchable: true, create_btn_options: nil })
+ render_with_template(template: "open_project/common/submenu_preview/default")
end
- # @label With create Button
- # `create_btn_options: { href: "/#", text: "User"}`
- def with_create_button
+ # @label Playground
+ # @display min_height 450px
+ # @param searchable [Boolean]
+ # @param with_create_button [Boolean]
+ # @param favored [Boolean]
+ # @param count [Integer]
+ # @param show_enterprise_icon [Boolean]
+ # @param icon [Symbol] octicon
+ def playground(searchable: false,
+ with_create_button: false,
+ favored: false,
+ count: nil,
+ show_enterprise_icon: false,
+ icon: nil)
render_with_template(template: "open_project/common/submenu_preview/playground",
- locals: { sidebar_menu_items: menu_items, searchable: true,
- create_btn_options: { href: "/#", module_key: "user" } })
- end
-
- private
-
- def menu_items
- [
- OpenProject::Menu::MenuGroup.new(
- header: nil,
- children: [
- OpenProject::Menu::MenuItem.new(title: I18n.t("members.menu.all"), href: "", selected: true),
- OpenProject::Menu::MenuItem.new(title: I18n.t("members.menu.locked"), href: "", selected: false),
- OpenProject::Menu::MenuItem.new(title: I18n.t("members.menu.invited"), href: "", selected: false)
- ]
- ),
- OpenProject::Menu::MenuGroup.new(
- header: I18n.t("members.menu.project_roles"),
- children: [
- OpenProject::Menu::MenuItem.new(title: "Developer", href: "", selected: false),
- OpenProject::Menu::MenuItem.new(title: "Manager", href: "", selected: true)
- ]
- ),
- OpenProject::Menu::MenuGroup.new(
- header: I18n.t("members.menu.groups"),
- children: [
- OpenProject::Menu::MenuItem.new(title: "UX", href: "", selected: false),
- OpenProject::Menu::MenuItem.new(title: "Customer success", href: "", selected: false),
- OpenProject::Menu::MenuItem.new(title: "Core dev team", href: "", selected: false)
- ]
- )
- ]
+ locals: {
+ searchable:,
+ create_btn_options: with_create_button ? { href: "/#", module_key: "user" } : nil,
+ favored:,
+ count:,
+ show_enterprise_icon:,
+ icon:
+ })
end
end
end
diff --git a/lookbook/previews/open_project/common/submenu_preview/default.html.erb b/lookbook/previews/open_project/common/submenu_preview/default.html.erb
new file mode 100644
index 00000000000..b03ffaf1ae0
--- /dev/null
+++ b/lookbook/previews/open_project/common/submenu_preview/default.html.erb
@@ -0,0 +1,34 @@
+<% sidebar_menu_items =
+ [
+ OpenProject::Menu::MenuGroup.new(
+ header: nil,
+ children: [
+ OpenProject::Menu::MenuItem.new(title: "Global query", href: "", selected: true),
+ OpenProject::Menu::MenuItem.new(title: "A second query", href: "", selected: false)
+ ]
+ ),
+ OpenProject::Menu::MenuGroup.new(
+ header: "Favourite",
+ children: [
+ OpenProject::Menu::MenuItem.new(title: "Very important version", href: "", selected: false),
+ OpenProject::Menu::MenuItem.new(title: "A baseline view", href: "", selected: true)
+ ]
+ ),
+ OpenProject::Menu::MenuGroup.new(
+ header: "Private",
+ children: [
+ OpenProject::Menu::MenuItem.new(title: "My query", href: "", selected: false),
+ OpenProject::Menu::MenuItem.new(title: "An awesome query", href: "", selected: false),
+ OpenProject::Menu::MenuItem.new(title: "A third query", href: "", selected: false)
+ ]
+ )
+ ] %>
+
+<div class="main-menu">
+ <!-- Meant to be rendered inside the left-handed sidebar and is currently depending on that -->
+ <!-- Please note, that due to that not all styles are correctly applied in the lookbook -->
+ <%= render OpenProject::Common::SubmenuComponent.new(
+ sidebar_menu_items: sidebar_menu_items
+ )
+ %>
+</div>
diff --git a/lookbook/previews/open_project/common/submenu_preview/playground.html.erb b/lookbook/previews/open_project/common/submenu_preview/playground.html.erb
index 2aead4a5728..6b796dc1362 100644
--- a/lookbook/previews/open_project/common/submenu_preview/playground.html.erb
+++ b/lookbook/previews/open_project/common/submenu_preview/playground.html.erb
@@ -1,4 +1,18 @@
-<div class="main-menu"> <!-- Meant to be rendered inside the left-handed sidebar and is currently depending on that -->
+<% sidebar_menu_items =
+ [
+ OpenProject::Menu::MenuGroup.new(
+ header: "Private",
+ children: [
+ OpenProject::Menu::MenuItem.new(title: "My query", href: "", selected: false, favored: favored),
+ OpenProject::Menu::MenuItem.new(title: "An awesome query", href: "", selected: false, count: count, icon: icon),
+ OpenProject::Menu::MenuItem.new(title: "A third query", href: "", selected: false, show_enterprise_icon: show_enterprise_icon)
+ ]
+ )
+ ] %>
+
+<div class="main-menu">
+ <!-- Meant to be rendered inside the left-handed sidebar and is currently depending on that -->
+ <!-- Please note, that due to that not all styles are correctly applied in the lookbook -->
<%= render OpenProject::Common::SubmenuComponent.new(
sidebar_menu_items: sidebar_menu_items,
searchable: searchable,