summaryrefslogtreecommitdiff
path: root/app/assets
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-11-24 18:35:24 -0500
committerRobert Speicher <rspeicher@gmail.com>2015-11-24 19:12:03 -0500
commit7dab8ed739359bc579d8bc4d3de61816993ca57d (patch)
tree9271059f8a674f36c9d49617e288087aba9c040b /app/assets
parentacc0f162c864d2a061461467473fca8761b6611f (diff)
downloadgitlab-ce-7dab8ed739359bc579d8bc4d3de61816993ca57d.tar.gz
Rework the copy_to_clipboard logic
It needed to be more flexible in how we set the target text or element.
Diffstat (limited to 'app/assets')
-rw-r--r--app/assets/javascripts/copy_to_clipboard.js.coffee57
1 files changed, 31 insertions, 26 deletions
diff --git a/app/assets/javascripts/copy_to_clipboard.js.coffee b/app/assets/javascripts/copy_to_clipboard.js.coffee
index 9c68c5cc1bc..24301e01b10 100644
--- a/app/assets/javascripts/copy_to_clipboard.js.coffee
+++ b/app/assets/javascripts/copy_to_clipboard.js.coffee
@@ -1,32 +1,37 @@
#= require clipboard
-$ ->
- clipboard = new Clipboard '.js-clipboard-trigger',
- text: (trigger) ->
- $target = $(trigger.nextElementSibling || trigger.previousElementSibling)
- $target.data('clipboard-text') || $target.text().trim()
+genericSuccess = (e) ->
+ showTooltip(e.trigger, 'Copied!')
+
+ # Clear the selection and blur the trigger so it loses its border
+ e.clearSelection()
+ $(e.trigger).blur()
- clipboard.on 'success', (e) ->
- $(e.trigger).
- tooltip(trigger: 'manual', placement: 'auto bottom', title: 'Copied!').
- tooltip('show').
- one('mouseleave', -> $(this).tooltip('hide'))
+# 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 = '&#8984;' # Command
+ else
+ key = 'Ctrl'
- # Clear the selection and blur the trigger so it loses its border
- e.clearSelection()
- $(e.trigger).blur()
+ showTooltip(e.trigger, "Press #{key}-C to copy")
- # 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 &#8984;-C to copy"
- else
- title = "Press Ctrl-C to copy"
+showTooltip = (target, title) ->
+ $(target).
+ tooltip(
+ container: 'body'
+ html: 'true'
+ placement: 'auto bottom'
+ title: title
+ trigger: 'manual'
+ ).
+ tooltip('show').
+ one('mouseleave', -> $(this).tooltip('hide'))
- $(e.trigger).
- tooltip(trigger: 'manual', placement: 'auto bottom', html: true, title: title).
- tooltip('show').
- one('mouseleave', -> $(this).tooltip('hide'))
+$ ->
+ clipboard = new Clipboard '[data-clipboard-target], [data-clipboard-text]'
+ clipboard.on 'success', genericSuccess
+ clipboard.on 'error', genericError