labels_helper.rb 5.4 KB
Newer Older
1
module LabelsHelper
2
  extend self
3 4
  include ActionView::Helpers::TagHelper

5 6 7 8 9 10 11
  def show_label_issuables_link?(label, issuables_type, current_user: nil, project: nil)
    return true if label.is_a?(GroupLabel)
    return true unless project

    project.feature_available?(issuables_type, current_user)
  end

R
Robert Speicher 已提交
12 13 14
  # Link to a Label
  #
  # label   - Label object to link to
15 16
  # subject - Project/Group object which will be used as the context for the
  #           label's link. If omitted, defaults to the label's own group/project.
17 18
  # type    - The type of item the link will point to (:issue or
  #           :merge_request). If omitted, defaults to :issue.
R
Robert Speicher 已提交
19 20 21 22 23 24
  # block   - An optional block that will be passed to `link_to`, forming the
  #           body of the link element. If omitted, defaults to
  #           `render_colored_label`.
  #
  # Examples:
  #
25
  #   # Allow the generated link to use the label's own subject
R
Robert Speicher 已提交
26 27
  #   link_to_label(label)
  #
28 29
  #   # Force the generated link to use a provided group
  #   link_to_label(label, subject: Group.last)
R
Robert Speicher 已提交
30 31
  #
  #   # Force the generated link to use a provided project
32
  #   link_to_label(label, subject: Project.last)
R
Robert Speicher 已提交
33
  #
34 35 36
  #   # Force the generated link to point to merge requests instead of issues
  #   link_to_label(label, type: :merge_request)
  #
R
Robert Speicher 已提交
37 38 39 40
  #   # Customize link body with a block
  #   link_to_label(label) { "My Custom Label Text" }
  #
  # Returns a String
41
  def link_to_label(label, subject: nil, type: :issue, tooltip: true, css_class: nil, &block)
42
    link = label_filter_path(subject || label.subject, label, type: type)
R
Robert Speicher 已提交
43 44

    if block_given?
P
Phil Hughes 已提交
45
      link_to link, class: css_class, &block
R
Robert Speicher 已提交
46
    else
P
Phil Hughes 已提交
47
      link_to render_colored_label(label, tooltip: tooltip), link, class: css_class
R
Robert Speicher 已提交
48 49 50
    end
  end

51
  def label_filter_path(subject, label, type: :issue)
52 53
    case subject
    when Group
54
      send("#{type.to_s.pluralize}_group_path", # rubocop:disable GitlabSecurity/PublicSend
55
                  subject,
56
                  label_name: [label.name])
57
    when Project
58
      send("namespace_project_#{type.to_s.pluralize}_path", # rubocop:disable GitlabSecurity/PublicSend
59 60
                  subject.namespace,
                  subject,
61 62
                  label_name: [label.name])
    end
63 64
  end

65 66 67
  def edit_label_path(label)
    case label
    when GroupLabel then edit_group_label_path(label.group, label)
68
    when ProjectLabel then edit_project_label_path(label.project, label)
69 70 71 72 73 74
    end
  end

  def destroy_label_path(label)
    case label
    when GroupLabel then group_label_path(label.group, label)
75
    when ProjectLabel then project_label_path(label.project, label)
76 77 78
    end
  end

79
  def render_colored_label(label, label_suffix = '', tooltip: true)
80
    text_color = text_color_for_bg(label.color)
81

R
Robert Speicher 已提交
82 83
    # Intentionally not using content_tag here so that this method can be called
    # by LabelReferenceFilter
84
    span = %(<span class="label color-label #{"has-tooltip" if tooltip}" ) +
85
      %(style="background-color: #{label.color}; color: #{text_color}" ) +
86
      %(title="#{escape_once(label.description)}" data-container="body">) +
87
      %(#{escape_once(label.name)}#{label_suffix}</span>)
R
Robert Speicher 已提交
88 89

    span.html_safe
90
  end
91 92 93

  def suggested_colors
    [
P
phortx 已提交
94
      '#0033CC',
95
      '#428BCA',
P
phortx 已提交
96 97
      '#44AD8E',
      '#A8D695',
98
      '#5CB85C',
P
phortx 已提交
99 100
      '#69D100',
      '#004E00',
101 102
      '#34495E',
      '#7F8C8D',
P
phortx 已提交
103 104
      '#A295D6',
      '#5843AD',
105
      '#8E44AD',
P
phortx 已提交
106
      '#FFECDB',
107 108 109 110 111 112 113 114
      '#AD4363',
      '#D10069',
      '#CC0033',
      '#FF0000',
      '#D9534F',
      '#D1D100',
      '#F0AD4E',
      '#AD8D43'
115 116
    ]
  end
117 118

  def text_color_for_bg(bg_color)
119 120 121 122 123
    if bg_color.length == 4
      r, g, b = bg_color[1, 4].scan(/./).map { |v| (v * 2).hex }
    else
      r, g, b = bg_color[1, 7].scan(/.{2}/).map(&:hex)
    end
124 125

    if (r + g + b) > 500
R
Robert Speicher 已提交
126
      '#333333'
127
    else
R
Robert Speicher 已提交
128
      '#FFFFFF'
129 130
    end
  end
131

F
Felipe Artur 已提交
132
  def labels_filter_path(only_group_labels = false)
133
    project = @target_project || @project
134

135
    if project
136
      project_labels_path(project, :json)
F
Felipe Artur 已提交
137 138 139
    elsif @group
      options = { only_group_labels: only_group_labels } if only_group_labels
      group_labels_path(@group, :json, options)
P
Phil Hughes 已提交
140
    else
141
      dashboard_labels_path(:json)
P
Phil Hughes 已提交
142
    end
143
  end
R
Robert Speicher 已提交
144

145
  def can_subscribe_to_label_in_different_levels?(label)
146
    defined?(@project) && label.is_a?(GroupLabel)
147 148
  end

149
  def label_subscription_status(label, project)
150
    return 'group-level' if label.subscribed?(current_user)
151
    return 'project-level' if label.subscribed?(current_user, project)
152 153 154 155

    'unsubscribed'
  end

156
  def toggle_subscription_label_path(label, project)
157 158 159 160
    return toggle_subscription_group_label_path(label.group, label) unless project

    case label_subscription_status(label, project)
    when 'group-level' then toggle_subscription_group_label_path(label.group, label)
161 162
    when 'project-level' then toggle_subscription_project_label_path(project, label)
    when 'unsubscribed' then toggle_subscription_project_label_path(project, label)
163 164 165
    end
  end

166
  def label_subscription_toggle_button_text(label, project = nil)
167
    label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
168 169
  end

170 171 172 173 174 175 176
  def label_deletion_confirm_text(label)
    case label
    when GroupLabel then 'Remove this label? This will affect all projects within the group. Are you sure?'
    when ProjectLabel then 'Remove this label? Are you sure?'
    end
  end

177
  # Required for Banzai::Filter::LabelReferenceFilter
178
  module_function :render_colored_label, :text_color_for_bg, :escape_once
179
end