summaryrefslogtreecommitdiff
path: root/app/helpers/nav_helper.rb
blob: 1da84bf2660d711fb9d67f450bcc1ae35734c034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

module NavHelper
  def header_links
    @header_links ||= get_header_links
  end

  def header_link?(link)
    header_links.include?(link)
  end

  def page_has_sidebar?
    defined?(@left_sidebar) && @left_sidebar
  end

  def page_has_collapsed_sidebar?
    page_has_sidebar? && collapsed_sidebar?
  end

  def page_has_collapsed_super_sidebar?
    page_has_sidebar? && collapsed_super_sidebar?
  end

  def page_with_sidebar_class
    class_name = page_gutter_class

    if show_super_sidebar?
      class_name << 'page-with-super-sidebar' if page_has_sidebar?
      class_name << 'page-with-super-sidebar-collapsed' if page_has_collapsed_super_sidebar?
    else
      class_name << 'page-with-contextual-sidebar' if page_has_sidebar?
      class_name << 'page-with-icon-sidebar' if page_has_collapsed_sidebar?
    end

    class_name -= ['right-sidebar-expanded'] if defined?(@right_sidebar) && !@right_sidebar

    class_name
  end

  def page_gutter_class
    moved_sidebar_enabled = @is_merge_request_with_flag

    if (page_has_markdown? || current_path?('projects/merge_requests#diffs')) && !current_controller?('conflicts')
      if cookies[:collapsed_gutter] == 'true'
        ["page-gutter", ('right-sidebar-collapsed' unless moved_sidebar_enabled).to_s]
      else
        ["page-gutter", ('right-sidebar-expanded' unless moved_sidebar_enabled).to_s]
      end
    elsif current_path?('jobs#show')
      %w[page-gutter build-sidebar right-sidebar-expanded]
    elsif current_controller?('wikis') && current_action?('show', 'create', 'edit', 'update', 'history', 'git_access', 'destroy', 'diff')
      %w[page-gutter wiki-sidebar right-sidebar-expanded]
    else
      []
    end
  end

  def nav_control_class
    "nav-control" if current_user
  end

  def user_dropdown_class
    class_names = []
    class_names << 'header-user-dropdown-toggle'
    class_names << 'impersonated-user' if session[:impersonator_id]

    class_names
  end

  def page_has_markdown?
    current_path?('projects/merge_requests#show') ||
      current_path?('projects/merge_requests/conflicts#show') ||
      current_path?('issues#show') ||
      current_path?('milestones#show') ||
      current_path?('issues#designs') ||
      current_path?('incidents#show')
  end

  def admin_monitoring_nav_links
    %w(system_info background_migrations background_jobs health_check)
  end

  def admin_analytics_nav_links
    %w(dev_ops_report usage_trends)
  end

  def show_super_sidebar?
    Feature.enabled?(:super_sidebar_nav, current_user) && current_user&.use_new_navigation && super_sidebar_supported?
  end

  private

  # This is a temporary measure until we support all other existing sidebars:
  # https://gitlab.com/gitlab-org/gitlab/-/issues/391500
  # https://gitlab.com/gitlab-org/gitlab/-/issues/391501
  # https://gitlab.com/gitlab-org/gitlab/-/issues/391502
  def super_sidebar_supported?
    return true if @nav.nil?

    %w(your_work explore project group profile user_profile search admin).include?(@nav)
  end

  def get_header_links
    links = if current_user
              [:user_dropdown]
            else
              [:sign_in]
            end

    if can?(current_user, :read_cross_project)
      links += [:issues, :merge_requests, :todos] if current_user.present?
    end

    if @project&.persisted? || can?(current_user, :read_cross_project)
      links << :search
    end

    if session[:impersonator_id]
      links << :admin_impersonation
    end

    if Gitlab::CurrentSettings.admin_mode && current_user_mode.admin_mode?
      links << :admin_mode
    end

    links
  end
end

NavHelper.prepend_mod_with('NavHelper')