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

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

    if block_given?
P
Phil Hughes 已提交
40
      link_to link, class: css_class, &block
R
Robert Speicher 已提交
41
    else
P
Phil Hughes 已提交
42
      link_to render_colored_label(label, tooltip: tooltip), link, class: css_class
R
Robert Speicher 已提交
43 44 45
    end
  end

46 47 48 49 50 51 52
  def label_filter_path(project, label, type: issue)
    send("namespace_project_#{type.to_s.pluralize}_path",
                project.namespace,
                project,
                label_name: [label.name])
  end

53
  def project_label_names
54
    @project.labels.pluck(:title)
55 56
  end

57
  def render_colored_label(label, label_suffix = '', tooltip: true)
D
Dmitriy Zaporozhets 已提交
58
    label_color = label.color || Label::DEFAULT_COLOR
59
    text_color = text_color_for_bg(label_color)
60

R
Robert Speicher 已提交
61 62
    # Intentionally not using content_tag here so that this method can be called
    # by LabelReferenceFilter
63
    span = %(<span class="label color-label #{"has-tooltip" if tooltip}" ) +
64 65
      %(style="background-color: #{label_color}; color: #{text_color}" ) +
      %(title="#{escape_once(label.description)}" data-container="body">) +
66
      %(#{escape_once(label.name)}#{label_suffix}</span>)
R
Robert Speicher 已提交
67 68

    span.html_safe
69
  end
70

71
  def render_colored_cross_project_label(label, tooltip: true)
72
    label_suffix = label.project.name_with_namespace
73
    label_suffix = " <i>in #{escape_once(label_suffix)}</i>"
74
    render_colored_label(label, label_suffix, tooltip: tooltip)
75 76
  end

77 78
  def suggested_colors
    [
P
phortx 已提交
79
      '#0033CC',
80
      '#428BCA',
P
phortx 已提交
81 82
      '#44AD8E',
      '#A8D695',
83
      '#5CB85C',
P
phortx 已提交
84 85
      '#69D100',
      '#004E00',
86 87
      '#34495E',
      '#7F8C8D',
P
phortx 已提交
88 89
      '#A295D6',
      '#5843AD',
90
      '#8E44AD',
P
phortx 已提交
91
      '#FFECDB',
92 93 94 95 96 97 98 99
      '#AD4363',
      '#D10069',
      '#CC0033',
      '#FF0000',
      '#D9534F',
      '#D1D100',
      '#F0AD4E',
      '#AD8D43'
100 101
    ]
  end
102 103

  def text_color_for_bg(bg_color)
104 105 106 107 108
    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
109 110

    if (r + g + b) > 500
R
Robert Speicher 已提交
111
      '#333333'
112
    else
R
Robert Speicher 已提交
113
      '#FFFFFF'
114 115
    end
  end
116

P
Phil Hughes 已提交
117 118 119 120
  def labels_filter_path
    if @project
      namespace_project_labels_path(@project.namespace, @project, :json)
    else
121
      dashboard_labels_path(:json)
P
Phil Hughes 已提交
122
    end
123
  end
R
Robert Speicher 已提交
124

125 126 127 128 129 130 131 132
  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

133
  # Required for Banzai::Filter::LabelReferenceFilter
134 135
  module_function :render_colored_label, :render_colored_cross_project_label,
                  :text_color_for_bg, :escape_once
136
end