2021-04-07 05:09:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Sidebars
|
|
|
|
class Menu
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
include ::Gitlab::Routing
|
|
|
|
include GitlabRoutingHelper
|
|
|
|
include Gitlab::Allowable
|
2021-04-21 08:09:16 -04:00
|
|
|
include ::Sidebars::Concerns::HasPill
|
|
|
|
include ::Sidebars::Concerns::HasIcon
|
|
|
|
include ::Sidebars::Concerns::PositionableList
|
|
|
|
include ::Sidebars::Concerns::Renderable
|
|
|
|
include ::Sidebars::Concerns::ContainerWithHtmlOptions
|
|
|
|
include ::Sidebars::Concerns::HasActiveRoutes
|
2021-07-21 17:10:10 -04:00
|
|
|
include ::Sidebars::Concerns::HasPartial
|
2021-04-07 05:09:06 -04:00
|
|
|
|
2021-05-10 14:10:41 -04:00
|
|
|
attr_reader :context
|
2022-02-28 13:14:03 -05:00
|
|
|
|
2021-04-07 05:09:06 -04:00
|
|
|
delegate :current_user, :container, to: :@context
|
|
|
|
|
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
|
|
|
@items = []
|
|
|
|
|
|
|
|
configure_menu_items
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure_menu_items
|
2021-04-23 08:09:52 -04:00
|
|
|
true
|
2021-04-07 05:09:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
override :render?
|
|
|
|
def render?
|
2021-07-21 17:10:10 -04:00
|
|
|
has_renderable_items? || menu_with_partial?
|
2021-04-07 05:09:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
override :link
|
|
|
|
def link
|
2021-08-23 05:10:23 -04:00
|
|
|
renderable_items.first&.link
|
2021-04-07 05:09:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# This method normalizes the information retrieved from the submenus and this menu
|
|
|
|
# Value from menus is something like: [{ path: 'foo', path: 'bar', controller: :foo }]
|
|
|
|
# This method filters the information and returns: { path: ['foo', 'bar'], controller: :foo }
|
|
|
|
def all_active_routes
|
|
|
|
@all_active_routes ||= begin
|
2021-05-10 14:10:41 -04:00
|
|
|
([active_routes] + renderable_items.map(&:active_routes)).flatten.each_with_object({}) do |pairs, hash|
|
2021-04-07 05:09:06 -04:00
|
|
|
pairs.each do |k, v|
|
|
|
|
hash[k] ||= []
|
|
|
|
hash[k] += Array(v)
|
|
|
|
hash[k].uniq!
|
|
|
|
end
|
|
|
|
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-10 14:10:41 -04:00
|
|
|
# Returns whether the menu has any menu item, no
|
|
|
|
# matter whether it is renderable or not
|
2021-04-07 05:09:06 -04:00
|
|
|
def has_items?
|
|
|
|
@items.any?
|
|
|
|
end
|
|
|
|
|
2021-05-10 14:10:41 -04:00
|
|
|
# Returns all renderable menu items
|
|
|
|
def renderable_items
|
|
|
|
@renderable_items ||= @items.select(&:render?)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns whether the menu has any renderable menu item
|
|
|
|
def has_renderable_items?
|
|
|
|
renderable_items.any?
|
|
|
|
end
|
|
|
|
|
2021-04-07 05:09:06 -04:00
|
|
|
def add_item(item)
|
|
|
|
add_element(@items, item)
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert_item_before(before_item, new_item)
|
|
|
|
insert_element_before(@items, before_item, new_item)
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert_item_after(after_item, new_item)
|
|
|
|
insert_element_after(@items, after_item, new_item)
|
|
|
|
end
|
|
|
|
|
2021-05-31 11:11:12 -04:00
|
|
|
override :container_html_options
|
|
|
|
def container_html_options
|
|
|
|
super.tap do |html_options|
|
|
|
|
# Flagging menus that can be rendered and with renderable menu items
|
|
|
|
if render? && has_renderable_items?
|
|
|
|
html_options[:class] = [*html_options[:class], 'has-sub-items'].join(' ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
private
|
2021-04-07 05:09:06 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
override :index_of
|
|
|
|
def index_of(list, element)
|
|
|
|
list.index { |e| e.item_id == element }
|
2021-04-07 05:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|