declare var jQuery: any; declare var ClipboardJS: any; declare var ACFW_QRCode: any; declare var acfw_edit_coupon: any; var $: any = jQuery; /** * URL Coupon events. * * @since 4.3.1 */ export default function url_coupon_events() { const moduleBlock = document.querySelector('#acfw_url_coupon'); $(moduleBlock).on('click', '.copy-coupon-url', showCopiedText); initCouponUrlClipboard(); // If the QR code setting is disabled, then the QR code library will not be loaded from the backend. if (typeof ACFW_QRCode !== 'undefined') { initQRCode(); } } /** * Initialize coupon URL field copy to clipboard button. * * @since 4.3.1 */ function initCouponUrlClipboard() { const $couponUrl = $('input#_acfw_coupon_url'); $couponUrl.after( `` ); new ClipboardJS('.copy-coupon-url'); } /** * Display a "Copied" text after clicking the copy button. * * @since 4.3.1 */ function showCopiedText() { // @ts-ignore const $button = $(this); $button.after(`${acfw_edit_coupon.copied_label}`); setTimeout(() => { $('span.coupon-url-copied').fadeOut(500); }, 1000); } /** * Initialize QR Code. * * @since 4.6.1 */ function initQRCode() { const $optionsGroupContainer = $('#acfw_url_coupon').find('.options_group'); // Prepend QR code HTML element $optionsGroupContainer.prepend( `
` ); generateQRCode(); } /** * Generate QR Code based coupon url. * * @since 4.6.1 */ function generateQRCode() { var couponUrl = $('input#_acfw_coupon_url').val(); var downloadLink = $('#acfw-download-qr-link'); // If couponUrl is exist, generate the QR Code. if (couponUrl) { let qrcodeContainer = $('#acfw-qr-code'); try { let qrcode = new ACFW_QRCode(qrcodeContainer[0], { text: couponUrl, width: 200, height: 200, colorDark: '#000000', colorLight: '#ffffff', correctLevel: ACFW_QRCode.CorrectLevel.L, }); qrcode.makeCode(couponUrl); downloadLink.click(function () { // Get the QR image and its src attribute var qrImage = $('#acfw-qr-code img'); var qrURL = qrImage.attr('src'); // Download the QR image with coupon title name downloadLink.attr('href', qrURL); downloadLink.attr('download', `${$('#title').val()}.png`); }); } catch (error) { $('#acfw-qr-code').remove(); $('#acfw-download-qr-link').addClass('disabled'); $('#acfw-download-qr-link').before(``); } } else { // Hide acfw-qr-code-container if the couponUrl is not exist $('.acfw-qr-code-container').hide(); } }