Fossil SCM
Code maintenance for the `copybtn.js' script: Remove global data, reference DOM elements by function-binding instead of by id (that is possibly reused for the short-lived tooltip), and normalize variable names and string quoting style.
Commit
d5f6621527b813c1ab0510982747445e1bc8dc603e5e87d200522e0e31492fea
Parent
0df4abc74b38b89…
1 file changed
+20
-24
+20
-24
| --- src/copybtn.js | ||
| +++ src/copybtn.js | ||
| @@ -44,56 +44,52 @@ | ||
| 44 | 44 | if( cchLength ) elButton.setAttribute("data-copylength",cchLength); |
| 45 | 45 | elButton.onclick = clickCopyButton; |
| 46 | 46 | return elButton; |
| 47 | 47 | } |
| 48 | 48 | setTimeout(function(){ |
| 49 | - var aButtons = document.getElementsByClassName("copy-button"); | |
| 50 | - for ( var i=0; i<aButtons.length; i++ ){ | |
| 51 | - initCopyButton(aButtons[i],0,0); | |
| 49 | + var elButtons = document.getElementsByClassName("copy-button"); | |
| 50 | + for ( var i=0; i<elButtons.length; i++ ){ | |
| 51 | + initCopyButton(elButtons[i],0,0); | |
| 52 | 52 | } |
| 53 | 53 | },1); |
| 54 | 54 | /* The onclick handler for the "Copy Button". */ |
| 55 | -var lockCopyText = false; | |
| 56 | 55 | function clickCopyButton(e){ |
| 57 | 56 | e.preventDefault(); /* Mandatory for <a> and <button>. */ |
| 58 | 57 | e.stopPropagation(); |
| 59 | - if( lockCopyText ) return; | |
| 60 | - lockCopyText = true; | |
| 58 | + if( this.getAttribute("data-copylocked") ) return; | |
| 59 | + this.setAttribute("data-copylocked","1"); | |
| 61 | 60 | this.style.transition = "opacity 400ms ease-in-out"; |
| 62 | 61 | this.style.opacity = 0; |
| 63 | 62 | var idTarget = this.getAttribute("data-copytarget"); |
| 64 | 63 | var elTarget = document.getElementById(idTarget); |
| 65 | 64 | if( elTarget ){ |
| 66 | - var text = elTarget.innerText.replace(/^\s+|\s+$/g,''); | |
| 65 | + var text = elTarget.innerText.replace(/^\s+|\s+$/g,""); | |
| 67 | 66 | var cchLength = parseInt(this.getAttribute("data-copylength")); |
| 68 | 67 | if( !isNaN(cchLength) && cchLength>0 ){ |
| 69 | - text = text.slice(0,cchLength); // Assume single-byte chars. | |
| 68 | + text = text.slice(0,cchLength); /* Assume single-byte chars. */ | |
| 70 | 69 | } |
| 71 | 70 | copyTextToClipboard(text); |
| 72 | 71 | } |
| 73 | - setTimeout(function(id){ | |
| 74 | - var elButton = document.getElementById(id); | |
| 75 | - if( elButton ){ | |
| 76 | - elButton.style.transition = ""; | |
| 77 | - elButton.style.opacity = 1; | |
| 78 | - } | |
| 79 | - lockCopyText = false; | |
| 80 | - }.bind(null,this.id),400); | |
| 72 | + setTimeout(function(){ | |
| 73 | + this.style.transition = ""; | |
| 74 | + this.style.opacity = 1; | |
| 75 | + this.removeAttribute("data-copylocked"); | |
| 76 | + }.bind(this),400); | |
| 81 | 77 | } |
| 82 | 78 | /* Create a temporary <textarea> element and copy the contents to clipboard. */ |
| 83 | 79 | function copyTextToClipboard(text){ |
| 84 | 80 | if( window.clipboardData && window.clipboardData.setData ){ |
| 85 | - window.clipboardData.setData('Text',text); | |
| 81 | + window.clipboardData.setData("Text",text); | |
| 86 | 82 | }else{ |
| 87 | - var x = document.createElement("textarea"); | |
| 88 | - x.style.position = 'fixed'; | |
| 89 | - x.value = text; | |
| 90 | - document.body.appendChild(x); | |
| 91 | - x.select(); | |
| 83 | + var elTextarea = document.createElement("textarea"); | |
| 84 | + elTextarea.style.position = "fixed"; | |
| 85 | + elTextarea.value = text; | |
| 86 | + document.body.appendChild(elTextarea); | |
| 87 | + elTextarea.select(); | |
| 92 | 88 | try{ |
| 93 | - document.execCommand('copy'); | |
| 89 | + document.execCommand("copy"); | |
| 94 | 90 | }catch(err){ |
| 95 | 91 | }finally{ |
| 96 | - document.body.removeChild(x); | |
| 92 | + document.body.removeChild(elTextarea); | |
| 97 | 93 | } |
| 98 | 94 | } |
| 99 | 95 | } |
| 100 | 96 |
| --- src/copybtn.js | |
| +++ src/copybtn.js | |
| @@ -44,56 +44,52 @@ | |
| 44 | if( cchLength ) elButton.setAttribute("data-copylength",cchLength); |
| 45 | elButton.onclick = clickCopyButton; |
| 46 | return elButton; |
| 47 | } |
| 48 | setTimeout(function(){ |
| 49 | var aButtons = document.getElementsByClassName("copy-button"); |
| 50 | for ( var i=0; i<aButtons.length; i++ ){ |
| 51 | initCopyButton(aButtons[i],0,0); |
| 52 | } |
| 53 | },1); |
| 54 | /* The onclick handler for the "Copy Button". */ |
| 55 | var lockCopyText = false; |
| 56 | function clickCopyButton(e){ |
| 57 | e.preventDefault(); /* Mandatory for <a> and <button>. */ |
| 58 | e.stopPropagation(); |
| 59 | if( lockCopyText ) return; |
| 60 | lockCopyText = true; |
| 61 | this.style.transition = "opacity 400ms ease-in-out"; |
| 62 | this.style.opacity = 0; |
| 63 | var idTarget = this.getAttribute("data-copytarget"); |
| 64 | var elTarget = document.getElementById(idTarget); |
| 65 | if( elTarget ){ |
| 66 | var text = elTarget.innerText.replace(/^\s+|\s+$/g,''); |
| 67 | var cchLength = parseInt(this.getAttribute("data-copylength")); |
| 68 | if( !isNaN(cchLength) && cchLength>0 ){ |
| 69 | text = text.slice(0,cchLength); // Assume single-byte chars. |
| 70 | } |
| 71 | copyTextToClipboard(text); |
| 72 | } |
| 73 | setTimeout(function(id){ |
| 74 | var elButton = document.getElementById(id); |
| 75 | if( elButton ){ |
| 76 | elButton.style.transition = ""; |
| 77 | elButton.style.opacity = 1; |
| 78 | } |
| 79 | lockCopyText = false; |
| 80 | }.bind(null,this.id),400); |
| 81 | } |
| 82 | /* Create a temporary <textarea> element and copy the contents to clipboard. */ |
| 83 | function copyTextToClipboard(text){ |
| 84 | if( window.clipboardData && window.clipboardData.setData ){ |
| 85 | window.clipboardData.setData('Text',text); |
| 86 | }else{ |
| 87 | var x = document.createElement("textarea"); |
| 88 | x.style.position = 'fixed'; |
| 89 | x.value = text; |
| 90 | document.body.appendChild(x); |
| 91 | x.select(); |
| 92 | try{ |
| 93 | document.execCommand('copy'); |
| 94 | }catch(err){ |
| 95 | }finally{ |
| 96 | document.body.removeChild(x); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |
| --- src/copybtn.js | |
| +++ src/copybtn.js | |
| @@ -44,56 +44,52 @@ | |
| 44 | if( cchLength ) elButton.setAttribute("data-copylength",cchLength); |
| 45 | elButton.onclick = clickCopyButton; |
| 46 | return elButton; |
| 47 | } |
| 48 | setTimeout(function(){ |
| 49 | var elButtons = document.getElementsByClassName("copy-button"); |
| 50 | for ( var i=0; i<elButtons.length; i++ ){ |
| 51 | initCopyButton(elButtons[i],0,0); |
| 52 | } |
| 53 | },1); |
| 54 | /* The onclick handler for the "Copy Button". */ |
| 55 | function clickCopyButton(e){ |
| 56 | e.preventDefault(); /* Mandatory for <a> and <button>. */ |
| 57 | e.stopPropagation(); |
| 58 | if( this.getAttribute("data-copylocked") ) return; |
| 59 | this.setAttribute("data-copylocked","1"); |
| 60 | this.style.transition = "opacity 400ms ease-in-out"; |
| 61 | this.style.opacity = 0; |
| 62 | var idTarget = this.getAttribute("data-copytarget"); |
| 63 | var elTarget = document.getElementById(idTarget); |
| 64 | if( elTarget ){ |
| 65 | var text = elTarget.innerText.replace(/^\s+|\s+$/g,""); |
| 66 | var cchLength = parseInt(this.getAttribute("data-copylength")); |
| 67 | if( !isNaN(cchLength) && cchLength>0 ){ |
| 68 | text = text.slice(0,cchLength); /* Assume single-byte chars. */ |
| 69 | } |
| 70 | copyTextToClipboard(text); |
| 71 | } |
| 72 | setTimeout(function(){ |
| 73 | this.style.transition = ""; |
| 74 | this.style.opacity = 1; |
| 75 | this.removeAttribute("data-copylocked"); |
| 76 | }.bind(this),400); |
| 77 | } |
| 78 | /* Create a temporary <textarea> element and copy the contents to clipboard. */ |
| 79 | function copyTextToClipboard(text){ |
| 80 | if( window.clipboardData && window.clipboardData.setData ){ |
| 81 | window.clipboardData.setData("Text",text); |
| 82 | }else{ |
| 83 | var elTextarea = document.createElement("textarea"); |
| 84 | elTextarea.style.position = "fixed"; |
| 85 | elTextarea.value = text; |
| 86 | document.body.appendChild(elTextarea); |
| 87 | elTextarea.select(); |
| 88 | try{ |
| 89 | document.execCommand("copy"); |
| 90 | }catch(err){ |
| 91 | }finally{ |
| 92 | document.body.removeChild(elTextarea); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 |