| 1 |
(function ($) { |
| 2 |
const CTCCore = { |
| 3 |
|
| 4 |
/** |
| 5 |
* Init |
| 6 |
*/ |
| 7 |
init: function () { |
| 8 |
this._bind(); |
| 9 |
}, |
| 10 |
|
| 11 |
/** |
| 12 |
* Binds events |
| 13 |
*/ |
| 14 |
_bind: function () { |
| 15 |
$( document ).on( 'click', '.ctc-block-copy', this.doCopy ); |
| 16 |
}, |
| 17 |
|
| 18 |
/** |
| 19 |
* Do Copy to Clipboard |
| 20 |
*/ |
| 21 |
doCopy: function (event) { |
| 22 |
event.preventDefault(); |
| 23 |
|
| 24 |
let btn = $( this ), |
| 25 |
btnText = btn.find( '.ctc-button-text' ), |
| 26 |
oldText = btnText.text(), |
| 27 |
copiedText = btn.attr( 'data-copied' ) || 'Copied', |
| 28 |
copyAsRaw = btn.attr( 'copy-as-raw' ) || '', |
| 29 |
block = btn.parents( '.ctc-block' ), |
| 30 |
textarea = block.find( '.ctc-copy-content' ), |
| 31 |
content = textarea.val(), |
| 32 |
selectionTarget = textarea.attr( 'selection-target' ) || '' |
| 33 |
|
| 34 |
// Copy as selection. |
| 35 |
if ( selectionTarget ) { |
| 36 |
const source = $( selectionTarget ) |
| 37 |
if ( ! source.length ) { |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
CTCWP.copySelection( source ) |
| 42 |
} else { |
| 43 |
if ( ! copyAsRaw ) { |
| 44 |
// Convert the <br/> tags into new line. |
| 45 |
content = content.replace( /<br\s*[\/]?>/gi, "\n" ); |
| 46 |
|
| 47 |
// Convert the <div> tags into new line. |
| 48 |
content = content.replace( /<div\s*[\/]?>/gi, "\n" ); |
| 49 |
|
| 50 |
// Convert the <p> tags into new line. |
| 51 |
content = content.replace( /<p\s*[\/]?>/gi, "\n\n" ); |
| 52 |
|
| 53 |
// Convert the <li> tags into new line. |
| 54 |
content = content.replace( /<li\s*[\/]?>/gi, "\n" ); |
| 55 |
|
| 56 |
// Remove all tags. |
| 57 |
content = content.replace( /(<([^>]+)>)/ig, '' ); |
| 58 |
|
| 59 |
// Remove white spaces. |
| 60 |
content = content.replace( new RegExp( "/^\s+$/" ), "" ); |
| 61 |
} |
| 62 |
|
| 63 |
// Remove first and last new line. |
| 64 |
content = $.trim( content ); |
| 65 |
|
| 66 |
// Support for IOS devices too. |
| 67 |
CTCWP.copy( content ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( btn.hasClass( 'ctc-block-copy-icon' ) ) { |
| 71 |
// Copied! |
| 72 |
btn.addClass( 'copied' ); |
| 73 |
setTimeout( |
| 74 |
function () { |
| 75 |
btn.removeClass( 'copied' ) |
| 76 |
}, |
| 77 |
1000 |
| 78 |
); |
| 79 |
} else { |
| 80 |
// Copied! |
| 81 |
btnText.text( copiedText ); |
| 82 |
block.addClass( 'copied' ) |
| 83 |
setTimeout( |
| 84 |
function () { |
| 85 |
btnText.text( oldText ) |
| 86 |
block.removeClass( 'copied' ) |
| 87 |
}, |
| 88 |
1000 |
| 89 |
); |
| 90 |
} |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
/** |
| 95 |
* Initialization |
| 96 |
*/ |
| 97 |
$( |
| 98 |
function () { |
| 99 |
CTCCore.init(); |
| 100 |
} |
| 101 |
); |
| 102 |
|
| 103 |
})( jQuery ); |
| 104 |
|