| 1 |
/** |
| 2 |
* Copy buttons for AI feature code samples. |
| 3 |
* |
| 4 |
* @package MainWP\Dashboard |
| 5 |
*/ |
| 6 |
|
| 7 |
jQuery(function ($) { |
| 8 |
const buttons = $('.mainwp-code-copy-button'); |
| 9 |
|
| 10 |
if (!buttons.length || typeof ClipboardJS === 'undefined') { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
buttons.each(function () { |
| 15 |
const button = $(this); |
| 16 |
const originalHtml = button.html(); |
| 17 |
const originalAriaLabel = button.attr('aria-label'); |
| 18 |
let resetTimer; |
| 19 |
|
| 20 |
const showStatus = (icon, label) => { |
| 21 |
globalThis.clearTimeout(resetTimer); |
| 22 |
button.empty().append($('<i>').addClass(icon), document.createTextNode(label)); |
| 23 |
button.attr('aria-label', label); |
| 24 |
resetTimer = globalThis.setTimeout(() => { |
| 25 |
button.html(originalHtml); |
| 26 |
button.attr('aria-label', originalAriaLabel); |
| 27 |
}, 2000); |
| 28 |
}; |
| 29 |
|
| 30 |
const clipboard = new ClipboardJS(this); |
| 31 |
|
| 32 |
clipboard.on('success', (event) => { |
| 33 |
event.clearSelection(); |
| 34 |
showStatus('check icon', button.data('copy-success')); |
| 35 |
}); |
| 36 |
|
| 37 |
clipboard.on('error', () => { |
| 38 |
showStatus('exclamation triangle icon', button.data('copy-error')); |
| 39 |
}); |
| 40 |
}); |
| 41 |
}); |
| 42 |
|