BigW Consortium Gitlab

labels_helper.rb 4.95 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 17 18 19
  # 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:
  #
  #   # Allow the generated link to use the label's own project
  #   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 35 36 37 38 39 40
    subject ||=
      case label
      when GroupLabel then label.group
      when ProjectLabel then label.project
      end

    link = label_filter_path(subject, label, type: type)
41 42

    if block_given?
Phil Hughes committed
43
      link_to link, class: css_class, &block
44
    else
Phil Hughes committed
45
      link_to render_colored_label(label, tooltip: tooltip), link, class: css_class
46 47 48
    end
  end

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

63 64 65
  def edit_label_path(label)
    case label
    when GroupLabel then edit_group_label_path(label.group, label)
66
    when ProjectLabel then edit_namespace_project_label_path(label.project.namespace, label.project, label)
67 68 69 70 71 72
    end
  end

  def destroy_label_path(label)
    case label
    when GroupLabel then group_label_path(label.group, label)
73
    when ProjectLabel then namespace_project_label_path(label.project.namespace, label.project, label)
74 75 76
    end
  end

77 78 79
  def toggle_subscription_label_path(label)
    case label
    when GroupLabel then toggle_subscription_group_label_path(label.group, label)
80
    when ProjectLabel then toggle_subscription_namespace_project_label_path(label.project.namespace, label.project, label)
81 82 83
    end
  end

84
  def render_colored_label(label, label_suffix = '', tooltip: true)
85
    label_color = label.color || Label::DEFAULT_COLOR
86
    text_color = text_color_for_bg(label_color)
87

Robert Speicher committed
88 89
    # Intentionally not using content_tag here so that this method can be called
    # by LabelReferenceFilter
90
    span = %(<span class="label color-label #{"has-tooltip" if tooltip}" ) +
91 92
      %(style="background-color: #{label_color}; color: #{text_color}" ) +
      %(title="#{escape_once(label.description)}" data-container="body">) +
93
      %(#{escape_once(label.name)}#{label_suffix}</span>)
Robert Speicher committed
94 95

    span.html_safe
96
  end
97

98 99
  def render_colored_cross_project_label(label, source_project = nil, tooltip: true)
    label_suffix = source_project ? source_project.name_with_namespace : label.project.name_with_namespace
100
    label_suffix = " <i>in #{escape_once(label_suffix)}</i>"
101
    render_colored_label(label, label_suffix, tooltip: tooltip)
102 103
  end

104 105
  def suggested_colors
    [
106
      '#0033CC',
107
      '#428BCA',
108 109
      '#44AD8E',
      '#A8D695',
110
      '#5CB85C',
111 112
      '#69D100',
      '#004E00',
113 114
      '#34495E',
      '#7F8C8D',
115 116
      '#A295D6',
      '#5843AD',
117
      '#8E44AD',
118
      '#FFECDB',
119 120 121 122 123 124 125 126
      '#AD4363',
      '#D10069',
      '#CC0033',
      '#FF0000',
      '#D9534F',
      '#D1D100',
      '#F0AD4E',
      '#AD8D43'
127 128
    ]
  end
129 130

  def text_color_for_bg(bg_color)
131 132 133 134 135
    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
136 137

    if (r + g + b) > 500
Robert Speicher committed
138
      '#333333'
139
    else
Robert Speicher committed
140
      '#FFFFFF'
141 142
    end
  end
143

144
  def labels_filter_path
145 146
    return group_labels_path(@group, :json) if @group

147
    project = @target_project || @project
148

149
    if project
150
      namespace_project_labels_path(project.namespace, project, :json)
151
    else
152
      dashboard_labels_path(:json)
153
    end
154
  end
Robert Speicher committed
155

156 157 158 159 160 161 162 163
  def label_subscription_status(label)
    label.subscribed?(current_user) ? 'subscribed' : 'unsubscribed'
  end

  def label_subscription_toggle_button_text(label)
    label.subscribed?(current_user) ? 'Unsubscribe' : 'Subscribe'
  end

164
  # Required for Banzai::Filter::LabelReferenceFilter
165 166
  module_function :render_colored_label, :render_colored_cross_project_label,
                  :text_color_for_bg, :escape_once
167
end