summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/copy_to_clipboard.js.coffee
blob: 9c68c5cc1bcdf377edf3a958c7dc5ffc2dc50554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#= require clipboard

$ ->
  clipboard = new Clipboard '.js-clipboard-trigger',
    text: (trigger) ->
      $target = $(trigger.nextElementSibling || trigger.previousElementSibling)
      $target.data('clipboard-text') || $target.text().trim()

  clipboard.on 'success', (e) ->
    $(e.trigger).
      tooltip(trigger: 'manual', placement: 'auto bottom', title: 'Copied!').
      tooltip('show').
      one('mouseleave', -> $(this).tooltip('hide'))

    # Clear the selection and blur the trigger so it loses its border
    e.clearSelection()
    $(e.trigger).blur()

  # Safari doesn't support `execCommand`, so instead we inform the user to
  # copy manually.
  #
  # See http://clipboardjs.com/#browser-support
  clipboard.on 'error', (e) ->
    if /Mac/i.test(navigator.userAgent)
      title = "Press ⌘-C to copy"
    else
      title = "Press Ctrl-C to copy"

    $(e.trigger).
      tooltip(trigger: 'manual', placement: 'auto bottom', html: true, title: title).
      tooltip('show').
      one('mouseleave', -> $(this).tooltip('hide'))