BigW Consortium Gitlab

labels_helper.rb 4.96 KB
Newer Older
1
module LabelsHelper
2 3
  include ActionView::Helpers::TagHelper

4 5 6
  # Link to a Label
  #
  # label   - Label object to link to
7 8
  # 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.
9 10
  # type    - The type of item the link will point to (:issue or
  #           :merge_request). If omitted, defaults to :issue.
11 12 13 14 15 16
  # 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:
  #
17
  #   # Allow the generated link to use the label's own subject
18 19
  #   link_to_label(label)
  #
20 21
  #   # Force the generated link to use a provided group
  #   link_to_label(label, subject: Group.last)
22 23
  #
  #   # Force the generated link to use a provided project
24
  #   link_to_label(label, subject: Project.last)
25
  #
26 27 28
  #   # Force the generated link to point to merge requests instead of issues
  #   link_to_label(label, type: :merge_request)
  #
29 30 31 32
  #   # Customize link body with a block
  #   link_to_label(label) { "My Custom Label Text" }
  #
  # Returns a String
33
  def link_to_label(label, subject: nil, type: :issue, tooltip: true, css_class: nil, &block)
34
    link = label_filter_path(subject || label.subject, label, type: type)
35 36

    if block_given?
Phil Hughes committed
37
      link_to link, class: css_class, &block
38
    else
Phil Hughes committed
39
      link_to render_colored_label(label, tooltip: tooltip), link, class: css_class
40 41 42
    end
  end

43
  def label_filter_path(subject, label, type: :issue)
44 45
    case subject
    when Group
46
      send("#{type.to_s.pluralize}_group_path",
47
                  subject,
48
                  label_name: [label.name])
49
    when Project
50
      send("namespace_project_#{type.to_s.pluralize}_path",
51 52
                  subject.namespace,
                  subject,
53 54
                  label_name: [label.name])
    end
55 56
  end

57 58 59
  def edit_label_path(label)
    case label
    when GroupLabel then edit_group_label_path(label.group, label)
60
    when ProjectLabel then edit_project_label_path(label.project, label)
61 62 63 64 65 66
    end
  end

  def destroy_label_path(label)
    case label
    when GroupLabel then group_label_path(label.group, label)
67
    when ProjectLabel then project_label_path(label.project, label)
68 69 70
    end
  end

71
  def render_colored_label(label, label_suffix = '', tooltip: true)
72
    text_color = text_color_for_bg(label.color)
73

Robert Speicher committed
74 75
    # Intentionally not using content_tag here so that this method can be called
    # by LabelReferenceFilter
76
    span = %(<span class="label color-label #{"has-tooltip" if tooltip}" ) +
77
      %(style="background-color: #{label.color}; color: #{text_color}" ) +
78
      %(title="#{escape_once(label.description)}" data-container="body">) +
79
      %(#{escape_once(label.name)}#{label_suffix}</span>)
Robert Speicher committed
80 81

    span.html_safe
82
  end
83 84 85

  def suggested_colors
    [
86
      '#0033CC',
87
      '#428BCA',
88 89
      '#44AD8E',
      '#A8D695',
90
      '#5CB85C',
91 92
      '#69D100',
      '#004E00',
93 94
      '#34495E',
      '#7F8C8D',
95 96
      '#A295D6',
      '#5843AD',
97
      '#8E44AD',
98
      '#FFECDB',
99 100 101 102 103 104 105 106
      '#AD4363',
      '#D10069',
      '#CC0033',
      '#FF0000',
      '#D9534F',
      '#D1D100',
      '#F0AD4E',
      '#AD8D43'
107 108
    ]
  end
109 110

  def text_color_for_bg(bg_color)
111 112 113 114 115
    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
116 117

    if (r + g + b) > 500
Robert Speicher committed
118
      '#333333'
119
    else
Robert Speicher committed
120
      '#FFFFFF'
121 122
    end
  end
123

124
  def labels_filter_path
125 126
    return group_labels_path(@group, :json) if @group

127
    project = @target_project || @project
128

129
    if project
130
      project_labels_path(project, :json)
131
    else
132
      dashboard_labels_path(:json)
133
    end
134
  end
Robert Speicher committed
135

136
  def can_subscribe_to_label_in_different_levels?(label)
137
    defined?(@project) && label.is_a?(GroupLabel)
138 139
  end

140
  def label_subscription_status(label, project)
141
    return 'group-level' if label.subscribed?(current_user)
142
    return 'project-level' if label.subscribed?(current_user, project)
143 144 145 146

    'unsubscribed'
  end

147
  def toggle_subscription_label_path(label, project)
148 149 150 151
    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)
152 153
    when 'project-level' then toggle_subscription_project_label_path(project, label)
    when 'unsubscribed' then toggle_subscription_project_label_path(project, label)
154 155 156
    end
  end

157
  def label_subscription_toggle_button_text(label, project = nil)
158
    label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
159 160
  end

161 162 163 164 165 166 167
  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

168
  # Required for Banzai::Filter::LabelReferenceFilter
169
  module_function :render_colored_label, :text_color_for_bg, :escape_once
170
end