BigW Consortium Gitlab

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

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
  # 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.
  # 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)
  #
  #   # Customize link body with a block
  #   link_to_label(label) { "My Custom Label Text" }
  #
  # Returns a String
  def link_to_label(label, project: nil, &block)
    project ||= @project || label.project
    link = namespace_project_issues_path(project.namespace, project,
                                         label_name: label.name)

    if block_given?
      link_to link, &block
    else
      link_to render_colored_label(label), link
    end
  end

42
  def project_label_names
43
    @project.labels.pluck(:title)
44 45
  end

46
  def render_colored_label(label)
47
    label_color = label.color || Label::DEFAULT_COLOR
48
    text_color = text_color_for_bg(label_color)
49

Robert Speicher committed
50 51 52 53
    # Intentionally not using content_tag here so that this method can be called
    # by LabelReferenceFilter
    span = %(<span class="label color-label") +
      %( style="background-color: #{label_color}; color: #{text_color}">) +
54
      escape_once(label.name) + '</span>'
Robert Speicher committed
55 56

    span.html_safe
57
  end
58 59 60

  def suggested_colors
    [
61
      '#0033CC',
62
      '#428BCA',
63 64
      '#44AD8E',
      '#A8D695',
65
      '#5CB85C',
66 67
      '#69D100',
      '#004E00',
68 69
      '#34495E',
      '#7F8C8D',
70 71
      '#A295D6',
      '#5843AD',
72
      '#8E44AD',
73
      '#FFECDB',
74 75 76 77 78 79 80 81
      '#AD4363',
      '#D10069',
      '#CC0033',
      '#FF0000',
      '#D9534F',
      '#D1D100',
      '#F0AD4E',
      '#AD8D43'
82 83
    ]
  end
84 85 86 87 88

  def text_color_for_bg(bg_color)
    r, g, b = bg_color.slice(1,7).scan(/.{2}/).map(&:hex)

    if (r + g + b) > 500
Robert Speicher committed
89
      '#333333'
90
    else
Robert Speicher committed
91
      '#FFFFFF'
92 93
    end
  end
94

95 96 97 98 99 100 101 102
  def projects_labels_options
    labels =
      if @project
        @project.labels
      else
        Label.where(project_id: @projects)
      end

103
    grouped_labels = GlobalLabel.build_collection(labels)
104 105 106 107
    grouped_labels.unshift(Label::None)
    grouped_labels.unshift(Label::Any)

    options_from_collection_for_select(grouped_labels, 'name', 'title', params[:label_name])
108
  end
Robert Speicher committed
109

110 111
  # Required for Gitlab::Markdown::LabelReferenceFilter
  module_function :render_colored_label, :text_color_for_bg, :escape_once
112
end