File: D:/HostingSpaces/stafa/stafa.nl/resources/js/kms/attributes/copyText.js
/**
* Give this class an htmlElement with a data-to-copy attribute,
* and it will copy the text in the value of the attribute to your clipboard
* when you click on it.
*/
class CopyText {
constructor(element) {
this.element = element;
if(!'toCopy' in this.element.dataset) {
console.error('CopyText: The given element must have a data-to-copy attribute with the value that you want to copy. But did not have that attribute.', element);
}
this.addEventListeners();
}
addEventListeners() {
this.element.addEventListener('click', this.clickHandler.bind(this))
}
clickHandler(mouseEvent) {
//Prevent the event from following a button if element is a button
mouseEvent.preventDefault();
//Get the text to copy
let text = this.element.dataset.toCopy;
//Temporary create an input element
let input = document.createElement('input');
document.body.appendChild(input);
input.value = text;
input.select();
document.execCommand("copy");
document.body.removeChild(input);
input = null;
}
}