| 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 |
content = block.find('.ctc-copy-content').val(); |
| 31 |
|
| 32 |
if ( ! copyAsRaw ) { |
| 33 |
// Convert the <br/> tags into new line. |
| 34 |
content = content.replace(/<br\s*[\/]?>/gi, "\n"); |
| 35 |
|
| 36 |
// Convert the <div> tags into new line. |
| 37 |
content = content.replace(/<div\s*[\/]?>/gi, "\n"); |
| 38 |
|
| 39 |
// Convert the <p> tags into new line. |
| 40 |
content = content.replace(/<p\s*[\/]?>/gi, "\n\n"); |
| 41 |
|
| 42 |
// Convert the <li> tags into new line. |
| 43 |
content = content.replace(/<li\s*[\/]?>/gi, "\n"); |
| 44 |
|
| 45 |
// Remove all tags. |
| 46 |
content = content.replace(/(<([^>]+)>)/ig, ''); |
| 47 |
|
| 48 |
// Remove white spaces. |
| 49 |
content = content.replace(new RegExp("/^\s+$/"), ""); |
| 50 |
} |
| 51 |
|
| 52 |
// Remove first and last new line. |
| 53 |
content = $.trim(content); |
| 54 |
|
| 55 |
// Support for IOS devices too. |
| 56 |
CTC.copy(content); |
| 57 |
|
| 58 |
if ( btn.hasClass('ctc-block-copy-icon') ) { |
| 59 |
// Copied! |
| 60 |
btn.addClass('copied'); |
| 61 |
setTimeout(function () { |
| 62 |
btn.removeClass('copied') |
| 63 |
}, 1000); |
| 64 |
} else { |
| 65 |
// Copied! |
| 66 |
btnText.text( copiedText ); |
| 67 |
block.addClass('copied') |
| 68 |
setTimeout(function () { |
| 69 |
btnText.text(oldText) |
| 70 |
block.removeClass('copied') |
| 71 |
}, 1000); |
| 72 |
} |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
/** |
| 77 |
* Initialization |
| 78 |
*/ |
| 79 |
$(function () { |
| 80 |
CTCCore.init(); |
| 81 |
}); |
| 82 |
|
| 83 |
})(jQuery); |