BigW Consortium Gitlab

button_helper.rb 2.58 KB
Newer Older
1
module ButtonHelper
2 3
  # Output a "Copy to Clipboard" button
  #
4 5 6 7
  # 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)
8 9 10 11
  #
  # Examples:
  #
  #   # Define the clipboard's text
12
  #   clipboard_button(text: "Foo")
13 14 15
  #   # => "<button class='...' data-clipboard-text='Foo'>...</button>"
  #
  #   # Define the target element
16
  #   clipboard_button(target: "div#foo")
17
  #   # => "<button class='...' data-clipboard-target='div#foo'>...</button>"
18 19
  #
  # See http://clipboardjs.com/#usage
Phil Hughes committed
20
  def clipboard_button(data = {})
21
    css_class = data[:class] || 'btn-clipboard btn-transparent'
22
    title = data[:title] || 'Copy to clipboard'
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

    # 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

38
    data = { toggle: 'tooltip', placement: 'bottom', container: 'body' }.merge(data)
39

40
    content_tag :button,
41
      icon('clipboard', 'aria-hidden': 'true'),
42
      class: "btn #{css_class}",
43
      data: data,
44
      type: :button,
45 46 47 48
      title: title,
      aria: {
        label: title
      }
Phil Hughes committed
49 50
  end

51
  def http_clone_button(project, placement = 'right', append_link: true)
52
    klass = 'http-selector'
53
    klass << ' has-tooltip' if current_user.try(:require_password?)
54 55 56

    protocol = gitlab_config.protocol.upcase

57
    content_tag (append_link ? :a : :span), protocol,
58
      class: klass,
59
      href: (project.http_url_to_repo if append_link),
60
      data: {
61
        html: true,
62
        placement: placement,
63
        container: 'body',
64
        title: _("Set a password on your account to pull or push via %{protocol}") % { protocol: protocol }
65
      }
66 67
  end

68
  def ssh_clone_button(project, placement = 'right', append_link: true)
69
    klass = 'ssh-selector'
70
    klass << ' has-tooltip' if current_user.try(:require_ssh_key?)
71

72
    content_tag (append_link ? :a : :span), 'SSH',
73
      class: klass,
74
      href: (project.ssh_url_to_repo if append_link),
75
      data: {
76
        html: true,
77
        placement: placement,
78
        container: 'body',
79
        title: _('Add an SSH key to your profile to pull or push via SSH.')
80
      }
81 82
  end
end