PluginProbe
Contact Forms by Cimatti / trunk
Contact Forms by Cimatti vtrunk
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / assets / js / admin / settings-page.js

settings-page.js in Contact Forms by Cimatti trunk, at assets/js/admin/settings-page.js

325 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Contact Forms - Settings Page JavaScript
3 *
4 * Handles the "Restore to Default" functionality for message sections
5 * in the default settings page.
6 *
7 * @package ContactForms
8 * @since 2.0.0-beta.6
9 */
10
11 (function ($) {
12 'use strict';
13
14 /**
15 * Initialize restore default buttons
16 */
17 function initRestoreButtons() {
18 $('.accua-restore-default-btn').on('click', function (e) {
19 e.preventDefault();
20
21 var $button = $(this);
22 var messageType = $button.data('message-type');
23
24 // Show confirmation dialog
25 if (!confirm(accuaFormsSettings.i18n.confirmRestore)) {
26 return;
27 }
28
29 // Disable button and show loading state
30 var originalText = $button.text();
31 $button
32 .prop('disabled', true)
33 .addClass('accua-restoring')
34 .text(accuaFormsSettings.i18n.restoring);
35
36 // Make AJAX request
37 $.ajax({
38 url: accuaFormsSettings.ajaxUrl,
39 type: 'POST',
40 data: {
41 action: 'accua_forms_restore_default_message',
42 nonce: accuaFormsSettings.nonce,
43 message_type: messageType
44 },
45 success: function (response) {
46 if (response.success) {
47 // Update the form fields with restored values
48 updateFields(messageType, response.data.values);
49
50 // Show success state
51 $button
52 .removeClass('accua-restoring')
53 .addClass('accua-restored')
54 .text(accuaFormsSettings.i18n.restored);
55
56 // Reset button after 2 seconds
57 setTimeout(function () {
58 $button
59 .prop('disabled', false)
60 .removeClass('accua-restored')
61 .text(originalText);
62 }, 2000);
63 } else {
64 showError($button, originalText, response.data ? response.data.message : accuaFormsSettings.i18n.error);
65 }
66 },
67 error: function () {
68 showError($button, originalText, accuaFormsSettings.i18n.error);
69 }
70 });
71 });
72 }
73
74 /**
75 * Show error state on button
76 *
77 * @param {jQuery} $button The button element
78 * @param {string} originalText Original button HTML
79 * @param {string} errorMessage Error message to log
80 */
81 function showError($button, originalText, errorMessage) {
82 console.error('Restore default error:', errorMessage);
83
84 $button
85 .removeClass('accua-restoring')
86 .addClass('accua-error')
87 .text(accuaFormsSettings.i18n.error);
88
89 // Reset button after 3 seconds
90 setTimeout(function () {
91 $button
92 .prop('disabled', false)
93 .removeClass('accua-error')
94 .text(originalText);
95 }, 3000);
96 }
97
98 /**
99 * Update form fields with restored values
100 *
101 * @param {string} messageType The type of message being restored
102 * @param {Object} values The restored values object
103 */
104 function updateFields(messageType, values) {
105 switch (messageType) {
106 case 'success_message':
107 updateTinyMCE('success_message', values.success_message);
108 break;
109
110 case 'error_message':
111 updateTinyMCE('error_message', values.error_message);
112 break;
113
114 case 'admin_emails':
115 // Update subject field
116 $('input[name="admin_emails_subject"]').val(values.admin_emails_subject);
117 // Update message editor
118 updateTinyMCE('admin_emails_message', values.admin_emails_message);
119 break;
120
121 case 'confirmation_emails':
122 // Update subject field
123 $('input[name="confirmation_emails_subject"]').val(values.confirmation_emails_subject);
124 // Update message editor
125 updateTinyMCE('confirmation_emails_message', values.confirmation_emails_message);
126 break;
127 }
128 }
129
130 /**
131 * Update TinyMCE editor content
132 *
133 * Handles both visual (TinyMCE) and text mode editors.
134 *
135 * @param {string} editorId The ID of the editor
136 * @param {string} content The new content
137 */
138 function updateTinyMCE(editorId, content) {
139 // Try to get TinyMCE editor instance
140 if (typeof tinymce !== 'undefined') {
141 var editor = tinymce.get(editorId);
142 if (editor && !editor.isHidden()) {
143 // Visual mode - update TinyMCE
144 editor.setContent(content);
145 editor.fire('change');
146 return;
147 }
148 }
149
150 // Fallback to textarea (text mode or no TinyMCE)
151 var $textarea = $('#' + editorId);
152 if ($textarea.length) {
153 $textarea.val(content).trigger('change');
154 }
155 }
156
157 /**
158 * Initialize Danger Zone - Bulk Anonymize (two-step: preview then confirm)
159 */
160 function initDangerZoneAnonymize() {
161 $('#accua_danger_anon_btn').on('click', function () {
162 var value = parseInt($('#accua_danger_anon_value').val(), 10);
163 var unit = $('#accua_danger_anon_unit').val();
164
165 if (!value || value < 1) {
166 alert(accuaFormsSettings.i18n.error);
167 return;
168 }
169
170 var $btn = $(this);
171 var $result = $('#accua_danger_anon_result');
172 var originalText = $btn.text();
173
174 // Step 1: fetch preview
175 $btn.prop('disabled', true).text(accuaFormsSettings.i18n.loading || 'Loading...');
176 $result.hide();
177
178 $.ajax({
179 url: accuaFormsSettings.ajaxUrl,
180 type: 'POST',
181 data: {
182 action: 'accua_forms_bulk_anonymize_preview',
183 nonce: accuaFormsSettings.dangerNonce,
184 value: value,
185 unit: unit
186 },
187 success: function (response) {
188 $btn.prop('disabled', false).text(originalText);
189 if (!response.success) {
190 $result.html('<div class="notice notice-error inline"><p>' + (response.data ? response.data.message : accuaFormsSettings.i18n.error) + '</p></div>').show();
191 return;
192 }
193
194 var data = response.data;
195 if (data.total === 0) {
196 $result.html('<div class="notice notice-info inline"><p>' + accuaFormsSettings.i18n.noSubmissionsFound + '</p></div>').show();
197 return;
198 }
199
200 // Build preview table
201 var html = '<div class="accua-anon-preview">';
202 html += '<p><strong>' + accuaFormsSettings.i18n.previewHeading.replace('%d', data.total) + '</strong></p>';
203 html += '<table class="widefat striped" style="margin: 8px 0;">';
204 html += '<thead><tr><th>' + accuaFormsSettings.i18n.formColumn + '</th><th style="text-align:right;">' + accuaFormsSettings.i18n.submissionsColumn + '</th></tr></thead>';
205 html += '<tbody>';
206 for (var i = 0; i < data.forms.length; i++) {
207 var f = data.forms[i];
208 html += '<tr><td>' + $('<span>').text(f.title).html() + ' <small style="color:#888;">(#' + f.id + ')</small></td><td style="text-align:right;">' + f.count + '</td></tr>';
209 }
210 html += '</tbody>';
211 html += '<tfoot><tr><th><strong>' + accuaFormsSettings.i18n.totalLabel + '</strong></th><th style="text-align:right;"><strong>' + data.total + '</strong></th></tr></tfoot>';
212 html += '</table>';
213 html += '<p><button type="button" id="accua_danger_anon_confirm" class="button" style="color:#d63638;border-color:#d63638;">' + accuaFormsSettings.i18n.confirmAnonymize + '</button> ';
214 html += '<button type="button" id="accua_danger_anon_cancel" class="button">' + accuaFormsSettings.i18n.cancel + '</button></p>';
215 html += '</div>';
216
217 $result.html(html).show();
218
219 // Cancel
220 $('#accua_danger_anon_cancel').on('click', function () {
221 $result.hide().empty();
222 });
223
224 // Step 2: execute
225 $('#accua_danger_anon_confirm').on('click', function () {
226 var $confirmBtn = $(this);
227 $confirmBtn.prop('disabled', true).text(accuaFormsSettings.i18n.anonymizing);
228 $('#accua_danger_anon_cancel').prop('disabled', true);
229
230 $.ajax({
231 url: accuaFormsSettings.ajaxUrl,
232 type: 'POST',
233 data: {
234 action: 'accua_forms_bulk_anonymize',
235 nonce: accuaFormsSettings.dangerNonce,
236 value: value,
237 unit: unit
238 },
239 success: function (resp) {
240 if (resp.success) {
241 $result.html('<div class="notice notice-success inline"><p>' + resp.data.message + '</p></div>').show();
242 } else {
243 $result.html('<div class="notice notice-error inline"><p>' + (resp.data ? resp.data.message : accuaFormsSettings.i18n.error) + '</p></div>').show();
244 }
245 },
246 error: function () {
247 $result.html('<div class="notice notice-error inline"><p>' + accuaFormsSettings.i18n.error + '</p></div>').show();
248 }
249 });
250 });
251 },
252 error: function () {
253 $btn.prop('disabled', false).text(originalText);
254 $result.html('<div class="notice notice-error inline"><p>' + accuaFormsSettings.i18n.error + '</p></div>').show();
255 }
256 });
257 });
258 }
259
260 /**
261 * Initialize Danger Zone - Delete All Data
262 */
263 function initDangerZoneDelete() {
264 $('#accua_danger_delete_btn').on('click', function () {
265 var typed = prompt(accuaFormsSettings.i18n.confirmDeletePrompt);
266
267 if (typed === null) {
268 return; // Cancelled
269 }
270
271 if (typed !== accuaFormsSettings.siteDomain) {
272 alert('Domain name does not match. Operation cancelled.');
273 return;
274 }
275
276 var $btn = $(this);
277 var $result = $('#accua_danger_delete_result');
278 var originalText = $btn.text();
279
280 $btn.prop('disabled', true).text(accuaFormsSettings.i18n.deleting);
281 $result.hide();
282
283 $.ajax({
284 url: accuaFormsSettings.ajaxUrl,
285 type: 'POST',
286 data: {
287 action: 'accua_forms_delete_all_data',
288 nonce: accuaFormsSettings.dangerNonce,
289 confirm_domain: typed
290 },
291 success: function (response) {
292 $btn.prop('disabled', false).text(originalText);
293 if (response.success) {
294 $result.html('<div class="notice notice-success inline"><p>' + response.data.message + '</p></div>').show();
295 } else {
296 $result.html('<div class="notice notice-error inline"><p>' + (response.data ? response.data.message : accuaFormsSettings.i18n.error) + '</p></div>').show();
297 }
298 },
299 error: function () {
300 $btn.prop('disabled', false).text(originalText);
301 $result.html('<div class="notice notice-error inline"><p>' + accuaFormsSettings.i18n.error + '</p></div>').show();
302 }
303 });
304 });
305 }
306
307 // Initialize when DOM is ready
308 $(document).ready(function () {
309 $('#accua_settings_tabs').cfTabs();
310 initRestoreButtons();
311 initDangerZoneAnonymize();
312 initDangerZoneDelete();
313
314 // Initialize color pickers on the Styling tab.
315 // No change callback here: triggering a jQuery change on the input
316 // re-enters Iris's own input listener before Iris has written the new
317 // colour back to the field, so Iris reads the stale value and either
318 // reverts the pick or, when the field was empty, clears the swatch.
319 if ($.fn.wpColorPicker) {
320 $('.accua-color-picker').wpColorPicker();
321 }
322 });
323
324 })(jQuery);
325