-
Notifications
You must be signed in to change notification settings - Fork 0
/
click-copy.lib.min.js
1 lines (1 loc) · 3.46 KB
/
click-copy.lib.min.js
1
var ClickCopy={init:function(e){clickToCopyHelper.each(e,function(e,t){1===t.childNodes.length&&0===t.childElementCount?(t.setAttribute("style","cursor: copy;"),ClickCopy.setupEventListeners(t)):t.childNodes.length>1||t.childElementCount>0?console.error("ERROR\nPlease check the element",t,"\nIt may contain an invalid item. Make sure to only add a string between the <click-copy></click-copy> tags."):console.error("ERROR\nPlease check the element",t,"\nIt should look like this:\n<click-copy>Your Text Here</click-copy>")})},setupEventListeners:function(e){clickToCopyHelper.offOn("click",e,function(e){e.stopPropagation(),clickToCopyHelper.copyToClipboard(this.innerText),ClickCopy.mouseHintOnClick(e,this)})},mouseHintOnClick:function(e,t){var n=t.getAttribute("hint-text");if(null!==n){var o={x:e.clientX+10+"px",y:e.clientY-6+"px"},i=document.createElement("div");i.classList.add("mouse-hint"),i.style.left=o.x,i.style.top=o.y,i.innerHTML=clickToCopyHelper.getMouseHintStyle();var c=document.createElement("span");i.appendChild(c),c.innerText=n,document.body.appendChild(i),clickToCopyHelper.fadeIn(i,250,function(){setTimeout(function(){clickToCopyHelper.fadeOut(i,500,function(){document.body.removeChild(i)})},1e3)})}}};document.addEventListener("DOMContentLoaded",function(e){ClickCopy.init(clickToCopyHelper.findAllClickToCopyElements())});var _ClickToCopyHelper=new function(){return{isFunction:function(e){return e&&"function"==typeof e},each:function(e,t){if(e&&clickToCopyHelper.isFunction(t))for(var n=0;n<e.length;n++)t(n,e[n])},eventsHandler:function(e,t,n,o){if(n&&!Array.isArray(n)&&!NodeList.prototype.isPrototypeOf(n)){var i=[];i.push(n),n=i}var c=t.split(" ");clickToCopyHelper.each(c,function(t,i){clickToCopyHelper.each(n,function(t,n){"addEventListener"===e?n.attachEvent?(n["e"+i+o]=o,n[i+o]=function(){n["e"+i+o](window.event)},n.attachEvent("on"+i,n[i+o])):n.addEventListener(i,o,!1):"removeEventListener"===e&&(n.detachEvent?(n["e"+i+o]=o,n[i+o]=function(){n["e"+i+o](window.event)},n.attachEvent("on"+i,n[i+o])):n.removeEventListener(i,o,!1))})})},off:function(e,t,n){this.eventsHandler("removeEventListener",e,t,n)},on:function(e,t,n){this.eventsHandler("addEventListener",e,t,n)},offOn:function(e,t,n){this.off(e,t,n),this.on(e,t,n)},fadeOut:function(e,t,n){e.style.opacity=1;var o=+new Date,i=function(){e.style.opacity=+e.style.opacity-(new Date-o)/t,o=+new Date,+e.style.opacity>0?window.requestAnimationFrame&&requestAnimationFrame(i)||setTimeout(i,16):(e.style.opacity=0,clickToCopyHelper.isFunction(n)&&n())};i()},fadeIn:function(e,t,n){e.style.opacity=0,e.style.display="block";var o=+new Date,i=function(){e.style.opacity=+e.style.opacity+(new Date-o)/t,o=+new Date,+e.style.opacity<1?window.requestAnimationFrame&&requestAnimationFrame(i)||setTimeout(i,16):(e.style.opacity=1,clickToCopyHelper.isFunction(n)&&n())};i()},findAllClickToCopyElements:function(){return document.querySelectorAll("click-copy")},copyToClipboard:function(e){var t=document.createElement("textarea");t.style.position="absolute",t.style.top="-99999px",t.style.left="-99999px",t.value=e,document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)},getMouseHintStyle:function(){return"<style>.mouse-hint { background-color: #ffffff; border: 1px solid #454545; border-radius: 1px; box-shadow: 0 0 8px 0 #00000050; color: #454545; display: none; padding: 0 4px; pointer-events: none; position: absolute; z-index: 1000;}</style>"}}},clickToCopyHelper=_ClickToCopyHelper;