copy_to_clipboard.js.coffee 917 Bytes
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2
#= require clipboard

3 4 5 6 7 8
genericSuccess = (e) ->
  showTooltip(e.trigger, 'Copied!')

  # Clear the selection and blur the trigger so it loses its border
  e.clearSelection()
  $(e.trigger).blur()
Robert Speicher's avatar
Robert Speicher committed
9

10 11 12 13 14 15 16 17 18
# Safari doesn't support `execCommand`, so instead we inform the user to
# copy manually.
#
# See http://clipboardjs.com/#browser-support
genericError = (e) ->
  if /Mac/i.test(navigator.userAgent)
    key = '⌘' # Command
  else
    key = 'Ctrl'
Robert Speicher's avatar
Robert Speicher committed
19

20
  showTooltip(e.trigger, "Press #{key}-C to copy")
Robert Speicher's avatar
Robert Speicher committed
21

22 23 24 25 26 27 28 29 30 31 32
showTooltip = (target, title) ->
  $(target).
    tooltip(
      container: 'body'
      html: 'true'
      placement: 'auto bottom'
      title: title
      trigger: 'manual'
    ).
    tooltip('show').
    one('mouseleave', -> $(this).tooltip('hide'))
33

34 35 36 37
$ ->
  clipboard = new Clipboard '[data-clipboard-target], [data-clipboard-text]'
  clipboard.on 'success', genericSuccess
  clipboard.on 'error',   genericError