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-08-25 08:19:26 -04:00
button_text = data [ :button_text ] || ''
hide_tooltip = data [ :hide_tooltip ] || false
hide_button_icon = data [ :hide_button_icon ] || false
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
2017-08-25 08:19:26 -04:00
unless hide_tooltip
data = { toggle : 'tooltip' , placement : 'bottom' , container : 'body' } . merge ( data )
end
2017-04-06 17:10:14 -04:00
2017-08-25 08:19:26 -04:00
button_attributes = {
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 ,
2017-08-25 08:19:26 -04:00
aria : { label : title }
}
content_tag :button , button_attributes do
concat ( icon ( 'clipboard' , 'aria-hidden' : 'true' ) ) unless hide_button_icon
concat ( button_text )
end
2016-05-24 12:06:49 -04:00
end
2017-11-13 13:21:26 -05:00
def http_clone_button ( project , append_link : true )
2015-11-22 23:52:02 -05:00
protocol = gitlab_config . protocol . upcase
2017-11-15 14:47:22 -05:00
dropdown_description = http_dropdown_description ( protocol )
append_url = project . http_url_to_repo if append_link
2015-11-22 23:52:02 -05:00
2017-11-15 14:47:22 -05:00
dropdown_item_with_description ( protocol , dropdown_description , href : append_url )
end
2017-11-10 17:58:55 -05:00
2017-11-15 14:47:22 -05:00
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 }
else
_ ( " Create a personal access token on your account to pull or push via %{protocol}. " ) % { protocol : protocol }
2017-11-10 17:58:55 -05:00
end
2015-11-22 23:52:02 -05:00
end
2017-11-10 17:58:55 -05:00
def ssh_clone_button ( project , append_link : true )
2017-11-15 14:47:22 -05:00
dropdown_description = _ ( " You won't be able to pull or push project code via SSH until you add an SSH key to your profile " ) if current_user . try ( :require_ssh_key? )
append_url = project . ssh_url_to_repo if append_link
dropdown_item_with_description ( 'SSH' , dropdown_description , href : append_url )
end
def dropdown_item_with_description ( title , description , href : nil )
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-11-22 23:52:02 -05:00
2017-11-15 14:47:22 -05:00
content_tag ( href ? :a : :span ) ,
2017-12-05 17:40:04 -05:00
( href ? button_content : title ) ,
2017-11-15 14:47:22 -05:00
class : " #{ title . downcase } -selector " ,
href : ( href if href )
2015-11-22 23:52:02 -05:00
end
end