Module: ButtonHelper
- Defined in:
- app/helpers/button_helper.rb
Instance Method Summary collapse
-
#clipboard_button(data = {}) ⇒ Object
Output a “Copy to Clipboard” button.
- #dropdown_item_with_description(title, description, href: nil, data: nil, default: false) ⇒ Object
- #http_clone_button(project, append_link: true) ⇒ Object
- #http_dropdown_description(protocol) ⇒ Object
- #ssh_clone_button(project, append_link: true) ⇒ Object
Instance Method Details
#clipboard_button(data = {}) ⇒ Object
Output a “Copy to Clipboard” button
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)
Examples:
# Define the clipboard's text
(text: "Foo")
# => "<button class='...' data-clipboard-text='Foo'>...</button>"
# Define the target element
(target: "div#foo")
# => "<button class='...' data-clipboard-target='div#foo'>...</button>"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/helpers/button_helper.rb', line 22 def (data = {}) css_class = data[:class] || 'btn-clipboard btn-transparent' title = data[:title] || _('Copy') = data[:button_text] || '' hide_tooltip = data[:hide_tooltip] || false = data[:hide_button_icon] || false # 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 unless hide_tooltip data = { toggle: 'tooltip', placement: 'bottom', container: 'body' }.merge(data) end = { class: "btn #{css_class}", data: data, type: :button, title: title, aria: { label: title } } content_tag :button, do concat(sprite_icon('copy-to-clipboard')) unless concat() end end |
#dropdown_item_with_description(title, description, href: nil, data: nil, default: false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/helpers/button_helper.rb', line 88 def dropdown_item_with_description(title, description, href: nil, data: nil, default: false) active_class = "is-active" if default = content_tag(:strong, title, class: 'dropdown-menu-inner-title') << content_tag(:span, description, class: 'dropdown-menu-inner-content') if description content_tag (href ? :a : :span), (href ? : title), class: "#{title.downcase}-selector #{active_class}", href: href, data: data end |
#http_clone_button(project, append_link: true) ⇒ Object
61 62 63 64 65 66 67 |
# File 'app/helpers/button_helper.rb', line 61 def (project, append_link: true) protocol = gitlab_config.protocol.upcase dropdown_description = http_dropdown_description(protocol) append_url = project.http_url_to_repo if append_link dropdown_item_with_description(protocol, dropdown_description, href: append_url, data: { clone_type: 'http' }) end |
#http_dropdown_description(protocol) ⇒ Object
69 70 71 72 73 74 75 |
# File 'app/helpers/button_helper.rb', line 69 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 } elsif current_user.try(:require_personal_access_token_creation_for_git_auth?) _("Create a personal access token on your account to pull or push via %{protocol}.") % { protocol: protocol } end end |
#ssh_clone_button(project, append_link: true) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'app/helpers/button_helper.rb', line 77 def (project, append_link: true) if Gitlab::CurrentSettings. && current_user.try(:require_ssh_key?) dropdown_description = _("You won't be able to pull or push project code via SSH until you add an SSH key to your profile") end append_url = project.ssh_url_to_repo if append_link dropdown_item_with_description('SSH', dropdown_description, href: append_url, data: { clone_type: 'ssh' }) end |