page_layout_helper.rb 3 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
6
7
8
module PageLayoutHelper
  def page_title(*titles)
    @page_title ||= []

    @page_title.push(*titles.compact) if titles.any?

9
    if titles.any? && !defined?(@breadcrumb_title)
Phil Hughes's avatar
Phil Hughes committed
10
      @breadcrumb_title = @page_title.last
11
12
    end

13
    # Segments are separated by middot
14
    @page_title.join(" · ")
15
16
  end

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  # Define or get a description for the current page
  #
  # description - String (default: nil)
  #
  # If this helper is called multiple times with an argument, only the last
  # description will be returned when called without an argument. Descriptions
  # have newlines replaced with spaces and all HTML tags are sanitized.
  #
  # Examples:
  #
  #   page_description # => "GitLab Community Edition"
  #   page_description("Foo")
  #   page_description # => "Foo"
  #
  #   page_description("<b>Bar</b>\nBaz")
  #   page_description # => "Bar Baz"
  #
  # Returns an HTML-safe String.
  def page_description(description = nil)
    if description.present?
37
      @page_description = description.squish
38
    elsif @page_description.present?
39
      sanitize(@page_description, tags: []).truncate_words(30)
40
41
42
    end
  end

43
  def favicon
44
    Gitlab::Favicon.main
45
46
  end

47
48
49
  def page_image
    default = image_url('gitlab_logo.png')

50
51
    subject = @project || @user || @group

52
53
    image = subject.avatar_url if subject.present?
    image || default
54
55
  end

56
57
58
59
60
61
62
63
64
  # Define or get attributes to be used as Twitter card metadata
  #
  # map - Hash of label => data pairs. Keys become labels, values become data
  #
  # Raises ArgumentError if given more than two attributes
  def page_card_attributes(map = {})
    raise ArgumentError, 'cannot provide more than two attributes' if map.length > 2

    @page_card_attributes ||= {}
65
    @page_card_attributes = map.reject { |_, v| v.blank? } if map.present?
66
67
68
69
    @page_card_attributes
  end

  def page_card_meta_tags
70
    tags = []
71
72
73
74
75
76

    page_card_attributes.each_with_index do |pair, i|
      tags << tag(:meta, property: "twitter:label#{i + 1}", content: pair[0])
      tags << tag(:meta, property: "twitter:data#{i + 1}",  content: pair[1])
    end

77
    tags.join.html_safe
78
79
  end

80
81
82
83
84
  def header_title(title = nil, title_url = nil)
    if title
      @header_title     = title
      @header_title_url = title_url
    else
Phil Hughes's avatar
Phil Hughes committed
85
86
      return @header_title unless @header_title_url

Phil Hughes's avatar
Phil Hughes committed
87
      breadcrumb_list_item(link_to(@header_title, @header_title_url))
88
89
90
91
92
93
94
95
96
97
    end
  end

  def sidebar(name = nil)
    if name
      @sidebar = name
    else
      @sidebar
    end
  end
98

99
100
101
102
103
104
105
106
  def nav(name = nil)
    if name
      @nav = name
    else
      @nav
    end
  end

107
108
  def fluid_layout
    current_user && current_user.layout == "fluid"
109
  end
110
111
112
113
114
115
116
117
118
119

  def blank_container(enabled = false)
    if @blank_container.nil?
      @blank_container = enabled
    else
      @blank_container
    end
  end

  def container_class
120
    css_class = ["container-fluid"]
121

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
122
    unless fluid_layout
123
      css_class << "container-limited"
124
125
126
    end

    if blank_container
127
      css_class << "container-blank"
128
129
    end

130
    css_class.join(' ')
131
  end
132
end