BigW Consortium Gitlab

copy_to_clipboard.js 1.38 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, one-var, no-var, one-var-declaration-per-line, no-undef, prefer-template, quotes, no-unused-vars, prefer-arrow-callback, padded-blocks, max-len */
Fatih Acet committed
2 3 4 5 6 7 8 9

/*= require clipboard */

(function() {
  var genericError, genericSuccess, showTooltip;

  genericSuccess = function(e) {
    showTooltip(e.trigger, 'Copied!');
10
    // Clear the selection and blur the trigger so it loses its border
Fatih Acet committed
11 12 13 14
    e.clearSelection();
    return $(e.trigger).blur();
  };

15 16 17 18
  // Safari doesn't support `execCommand`, so instead we inform the user to
  // copy manually.
  //
  // See http://clipboardjs.com/#browser-support
Fatih Acet committed
19 20 21
  genericError = function(e) {
    var key;
    if (/Mac/i.test(navigator.userAgent)) {
22
      key = '⌘'; // Command
Fatih Acet committed
23 24 25 26 27 28 29
    } else {
      key = 'Ctrl';
    }
    return showTooltip(e.trigger, "Press " + key + "-C to copy");
  };

  showTooltip = function(target, title) {
30 31 32 33 34 35 36 37 38
    var $target = $(target);
    var originalTitle = $target.data('original-title');

    $target
      .attr('title', 'Copied!')
      .tooltip('fixTitle')
      .tooltip('show')
      .attr('title', originalTitle)
      .tooltip('fixTitle');
Fatih Acet committed
39 40 41 42
  };

  $(function() {
    var clipboard;
Phil Hughes committed
43

44 45 46
    clipboard = new Clipboard('[data-clipboard-target], [data-clipboard-text]');
    clipboard.on('success', genericSuccess);
    return clipboard.on('error', genericError);
Fatih Acet committed
47 48 49
  });

}).call(this);