PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 4.0.0
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v4.0.0
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 4.1.1 All 78 releases
copy-the-code / assets / js / page.js

page.js in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 4.0.0, at assets/js/page.js

218 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 window.CopyTheCodeToClipboard = (function(window, document, navigator) {
2 var textArea,
3 copy;
4
5 function isOS() {
6 return navigator.userAgent.match( /ipad|iphone/i );
7 }
8
9 function createTextArea(text) {
10 textArea = document.createElement( 'textArea' );
11 textArea.value = text;
12 document.body.appendChild( textArea );
13 }
14
15 function selectText() {
16 var range,
17 selection;
18
19 if (isOS()) {
20 range = document.createRange();
21 range.selectNodeContents( textArea );
22 selection = window.getSelection();
23 selection.removeAllRanges();
24 selection.addRange( range );
25 textArea.setSelectionRange( 0, 999999 );
26 } else {
27 textArea.select();
28 }
29 }
30
31 function copyToClipboard() {
32 document.execCommand( 'copy' );
33 document.body.removeChild( textArea );
34 }
35
36 copy = function(text) {
37 createTextArea( text );
38 selectText();
39 copyToClipboard();
40 };
41
42 return {
43 copy: copy
44 };
45 })( window, document, navigator );
46
47
48 (function($) {
49
50 CopyTheCodePage = {
51
52 /**
53 * Init
54 */
55 init: function()
56 {
57 this._bind();
58 this._preview();
59 $( '.copy-the-code .nav-tab-wrapper > .nav-tab:first-child' ).addClass( 'nav-tab-active' );
60 },
61
62 _bind: function()
63 {
64 $( document ).on( 'input', '[name="button-text"], [name="button-copy-text"], [name="button-title"]', CopyTheCodePage._preview );
65 $( document ).on( 'change', '[name="style"], [name="button-position"]', CopyTheCodePage._preview );
66 $( document ).on( 'click', '.copy-the-code-button', CopyTheCodePage.copyCode );
67 },
68
69 /**
70 * Copy to Clipboard
71 */
72 copyCode: function( event )
73 {
74 var btn = $( this ),
75 preview = $( '#preview' ).find( 'pre' ),
76 source = btn.parents( '.copy-the-code-wrap' ).find( preview ),
77 oldText = btn.text();
78
79 var buttonMarkup = $( '.copy-the-code-wrap' ).html() || '';
80 var copy_as = 'text';
81 var button_text = $( '[name="button-text"]' ).val() || '';
82 var button_copy_text = $( '[name="button-copy-text"]' ).val() || '';
83 var style = $( '[name="style"]' ).val() || 'button';
84
85 if ( 'text' === copy_as ) {
86 var html = source.text();
87
88 // Remove the 'copy' text.
89 var tempHTML = html.replace( button_text, '' );
90
91 } else {
92 var html = source.html();
93
94 // Remove the <copy> button.
95 var tempHTML = html.replace( buttonMarkup, '' );
96 }
97
98 // Copy the Code.
99 var tempPre = $( "<textarea id='temp-pre'>" ),
100 temp = $( "<textarea>" ),
101 brRegex = '/<br\s*[/\]?>/gi';
102
103 // Append temporary elements to DOM.
104 $( "body" ).append( temp );
105 $( "body" ).append( tempPre );
106
107 // Set temporary HTML markup.
108 tempPre.html( tempHTML );
109
110 // Format the HTML markup.
111 temp.val( tempPre.text().replace( brRegex, "\r\n" ) ).select();
112
113 // Support for IOS devices too.
114 CopyTheCodeToClipboard.copy( tempPre.text().replace( brRegex, "\r\n" ) );
115
116 // Remove temporary elements.
117 temp.remove();
118 tempPre.remove();
119
120 // Copied!
121 btn.text( button_copy_text );
122 setTimeout(
123 function() {
124 if ( 'svg-icon' === style ) {
125 btn.html( copyTheCode.buttonSvg );
126 } else {
127 btn.text( oldText );
128 }
129 },
130 1000
131 );
132 },
133
134 _preview: function() {
135 var button_position = $( '[name="button-position"]' ).val() || '';
136 // var value = $( 'select[name="copy-as"]' ).children("option:selected").val() || '';
137 var style = $( '[name="style"]' ).val() || 'button';
138 var button_text = $( '[name="button-text"]' ).val() || '';
139 var button_copy_text = $( '[name="button-copy-text"]' ).val() || '';
140 var button_title = $( '[name="button-title"]' ).val() || '';
141 var buttonMarkup = copyTheCode.buttonMarkup || '';
142
143 var markup = '<div class="outer">';
144 markup += '<div class="inner copy-as-text">';
145 markup += '<p>Sample Preview 1</p>';
146 markup += '<pre id="pre-html"></pre>';
147 markup += '</div>';
148
149 markup += '<div class="inner copy-as-html">';
150 markup += '<p>Sample Preview 2</p>';
151 markup += '<pre id="pre-text"></pre>';
152 markup += '</div>';
153
154 markup += '</div>';
155
156 $( '#preview' ).html( markup );
157 $( '#preview #pre-html' ).html( copyTheCode.previewMarkup );
158 $( '#preview #pre-text' ).text( copyTheCode.previewMarkup );
159
160 if ( 'cover' !== style && 'outside' === button_position ) {
161 $( '#preview pre' ).wrap( '<span class="copy-the-code-style-' + style + ' copy-the-code-wrap copy-the-code-outside-wrap"></span>' );
162 $( '#preview pre' ).parent().prepend( '<div class="copy-the-code-outside">' + buttonMarkup + '</div>' );
163 } else {
164 $( '#preview pre' ).wrap( '<span class="copy-the-code-style-' + style + ' copy-the-code-wrap copy-the-code-inside-wrap"></span>' );
165 $( '#preview pre' ).append( buttonMarkup );
166 }
167
168 $( '#preview' ).find( '.copy-the-code-button' ).attr( 'title', button_title );
169 $( '#preview' ).find( '.copy-the-code-button' ).attr( 'style', style );
170
171 if ( 'svg-icon' === style ) {
172 $( '[name="button-text"]' ).parents( 'tr' ).hide();
173 $( '[name="button-position"]' ).parents( 'tr' ).hide();
174 } else {
175 $( '[name="button-text"]' ).parents( 'tr' ).show();
176 $( '[name="button-position"]' ).parents( 'tr' ).show();
177 }
178 if ( 'cover' === style ) {
179 $( '[name="button-position"]' ).parents( 'tr' ).hide();
180 } else {
181 $( '[name="button-position"]' ).parents( 'tr' ).show();
182 }
183
184 switch ( style ) {
185 case 'svg-icon':
186 $( '#preview' ).find( '.copy-the-code-button' ).html( copyTheCode.buttonSvg );
187 break;
188 case 'cover':
189 case 'button':
190 default:
191 $( '#preview' ).find( '.copy-the-code-button' ).html( button_text );
192 break;
193 }
194
195 $( document ).trigger( 'copy-the-code-style-change', style );
196
197 },
198
199 _switch_tab: function( event ) {
200 $( this ).siblings().removeClass( 'nav-tab-active' );
201 $( this ).addClass( 'nav-tab-active' );
202 $( '#' + $( this ).attr( 'data-id' ) ).siblings().hide();
203 $( '#' + $( this ).attr( 'data-id' ) ).show();
204 }
205
206 };
207
208 /**
209 * Initialization
210 */
211 $(
212 function() {
213 CopyTheCodePage.init();
214 }
215 );
216
217 })( jQuery );
218