summaryrefslogtreecommitdiffstats
path: root/asset/js/widget/CopyToClipboard.js
diff options
context:
space:
mode:
Diffstat (limited to 'asset/js/widget/CopyToClipboard.js')
-rw-r--r--asset/js/widget/CopyToClipboard.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/asset/js/widget/CopyToClipboard.js b/asset/js/widget/CopyToClipboard.js
new file mode 100644
index 0000000..e3b348c
--- /dev/null
+++ b/asset/js/widget/CopyToClipboard.js
@@ -0,0 +1,49 @@
+define(["../notjQuery"], function ($) {
+
+ "use strict";
+
+ class CopyToClipboard {
+ constructor(button)
+ {
+ button.classList.add('active');
+ button.removeAttribute('tabindex');
+ $(button).on('click', null, this.onClick, this);
+ }
+
+ onClick(event)
+ {
+ let button = event.currentTarget;
+ let clipboardSource = button.parentElement.querySelector("[data-clipboard-source]");
+ let copyText;
+
+ if (clipboardSource) {
+ copyText = clipboardSource.innerText;
+ } else {
+ throw new Error('Clipboard source is required but not provided');
+ }
+
+ if (navigator.clipboard) {
+ navigator.clipboard.writeText(copyText).then(() => {
+ let previousHtml = button.innerHTML;
+ button.innerText = button.dataset.copiedLabel;
+ button.classList.add('copied');
+
+ setTimeout(() => {
+ // after 4 second, reset it.
+ button.classList.remove('copied');
+ button.innerHTML = previousHtml;
+ }, 4000);
+ }).catch((err) => {
+ console.error('Failed to copy: ', err);
+ });
+ } else {
+ throw new Error('Copy to clipboard requires HTTPS connection');
+ }
+
+ event.stopPropagation();
+ event.preventDefault();
+ }
+ }
+
+ return CopyToClipboard;
+});