debian-mirror-gitlab/app/helpers/nav/top_nav_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

360 lines
12 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module Nav
module TopNavHelper
PROJECTS_VIEW = :projects
GROUPS_VIEW = :groups
2021-09-04 01:27:46 +05:30
NEW_VIEW = :new
SEARCH_VIEW = :search
2021-06-08 01:23:25 +05:30
def top_nav_view_model(project:, group:)
builder = ::Gitlab::Nav::TopNavViewModelBuilder.new
2021-09-04 01:27:46 +05:30
build_base_view_model(builder: builder, project: project, group: group)
builder.build
end
def top_nav_responsive_view_model(project:, group:)
builder = ::Gitlab::Nav::TopNavViewModelBuilder.new
build_base_view_model(builder: builder, project: project, group: group)
new_view_model = new_dropdown_view_model(project: project, group: group)
2021-09-30 23:02:18 +05:30
if new_view_model && new_view_model.fetch(:menu_sections)&.any?
2021-09-04 01:27:46 +05:30
builder.add_view(NEW_VIEW, new_view_model)
end
if top_nav_show_search
builder.add_view(SEARCH_VIEW, ::Gitlab::Nav::TopNavMenuItem.build(**top_nav_search_menu_item_attrs))
2021-06-08 01:23:25 +05:30
end
builder.build
end
2021-09-04 01:27:46 +05:30
def top_nav_show_search
header_link?(:search)
end
def top_nav_search_menu_item_attrs
{
id: 'search',
title: _('Search'),
icon: 'search',
href: search_context.search_url
}
end
2021-06-08 01:23:25 +05:30
private
2022-10-11 01:57:18 +05:30
def top_nav_localized_headers
{
explore: s_('TopNav|Explore'),
switch_to: s_('TopNav|Switch to')
}.freeze
end
2021-09-04 01:27:46 +05:30
def build_base_view_model(builder:, project:, group:)
if current_user
build_view_model(builder: builder, project: project, group: group)
else
build_anonymous_view_model(builder: builder)
end
end
2021-06-08 01:23:25 +05:30
def build_anonymous_view_model(builder:)
# These come from `app/views/layouts/nav/_explore.html.ham`
if explore_nav_link?(:projects)
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:explore],
2021-09-04 01:27:46 +05:30
href: explore_root_path,
active: nav == 'project' || active_nav_link?(path: %w[dashboard#show root#show projects#trending projects#starred projects#index]),
**projects_menu_item_attrs
2021-06-08 01:23:25 +05:30
)
end
if explore_nav_link?(:groups)
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:explore],
2021-09-04 01:27:46 +05:30
href: explore_groups_path,
active: nav == 'group' || active_nav_link?(controller: [:groups, 'groups/milestones', 'groups/group_members']),
**groups_menu_item_attrs
2021-06-08 01:23:25 +05:30
)
end
if explore_nav_link?(:snippets)
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:explore],
2021-09-04 01:27:46 +05:30
active: active_nav_link?(controller: :snippets),
href: explore_snippets_path,
**snippets_menu_item_attrs
2021-06-08 01:23:25 +05:30
)
end
end
def build_view_model(builder:, project:, group:)
# These come from `app/views/layouts/nav/_dashboard.html.haml`
if dashboard_nav_link?(:projects)
current_item = project ? current_project(project: project) : {}
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:switch_to],
2021-09-04 01:27:46 +05:30
active: nav == 'project' || active_nav_link?(path: %w[root#index projects#trending projects#starred dashboard/projects#index]),
css_class: 'qa-projects-dropdown',
2021-11-11 11:23:49 +05:30
data: { track_label: "projects_dropdown", track_action: "click_dropdown" },
2021-09-04 01:27:46 +05:30
view: PROJECTS_VIEW,
shortcut_href: dashboard_projects_path,
**projects_menu_item_attrs
2021-06-08 01:23:25 +05:30
)
builder.add_view(PROJECTS_VIEW, container_view_props(namespace: 'projects', current_item: current_item, submenu: projects_submenu))
end
if dashboard_nav_link?(:groups)
current_item = group ? current_group(group: group) : {}
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:switch_to],
2021-09-04 01:27:46 +05:30
active: nav == 'group' || active_nav_link?(path: %w[dashboard/groups explore/groups]),
css_class: 'qa-groups-dropdown',
2021-11-11 11:23:49 +05:30
data: { track_label: "groups_dropdown", track_action: "click_dropdown" },
2021-09-04 01:27:46 +05:30
view: GROUPS_VIEW,
shortcut_href: dashboard_groups_path,
**groups_menu_item_attrs
2021-06-08 01:23:25 +05:30
)
builder.add_view(GROUPS_VIEW, container_view_props(namespace: 'groups', current_item: current_item, submenu: groups_submenu))
end
if dashboard_nav_link?(:milestones)
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2021-06-08 01:23:25 +05:30
id: 'milestones',
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:explore],
2022-03-02 08:16:31 +05:30
title: _('Milestones'),
2021-09-04 01:27:46 +05:30
href: dashboard_milestones_path,
2021-06-08 01:23:25 +05:30
active: active_nav_link?(controller: 'dashboard/milestones'),
icon: 'clock',
2022-07-23 23:45:48 +05:30
data: { qa_selector: 'milestones_link', **menu_data_tracking_attrs('milestones') },
2021-09-04 01:27:46 +05:30
shortcut_class: 'dashboard-shortcuts-milestones'
2021-06-08 01:23:25 +05:30
)
end
if dashboard_nav_link?(:snippets)
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:explore],
2021-09-04 01:27:46 +05:30
active: active_nav_link?(controller: 'dashboard/snippets'),
2022-07-23 23:45:48 +05:30
data: { qa_selector: 'snippets_link', **menu_data_tracking_attrs('snippets') },
2021-09-04 01:27:46 +05:30
href: dashboard_snippets_path,
**snippets_menu_item_attrs
2021-06-08 01:23:25 +05:30
)
end
if dashboard_nav_link?(:activity)
2021-09-04 01:27:46 +05:30
builder.add_primary_menu_item_with_shortcut(
2021-06-08 01:23:25 +05:30
id: 'activity',
2022-10-11 01:57:18 +05:30
header: top_nav_localized_headers[:explore],
2022-03-02 08:16:31 +05:30
title: _('Activity'),
2021-09-04 01:27:46 +05:30
href: activity_dashboard_path,
2021-06-08 01:23:25 +05:30
active: active_nav_link?(path: 'dashboard#activity'),
icon: 'history',
2022-07-23 23:45:48 +05:30
data: { qa_selector: 'activity_link', **menu_data_tracking_attrs('activity') },
2021-09-04 01:27:46 +05:30
shortcut_class: 'dashboard-shortcuts-activity'
2021-06-08 01:23:25 +05:30
)
end
# Using admin? is generally discouraged because it does not check for
# "admin_mode". In this case we are migrating code and check both, so
# we should be good.
# rubocop: disable Cop/UserAdmin
if current_user&.admin?
2022-07-23 23:45:48 +05:30
title = _('Admin')
2021-06-08 01:23:25 +05:30
builder.add_secondary_menu_item(
id: 'admin',
2022-07-23 23:45:48 +05:30
title: title,
2021-06-08 01:23:25 +05:30
active: active_nav_link?(controller: 'admin/dashboard'),
icon: 'admin',
css_class: 'qa-admin-area-link',
2022-07-23 23:45:48 +05:30
href: admin_root_path,
data: { qa_selector: 'menu_item_link', qa_title: title, **menu_data_tracking_attrs(title) }
2021-06-08 01:23:25 +05:30
)
end
if Gitlab::CurrentSettings.admin_mode
if header_link?(:admin_mode)
builder.add_secondary_menu_item(
id: 'leave_admin_mode',
title: _('Leave Admin Mode'),
active: active_nav_link?(controller: 'admin/sessions'),
icon: 'lock-open',
href: destroy_admin_session_path,
2022-07-23 23:45:48 +05:30
data: { method: 'post', **menu_data_tracking_attrs('leave_admin_mode') }
2021-06-08 01:23:25 +05:30
)
elsif current_user.admin?
2022-07-23 23:45:48 +05:30
title = _('Enter Admin Mode')
2021-06-08 01:23:25 +05:30
builder.add_secondary_menu_item(
id: 'enter_admin_mode',
2022-07-23 23:45:48 +05:30
title: title,
2021-06-08 01:23:25 +05:30
active: active_nav_link?(controller: 'admin/sessions'),
icon: 'lock',
2022-07-23 23:45:48 +05:30
href: new_admin_session_path,
data: { qa_selector: 'menu_item_link', qa_title: title, **menu_data_tracking_attrs(title) }
2021-06-08 01:23:25 +05:30
)
end
end
# rubocop: enable Cop/UserAdmin
end
def projects_menu_item_attrs
{
id: 'project',
title: _('Projects'),
2021-09-04 01:27:46 +05:30
icon: 'project',
shortcut_class: 'dashboard-shortcuts-projects'
2021-06-08 01:23:25 +05:30
}
end
def groups_menu_item_attrs
{
id: 'groups',
2022-03-02 08:16:31 +05:30
title: _('Groups'),
2021-09-04 01:27:46 +05:30
icon: 'group',
shortcut_class: 'dashboard-shortcuts-groups'
2021-06-08 01:23:25 +05:30
}
end
def snippets_menu_item_attrs
{
id: 'snippets',
title: _('Snippets'),
2021-09-04 01:27:46 +05:30
icon: 'snippet',
shortcut_class: 'dashboard-shortcuts-snippets'
2021-06-08 01:23:25 +05:30
}
end
2022-07-23 23:45:48 +05:30
def menu_data_tracking_attrs(label)
tracking_attrs(
"menu_#{label.underscore.parameterize(separator: '_')}",
'click_dropdown',
'navigation'
)[:data] || {}
end
2021-06-08 01:23:25 +05:30
def container_view_props(namespace:, current_item:, submenu:)
{
namespace: namespace,
currentUserName: current_user&.username,
currentItem: current_item,
linksPrimary: submenu[:primary],
linksSecondary: submenu[:secondary]
}
end
def current_project(project:)
return {} unless project.persisted?
{
id: project.id,
name: project.name,
namespace: project.full_name,
webUrl: project_path(project),
avatarUrl: project.avatar_url
}
end
def current_group(group:)
return {} unless group.persisted?
{
id: group.id,
name: group.name,
namespace: group.full_name,
webUrl: group_path(group),
avatarUrl: group.avatar_url
}
end
def projects_submenu
builder = ::Gitlab::Nav::TopNavMenuBuilder.new
2022-04-04 11:22:00 +05:30
projects_submenu_items(builder: builder)
builder.build
end
def projects_submenu_items(builder:)
2022-10-11 01:57:18 +05:30
if Feature.enabled?(:remove_extra_primary_submenu_options)
title = _('View all projects')
2022-07-23 23:45:48 +05:30
builder.add_primary_menu_item(
2022-10-11 01:57:18 +05:30
id: 'your',
title: title,
href: dashboard_projects_path,
data: { qa_selector: 'menu_item_link', qa_title: title, **menu_data_tracking_attrs(title) }
2022-07-23 23:45:48 +05:30
)
2022-10-11 01:57:18 +05:30
else
# These project links come from `app/views/layouts/nav/projects_dropdown/_show.html.haml`
[
{ id: 'your', title: _('Your projects'), href: dashboard_projects_path },
{ id: 'starred', title: _('Starred projects'), href: starred_dashboard_projects_path },
{ id: 'explore', title: _('Explore projects'), href: explore_root_path },
{ id: 'topics', title: _('Explore topics'), href: topics_explore_projects_path }
].each do |item|
builder.add_primary_menu_item(
**item,
data: { qa_selector: 'menu_item_link', qa_title: item[:title], **menu_data_tracking_attrs(item[:title]) }
)
end
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
title = _('Create new project')
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
builder.add_secondary_menu_item(
id: 'create',
title: title,
href: new_project_path,
data: { qa_selector: 'menu_item_link', qa_title: title, **menu_data_tracking_attrs(title) }
)
end
2021-06-08 01:23:25 +05:30
end
def groups_submenu
# These group links come from `app/views/layouts/nav/groups_dropdown/_show.html.haml`
builder = ::Gitlab::Nav::TopNavMenuBuilder.new
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
if Feature.enabled?(:remove_extra_primary_submenu_options)
title = _('View all groups')
2021-09-04 01:27:46 +05:30
2022-10-11 01:57:18 +05:30
builder.add_primary_menu_item(
id: 'your',
2022-07-23 23:45:48 +05:30
title: title,
2022-10-11 01:57:18 +05:30
href: dashboard_groups_path,
2022-07-23 23:45:48 +05:30
data: { qa_selector: 'menu_item_link', qa_title: title, **menu_data_tracking_attrs(title) }
)
2022-10-11 01:57:18 +05:30
else
[
{ id: 'your', title: _('Your groups'), href: dashboard_groups_path },
{ id: 'explore', title: _('Explore groups'), href: explore_groups_path }
].each do |item|
builder.add_primary_menu_item(
**item,
data: { qa_selector: 'menu_item_link', qa_title: item[:title], **menu_data_tracking_attrs(item[:title]) }
)
end
if current_user.can_create_group?
title = _('Create group')
builder.add_secondary_menu_item(
id: 'create',
title: title,
href: new_group_path,
data: { qa_selector: 'menu_item_link', qa_title: title, **menu_data_tracking_attrs(title) }
)
end
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
builder.build
end
end
end
Nav::TopNavHelper.prepend_mod