2015-11-22 23:52:02 -05:00
|
|
|
module ButtonHelper
|
2015-11-24 18:35:24 -05:00
|
|
|
# Output a "Copy to Clipboard" button
|
|
|
|
#
|
2017-04-06 17:10:14 -04:00
|
|
|
# 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-11-24 18:35:24 -05:00
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# # Define the clipboard's text
|
2017-04-06 17:10:14 -04:00
|
|
|
# clipboard_button(text: "Foo")
|
2015-11-24 18:35:24 -05:00
|
|
|
# # => "<button class='...' data-clipboard-text='Foo'>...</button>"
|
|
|
|
#
|
|
|
|
# # Define the target element
|
2017-04-06 17:10:14 -04:00
|
|
|
# clipboard_button(target: "div#foo")
|
2015-12-15 16:11:01 -05:00
|
|
|
# # => "<button class='...' data-clipboard-target='div#foo'>...</button>"
|
2015-11-24 18:35:24 -05:00
|
|
|
#
|
|
|
|
# See http://clipboardjs.com/#usage
|
2016-05-25 04:11:47 -04:00
|
|
|
def clipboard_button(data = {})
|
2016-10-11 05:17:56 -04:00
|
|
|
css_class = data[:class] || 'btn-clipboard btn-transparent'
|
2016-12-09 16:48:23 -05:00
|
|
|
title = data[:title] || 'Copy to clipboard'
|
2017-04-06 17:10:14 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2016-07-08 21:06:12 -04:00
|
|
|
data = { toggle: 'tooltip', placement: 'bottom', container: 'body' }.merge(data)
|
2017-04-06 17:10:14 -04:00
|
|
|
|
2015-11-22 23:53:55 -05:00
|
|
|
content_tag :button,
|
2017-02-28 14:28:58 -05:00
|
|
|
icon('clipboard', 'aria-hidden': 'true'),
|
2016-10-10 04:59:35 -04:00
|
|
|
class: "btn #{css_class}",
|
2015-11-24 18:35:24 -05:00
|
|
|
data: data,
|
2016-07-08 21:06:12 -04:00
|
|
|
type: :button,
|
2017-05-12 06:22:50 -04:00
|
|
|
title: title,
|
|
|
|
aria: {
|
|
|
|
label: title
|
|
|
|
}
|
2016-05-24 12:06:49 -04:00
|
|
|
end
|
|
|
|
|
2016-07-05 17:48:48 -04:00
|
|
|
def http_clone_button(project, placement = 'right', append_link: true)
|
2016-03-20 01:46:56 -04:00
|
|
|
klass = 'http-selector'
|
2017-06-27 14:02:09 -04:00
|
|
|
klass << ' has-tooltip' if current_user.try(:require_password_creation?) || current_user.try(:require_personal_access_token_creation_for_git_auth?)
|
2015-11-22 23:52:02 -05:00
|
|
|
|
|
|
|
protocol = gitlab_config.protocol.upcase
|
|
|
|
|
2017-06-12 12:13:22 -04:00
|
|
|
tooltip_title =
|
2017-06-27 14:02:09 -04:00
|
|
|
if current_user.try(:require_password_creation?)
|
2017-06-12 12:13:22 -04:00
|
|
|
_("Set a password on your account to pull or push via %{protocol}.") % { protocol: protocol }
|
|
|
|
else
|
|
|
|
_("Create a personal access token on your account to pull or push via %{protocol}.") % { protocol: protocol }
|
|
|
|
end
|
|
|
|
|
2016-07-05 17:48:48 -04:00
|
|
|
content_tag (append_link ? :a : :span), protocol,
|
2015-11-22 23:52:02 -05:00
|
|
|
class: klass,
|
2017-05-30 08:11:58 -04:00
|
|
|
href: (project.http_url_to_repo if append_link),
|
2015-11-22 23:52:02 -05:00
|
|
|
data: {
|
2016-03-20 01:46:56 -04:00
|
|
|
html: true,
|
2016-06-20 21:40:56 -04:00
|
|
|
placement: placement,
|
2016-03-20 10:52:06 -04:00
|
|
|
container: 'body',
|
2017-06-12 12:13:22 -04:00
|
|
|
title: tooltip_title
|
2016-03-20 01:46:56 -04:00
|
|
|
}
|
2015-11-22 23:52:02 -05:00
|
|
|
end
|
|
|
|
|
2016-07-05 17:48:48 -04:00
|
|
|
def ssh_clone_button(project, placement = 'right', append_link: true)
|
2016-03-20 01:46:56 -04:00
|
|
|
klass = 'ssh-selector'
|
2016-03-20 15:01:46 -04:00
|
|
|
klass << ' has-tooltip' if current_user.try(:require_ssh_key?)
|
2015-11-22 23:52:02 -05:00
|
|
|
|
2016-07-05 17:48:48 -04:00
|
|
|
content_tag (append_link ? :a : :span), 'SSH',
|
2015-11-22 23:52:02 -05:00
|
|
|
class: klass,
|
2016-06-29 16:25:04 -04:00
|
|
|
href: (project.ssh_url_to_repo if append_link),
|
2015-11-22 23:52:02 -05:00
|
|
|
data: {
|
2016-03-20 01:46:56 -04:00
|
|
|
html: true,
|
2016-06-20 21:40:56 -04:00
|
|
|
placement: placement,
|
2016-03-20 10:52:06 -04:00
|
|
|
container: 'body',
|
2017-06-07 16:13:44 -04:00
|
|
|
title: _('Add an SSH key to your profile to pull or push via SSH.')
|
2016-03-20 01:46:56 -04:00
|
|
|
}
|
2015-11-22 23:52:02 -05:00
|
|
|
end
|
|
|
|
end
|