gitlab-org--gitlab-foss/app/assets/javascripts/copy_to_clipboard.js.coffee

38 lines
917 B
CoffeeScript
Raw Normal View History

2015-09-30 18:09:45 -04:00
#= require clipboard
genericSuccess = (e) ->
showTooltip(e.trigger, 'Copied!')
# Clear the selection and blur the trigger so it loses its border
e.clearSelection()
$(e.trigger).blur()
2015-09-30 18:09:45 -04:00
# 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'
2015-09-30 18:09:45 -04:00
showTooltip(e.trigger, "Press #{key}-C to copy")
2015-09-30 18:09:45 -04:00
showTooltip = (target, title) ->
$(target).
tooltip(
container: 'body'
html: 'true'
placement: 'auto bottom'
title: title
trigger: 'manual'
).
tooltip('show').
one('mouseleave', -> $(this).tooltip('hide'))
$ ->
clipboard = new Clipboard '[data-clipboard-target], [data-clipboard-text]'
clipboard.on 'success', genericSuccess
clipboard.on 'error', genericError