You've already forked wc-licensed-product
91 lines
2.5 KiB
JavaScript
91 lines
2.5 KiB
JavaScript
|
|
/**
|
||
|
|
* WC Licensed Product - Frontend Scripts
|
||
|
|
*
|
||
|
|
* @package Jeremias\WcLicensedProduct
|
||
|
|
*/
|
||
|
|
|
||
|
|
(function($) {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
var WCLicensedProductFrontend = {
|
||
|
|
init: function() {
|
||
|
|
this.bindEvents();
|
||
|
|
},
|
||
|
|
|
||
|
|
bindEvents: function() {
|
||
|
|
$(document).on('click', '.copy-license-btn', this.copyLicenseKey);
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Copy license key to clipboard
|
||
|
|
*/
|
||
|
|
copyLicenseKey: function(e) {
|
||
|
|
e.preventDefault();
|
||
|
|
|
||
|
|
var $btn = $(this);
|
||
|
|
var licenseKey = $btn.data('license-key');
|
||
|
|
|
||
|
|
if (!licenseKey) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Use modern clipboard API if available
|
||
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||
|
|
navigator.clipboard.writeText(licenseKey)
|
||
|
|
.then(function() {
|
||
|
|
WCLicensedProductFrontend.showCopyFeedback($btn, true);
|
||
|
|
})
|
||
|
|
.catch(function() {
|
||
|
|
WCLicensedProductFrontend.fallbackCopy(licenseKey, $btn);
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
WCLicensedProductFrontend.fallbackCopy(licenseKey, $btn);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Fallback copy method for older browsers
|
||
|
|
*/
|
||
|
|
fallbackCopy: function(text, $btn) {
|
||
|
|
var $temp = $('<textarea>');
|
||
|
|
$('body').append($temp);
|
||
|
|
$temp.val(text).select();
|
||
|
|
|
||
|
|
try {
|
||
|
|
var success = document.execCommand('copy');
|
||
|
|
WCLicensedProductFrontend.showCopyFeedback($btn, success);
|
||
|
|
} catch (err) {
|
||
|
|
WCLicensedProductFrontend.showCopyFeedback($btn, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
$temp.remove();
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show feedback after copy attempt
|
||
|
|
*/
|
||
|
|
showCopyFeedback: function($btn, success) {
|
||
|
|
var message = success
|
||
|
|
? wcLicensedProduct.strings.copied
|
||
|
|
: wcLicensedProduct.strings.copyFailed;
|
||
|
|
|
||
|
|
var $feedback = $('<span class="copy-feedback"></span>')
|
||
|
|
.text(message)
|
||
|
|
.addClass(success ? 'success' : 'error');
|
||
|
|
|
||
|
|
$btn.after($feedback);
|
||
|
|
|
||
|
|
setTimeout(function() {
|
||
|
|
$feedback.fadeOut(300, function() {
|
||
|
|
$(this).remove();
|
||
|
|
});
|
||
|
|
}, 1500);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
$(document).ready(function() {
|
||
|
|
WCLicensedProductFrontend.init();
|
||
|
|
});
|
||
|
|
|
||
|
|
})(jQuery);
|