page_layout_helper.rb 3.0 KB
Newer Older
1
# coding: utf-8
2 3
# frozen_string_literal: true

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

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

P
Phil Hughes 已提交
10
    if titles.any? && !defined?(@breadcrumb_title)
P
Phil Hughes 已提交
11
      @breadcrumb_title = @page_title.last
12 13
    end

G
George Tsiolis 已提交
14
    # Segments are separated by middot
15
    @page_title.join(" · ")
16 17
  end

R
Robert Speicher 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  # 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?
38
      @page_description = description.squish
39
    elsif @page_description.present?
40
      sanitize(@page_description.truncate_words(30), tags: [])
R
Robert Speicher 已提交
41 42 43
    end
  end

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

R
Robert Speicher 已提交
48 49 50
  def page_image
    default = image_url('gitlab_logo.png')

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

53 54
    image = subject.avatar_url if subject.present?
    image || default
R
Robert Speicher 已提交
55 56
  end

57 58 59 60 61 62 63 64 65
  # 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 ||= {}
66
    @page_card_attributes = map.reject { |_, v| v.blank? } if map.present?
67 68 69 70
    @page_card_attributes
  end

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

    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

78
    tags.join.html_safe
79 80
  end

81 82 83 84 85
  def header_title(title = nil, title_url = nil)
    if title
      @header_title     = title
      @header_title_url = title_url
    else
P
Phil Hughes 已提交
86 87
      return @header_title unless @header_title_url

P
Phil Hughes 已提交
88
      breadcrumb_list_item(link_to(@header_title, @header_title_url))
89 90 91 92 93 94 95 96 97 98
    end
  end

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

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

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

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

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

D
Dmitriy Zaporozhets 已提交
123
    unless fluid_layout
124
      css_class << "container-limited"
125 126 127
    end

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

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