Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/scripts/forms.js

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + "use strict";
2 +
3 + function aiomatic_type_changed() {
4 + var value = jQuery('#aiomatic-type').children(":selected").attr("value");
5 + if(value == 'text') {
6 + jQuery('.hide-when-not-text').show();
7 + jQuery('.hide-omniblock-form').hide();
8 + }
9 + else if(value == 'omniblock-form') {
10 + jQuery('.hide-when-not-text').hide();
11 + jQuery('.hide-omniblock-form').show();
12 + }
13 + else {
14 + jQuery('.hide-when-not-text').hide();
15 + jQuery('.hide-omniblock-form').hide();
16 + }
17 + }
18 + jQuery(document).ready(function ($)
19 + {
20 + $(document).on('change','.aiomatic-create-template-field-type', function(e){
21 + var type = $(e.currentTarget).val();
22 + var parentEl = $(e.currentTarget).closest('.aiomatic-template-form-field');
23 + parentEl.find('.aiomatic-create-template-field-value-main').show();
24 + if(type === 'select' || type === 'radio' || type === 'checkbox' || type === 'html'){
25 + parentEl.find('.aiomatic-create-template-field-options-main').show();
26 + parentEl.find('.aiomatic-create-template-field-min-main').hide();
27 + parentEl.find('.aiomatic-create-template-field-placeholder-main').hide();
28 + parentEl.find('.aiomatic-create-template-field-limit-main').hide();
29 + parentEl.find('.aiomatic-create-template-field-max-main').hide();
30 + parentEl.find('.aiomatic-create-template-field-rows-main').hide();
31 + parentEl.find('.aiomatic-create-template-field-cols-main').hide();
32 + }
33 + else if(type === 'color' || type === 'date' || type === 'time' || type === 'datetime' || type === 'captcha' || type === 'month' || type === 'week'){
34 + parentEl.find('.aiomatic-create-template-field-options-main').hide();
35 + parentEl.find('.aiomatic-create-template-field-min-main').hide();
36 + parentEl.find('.aiomatic-create-template-field-placeholder-main').hide();
37 + parentEl.find('.aiomatic-create-template-field-limit-main').hide();
38 + parentEl.find('.aiomatic-create-template-field-max-main').hide();
39 + parentEl.find('.aiomatic-create-template-field-rows-main').hide();
40 + parentEl.find('.aiomatic-create-template-field-cols-main').hide();
41 + }
42 + else if(type === 'file'){
43 + parentEl.find('.aiomatic-create-template-field-options-main').show();
44 + parentEl.find('.aiomatic-create-template-field-min-main').hide();
45 + parentEl.find('.aiomatic-create-template-field-placeholder-main').hide();
46 + parentEl.find('.aiomatic-create-template-field-limit-main').show();
47 + parentEl.find('.aiomatic-create-template-field-max-main').hide();
48 + parentEl.find('.aiomatic-create-template-field-rows-main').hide();
49 + parentEl.find('.aiomatic-create-template-field-cols-main').hide();
50 + }
51 + else if(type === 'textarea'){
52 + parentEl.find('.aiomatic-create-template-field-rows-main').show();
53 + parentEl.find('.aiomatic-create-template-field-cols-main').show();
54 + parentEl.find('.aiomatic-create-template-field-options-main').hide();
55 + parentEl.find('.aiomatic-create-template-field-placeholder-main').show();
56 + parentEl.find('.aiomatic-create-template-field-limit-main').show();
57 + parentEl.find('.aiomatic-create-template-field-min-main').hide();
58 + parentEl.find('.aiomatic-create-template-field-max-main').hide();
59 + }
60 + else if(type === 'range'){
61 + parentEl.find('.aiomatic-create-template-field-rows-main').hide();
62 + parentEl.find('.aiomatic-create-template-field-cols-main').hide();
63 + parentEl.find('.aiomatic-create-template-field-options-main').hide();
64 + parentEl.find('.aiomatic-create-template-field-placeholder-main').show();
65 + parentEl.find('.aiomatic-create-template-field-limit-main').show();
66 + parentEl.find('.aiomatic-create-template-field-min-main').show();
67 + parentEl.find('.aiomatic-create-template-field-max-main').show();
68 + }
69 + else if(type === 'number'){
70 + parentEl.find('.aiomatic-create-template-field-rows-main').hide();
71 + parentEl.find('.aiomatic-create-template-field-cols-main').hide();
72 + parentEl.find('.aiomatic-create-template-field-options-main').hide();
73 + parentEl.find('.aiomatic-create-template-field-placeholder-main').show();
74 + parentEl.find('.aiomatic-create-template-field-limit-main').show();
75 + parentEl.find('.aiomatic-create-template-field-min-main').show();
76 + parentEl.find('.aiomatic-create-template-field-max-main').show();
77 + }
78 + else{
79 + parentEl.find('.aiomatic-create-template-field-rows-main').hide();
80 + parentEl.find('.aiomatic-create-template-field-cols-main').hide();
81 + parentEl.find('.aiomatic-create-template-field-options-main').hide();
82 + parentEl.find('.aiomatic-create-template-field-placeholder-main').show();
83 + parentEl.find('.aiomatic-create-template-field-limit-main').show();
84 + parentEl.find('.aiomatic-create-template-field-min-main').hide();
85 + parentEl.find('.aiomatic-create-template-field-max-main').hide();
86 + }
87 + })
88 + var aiomaticCreateField = $('.aiomatic-template-form-field-default');
89 + $('#aiomatic-create-form-field').on("click", function(e){
90 + e.preventDefault();
91 + if(aiomaticCreateField !== null)
92 + {
93 + var temphtml = aiomaticCreateField.html().replace(' aiomatic-hidden-form','');
94 + $('.aiomatic-template-fields').append(temphtml);
95 + aiomaticSortField();
96 + }
97 + });
98 + $('body').on("click", '.aiomatic-create-form-field', function(e){
99 + e.preventDefault();
100 + if(aiomaticCreateField !== null)
101 + {
102 + var temphtml = aiomaticCreateField.html().replace(' aiomatic-hidden-form','');
103 + $('.aiomatic-template-fields').append(temphtml);
104 + aiomaticSortField();
105 + }
106 + });
107 + $('#aiomatic-show-hide-field').on("click", function(e){
108 + e.preventDefault();
109 + if(jQuery('#hideAdv').is(":visible"))
110 + {
111 + jQuery("#hideAdv").hide();
112 + }
113 + else
114 + {
115 + jQuery("#hideAdv").show();
116 + }
117 + });
118 + $(document).on('click','.aiomatic-show-hide-field', function(e){
119 + e.preventDefault();
120 + if(jQuery('#hideAdv_edit').is(":visible"))
121 + {
122 + jQuery("#hideAdv_edit").hide();
123 + }
124 + else
125 + {
126 + jQuery("#hideAdv_edit").show();
127 + }
128 + });
129 + $(document).on('click','.header-al-right', function(e){
130 + e.preventDefault();
131 + if(jQuery('.main-form-holder').is(":visible"))
132 + {
133 + jQuery(".main-form-holder").hide();
134 + jQuery('.header-al-right').html('Show Input Fields');
135 + }
136 + else
137 + {
138 + jQuery(".main-form-holder").show();
139 + jQuery('.header-al-right').html('Hide Input Fields');
140 + }
141 + });
142 + function aioGenerateRandomString(length) {
143 + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
144 + let result = '';
145 + const charactersLength = characters.length;
146 + for (let i = 0; i < length; i++) {
147 + result += characters.charAt(Math.floor(Math.random() * charactersLength));
148 + }
149 + return result;
150 + }
151 + $(document).on('click','.aiomatic-field-delete', function(e){
152 + if(confirm('Are you sure you want to delete this field?'))
153 + {
154 + $(e.currentTarget).parent().parent().parent().remove();
155 + aiomaticSortField();
156 + }
157 + });
158 + $(document).on('click','.aiomatic-field-up', function(e){
159 + let field = $(e.currentTarget).parent().parent().parent();
160 + let prevField = field.prev('.aiomatic-template-form-field');
161 + if (prevField.length !== 0) {
162 + field.insertBefore(prevField);
163 + aiomaticSortField();
164 + }
165 + });
166 + $(document).on('click','.aiomatic-field-down', function(e){
167 + let field = $(e.currentTarget).parent().parent().parent();
168 + let nextField = field.next('.aiomatic-template-form-field');
169 + if (nextField.length !== 0) {
170 + field.insertAfter(nextField);
171 + aiomaticSortField();
172 + }
173 + });
174 + $(document).on('click', '.aiomatic-field-duplicate', function(e)
175 + {
176 + if(confirm('Are you sure you want to duplicate this field?'))
177 + {
178 + let clonedElement = $(e.currentTarget).parent().parent().parent().clone();
179 + let inputField = clonedElement.find('input[placeholder="my_unique_input_id"]');
180 + let originalValue = inputField.val();
181 + inputField.val(originalValue + '-' + aioGenerateRandomString(5));
182 + $(e.currentTarget).parent().parent().parent().after(clonedElement);
183 + aiomaticSortField();
184 + }
185 + });
186 + $(document).on('click','.aiomatic-field-delete-add', function(e){
187 + if(confirm('Are you sure you want to delete this field?'))
188 + {
189 + $(e.currentTarget).parent().parent().remove();
190 + aiomaticSortField();
191 + }
192 + });
193 + $(document).on('click','.aiomatic-field-up-add', function(e){
194 + let field = $(e.currentTarget).parent().parent();
195 + let prevField = field.prev('.aiomatic-template-form-field');
196 + if (prevField.length !== 0) {
197 + field.insertBefore(prevField);
198 + aiomaticSortField();
199 + }
200 + });
201 + $(document).on('click','.aiomatic-field-down-add', function(e){
202 + let field = $(e.currentTarget).parent().parent();
203 + let nextField = field.next('.aiomatic-template-form-field');
204 + if (nextField.length !== 0) {
205 + field.insertAfter(nextField);
206 + aiomaticSortField();
207 + }
208 + });
209 + $(document).on('click', '.aiomatic-field-duplicate-add', function(e)
210 + {
211 + if(confirm('Are you sure you want to duplicate this field?'))
212 + {
213 + let clonedElement = $(e.currentTarget).parent().parent().clone();
214 + let inputField = clonedElement.find('input[placeholder="my_unique_input_id"]');
215 + let originalValue = inputField.val();
216 + inputField.val(originalValue + '-' + aioGenerateRandomString(5));
217 + $(e.currentTarget).parent().parent().after(clonedElement);
218 + aiomaticSortField();
219 + }
220 + });
221 + var aiomaticFieldInputs = ['label','id','required','type','min','max','options','rows','cols','placeholder','limit','value'];
222 + function aiomaticSortField(){
223 + $('.aiomatic-template-fields .aiomatic-template-form-field').each(function(idx, item){
224 + $.each(aiomaticFieldInputs, function(idxy, field){
225 + $(item).find('.aiomatic-create-template-field-'+field).attr('name','aiomaticfields['+idx+']['+field+']');
226 + });
227 + })
228 + }
229 + $("#checkedAll").on('change', function() {
230 + if (this.checked) {
231 + $(".aiomatic-select-form").each(function() {
232 + this.checked=true;
233 + });
234 + } else {
235 + $(".aiomatic-select-form").each(function() {
236 + this.checked=false;
237 + });
238 + }
239 + });
240 + function aiomaticLoading(btn)
241 + {
242 + btn.attr('disabled','disabled');
243 + if(!btn.find('spinner').length){
244 + btn.append('<span class="spinner"></span>');
245 + }
246 + btn.find('.spinner').css('visibility','unset');
247 + }
248 + function aiomaticDisable(btn)
249 + {
250 + btn.prop('disabled', true);
251 + }
252 + function aiomaticEnable(btn)
253 + {
254 + btn.removeAttr('disabled');
255 + }
256 + function aiomaticRmLoading(btn)
257 + {
258 + btn.removeAttr('disabled');
259 + btn.find('.spinner').remove();
260 + }
261 + $('#aiomatic_sync_forms').on('click', function (){
262 + var btn = $(this);
263 + aiomaticLoading(btn);
264 + var currentUrl = window.location.href;
265 + var updatedUrl = currentUrl.replace(/(\?|&)wpage=[^&]+/, '');
266 + window.location.href = updatedUrl;
267 + });
268 + $('#aiomatic_upload_forms').on('click', function (){
269 + if(confirm('Are you sure you want to add forms from the CSV file?'))
270 + {
271 + var aiomatic_csv_upload = $('#aiomatic_csv_upload');
272 + var btn = $(this);
273 + aiomaticLoading(btn);
274 + var data = {
275 + action: 'aiomatic_upload_forms',
276 + nonce: aiomatic_object.nonce,
277 + };
278 + if(aiomatic_csv_upload[0].files.length === 0){
279 + alert('Please select a file!');
280 + }
281 + else{
282 + var aiomatic_max_file_size = aiomatic_object.maxfilesize;
283 + var aiomatic_max_size_in_mb = aiomatic_object.maxfilesize / (1024 ** 2);
284 + var aiomatic_form_file = aiomatic_csv_upload[0].files[0];
285 + var aiomatic_form_file_extension = aiomatic_form_file.name.substr( (aiomatic_form_file.name.lastIndexOf('.') +1) );
286 + if(aiomatic_form_file_extension !== 'csv'){
287 + aiomatic_csv_upload.val('');
288 + alert('This feature only accepts csv file type!');
289 + }
290 + else if(aiomatic_form_file.size > aiomatic_max_file_size){
291 + aiomatic_csv_upload.val('');
292 + alert('Dataset allowed maximum size (MB): '+ aiomatic_max_size_in_mb)
293 + }
294 + else{
295 + var reader = new FileReader();
296 + reader.readAsText(aiomatic_form_file, "UTF-8");
297 + reader.onload = function (evt) {
298 + var formData = new FormData();
299 + formData.append('action', 'aiomatic_forms_upload');
300 + formData.append('xfile', evt.target.result);
301 + formData.append('nonce', aiomatic_object.nonce);
302 + $.ajax({
303 + url: aiomatic_object.ajax_url,
304 + type: 'POST',
305 + dataType: 'JSON',
306 + data: formData,
307 + success: function(res) {
308 + if(res.status === 'success'){
309 + aiomaticRmLoading(btn);
310 + alert('File uploaded successfully!');
311 + }
312 + else{
313 + aiomaticRmLoading(btn);
314 + alert('An error occured: ' + JSON.stringify(res));
315 + }
316 + },
317 + cache: false,
318 + contentType: false,
319 + processData: false,
320 + error: function (r, s, error){
321 + aiomaticRmLoading(btn);
322 + alert('Unable to upload file: ' + error);
323 + }
324 + });
325 + }
326 + reader.onerror = function (evt) {
327 + alert("Error reading file: " + aiomatic_form_file.name + ' - ' + reader.error);
328 + }
329 + }
330 + }
331 + }
332 + });
333 + $('#aiomatic_deleteall_forms').on('click', function (e){
334 + e.preventDefault();
335 + if(confirm('Are you sure you want to delete ALL forms?'))
336 + {
337 + var btn = $(this);
338 + aiomaticLoading(btn);
339 + var data = {
340 + action: 'aiomatic_deleteall_forms',
341 + nonce: aiomatic_object.nonce,
342 + };
343 + $.ajax({
344 + url: aiomatic_object.ajax_url,
345 + data: data,
346 + dataType: 'JSON',
347 + type: 'POST',
348 + success: function (res){
349 + aiomaticRmLoading(btn);
350 + if(res.status === 'success'){
351 + $('.aiomatic-forms-success').show();
352 + $('.aiomatic-forms-content').val('');
353 + setTimeout(function (){
354 + $('.aiomatic-forms-success').hide();
355 + },2000);
356 + location.reload();
357 + }
358 + else{
359 + alert(res.msg);
360 + }
361 + },
362 + error: function (r, s, error){
363 + aiomaticRmLoading(btn);
364 + alert('Error in processing form removal: ' + error);
365 + }
366 + });
367 + }
368 + });
369 + $('#aiomatic_delete_selected_forms').on('click', function (e){
370 + e.preventDefault();
371 + if(confirm('Are you sure you want to delete selected forms?'))
372 + {
373 + var btn = $(this);
374 + aiomaticLoading(btn);
375 + var ids = [];
376 + $('.aiomatic-select-form:checked').each(function (idx, item) {
377 + ids.push($(item).val())
378 + });
379 + if (ids.length) {
380 + var data = {
381 + action: 'aiomatic_delete_selected_form',
382 + nonce: aiomatic_object.nonce,
383 + ids: ids
384 + };
385 + $.ajax({
386 + url: aiomatic_object.ajax_url,
387 + data: data,
388 + dataType: 'JSON',
389 + type: 'POST',
390 + success: function (res){
391 + aiomaticRmLoading(btn);
392 + if(res.status === 'success'){
393 + $('.aiomatic-forms-success').show();
394 + $('.aiomatic-forms-content').val('');
395 + setTimeout(function (){
396 + $('.aiomatic-forms-success').hide();
397 + },2000);
398 + location.reload();
399 + }
400 + else{
401 + alert(res.msg);
402 + }
403 + },
404 + error: function (r, s, error){
405 + aiomaticRmLoading(btn);
406 + alert('Error in processing form removal: ' + error);
407 + }
408 + });
409 + } else {
410 + alert('No forms selected');
411 + aiomaticRmLoading(btn);
412 + }
413 + }
414 + });
415 + $('#aiomatic_forms_form').on('submit', function (e)
416 + {
417 + e.preventDefault();
418 + var form = $('#aiomatic_forms_form');
419 + var btn = form.find('#aiomatic-form-save-button');
420 + var title = $('#aiomatic-form-title').val();
421 + var prompt = $('#aiomatic-form-prompt').val();
422 + if(title === '' || prompt === ''){
423 + alert('Please insert all required values!');
424 + }
425 + else{
426 + var data = form.serialize();
427 + $.ajax({
428 + url: aiomatic_object.ajax_url,
429 + data: data,
430 + dataType: 'JSON',
431 + type: 'POST',
432 + beforeSend: function (){
433 + aiomaticLoading(btn);
434 + },
435 + success: function (res){
436 + aiomaticRmLoading(btn);
437 + if(res.status === 'success'){
438 + $('.aiomatic-forms-success').html("Form saved successfully, you can use the following shortcode to show it:<br/><b>[aimogen-form id='" + res.id + "']</b>");
439 + $('.aiomatic-forms-success').show();
440 + location.reload();
441 + }
442 + else{
443 + alert(res.msg);
444 + }
445 + },
446 + error: function (r, s, error){
447 + aiomaticRmLoading(btn);
448 + alert('Error in processing form saving: ' + error);
449 + }
450 + });
451 + }
452 + return false;
453 + });
454 + $('body').on("click", '#aiomatic-form-save-button_edit',function(e)
455 + {
456 + var form = $('#aiomatic_forms_form_edit');
457 + var btn = form.find('#aiomatic-form-save-button_edit');
458 + var title = $('#aiomatic-form-title_edit').val();
459 + var prompt = $('#aiomatic-form-prompt_edit').val();
460 + var submit = $('#aiomatic-submit_edit').val();
461 + if(title === '' || prompt === '' || submit == ''){
462 + alert('Please insert all required values!');
463 + }
464 + else{
465 + var data = form.serialize();
466 + $.ajax({
467 + url: aiomatic_object.ajax_url,
468 + data: data,
469 + dataType: 'JSON',
470 + type: 'POST',
471 + beforeSend: function (){
472 + aiomaticLoading(btn);
473 + },
474 + success: function (res){
475 + aiomaticRmLoading(btn);
476 + if(res.status === 'success'){
477 + $('.aiomatic-forms-success').html("Form saved successfully, you can use the following shortcode to show it:<br/><b>[aimogen-form id='" + res.id + "']</b>");
478 + $('.aiomatic-forms-success').show();
479 + location.reload();
480 + }
481 + else{
482 + alert(res.msg);
483 + }
484 + },
485 + error: function (r, s, error){
486 + aiomaticRmLoading(btn);
487 + alert('Error while processing form saving: ' + error);
488 + }
489 + });
490 + }
491 + return false;
492 + });
493 + $(".aiomatic_delete_form").on('click', function(e) {
494 + if(confirm('Are you sure you want to delete this form?'))
495 + {
496 + var formid = $(this).attr("delete-id");
497 + if(formid == '')
498 + {
499 + alert('Incorrect delete id submitted');
500 + }
501 + else
502 + {
503 + e.preventDefault();
504 + var data = {
505 + action: 'aiomatic_delete_form',
506 + formid: formid,
507 + nonce: aiomatic_object.nonce,
508 + };
509 + jQuery.ajax({
510 + url: aiomatic_object.ajax_url,
511 + data: data,
512 + dataType: 'JSON',
513 + type: 'POST',
514 + beforeSend: function (){
515 + aiomaticDisable($('#aiomatic_delete_form_' + formid));
516 + },
517 + success: function (res){
518 + if(res.status === 'success'){
519 + location.reload();
520 + }
521 + else{
522 + alert(res.msg);
523 + location.reload();
524 + }
525 + },
526 + error: function (r, s, error){
527 + alert('Error in processing form deletion: ' + error);
528 + location.reload();
529 + }
530 + });
531 + }
532 + }
533 + });
534 + $(".aiomatic_duplicate_form").on('click', function(e) {
535 + if(confirm('Are you sure you want to duplicate this form?'))
536 + {
537 + var formid = $(this).attr("data-id");
538 + if(formid == '')
539 + {
540 + alert('Incorrect data id submitted');
541 + }
542 + else
543 + {
544 + e.preventDefault();
545 + var data = {
546 + action: 'aiomatic_duplicate_form',
547 + formid: formid,
548 + nonce: aiomatic_object.nonce,
549 + };
550 + jQuery.ajax({
551 + url: aiomatic_object.ajax_url,
552 + data: data,
553 + dataType: 'JSON',
554 + type: 'POST',
555 + beforeSend: function (){
556 + aiomaticDisable($('#aiomatic_duplicate_form_' + formid));
557 + },
558 + success: function (res){
559 + if(res.status === 'success'){
560 + location.reload();
561 + }
562 + else{
563 + alert(res.msg);
564 + location.reload();
565 + }
566 + },
567 + error: function (r, s, error){
568 + alert('Error in processing form duplication: ' + error);
569 + location.reload();
570 + }
571 + });
572 + }
573 + }
574 + });
575 + var aiomatic_form_button = $('#aiomatic_form_default_button');
576 + aiomatic_form_button.on('click', function (e){
577 + if(confirm('Are you sure you want to load the default forms which came bundled with the plugin?'))
578 + {
579 + e.preventDefault();
580 + var data = {
581 + action: 'aiomatic_default_form',
582 + nonce: aiomatic_object.nonce,
583 + };
584 + jQuery.ajax({
585 + url: aiomatic_object.ajax_url,
586 + data: data,
587 + dataType: 'JSON',
588 + type: 'POST',
589 + beforeSend: function (){
590 + aiomaticDisable($('#aiomatic_form_default_button'));
591 + },
592 + success: function (res){
593 + if(res.status === 'success'){
594 + alert('Default forms loaded successfully!');
595 + location.reload();
596 + }
597 + else{
598 + alert(res.msg);
599 + location.reload();
600 + }
601 + },
602 + error: function (r, s, error){
603 + alert('Error in processing form loading: ' + error);
604 + location.reload();
605 + }
606 + });
607 + }
608 + });
609 + var aiomatic_manage_form = $('.aiomatic_manage_form');
610 + var aiomaticAjaxRunning = false;
611 + aiomatic_manage_form.on('click', function (){
612 + if(!aiomaticAjaxRunning) {
613 + var btn = $(this);
614 + var id = btn.attr('data-id');
615 + $.ajax({
616 + url: aiomatic_object.ajax_url,
617 + data: {action: 'aiomatic_get_form', id: id, nonce: aiomatic_object.nonce},
618 + dataType: 'JSON',
619 + type: 'POST',
620 + beforeSend: function () {
621 + aiomaticAjaxRunning = true;
622 + aiomaticLoading(btn);
623 + },
624 + success: function (res) {
625 + aiomaticAjaxRunning = false;
626 + aiomaticRmLoading(btn);
627 + if (res.status === 'success') {
628 + $('.aiomatic_modal_title').html('Edit this form');
629 + $('.aiomatic_modal_content').html(res.data);
630 + $('.aiomatic-overlay').show();
631 + $('.aiomatic_modal').show();
632 + } else {
633 + alert(res.msg);
634 + }
635 + },
636 + error: function (r, s, error) {
637 + aiomaticAjaxRunning = false;
638 + aiomaticRmLoading(btn);
639 + alert('Error in processing form editing: ' + error);
640 + }
641 + });
642 + }
643 + });
644 +
645 + var aiomatic_preview_form = $('.aiomatic_preview_form');
646 + aiomatic_preview_form.on('click', function (){
647 + if(!aiomaticAjaxRunning) {
648 + var btn = $(this);
649 + var id = btn.attr('data-id');
650 + $.ajax({
651 + url: aiomatic_object.ajax_url,
652 + data: {action: 'aiomatic_preview_form', id: id, nonce: aiomatic_object.nonce},
653 + type: 'POST',
654 + beforeSend: function () {
655 + aiomaticAjaxRunning = true;
656 + $('.aiomatic_modal_content').html('');
657 + aiomaticLoading(btn);
658 + },
659 + success: function (res) {
660 + aiomaticAjaxRunning = false;
661 + aiomaticRmLoading(btn);
662 + $('.aiomatic_modal_title').html('');
663 + $('.aiomatic_modal_content').html(res);
664 + $('.aiomatic-overlay').show();
665 + $('.aiomatic_modal').show();
666 + },
667 + error: function (r, s, error) {
668 + aiomaticAjaxRunning = false;
669 + aiomaticRmLoading(btn);
670 + alert('Error in processing form previewing: ' + error);
671 + }
672 + });
673 + }
674 + });
675 +
676 + var aiomatic_history_form = $('.aiomatic_history_form');
677 + aiomatic_history_form.on('click', function (){
678 + if(!aiomaticAjaxRunning) {
679 + var btn = $(this);
680 + var id = btn.attr('data-id');
681 + $.ajax({
682 + url: aiomatic_object.ajax_url,
683 + data: {action: 'aiomatic_history_form', id: id, nonce: aiomatic_object.nonce},
684 + type: 'POST',
685 + beforeSend: function () {
686 + aiomaticAjaxRunning = true;
687 + $('.aiomatic_modal_content').html('');
688 + aiomaticLoading(btn);
689 + },
690 + success: function (res) {
691 + aiomaticAjaxRunning = false;
692 + aiomaticRmLoading(btn);
693 + $('.aiomatic_modal_title').html('');
694 + $('.aiomatic_modal_content').html(res);
695 + $('.aiomatic-overlay').show();
696 + $('.aiomatic_modal').show();
697 + },
698 + error: function (r, s, error) {
699 + aiomaticAjaxRunning = false;
700 + aiomaticRmLoading(btn);
701 + alert('Error in processing history form previewing: ' + error);
702 + }
703 + });
704 + }
705 + });
706 +
707 + $('.aiomatic_modal_close').on('click', function (){
708 + $('.aiomatic_modal_close').closest('.aiomatic_modal').hide();
709 + $('.aiomatic_modal_close').closest('.aiomatic_modal').removeClass('aiomatic-small-modal');
710 + $('.aiomatic-overlay').hide();
711 + });
712 + var aiomatic_form_button = $('#aiomatic_form_button');
713 + aiomatic_form_button.on('click', function (){
714 + if(confirm('Are you sure you want to load forms from file?'))
715 + {
716 + var aiomatic_form_upload = $('#aiomatic_form_upload');
717 + if($("#aiomatic_overwrite").is(':checked'))
718 + {
719 + var overwrite = '1';
720 + }
721 + else
722 + {
723 + var overwrite = '0';
724 + }
725 + if(aiomatic_form_upload[0].files.length === 0){
726 + alert('Please select a file!');
727 + }
728 + else{
729 + var aiomatic_progress = $('.aiomatic_progress');
730 + var aiomatic_error_message = $('.aiomatic-error-msg');
731 + var aiomatic_upload_success = $('.aiomatic_upload_success');
732 + var aiomatic_max_file_size = aiomatic_object.maxfilesize;
733 + var aiomatic_max_size_in_mb = aiomatic_object.maxfilesize / (1024 ** 2);
734 + var aiomatic_form_file = aiomatic_form_upload[0].files[0];
735 + var aiomatic_form_file_extension = aiomatic_form_file.name.substr( (aiomatic_form_file.name.lastIndexOf('.') +1) );
736 + if(aiomatic_form_file_extension !== 'json'){
737 + aiomatic_form_upload.val('');
738 + alert('This feature only accepts JSON file type!');
739 + }
740 + else if(aiomatic_form_file.size > aiomatic_max_file_size){
741 + aiomatic_form_upload.val('');
742 + alert('Dataset allowed maximum size (MB): '+ aiomatic_max_size_in_mb)
743 + }
744 + else{
745 + var formData = new FormData();
746 + formData.append('action', 'aiomatic_form_upload');
747 + formData.append('nonce', aiomatic_object.nonce);
748 + formData.append('overwrite', overwrite);
749 + formData.append('file', aiomatic_form_file);
750 + $.ajax({
751 + url: aiomatic_object.ajax_url,
752 + type: 'POST',
753 + dataType: 'JSON',
754 + data: formData,
755 + beforeSend: function (){
756 + aiomatic_progress.find('span').css('width','0');
757 + aiomatic_progress.show();
758 + aiomaticLoading(aiomatic_form_button);
759 + aiomatic_error_message.hide();
760 + aiomatic_upload_success.hide();
761 + },
762 + xhr: function() {
763 + var xhr = $.ajaxSettings.xhr();
764 + xhr.upload.addEventListener("progress", function(evt) {
765 + if (evt.lengthComputable) {
766 + var percentComplete = evt.loaded / evt.total;
767 + aiomatic_progress.find('span').css('width',(Math.round(percentComplete * 100))+'%');
768 + }
769 + }, false);
770 + return xhr;
771 + },
772 + success: function(res) {
773 + if(res.status === 'success'){
774 + aiomaticRmLoading(aiomatic_form_button);
775 + aiomatic_progress.hide();
776 + aiomatic_form_upload.val('');
777 + aiomatic_upload_success.show();
778 + location.reload();
779 + }
780 + else{
781 + aiomaticRmLoading(aiomatic_form_button);
782 + aiomatic_progress.find('small').html('Error');
783 + aiomatic_progress.addClass('aiomatic_error');
784 + aiomatic_error_message.html(res.msg);
785 + aiomatic_error_message.show();
786 + }
787 + },
788 + cache: false,
789 + contentType: false,
790 + processData: false,
791 + error: function (r, s, error){
792 + aiomatic_form_upload.val('');
793 + aiomaticRmLoading(aiomatic_form_button);
794 + aiomatic_progress.addClass('aiomatic_error');
795 + aiomatic_progress.find('small').html('Error');
796 + alert('Error in processing forms uploading: ' + error);
797 + aiomatic_error_message.show();
798 + }
799 + });
800 + }
801 + }
802 + }
803 + });
804 + $('body').on("change", '#aiomatic-edit-type', function(e){
805 + var value = $(this).children(":selected").attr("value");
806 + if(value == 'text')
807 + {
808 + $('.aiomatic-hide-not-text').show();
809 + $('.aiomatic-hide-omniblock-form').hide();
810 + }
811 + else
812 + {
813 + if(value == 'omniblock-form')
814 + {
815 + $('.aiomatic-hide-not-text').hide();
816 + $('.aiomatic-hide-omniblock-form').show();
817 + }
818 + else
819 + {
820 + $('.aiomatic-hide-not-text').hide();
821 + $('.aiomatic-hide-omniblock-form').hide();
822 + }
823 + }
824 + });
825 +
826 + $('body').on("change", '#aiomatic-type', function(e){
827 + aiomatic_type_changed();
828 + });
829 +
830 + aiomatic_type_changed();
831 + function loadHistoryPage(formId, page) {
832 + $.ajax({
833 + url: aiomatic_history_ajax_object_forms.ajax_url,
834 + method: 'POST',
835 + data: {
836 + action: 'aiomatic_history_form',
837 + nonce: aiomatic_history_ajax_object_forms.nonce,
838 + id: formId,
839 + page: page,
840 + },
841 + beforeSend: function () {
842 + $('#aiomatic-history-container').html('<p>Loading...</p>');
843 + },
844 + success: function (response) {
845 + $('#aiomatic-history-container').html(response);
846 +
847 + // Update URL without page reload
848 + const newUrl = new URL(window.location.href);
849 + newUrl.searchParams.set('history_page', page);
850 + window.history.pushState({ formId, page }, '', newUrl);
851 + },
852 + error: function () {
853 + $('#aiomatic-history-container').html('<p>Error loading history. Please try again.</p>');
854 + },
855 + });
856 + }
857 +
858 + $(document).on('click', '.aiomatic-pagination-button', function () {
859 + const formId = $(this).data('form-id');
860 + const page = $(this).data('page');
861 + loadHistoryPage(formId, page);
862 + });
863 +
864 + window.onpopstate = function (event) {
865 + if (event.state) {
866 + loadHistoryPage(event.state.formId, event.state.page);
867 + }
868 + };
869 +
870 + const urlParams = new URLSearchParams(window.location.search);
871 + const initialPage = urlParams.get('history_page') || 1;
872 + const formId = $('#aiomatic-history-container').data('form-id');
873 + if (formId) {
874 + loadHistoryPage(formId, initialPage);
875 + }
876 + });