| 1 |
window.CopyTheCodeToClipboard = (function(window, document, navigator) { |
| 2 |
var textArea, |
| 3 |
copy; |
| 4 |
|
| 5 |
function isOS() { |
| 6 |
return navigator.userAgent.match(/ipad|iphone/i); |
| 7 |
} |
| 8 |
|
| 9 |
function createTextArea(text) { |
| 10 |
textArea = document.createElement('textArea'); |
| 11 |
textArea.value = text; |
| 12 |
document.body.appendChild(textArea); |
| 13 |
} |
| 14 |
|
| 15 |
function selectText() { |
| 16 |
var range, |
| 17 |
selection; |
| 18 |
|
| 19 |
if (isOS()) { |
| 20 |
range = document.createRange(); |
| 21 |
range.selectNodeContents(textArea); |
| 22 |
selection = window.getSelection(); |
| 23 |
selection.removeAllRanges(); |
| 24 |
selection.addRange(range); |
| 25 |
textArea.setSelectionRange(0, 999999); |
| 26 |
} else { |
| 27 |
textArea.select(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
function copyToClipboard() { |
| 32 |
document.execCommand('copy'); |
| 33 |
document.body.removeChild(textArea); |
| 34 |
} |
| 35 |
|
| 36 |
copy = function(text) { |
| 37 |
createTextArea(text); |
| 38 |
selectText(); |
| 39 |
copyToClipboard(); |
| 40 |
}; |
| 41 |
|
| 42 |
return { |
| 43 |
copy: copy |
| 44 |
}; |
| 45 |
})(window, document, navigator); |
| 46 |
|
| 47 |
(function($) { |
| 48 |
|
| 49 |
CopyTheCode = { |
| 50 |
|
| 51 |
selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre', |
| 52 |
copy_as: copyTheCode.settings['copy-as'] || 'html', |
| 53 |
button_position: copyTheCode.settings['button-position'] || 'inside', |
| 54 |
|
| 55 |
/** |
| 56 |
* Init |
| 57 |
*/ |
| 58 |
init: function() |
| 59 |
{ |
| 60 |
this._bind(); |
| 61 |
this._initialize(); |
| 62 |
}, |
| 63 |
|
| 64 |
/** |
| 65 |
* Binds events |
| 66 |
*/ |
| 67 |
_bind: function() |
| 68 |
{ |
| 69 |
$( document ).on('click', '.copy-the-code-button', CopyTheCode.copyCode ); |
| 70 |
}, |
| 71 |
|
| 72 |
/** |
| 73 |
* Initialize the Button |
| 74 |
*/ |
| 75 |
_initialize: function() |
| 76 |
{ |
| 77 |
if( ! $( CopyTheCode.selector ).length ) |
| 78 |
{ |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$( CopyTheCode.selector ).each(function(index, el) { |
| 83 |
if( 'outside' === CopyTheCode.button_position ) { |
| 84 |
$( el ).wrap( '<span class="copy-the-code-wrap copy-the-code-outside-wrap"></span>' ); |
| 85 |
$( el ).parent().prepend('<div class="copy-the-code-outside">' + CopyTheCode._getButtonMarkup() + '</div>'); |
| 86 |
} else { |
| 87 |
$( el ).wrap( '<span class="copy-the-code-wrap copy-the-code-inside-wrap"></span>' ); |
| 88 |
$( el ).append( CopyTheCode._getButtonMarkup() ); |
| 89 |
} |
| 90 |
}); |
| 91 |
}, |
| 92 |
|
| 93 |
/** |
| 94 |
* Get Copy Button Markup |
| 95 |
*/ |
| 96 |
_getButtonMarkup: function() |
| 97 |
{ |
| 98 |
return '<button class="copy-the-code-button" title="' + copyTheCode.string.title + '">' + copyTheCode.string.copy + '</button>'; |
| 99 |
}, |
| 100 |
|
| 101 |
/** |
| 102 |
* Copy to Clipboard |
| 103 |
*/ |
| 104 |
copyCode: function( event ) |
| 105 |
{ |
| 106 |
var btn = $( this ), |
| 107 |
source = btn.parents('.copy-the-code-wrap').find( CopyTheCode.selector ), |
| 108 |
oldText = btn.text(); |
| 109 |
|
| 110 |
if( 'text' === CopyTheCode.copy_as ) { |
| 111 |
var html = source.text(); |
| 112 |
|
| 113 |
// Remove the 'copy' text. |
| 114 |
var tempHTML = html.replace(copyTheCode.string.copy, ''); |
| 115 |
|
| 116 |
} else { |
| 117 |
var html = source.html(); |
| 118 |
|
| 119 |
// Remove the <copy> button. |
| 120 |
var tempHTML = html.replace(CopyTheCode._getButtonMarkup(), ''); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
// Copy the Code. |
| 125 |
var tempPre = $("<textarea id='temp-pre'>"), |
| 126 |
temp = $("<textarea>"), |
| 127 |
brRegex = /<br\s*[\/]?>/gi; |
| 128 |
|
| 129 |
// Append temporary elements to DOM. |
| 130 |
$("body").append(temp); |
| 131 |
$("body").append(tempPre); |
| 132 |
|
| 133 |
// Set temporary HTML markup. |
| 134 |
tempPre.html( tempHTML ); |
| 135 |
|
| 136 |
// Format the HTML markup. |
| 137 |
temp.val( tempPre.text().replace(brRegex, "\r\n" ) ).select(); |
| 138 |
|
| 139 |
// Support for IOS devices too. |
| 140 |
CopyTheCodeToClipboard.copy( tempPre.text().replace(brRegex, "\r\n" ) ); |
| 141 |
|
| 142 |
// Remove temporary elements. |
| 143 |
temp.remove(); |
| 144 |
tempPre.remove(); |
| 145 |
|
| 146 |
// Copied! |
| 147 |
btn.text( copyTheCode.string.copied ); |
| 148 |
setTimeout(function() { |
| 149 |
btn.text( oldText ); |
| 150 |
}, 1000); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
/** |
| 155 |
* Initialization |
| 156 |
*/ |
| 157 |
$(function() { |
| 158 |
CopyTheCode.init(); |
| 159 |
}); |
| 160 |
|
| 161 |
})(jQuery); |