Add copy-to-clipboard feature
Change-Id: If531e8bb3b45b8659d02156e4b4eb76c253492b1
diff --git a/dev/js/spec/utilSpec.js b/dev/js/spec/utilSpec.js
index 895a5e9..0c1afa5 100644
--- a/dev/js/spec/utilSpec.js
+++ b/dev/js/spec/utilSpec.js
@@ -37,4 +37,21 @@
expect(div.lastChild.classList.contains("hide")).toBeTruthy();
});
});
+
+ describe('KorAP.util.initCopyToClipboard', function () {
+ it('should be initializable', function () {
+ const div = document.createElement('div');
+ let input = div.addE('input');
+ input.value = "abcde";
+ input.setAttribute('type', 'text');
+ input.setAttribute('class', 'copy-to-clipboard');
+ expect(div.children.length).toEqual(1);
+ initCopyToClipboard(div);
+ expect(div.children.length).toEqual(2);
+ expect(div.lastChild.tagName).toEqual("A");
+ });
+
+ // document.execCommand() can't be tested without user
+ // intervention.
+ });
});
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index 98dc54a..d246a8c 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -437,6 +437,13 @@
KorAP.Panel['query'] = queryPanel;
}
+
+ /**
+ * Initialize password toggle.
+ */
+ initCopyToClipboard(d);
+
+
/**
* Initialize password toggle.
*/
diff --git a/dev/js/src/util.js b/dev/js/src/util.js
index 14480bc..86e6214 100644
--- a/dev/js/src/util.js
+++ b/dev/js/src/util.js
@@ -110,6 +110,25 @@
};
+/**
+ * Add option to copy to clipboard.
+ */
+function initCopyToClipboard (element) {
+ const el = element.querySelectorAll("input.copy-to-clipboard");
+ for (let x = 0; x < el.length; x++) {
+ const text = el[x];
+ const a = document.createElement('a');
+ a.classList.add('copy-to-clipboard');
+ a.addEventListener('click', function () {
+ text.select();
+ text.setSelectionRange(0, 99999);
+ document.execCommand("copy");
+ });
+ text.parentNode.insertBefore(a, text.nextSibling);
+ };
+};
+
+
define(function () {
// Todo: That's double now!
KorAP.API = KorAP.API || {};