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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
module ButtonHelper
# Output a "Copy to Clipboard" button
#
2017-08-17 22:00:37 +05:30
# data - Data attributes passed to `content_tag` (default: {}):
# :text - Text to copy (optional)
# :gfm - GitLab Flavored Markdown to copy, if different from `text` (optional)
# :target - Selector for target element to copy from (optional)
2015-12-23 02:04:40 +05:30
#
# Examples:
#
# # Define the clipboard's text
2017-08-17 22:00:37 +05:30
# clipboard_button(text: "Foo")
2015-12-23 02:04:40 +05:30
# # => "<button class='...' data-clipboard-text='Foo'>...</button>"
#
# # Define the target element
2017-08-17 22:00:37 +05:30
# clipboard_button(target: "div#foo")
2015-12-23 02:04:40 +05:30
# # => "<button class='...' data-clipboard-target='div#foo'>...</button>"
#
# See http://clipboardjs.com/#usage
def clipboard_button(data = {})
2023-03-17 16:20:25 +05:30
css_class = data.delete(:class) || 'btn-clipboard gl-button btn-default-tertiary btn-icon btn-sm'
title = data.delete(:title) || _('Copy')
2021-10-27 15:23:28 +05:30
button_text = data[:button_text] || nil
2018-03-17 18:26:18 +05:30
hide_tooltip = data[:hide_tooltip] || false
hide_button_icon = data[:hide_button_icon] || false
2021-04-29 21:17:54 +05:30
item_prop = data[:itemprop] || nil
2017-08-17 22:00:37 +05:30
# This supports code in app/assets/javascripts/copy_to_clipboard.js that
# works around ClipboardJS limitations to allow the context-specific copy/pasting of plain text or GFM.
if text = data.delete(:text)
data[:clipboard_text] =
if gfm = data.delete(:gfm)
{ text: text, gfm: gfm }
else
text
end
end
target = data.delete(:target)
data[:clipboard_target] = target if target
2018-03-17 18:26:18 +05:30
unless hide_tooltip
data = { toggle: 'tooltip', placement: 'bottom', container: 'body' }.merge(data)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
button_attributes = {
2016-11-03 12:29:30 +05:30
class: "btn #{css_class}",
2016-06-22 15:30:34 +05:30
data: data,
2016-08-24 12:49:21 +05:30
type: :button,
2017-09-10 17:25:29 +05:30
title: title,
2022-03-02 08:16:31 +05:30
aria: { label: title, live: 'polite' },
2021-04-29 21:17:54 +05:30
itemprop: item_prop
2018-03-17 18:26:18 +05:30
}
2016-06-22 15:30:34 +05:30
2018-03-17 18:26:18 +05:30
content_tag :button, button_attributes do
2021-10-27 15:23:28 +05:30
concat(sprite_icon('copy-to-clipboard', css_class: ['gl-icon', *('gl-button-icon' unless button_text.nil?)].join(' '))) unless hide_button_icon
concat(content_tag(:span, button_text, class: 'gl-button-text')) unless button_text.nil?
2018-03-17 18:26:18 +05:30
end
end
2015-12-23 02:04:40 +05:30
2021-02-22 17:27:13 +05:30
def http_clone_button(container, append_link: true)
2015-12-23 02:04:40 +05:30
protocol = gitlab_config.protocol.upcase
2018-03-17 18:26:18 +05:30
dropdown_description = http_dropdown_description(protocol)
2021-02-22 17:27:13 +05:30
append_url = container.http_url_to_repo if append_link
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
dropdown_item_with_description(protocol, dropdown_description, href: append_url, data: { clone_type: 'http' })
2018-03-17 18:26:18 +05:30
end
def http_dropdown_description(protocol)
if current_user.try(:require_password_creation_for_git?)
_("Set a password on your account to pull or push via %{protocol}.") % { protocol: protocol }
2018-11-20 20:47:30 +05:30
elsif current_user.try(:require_personal_access_token_creation_for_git_auth?)
2018-03-17 18:26:18 +05:30
_("Create a personal access token on your account to pull or push via %{protocol}.") % { protocol: protocol }
end
end
2015-12-23 02:04:40 +05:30
2021-02-22 17:27:13 +05:30
def ssh_clone_button(container, append_link: true)
2018-11-20 20:47:30 +05:30
if Gitlab::CurrentSettings.user_show_add_ssh_key_message? &&
current_user.try(:require_ssh_key?)
2021-02-22 17:27:13 +05:30
dropdown_description = s_("MissingSSHKeyWarningLink|You won't be able to pull or push repositories via SSH until you add an SSH key to your profile")
2018-11-20 20:47:30 +05:30
end
2021-02-22 17:27:13 +05:30
append_url = container.ssh_url_to_repo if append_link
2017-09-10 17:25:29 +05:30
2018-11-20 20:47:30 +05:30
dropdown_item_with_description('SSH', dropdown_description, href: append_url, data: { clone_type: 'ssh' })
2015-12-23 02:04:40 +05:30
end
2019-02-15 15:39:39 +05:30
def dropdown_item_with_description(title, description, href: nil, data: nil, default: false)
active_class = "is-active" if default
2018-03-17 18:26:18 +05:30
button_content = content_tag(:strong, title, class: 'dropdown-menu-inner-title')
button_content << content_tag(:span, description, class: 'dropdown-menu-inner-content') if description
2015-12-23 02:04:40 +05:30
2018-03-17 18:26:18 +05:30
content_tag (href ? :a : :span),
(href ? button_content : title),
2019-02-15 15:39:39 +05:30
class: "#{title.downcase}-selector #{active_class}",
2020-05-24 23:13:21 +05:30
href: href,
data: data
2015-12-23 02:04:40 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ButtonHelper.prepend_mod_with('ButtonHelper')