2021-05-05 23:10:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Nav
|
|
|
|
module TopNavHelper
|
|
|
|
PROJECTS_VIEW = :projects
|
2021-05-12 05:10:19 -04:00
|
|
|
GROUPS_VIEW = :groups
|
2021-06-09 11:10:05 -04:00
|
|
|
NEW_VIEW = :new
|
|
|
|
SEARCH_VIEW = :search
|
2021-05-05 23:10:25 -04:00
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
def top_nav_view_model(project:, group:)
|
2021-05-05 23:10:25 -04:00
|
|
|
builder = ::Gitlab::Nav::TopNavViewModelBuilder.new
|
|
|
|
|
2021-06-09 11:10:05 -04:00
|
|
|
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-06-17 23:10:18 -04:00
|
|
|
if new_view_model && new_view_model.fetch(:menu_sections)&.any?
|
2021-06-09 11:10:05 -04:00
|
|
|
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-05-05 23:10:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
builder.build
|
|
|
|
end
|
|
|
|
|
2021-06-09 11:10:05 -04:00
|
|
|
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-05-05 23:10:25 -04:00
|
|
|
private
|
|
|
|
|
2021-06-09 11:10:05 -04:00
|
|
|
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-05-05 23:10:25 -04:00
|
|
|
def build_anonymous_view_model(builder:)
|
|
|
|
# These come from `app/views/layouts/nav/_explore.html.ham`
|
|
|
|
if explore_nav_link?(:projects)
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
|
|
|
href: explore_root_path,
|
2021-06-07 20:10:34 -04:00
|
|
|
active: nav == 'project' || active_nav_link?(path: %w[dashboard#show root#show projects#trending projects#starred projects#index]),
|
2021-05-19 17:12:42 -04:00
|
|
|
**projects_menu_item_attrs
|
2021-05-12 05:10:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if explore_nav_link?(:groups)
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
|
|
|
href: explore_groups_path,
|
2021-06-07 20:10:34 -04:00
|
|
|
active: nav == 'group' || active_nav_link?(controller: [:groups, 'groups/milestones', 'groups/group_members']),
|
2021-05-19 17:12:42 -04:00
|
|
|
**groups_menu_item_attrs
|
2021-05-12 05:10:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if explore_nav_link?(:snippets)
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
|
|
|
active: active_nav_link?(controller: :snippets),
|
|
|
|
href: explore_snippets_path,
|
|
|
|
**snippets_menu_item_attrs
|
2021-05-05 23:10:25 -04:00
|
|
|
)
|
|
|
|
end
|
2021-05-24 05:10:31 -04:00
|
|
|
|
|
|
|
builder.add_secondary_menu_item(
|
|
|
|
id: 'help',
|
|
|
|
title: _('Help'),
|
|
|
|
icon: 'question-o',
|
|
|
|
href: help_path
|
|
|
|
)
|
2021-05-05 23:10:25 -04:00
|
|
|
end
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
def build_view_model(builder:, project:, group:)
|
2021-05-05 23:10:25 -04:00
|
|
|
# These come from `app/views/layouts/nav/_dashboard.html.haml`
|
|
|
|
if dashboard_nav_link?(:projects)
|
|
|
|
current_item = project ? current_project(project: project) : {}
|
|
|
|
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
2021-06-07 20:10:34 -04:00
|
|
|
active: nav == 'project' || active_nav_link?(path: %w[root#index projects#trending projects#starred dashboard/projects#index]),
|
2021-05-19 17:12:42 -04:00
|
|
|
css_class: 'qa-projects-dropdown',
|
2021-09-15 14:11:29 -04:00
|
|
|
data: { track_label: "projects_dropdown", track_action: "click_dropdown" },
|
2021-05-19 17:12:42 -04:00
|
|
|
view: PROJECTS_VIEW,
|
|
|
|
shortcut_href: dashboard_projects_path,
|
|
|
|
**projects_menu_item_attrs
|
2021-05-05 23:10:25 -04:00
|
|
|
)
|
2021-05-12 05:10:19 -04:00
|
|
|
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-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
2021-06-07 20:10:34 -04:00
|
|
|
active: nav == 'group' || active_nav_link?(path: %w[dashboard/groups explore/groups]),
|
2021-05-19 17:12:42 -04:00
|
|
|
css_class: 'qa-groups-dropdown',
|
2021-09-15 14:11:29 -04:00
|
|
|
data: { track_label: "groups_dropdown", track_action: "click_dropdown" },
|
2021-05-19 17:12:42 -04:00
|
|
|
view: GROUPS_VIEW,
|
|
|
|
shortcut_href: dashboard_groups_path,
|
|
|
|
**groups_menu_item_attrs
|
2021-05-12 05:10:19 -04:00
|
|
|
)
|
|
|
|
builder.add_view(GROUPS_VIEW, container_view_props(namespace: 'groups', current_item: current_item, submenu: groups_submenu))
|
2021-05-05 23:10:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if dashboard_nav_link?(:milestones)
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
2021-05-05 23:10:25 -04:00
|
|
|
id: 'milestones',
|
|
|
|
title: 'Milestones',
|
2021-05-19 17:12:42 -04:00
|
|
|
href: dashboard_milestones_path,
|
2021-05-05 23:10:25 -04:00
|
|
|
active: active_nav_link?(controller: 'dashboard/milestones'),
|
|
|
|
icon: 'clock',
|
|
|
|
data: { qa_selector: 'milestones_link' },
|
2021-05-19 17:12:42 -04:00
|
|
|
shortcut_class: 'dashboard-shortcuts-milestones'
|
2021-05-05 23:10:25 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
if dashboard_nav_link?(:snippets)
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
|
|
|
active: active_nav_link?(controller: 'dashboard/snippets'),
|
|
|
|
data: { qa_selector: 'snippets_link' },
|
|
|
|
href: dashboard_snippets_path,
|
|
|
|
**snippets_menu_item_attrs
|
2021-05-12 05:10:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if dashboard_nav_link?(:activity)
|
2021-05-19 17:12:42 -04:00
|
|
|
builder.add_primary_menu_item_with_shortcut(
|
2021-05-12 05:10:19 -04:00
|
|
|
id: 'activity',
|
|
|
|
title: 'Activity',
|
2021-05-19 17:12:42 -04:00
|
|
|
href: activity_dashboard_path,
|
2021-05-12 05:10:19 -04:00
|
|
|
active: active_nav_link?(path: 'dashboard#activity'),
|
|
|
|
icon: 'history',
|
|
|
|
data: { qa_selector: 'activity_link' },
|
2021-05-19 17:12:42 -04:00
|
|
|
shortcut_class: 'dashboard-shortcuts-activity'
|
2021-05-12 05:10:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-05-05 23:10:25 -04:00
|
|
|
# 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?
|
|
|
|
builder.add_secondary_menu_item(
|
|
|
|
id: 'admin',
|
|
|
|
title: _('Admin'),
|
|
|
|
active: active_nav_link?(controller: 'admin/dashboard'),
|
|
|
|
icon: 'admin',
|
|
|
|
css_class: 'qa-admin-area-link',
|
|
|
|
href: admin_root_path
|
|
|
|
)
|
|
|
|
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,
|
2021-05-21 08:10:27 -04:00
|
|
|
data: { method: 'post' }
|
2021-05-05 23:10:25 -04:00
|
|
|
)
|
|
|
|
elsif current_user.admin?
|
|
|
|
builder.add_secondary_menu_item(
|
|
|
|
id: 'enter_admin_mode',
|
|
|
|
title: _('Enter Admin Mode'),
|
|
|
|
active: active_nav_link?(controller: 'admin/sessions'),
|
|
|
|
icon: 'lock',
|
|
|
|
href: new_admin_session_path
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable Cop/UserAdmin
|
2021-05-12 05:10:19 -04:00
|
|
|
|
|
|
|
if Gitlab::Sherlock.enabled?
|
|
|
|
builder.add_secondary_menu_item(
|
|
|
|
id: 'sherlock',
|
|
|
|
title: _('Sherlock Transactions'),
|
|
|
|
icon: 'admin',
|
|
|
|
href: sherlock_transactions_path
|
|
|
|
)
|
|
|
|
end
|
2021-05-05 23:10:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def projects_menu_item_attrs
|
|
|
|
{
|
|
|
|
id: 'project',
|
|
|
|
title: _('Projects'),
|
2021-05-19 17:12:42 -04:00
|
|
|
icon: 'project',
|
|
|
|
shortcut_class: 'dashboard-shortcuts-projects'
|
2021-05-05 23:10:25 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
def groups_menu_item_attrs
|
2021-05-05 23:10:25 -04:00
|
|
|
{
|
2021-05-12 05:10:19 -04:00
|
|
|
id: 'groups',
|
|
|
|
title: 'Groups',
|
2021-05-19 17:12:42 -04:00
|
|
|
icon: 'group',
|
|
|
|
shortcut_class: 'dashboard-shortcuts-groups'
|
2021-05-12 05:10:19 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def snippets_menu_item_attrs
|
|
|
|
{
|
|
|
|
id: 'snippets',
|
|
|
|
title: _('Snippets'),
|
2021-05-19 17:12:42 -04:00
|
|
|
icon: 'snippet',
|
|
|
|
shortcut_class: 'dashboard-shortcuts-snippets'
|
2021-05-12 05:10:19 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def container_view_props(namespace:, current_item:, submenu:)
|
|
|
|
{
|
|
|
|
namespace: namespace,
|
2021-05-05 23:10:25 -04:00
|
|
|
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
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
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
|
|
|
|
|
2021-05-05 23:10:25 -04:00
|
|
|
def projects_submenu
|
|
|
|
# These project links come from `app/views/layouts/nav/projects_dropdown/_show.html.haml`
|
|
|
|
builder = ::Gitlab::Nav::TopNavMenuBuilder.new
|
|
|
|
builder.add_primary_menu_item(id: 'your', title: _('Your projects'), href: dashboard_projects_path)
|
|
|
|
builder.add_primary_menu_item(id: 'starred', title: _('Starred projects'), href: starred_dashboard_projects_path)
|
|
|
|
builder.add_primary_menu_item(id: 'explore', title: _('Explore projects'), href: explore_root_path)
|
2021-11-04 05:12:56 -04:00
|
|
|
builder.add_primary_menu_item(id: 'topics', title: _('Explore topics'), href: topics_explore_projects_path)
|
2021-08-10 02:08:47 -04:00
|
|
|
builder.add_secondary_menu_item(id: 'create', title: _('Create new project'), href: new_project_path)
|
2021-05-05 23:10:25 -04:00
|
|
|
builder.build
|
|
|
|
end
|
2021-05-12 05:10:19 -04:00
|
|
|
|
|
|
|
def groups_submenu
|
|
|
|
# These group links come from `app/views/layouts/nav/groups_dropdown/_show.html.haml`
|
|
|
|
builder = ::Gitlab::Nav::TopNavMenuBuilder.new
|
|
|
|
builder.add_primary_menu_item(id: 'your', title: _('Your groups'), href: dashboard_groups_path)
|
|
|
|
builder.add_primary_menu_item(id: 'explore', title: _('Explore groups'), href: explore_groups_path)
|
2021-06-14 11:09:48 -04:00
|
|
|
|
|
|
|
if current_user.can_create_group?
|
|
|
|
builder.add_secondary_menu_item(id: 'create', title: _('Create group'), href: new_group_path)
|
|
|
|
end
|
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
builder.build
|
|
|
|
end
|
2021-05-05 23:10:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Nav::TopNavHelper.prepend_mod
|