PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 5.5.4
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v5.5.4
5.5.4 5.5.3 3.1.0 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 4.0.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 All 79 releases
copy-the-code / includes / elementor / widgets / coupon / script.js

script.js in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 5.5.4, at includes/elementor/widgets/coupon/script.js

136 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 const CTCCoupon = {
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-coupon-toggle-link', this.toggleDetails );
16 $( document ).on( 'click', '.ctc-coupon-link', this.handleClick );
17 },
18
19 /**
20 * Copy text to clipboard using CTC CopyEngine.
21 *
22 * @param {string} text Text to copy.
23 * @return {Promise<boolean>} Success status.
24 */
25 copyToClipboard: async function ( text ) {
26 // Method 1: Use CTC CopyEngine.
27 if ( window.CTC && window.CTC.CopyEngine ) {
28 try {
29 const copyEngine = new window.CTC.CopyEngine();
30 const result = await copyEngine.execute( { value: text } );
31 if ( result.success ) {
32 return true;
33 }
34 } catch ( err ) {
35 // Fall through to fallbacks.
36 }
37 }
38
39 // Method 2: Clipboard API.
40 if ( navigator.clipboard && navigator.clipboard.writeText ) {
41 try {
42 await navigator.clipboard.writeText( text );
43 return true;
44 } catch ( err ) {
45 // Fall through.
46 }
47 }
48
49 // Method 3: execCommand fallback.
50 try {
51 const textArea = document.createElement( 'textarea' );
52 textArea.value = text;
53 textArea.style.cssText = 'position:fixed;opacity:0;pointer-events:none;';
54 document.body.appendChild( textArea );
55 textArea.select();
56 const success = document.execCommand( 'copy' );
57 document.body.removeChild( textArea );
58 return success;
59 } catch ( err ) {
60 return false;
61 }
62 },
63
64 /**
65 * Handle click
66 */
67 handleClick: async function (event) {
68 event.preventDefault()
69
70 const self = $( this )
71 const parent = self.parents( '.ctc-coupon' )
72 if ( parent.hasClass( 'ctc-coupon-clicked' ) ) {
73 return
74 }
75
76 const href = self.attr( 'href' )
77 const target = self.attr( 'target' )
78 const couponCode = parent.find( '.ctc-coupon-code' ).text().trim() || ''
79
80 parent.addClass( 'ctc-coupon-clicked' )
81
82 await CTCCoupon.copyToClipboard( couponCode )
83
84 window.open( href, target )
85 },
86
87 /**
88 * Handle click
89 */
90 oldHandleClick: function (event) {
91 event.preventDefault();
92
93 const self = $( this )
94 const href = self.data( 'href' )
95 const target = self.data( 'target' )
96 const btn = self.find( '.ctc-coupon-button' )
97
98 // Clicked then open the link
99 if ( ! self.hasClass( 'ctc-coupon-link-clicked' ) ) {
100 window.open( href, target )
101
102 self.addClass( 'ctc-coupon-link-clicked' )
103 btn.text( 'Copy Code' )
104 }
105
106 if ( self.hasClass( 'ctc-coupon-link-clicked' ) ) {
107 self.addClass( 'ctc-coupon-link-copied' )
108 btn.text( 'Copied' )
109 }
110 },
111
112 /**
113 * Toggle Details
114 */
115 toggleDetails: function (event) {
116 event.preventDefault();
117
118 const self = $( this )
119 const coupon = self.parents( '.ctc-coupon' )
120 const details = coupon.find( '.ctc-toggle-details' )
121
122 details.slideToggle( 'fast' )
123 }
124 };
125
126 /**
127 * Initialization
128 */
129 $(
130 function () {
131 CTCCoupon.init();
132 }
133 );
134
135 })( jQuery );
136