PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 3.11.0
Image Source Control Lite – Show Image Credits and Captions v3.11.0
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / admin / assets / js / settings.js

settings.js in Image Source Control Lite – Show Image Credits and Captions 3.11.0, at admin/assets/js/settings.js

318 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Scripts for Settings > Image Sources
3 */
4 jQuery( document ).ready(
5 function($) {
6 // Initial setup
7 isc_thumbnail_input_checkstate();
8 isc_caption_checkstate();
9 isc_licenses_checkstate();
10 isc_toggle_caption_position();
11 isc_toggle_module_sections();
12
13 // Event handlers
14 $( '#isc-settings-overlay-enable' ).on( 'click', function(){ isc_caption_checkstate() } );
15 $( '#isc-settings-licenses-enable' ).on( 'click', function(){ isc_licenses_checkstate() } );
16 $( '.isc-settings-standard-source input' ).on( 'change', isc_toggle_standard_source_text );
17 $( '#thumbnail-size-select, #use-thumbnail' ).on( 'change', function(){ isc_thumbnail_input_checkstate(); } );
18 $( '#isc-settings-caption-style input' ).on( 'change', function(){ isc_toggle_caption_position(); } );
19 $( '#isc-settings-global-list-indexed-images' ).on( 'change', function(){ isc_show_reindex_warning(); } );
20 $( '#isc-settings-plugin-modules input[type="checkbox"]').on('change', function() { isc_toggle_module_sections(); });
21 $( '#isc-settings-plugin-images-only' ).on( 'change', function() {
22 const enabled = this.checked;
23 $( '#isc-settings-plugin-images-only-indexer' ).toggleClass( 'hidden' );
24 $( '#isc-settings-plugin-images-only-cleanup-wrapper' ).toggleClass( 'hidden', !enabled );
25 });
26
27 // Copy log URL to clipboard
28 $( '#isc-copy-log-url-btn' ).on( 'click', function() {
29 const urlField = document.getElementById( 'isc-log-url-field' );
30 if ( ! urlField ) {
31 return;
32 }
33
34 const showSuccess = function() {
35 $( '#isc-copy-log-url-success' ).stop( true, true ).fadeIn().delay( 2000 ).fadeOut();
36 };
37
38 const fallbackCopy = function() {
39 urlField.focus();
40 urlField.select();
41 urlField.setSelectionRange( 0, urlField.value.length );
42 if ( document.execCommand( 'copy' ) ) {
43 showSuccess();
44 }
45 };
46
47 if ( navigator.clipboard?.writeText ) {
48 navigator.clipboard.writeText( urlField.value ).then( showSuccess ).catch( fallbackCopy );
49 } else {
50 fallbackCopy();
51 }
52 } );
53
54 // Download log file
55 $( '#isc-download-log-btn' ).on( 'click', function() {
56 // Use AJAX to download the file via WordPress backend
57 $.ajax({
58 type: 'POST',
59 url: ajaxurl,
60 data: {
61 action: 'isc_download_log',
62 nonce: isc.ajaxNonce,
63 },
64 xhrFields: {
65 responseType: 'blob'
66 },
67 success: function( response, status, xhr ) {
68 // Get filename from Content-Disposition header or use default
69 const disposition = xhr.getResponseHeader( 'Content-Disposition' );
70 let filename = 'image-source-control.log';
71 if ( disposition && disposition.indexOf( 'filename=' ) !== -1 ) {
72 const filenameMatch = disposition.match( /filename="?([^"]+)"?/ );
73 if ( filenameMatch && filenameMatch[1] ) {
74 filename = filenameMatch[1];
75 }
76 }
77
78 // Create a blob URL and trigger download
79 const blob = new Blob( [response], { type: 'text/plain' } );
80 const url = window.URL.createObjectURL( blob );
81 const link = document.createElement( 'a' );
82 link.href = url;
83 link.download = filename;
84 document.body.appendChild( link );
85 link.click();
86 document.body.removeChild( link );
87 window.URL.revokeObjectURL( url );
88 },
89 error: function( xhr ) {
90 // Show error message
91 console.error( 'Error downloading log file:', xhr.statusText );
92 let errorMsg = 'Error downloading log file.';
93 if (xhr.status) {
94 errorMsg += ' HTTP status: ' + xhr.status + ' (' + xhr.statusText + ').';
95 }
96 // Try to extract a more specific error message from the response, if available
97 if (xhr.responseText) {
98 errorMsg += '\nDetails: ' + xhr.responseText;
99 }
100 alert(errorMsg);
101 }
102 });
103 } );
104
105 // Toggle log URL field visibility when checkbox is changed
106 $( '#isc-enable-log-checkbox' ).on( 'change', function() {
107 $( '#isc-log-url-wrapper' ).toggle( this.checked );
108 });
109
110 // Show and update preview when a position option is clicked
111 $('#isc-settings-caption-pos-options button').on( 'click', function (event) {
112 // Stop propagation to prevent document click event from hiding the preview immediately
113 event.stopPropagation();
114
115 $('#isc-settings-caption-pos-options button.selected').removeClass('selected');
116 $(this).addClass('selected');
117 $('#isc-settings-caption-position').val($(this).data('position'));
118
119 var iframe = document.createElement('iframe');
120 iframe.src = isc_settings.baseurl + 'admin/templates/settings/preview/caption-preview.html' + "?position=" + $(this).data('position') + "&pretext=" + encodeURIComponent($('#source-pretext').val());
121 iframe.width = "250";
122 iframe.height = "181";
123
124 var preview_container = $('#isc-settings-caption-preview');
125 preview_container.find('iframe').remove();
126 preview_container.append(iframe);
127 preview_container.removeClass('hidden');
128 });
129
130 // Hide the preview when the mouse leaves the option area or a click occurs outside the option area
131 $(document).on('click mouseout', function (event) {
132 if (!$(event.target).closest('#isc-settings-caption-pos-options').length) {
133 $('#isc-settings-caption-preview').addClass('hidden');
134 }
135 });
136
137 $('#isc-signup-nl').prop( 'disabled', false );
138 $('#isc-signup-nl').on( 'click', function() {
139 $('#isc-signup-nl').prop( 'disabled', true );
140 $('#isc-signup-loader').removeClass('hidden');
141 $.ajax({
142 type: 'POST',
143 url: ajaxurl,
144 data: {
145 action: 'newsletter_signup',
146 nonce: isc.ajaxNonce,
147 },
148 dataType: 'json',
149 success: function( response ) {
150 if ( ! response.success ) {
151 $('#isc-signup-nl-error').removeClass('hidden').html(response.message);
152 } else {
153 $('#isc-signup-nl-success').removeClass('hidden').html(response.message);
154 }
155 $('#isc-signup-loader').addClass('hidden');
156 }
157 });
158 });
159 // close the newsletter signup box without signing up
160 $('#isc_settings_section_signup .postbox-header .dashicons-no-alt').on( 'click', function() {
161 $('#isc-signup-nl').prop( 'disabled', true );
162 $('#isc-signup-loader').removeClass('hidden');
163 $.ajax({
164 type: 'POST',
165 url: ajaxurl,
166 data: {
167 action: 'newsletter_close',
168 nonce: isc.ajaxNonce,
169 },
170 dataType: 'json',
171 success: function( response ) {
172 $('#isc_settings_section_signup').remove();
173 }
174 });
175 });
176
177 // Add special characters to the source-pretext field by clicking on a button
178 document.querySelectorAll('#source-pretext-buttons button').forEach( function( button ) {
179 button.addEventListener('click', function() {
180 var inputField = document.getElementById('source-pretext');
181 inputField.value += this.textContent; // Use the button's content
182 inputField.focus(); // Optionally, refocus on the input field
183 });
184 });
185 // Show the buttons when the input is focused
186 document.getElementById('source-pretext').addEventListener('focus', function() {
187 document.getElementById('source-pretext-buttons').classList.remove('hidden');
188 });
189 }
190 );
191
192 /**
193 * Toggle visibility of module-related sections based on checkbox states
194 */
195 function isc_toggle_module_sections() {
196 Object.entries( isc.moduleSections ).forEach(([module, sections]) => {
197 const checkbox = document.querySelector(`#isc-settings-plugin-modules input[value="${module}"]`);
198 if (checkbox) {
199 sections.forEach(sectionId => {
200 const sectionElement = document.getElementById(sectionId);
201 const section = sectionElement ? sectionElement.querySelector('.inside') : null;
202 if (section) {
203 if (checkbox.checked) {
204 section.classList.remove('hidden');
205 } else {
206 section.classList.add('hidden');
207 }
208 }
209 });
210 }
211 });
212 }
213
214 /**
215 * Toggle the state of the thumbnail size options
216 */
217 function isc_thumbnail_input_checkstate(){
218 // enable the thumbnail size select field when thumbnails are enabled in general
219 if ( document.getElementById( 'use-thumbnail' ).checked ) {
220 document.getElementById( 'thumbnail-size-select' ).classList.remove( 'hidden' );
221 } else {
222 document.getElementById( 'thumbnail-size-select' ).classList.add( 'hidden' )
223 }
224
225 // toggle the state of the thumbnail custom size options in the plugin settings to only enable them if the "custom" thumbnails size is used
226 if ( 'custom' === document.getElementById( 'thumbnail-size-select' ).value && document.getElementById( 'use-thumbnail' ).checked ) {
227 document.getElementById( 'isc-settings-custom-size' ).classList.remove( 'hidden' );
228 } else {
229 document.getElementById( 'isc-settings-custom-size' ).classList.add( 'hidden' );
230 }
231 }
232
233 /**
234 * Toggle the state of the options in the Overlay settings
235 */
236 function isc_caption_checkstate() {
237 var overlay_enabled = document.getElementById( 'isc-settings-overlay-enable' );
238
239 if ( ! overlay_enabled ) {
240 return;
241 }
242 var elements = document.querySelectorAll( '.isc_settings_section_overlay .form-table tr:not(:first-of-type)' );
243 if ( overlay_enabled.checked ) {
244 Array.prototype.forEach.call( elements, function(el, i) {
245 el.style.display = 'table-row';
246 } );
247 } else {
248 Array.prototype.forEach.call( elements, function(el, i) {
249 el.style.display = 'none';
250 } );
251 }
252 }
253
254 /**
255 * Toggle the state of the options in the Licenses settings
256 */
257 function isc_licenses_checkstate() {
258 var licenses_enabled = document.getElementById( 'isc-settings-licenses-enable' );
259
260 if ( ! licenses_enabled ) {
261 return;
262 }
263 var elements = document.querySelectorAll( '.isc_settings_section_licenses .form-table tr:not(:first-of-type)' );
264 if ( licenses_enabled.checked ) {
265 Array.prototype.forEach.call( elements, function(el, i) {
266 el.style.display = 'table-row';
267 } );
268 } else {
269 Array.prototype.forEach.call( elements, function(el, i) {
270 el.style.display = 'none';
271 } );
272 }
273 }
274
275 /**
276 * Toggle the state of the Custom Text option in the Standard Sources settings
277 */
278 function isc_toggle_standard_source_text() {
279 var standard_source_custom = document.getElementById( 'isc-custom-text-select' );
280
281 if ( ! standard_source_custom ) {
282 return;
283 }
284
285 if ( standard_source_custom.checked ) {
286 document.getElementById( 'isc-custom-text' ).removeAttribute( 'disabled' );
287 } else {
288 document.getElementById( 'isc-custom-text' ).setAttribute( 'disabled', 'disabled' );
289 }
290 }
291
292 /**
293 * Toggle the visibility of the position option when the caption style is changed
294 */
295 function isc_toggle_caption_position() {
296 var caption_style = document.querySelector( '#isc-settings-caption-style input:checked' );
297
298 if ( ! caption_style ) {
299 return;
300 }
301
302 if ( caption_style.value === 'none' ) {
303 document.getElementById( 'isc-settings-caption-position-options-wrapper' ).classList.add( 'hidden' );
304 // add class also to the sibling H4 element
305 document.getElementById( 'isc-settings-caption-position-options-wrapper' ).previousElementSibling.classList.add( 'hidden' );
306 } else {
307 document.getElementById( 'isc-settings-caption-position-options-wrapper' ).classList.remove( 'hidden' );
308 // remove class also from the sibling H4 element
309 document.getElementById( 'isc-settings-caption-position-options-wrapper' ).previousElementSibling.classList.remove( 'hidden' );
310 }
311 }
312
313 /**
314 * Toggle the visibility of the warning about a reindex when changing the Index-Images option for Global Lists
315 */
316 function isc_show_reindex_warning() {
317 document.getElementById( 'isc-settings-global-list-indexed-images-warning' ).classList.remove( 'hidden' );
318 }