1/** 2 * Tap to copy 3 * 4 * @copyright 2022 Parente 5 */ 6 7var app = app || {}; 8 9app.ClipBoard = function() { 10 11 // ã³ã³ã¹ãã©ã¯ã¿ 12 var ClipBoard = function (elm) { 13 this.elm = elm || {}; 14 this.settings = { 15 trigger: '.js_copy_btn', 16 copiedText: 'ã¯ã¼ãã³ã³ã¼ããã³ãã¼ãã¾ãã', 17 fadeSpeed: 300, 18 displayTime: 800 19 } 20 this.disabled = false; 21 this.code = elm.getAttribute('data-copy-code'); 22 if(this.code) { 23 this.bindStyle(); 24 this.bindEvents(); 25 } 26 } 27 28 // ã³ãã¼å®è¡å¦ç 29 ClipBoard.prototype.execCopy = function(code) { 30 // 空div çæ 31 var tmpElm = document.createElement("div"); 32 // 鏿ç¨ã®ã¿ã°çæ 33 var preElm = document.createElement('pre'); 34 // 親è¦ç´ ã®CSSã§ user-select: none ã ã¨ã³ãã¼ã§ããªãã®ã§æ¸ãæãã 35 preElm.style.webkitUserSelect = 'auto'; 36 preElm.style.userSelect = 'auto'; 37 // çæããã¿ã°ã«ããã¹ãã追å 38 tmpElm.appendChild(preElm).textContent = code; 39 // è¦ç´ ãç»é¢å¤ã¸ 40 var s = tmpElm.style; 41 s.position = 'fixed'; 42 s.right = '200%'; 43 // body ã«è¿½å 44 document.body.appendChild(tmpElm); 45 // è¦ç´ ã鏿 46 document.getSelection().selectAllChildren(tmpElm); 47 // ã¯ãªãããã¼ãã«ã³ãã¼ 48 var result = document.execCommand("copy"); 49 // è¦ç´ åé¤ 50 tmpElm.remove(); 51 52 return result; 53 }, 54 55 // ã¡ãã»ã¼ã¸ããã¯ã¹ã使ããå¦ç 56 ClipBoard.prototype.createMsgBox = function() { 57 var self = this; 58 // 空ããã¯ã¹ã«ã¯ã©ã¹ãä»ä¸ 59 self.msgBox = document.createElement('div'); 60 self.msgBox.setAttribute('class', 'js_clipboard_msgbox'); 61 // ã¡ãã»ã¼ã¸ããã¯ã¹ã«ããã¹ãã追å ãã 62 self.msgBox.textContent = self.settings.copiedText; 63 // ã¹ã¿ã¤ã«ãä»ä¸ 64 var msgBoxStyles = { 65 display: 'none', 66 position: 'fixed', 67 top: '50%', 68 left: '0', 69 width: '100%', 70 padding: '15px 0', 71 backgroundColor: '#444444', 72 fontSize: '14px', 73 lineHeight: '1', 74 color: '#ffffff', 75 textAlign: 'center', 76 zIndex: '2147483647', 77 opacity: '0', 78 transform: 'translateY(-50%)', 79 transition: 'opacity ' + self.settings.fadeSpeed + 'ms ease' 80 } 81 for(var prop in msgBoxStyles) { 82 self.msgBox.style[prop] = msgBoxStyles[prop]; 83 } 84 // bodyã«è¿½å 85 document.body.appendChild(self.msgBox); 86 } 87 88 // ã¡ãã»ã¼ã¸ããã¯ã¹ã表示ããå¦ç 89 ClipBoard.prototype.showMsgBox = function(time, callback) { 90 var self = this; 91 // ã¢ãã¡ã¼ã·ã§ã³çµäºå¾ã®å¦ç 92 var transitionEnd = function() { 93 if( typeof callback === 'function' ){ 94 setTimeout(callback, time); 95 self.msgBox.removeEventListener('transitionend', transitionEnd); 96 }; 97 } 98 // transitionend ã§ã¢ãã¡ã¼ã·ã§ã³ã®çµããã«å®è¡ããã 99 self.msgBox.addEventListener('transitionend', transitionEnd); 100 // ã¡ãã»ã¼ã¸ããã¯ã¹ãdisplay:blockã«ãã 101 self.msgBox.style.display = 'block'; 102 // transitionendãçºç«ãããããã«setTimeoutã使ç¨ãã 103 setTimeout(function() { 104 self.msgBox.style.opacity = 1; 105 }, 0); 106 } 107 108 // ã¡ãã»ã¼ã¸ããã¯ã¹ãé表示ã«ããå¦ç 109 ClipBoard.prototype.hideMsgBox = function(time, callback) { 110 var self = this; 111 // ã¢ãã¡ã¼ã·ã§ã³çµäºå¾ã®å¦ç 112 var transitionEnd = function() { 113 if( typeof callback === 'function' ){ 114 setTimeout(callback, time); 115 self.msgBox.removeEventListener('transitionend', transitionEnd); 116 }; 117 } 118 // transitionend ã§ã¢ãã¡ã¼ã·ã§ã³ã®çµããã«å®è¡ããã 119 self.msgBox.addEventListener('transitionend', transitionEnd); 120 // transitionendãçºç«ãããããã«setTimeoutã使ç¨ãã 121 setTimeout(function() { 122 self.msgBox.style.opacity = 0; 123 }, 0); 124 } 125 126 // ã¡ãã»ã¼ã¸ããã¯ã¹ãåé¤ããå¦ç 127 ClipBoard.prototype.removeMsgBox = function() { 128 this.msgBox.remove(); 129 } 130 131 // ã³ãã¼æåæã®å¦ç 132 ClipBoard.prototype.copySuccessed = function() { 133 var self = this; 134 // ã¡ãã»ã¼ã¸ããã¯ã¹ã使ãã 135 self.createMsgBox(); 136 // ã¡ãã»ã¼ã¸ããã¯ã¹ãæå®æé表示å¾ãåé¤ãã 137 self.showMsgBox(self.settings.displayTime, function(){ 138 self.hideMsgBox(0, function(){ 139 self.removeMsgBox(); 140 self.disabled = false;
141 }); 142 }); 143 } 144 145 // ã³ãã¼å¤±ææã®å¦ç 146 ClipBoard.prototype.copyError = function() { 147 alert('ãã®ãã©ã¦ã¶ã§ã¯ã³ãã¼ãã§ãã¾ãããç³ã訳ãããã¾ãããå¥ã®ãã©ã¦ã¶ã§ã試ããã ããã'); 148 } 149 150 // CSSãé©ç¨ 151 ClipBoard.prototype.bindStyle = function() { 152 var self = this; 153 self.elm.style.cursor = 'pointer'; 154 } 155 156 // ã¯ãªãã¯ã¤ãã³ããé©ç¨ 157 ClipBoard.prototype.bindEvents = function() { 158 var self = this; 159 self.elm.onclick = function(){ 160 if(!self.disabled) { 161 self.disabled = true; 162 self.execCopy(self.code)? self.copySuccessed(self.code) : self.copyError(); 163 } 164 } 165 } 166 167 // 対象è¦ç´ ãåå¾ 168 var clipBoard = []; 169 var clipBoards = document.querySelectorAll('.js_copy_btn'); 170 Array.prototype.forEach.call(clipBoards, function (elm, i) { 171 clipBoard[i] = new ClipBoard(elm); 172 }); 173} 174 175//DOMæ§ç¯å®äºå¾ã«å®è¡ 176document.addEventListener('DOMContentLoaded', function(){ 177 app.ClipBoard(); 178},false);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.