(function(F){ const D = F.dom; F.copyButton = function f(e, opt){ if('string'===typeof e){ e = document.querySelector(e); } opt = F.mergeLastWins(f.defaultOptions, opt); if(opt.cssClass){ D.addClass(e, opt.cssClass); } var srcId, srcElem; if(opt.copyFromElement){ srcElem = opt.copyFromElement; }else if((srcId = opt.copyFromId || e.dataset.copyFromId)){ srcElem = document.querySelector('#'+srcId); } const extract = opt.extractText || ( undefined===srcElem.value ? ()=>srcElem.innerText : ()=>srcElem.value ); D.copyStyle(e, opt.style); e.addEventListener( 'click', function(ev){ ev.preventDefault(); ev.stopPropagation(); if(e.disabled) return; const txt = extract.call(opt); if(txt && D.copyTextToClipboard(txt)){ e.dispatchEvent(new CustomEvent('text-copied',{ detail: {text: txt} })); } }, false ); if('function' === typeof opt.oncopy){ e.addEventListener('text-copied', opt.oncopy, false); } if(e.childElementCount!=1 || e.firstChild.tagName!='SPAN'){ D.append(D.clearElement(e), D.span()); } return e; }; F.copyButton.defaultOptions = { cssClass: 'copy-button', oncopy: undefined, style: {} }; })(window.fossil);