gaber-elsayed

clipboard js

Apr 23rd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <script>
  2.  
  3. var copied = 'تم النسخ';
  4. var clipboardDemos = new ClipboardJS('[data-clipboard]');
  5. clipboardDemos.on('success', function(e) {
  6. e.clearSelection();
  7. showTooltip(e.trigger, copied);
  8. });
  9. clipboardDemos.on('error', function(e) {
  10. showTooltip(e.trigger, fallbackMessage(e.action));
  11. });
  12.  
  13. // Tooltip code
  14. var btns = document.querySelectorAll('.copy-btn');
  15. for (var i = 0; i < btns.length; i++) {
  16. btns[i].addEventListener('mouseleave', clearTooltip);
  17. btns[i].addEventListener('blur', clearTooltip);
  18. }
  19. function clearTooltip(e) {
  20. e.currentTarget.removeAttribute('aria-label');
  21. }
  22. function showTooltip(elem, msg) {
  23. $('.copy-btn').addClass('tooltipped');
  24. $('.copy-btn').addClass('tooltipped-s');
  25. elem.setAttribute('aria-label', msg);
  26. }
  27. function fallbackMessage(action) {
  28. var actionMsg = '';
  29. var actionKey = (action === 'cut' ? 'X' : 'C');
  30. if (/iPhone|iPad/i.test(navigator.userAgent)) {
  31. actionMsg = 'No support :(';
  32. } else if (/Mac/i.test(navigator.userAgent)) {
  33. actionMsg = 'Press ⌘-' + actionKey + ' to ' + action;
  34. } else {
  35. actionMsg = 'Press Ctrl-' + actionKey + ' to ' + action;
  36. }
  37. return actionMsg;
  38. }
  39. </script>
  40.  
Advertisement
Add Comment
Please, Sign In to add comment