Skip to content

Commit 02f8c28

Browse files
committed
htdocs: Add clipboard.js
We provide our own, much smaller implementation of this currently. However, we want to be able to implement slightly different variants of this elsewhere and using an existing library helps avoid reinventing the wheel and lets us use already battle-tested code. Signed-off-by: Stephen Finucane <stephen@that.guru> Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
1 parent 29e068a commit 02f8c28

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

htdocs/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ js
6565

6666
Part of Patchwork.
6767

68+
``clipboard.min.js``
69+
70+
Modern copy to clipboard. No Flash. Just 3kb gzipped
71+
72+
This is used to allow us to "click to copy" various elements in the UI.
73+
74+
:Website: https://clipboardjs.com/
75+
:GitHub: https://github.com/zenorocha/clipboard.js/
76+
:Version: 1.7.1
77+
6878
``jquery.js``
6979

7080
jQuery is a fast, small, and feature-rich JavaScript library. It makes things

htdocs/js/clipboard.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

patchwork/templates/patchwork/patch-list.html

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,8 @@
5050
e.preventDefault();
5151
});
5252
});
53-
54-
{% if user.is_authenticated and user.profile.show_ids %}
55-
function copyToClipboard(patch_id) {
56-
input = document.createElement('input');
57-
input.setAttribute('type', 'text');
58-
input.setAttribute('value', patch_id);
59-
input = document.body.appendChild(input);
60-
input.select();
61-
document.execCommand('copy');
62-
input.remove();
63-
}
64-
{% endif %}
6553
</script>
54+
6655
<form method="post">
6756
{% csrf_token %}
6857
<input type="hidden" name="form" value="patchlistform"/>
@@ -195,9 +184,10 @@
195184
{% endif %}
196185
{% if user.is_authenticated and user.profile.show_ids %}
197186
<td>
198-
<button type="button" class="btn btn-xs"
199-
onClick="javascript:copyToClipboard('{{patch.id}}');"
200-
title="Copy to Clipboard">{{ patch.id }}</button>
187+
<button type="button" class="btn btn-xs btn-copy"
188+
data-clipboard-text="{{ patch.id }}" title="Copy to Clipboard">
189+
{{ patch.id }}
190+
</button>
201191
</td>
202192
{% endif %}
203193
<td>

templates/base.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<![endif]-->
2121
<script type="text/javascript" src="{% static "js/bootstrap.min.js" %}"></script>
2222
<script type="text/javascript" src="{% static "js/selectize.min.js" %}"></script>
23+
<script type="text/javascript" src="{% static "js/clipboard.min.js" %}"></script>
24+
<script type="text/javascript">
25+
$(document).ready(function() {
26+
new Clipboard(document.querySelectorAll('button.btn-copy'));
27+
});
28+
</script>
2329
{% block headers %}{% endblock %}
2430
</head>
2531
<body>

0 commit comments

Comments
 (0)