summaryrefslogtreecommitdiffstats
path: root/src/main/resources/clipboard/gitblit-ctcbtn.js
blob: ddb2ddada5c1c848f62d3fead2d20cb15e21d60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Instantiate the clipboarding
var clipboard = new ClipboardJS('.ctcbtn');

clipboard.on('success', function (e) {
  showTooltip(e.trigger, "Copied!");
});

clipboard.on('error', function (e) {
  showTooltip(e.trigger, fallbackMessage(e.action));
});

// Attach events to buttons to clear tooltip again
var btns = document.querySelectorAll('.ctcbtn');
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener('mouseleave', clearTooltip);
  btns[i].addEventListener('blur', clearTooltip);
}


function findTooltipped(elem) {
  do {
    if (elem.classList.contains('tooltipped')) return elem;
    elem = elem.parentElement;
  } while (elem != null);
  return null;
}

// Show or hide tooltip by setting the tooltipped-active class
// on a parent that contains tooltipped. Since the copy button
// could be and image, or be hidden after clicking, the tooltipped
// element might be higher in the hierarchy.
var ttset;
function showTooltip(elem, msg) {
  let ttelem = findTooltipped(elem);
  if (ttelem != null) {
    ttelem.classList.add('tooltipped-active');
    ttelem.setAttribute('data-tt-text', msg);
    ttset=Date.now();
  }
  else {
    console.warn("Could not find any tooltipped element for clipboard button.", elem);
  }
}

function clearTooltip(e) {
  let ttelem = findTooltipped(e.currentTarget);
  if (ttelem != null) {
    let now = Date.now();
    if (now - ttset < 500) {
        // Give the tooltip some time to display
        setTimeout(function(){ttelem.classList.remove('tooltipped-active')}, 1000)
    }
    else {
        ttelem.classList.remove('tooltipped-active');
    }
  }
  else {
    console.warn("Could not find any tooltipped element for clipboard button.", e.currentTarget);
  }
}

// If the API is not supported, at least fall back to a message saying
// that now that the text is selected, Ctrl-C can be used.
// This is still a problem in the repo URL dropdown. When it is hidden, Ctrl-C doesn't work.
function fallbackMessage(action) {
  var actionMsg = "";
  if (/Mac/i.test(navigator.userAgent)) {
    actionMsg = "Press ⌘-C to copy";
  }
  else {
    actionMsg = "Press Ctrl-C to copy";
  }
  return actionMsg;
}