debian-mirror-gitlab/app/helpers/page_layout_helper.rb

143 lines
3.3 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
module PageLayoutHelper
def page_title(*titles)
@page_title ||= []
@page_title.push(*titles.compact) if titles.any?
2018-03-17 18:26:18 +05:30
if titles.any? && !defined?(@breadcrumb_title)
2017-09-10 17:25:29 +05:30
@breadcrumb_title = @page_title.last
end
2018-12-13 13:39:08 +05:30
# Segments are separated by middot
2018-03-17 18:26:18 +05:30
@page_title.join(" · ")
2015-09-11 14:41:01 +05:30
end
# 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?
@page_description = description.squish
elsif @page_description.present?
2019-09-04 21:01:54 +05:30
sanitize(@page_description.truncate_words(30), tags: [])
end
end
2017-08-17 22:00:37 +05:30
def favicon
2018-11-08 19:23:39 +05:30
Gitlab::Favicon.main
2017-08-17 22:00:37 +05:30
end
def page_image
default = image_url('gitlab_logo.png')
subject = @project || @user || @group
image = subject.avatar_url if subject.present?
image || default
end
# 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 ||= {}
2016-08-24 12:49:21 +05:30
@page_card_attributes = map.reject { |_, v| v.blank? } if map.present?
@page_card_attributes
end
def page_card_meta_tags
2018-12-05 23:21:45 +05:30
tags = []
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
2018-12-05 23:21:45 +05:30
tags.join.html_safe
end
2015-09-11 14:41:01 +05:30
def header_title(title = nil, title_url = nil)
if title
@header_title = title
@header_title_url = title_url
else
2018-03-17 18:26:18 +05:30
return @header_title unless @header_title_url
breadcrumb_list_item(link_to(@header_title, @header_title_url))
2015-09-11 14:41:01 +05:30
end
end
def sidebar(name = nil)
if name
@sidebar = name
else
@sidebar
end
end
2015-09-25 12:07:36 +05:30
2016-06-02 11:05:42 +05:30
def nav(name = nil)
if name
@nav = name
else
@nav
end
end
2020-06-23 00:09:42 +05:30
# This helper ensures there is always a default `Gitlab::SearchContext` available
# to all controller that use the application layout.
def search_context
strong_memoize(:search_context) do
next super if defined?(super)
Gitlab::SearchContext::Builder.new(controller.view_context).build!
end
end
2016-11-03 12:29:30 +05:30
def fluid_layout
current_user && current_user.layout == "fluid"
2015-09-25 12:07:36 +05:30
end
def blank_container(enabled = false)
if @blank_container.nil?
@blank_container = enabled
else
@blank_container
end
end
def container_class
2018-12-05 23:21:45 +05:30
css_class = ["container-fluid"]
2015-09-25 12:07:36 +05:30
unless fluid_layout
2018-12-05 23:21:45 +05:30
css_class << "container-limited"
2015-09-25 12:07:36 +05:30
end
if blank_container
2018-12-05 23:21:45 +05:30
css_class << "container-blank"
2015-09-25 12:07:36 +05:30
end
2018-12-05 23:21:45 +05:30
css_class.join(' ')
2015-09-25 12:07:36 +05:30
end
2015-09-11 14:41:01 +05:30
end