PluginProbe
MailerLite – WooCommerce integration / 2.0
MailerLite – WooCommerce integration v2.0
3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 All 147 releases
woo-mailerlite / public / js / admin.js

admin.js in MailerLite – WooCommerce integration 2.0, at public/js/admin.js

662 lines 24.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var syncResourcesRequest = null;
2 var groupSaved = false;
3 jQuery(document).ready(function (a) {
4 var c = a("#woocommerce_mailerlite_group");
5
6 if (0 !== c.length) {
7 var d = c.next(".select2-container");
8 a('<span id="woo-ml-refresh-groups" class="woo-ml-icon-refresh" data-woo-ml-refresh-groups="true"></span>').insertAfter(d);
9 }
10
11 var e = !1;
12
13 a(document).on("click", "[data-woo-ml-refresh-groups]", function (b) {
14 if (b.preventDefault(), !e) {
15 refreshGroups();
16 }
17 });
18
19 let i = 0, j = a("#woo-ml-sync-untracked-resources-progress-bar");
20
21 a(document).on("click", "[data-woo-ml-sync-untracked-resources]", function (c) {
22
23 c.preventDefault();
24
25 syncResources(this);
26 });
27
28 var syncResources = function (el) {
29
30 var d = a(el);
31
32 orders_tracked = d.data("woo-ml-untracked-resources-count");
33 all_untracked_orders_left = d.data("woo-ml-untracked-resources-left");
34
35 i = d.data("woo-ml-untracked-resources-cycle");
36
37 fail = a('#woo-ml-sync-untracked-resources-fail');
38 success = a("#woo-ml-sync-untracked-resources-success");
39
40 let r = 0;
41
42 console.log("inside the loop!");
43
44 a.ajax({
45 url: woo_ml_post.ajax_url,
46 type: "post",
47 beforeSend: function () {
48 d.prop("disabled", !0);
49 j.show();
50 r++;
51 },
52 data: {
53 action: "post_woo_ml_sync_untracked_resources"
54 },
55 async: 1,
56 success: function (data) {
57
58 var response = JSON.parse(data);
59
60 if (response.allDone) {
61
62 console.log("done!");
63 console.log("Response: True");
64 d.hide();
65 j.hide();
66 fail.hide();
67 success.show();
68 } else if (response.error) {
69
70 if (response.message) {
71 fail.html(response.message);
72 }
73
74 d.prop('disabled', 0);
75 j.hide();
76 fail.show();
77 } else {
78
79 if (response.completed) {
80 let untracked = parseInt(d.data("woo-ml-untracked-resources-left")) - parseInt(response.completed);
81
82 d.data("woo-ml-untracked-resources-left", untracked);
83 d.html('Synchronize ' + untracked.toString() + ' untracked resources');
84 }
85
86 syncResources(el);
87 }
88 }
89 });
90 };
91
92 a(document).on('click', '[data-woo-ml-reset-resources-sync]', function (event) {
93
94 event.preventDefault();
95
96 a(this).prop('disabled', true);
97 a(this).text("Please wait. Do not close this window until the reset finishes.");
98
99 resetResourcesSync();
100 });
101
102 var resetResourcesSync = function (cancelSync = false) {
103
104 let data = {};
105 data.action = 'post_woo_ml_reset_resources_sync';
106 if(cancelSync) {
107 data.cancelSync = true;
108 }
109 a.ajax({
110 url: woo_ml_post.ajax_url,
111 type: 'post',
112 data: data,
113 async: true,
114 success: function (responseStr) {
115
116 var response = responseStr;
117
118 if (typeof responseStr === "string") {
119 response = parseJSON(responseStr);
120 }
121
122 if (response.allDone) {
123 window.location.reload();
124 } else {
125
126 resetResourcesSync();
127 }
128 }
129 });
130 }
131
132 var field = a("#woocommerce_mailerlite_api_key");
133
134 a('<button id="woo-ml-validate-key" class="button-primary">Validate Key</button>').insertAfter(field);
135
136 a(document).on("click", "#woo-ml-validate-key", function (b) {
137 if (b.preventDefault(), !e) {
138 var key = a("#woocommerce_mailerlite_api_key").val();
139 a.ajax({
140 url: woo_ml_post.ajax_url,
141 type: "post",
142 data: {
143 action: "post_woo_ml_validate_key",
144 key: key
145 },
146 success: function(a) {
147 location.reload()
148 }
149 })
150 }
151 });
152
153 if (a('#woocommerce_mailerlite_group').length > 0) {
154 a('#woocommerce_mailerlite_group').select2();
155 }
156
157 if (a('#woocommerce_mailerlite_ignore_product_list').length > 0) {
158 a('#woocommerce_mailerlite_ignore_product_list').select2();
159 }
160
161 var cs_field = a('#woocommerce_mailerlite_consumer_secret');
162
163 if (0 !== cs_field.length) {
164 var field_desc = cs_field.next(".description");
165 field_desc.closest('tr').after(
166 '<h2>Integration Details</h2>\
167 <p class="section-description">Customize MailerLite integration for WooCommerce</p>');
168 }
169
170 var ml_platform = '';
171
172 if (a('#ml_platform').length > 0) {
173 ml_platform = a('#ml_platform').val();
174 }
175
176 var tracking_field = a('#woocommerce_mailerlite_popups');
177
178 tracking_field.closest('tr').before(
179 '<h2>Popups</h2>\
180 <p class="section-description">Display pop-up subscribe forms created within MailerLite</p>');
181
182 var button = a('[name="save"]');
183
184 if (field.length !== 0 && (cs_field.length === 0 && ml_platform !== '2')) {
185 button.hide();
186 } else {
187 button.show();
188 }
189
190 var ignored_p_field = a('#woocommerce_mailerlite_ignore_product_list');
191
192 if (ignored_p_field.length !== 0) {
193 ignored_p_field.closest('tr').before(
194 '<h2>E-commerce Automations</h2>\
195 <p class="section-description">Customize settings for your e-commerce automations created in MailerLite </p>'
196 )
197 }
198
199 var auto_update_field = a('#woocommerce_mailerlite_auto_update_plugin');
200
201 if (auto_update_field.length !== 0) {
202 auto_update_field.closest('tr').before(
203 '<h2>Plugin Updates</h2>\
204 <p class="section-description">Customize settings for MailerLite plugin </p>'
205 )
206 }
207
208 var refreshGroups = function () {
209 var c = a(this);
210 c.removeClass("error"), c.addClass("running");
211 a.ajax({
212 url: woo_ml_post.ajax_url,
213 type: "post",
214 dataType: 'JSON',
215 data: {
216 action: "post_woo_ml_refresh_groups"
217 },
218 success: function (res) {
219 c.removeClass("running");
220
221 let has_group = false;
222
223 if (res.groups) {
224 a('#woocommerce_mailerlite_group').empty();
225
226 for (const [id, name] of Object.entries(res.groups)) {
227
228 if (res.current && parseInt(res.current) === parseInt(id)) {
229 has_group = true;
230 }
231
232 a('#woocommerce_mailerlite_group, #wooMlSubGroup').append(a('<option>', {
233 value: id,
234 text: name
235 }));
236 }
237 }
238
239 if (res.current && has_group) {
240 a('#woocommerce_mailerlite_group').val(res.current);
241 }
242 },
243 error: function (x, status) {
244 c.addClass("error");
245 }
246 });
247 }
248
249 var parseJSON = function (jsonStr) {
250 try {
251 var parsed = JSON.parse(jsonStr);
252
253 if (parsed && typeof parsed === "object") {
254 return parsed;
255 }
256 } catch (e) {
257 }
258
259 return false;
260 }
261
262 if (a('#woocommerce_mailerlite_api_key').length > 0) {
263 refreshGroups();
264 }
265
266 (function ($) {
267 var selectedGroup = null;
268
269 if($('#selectedGroupValue').val()) {
270 selectedGroup = $('#selectedGroupValue').val();
271 }
272 var errors = document.getElementsByClassName('woo-ml-wizard-error');
273 groupSelectInput();
274 $('.woo-ml-close, .close').on('click', function () {
275 $('.woo-ml-wizard-modal').each(function(index, currentElement) {
276 if(currentElement.style.display == 'block') {
277 currentElement.style.display = 'none';
278 }
279 });
280 });
281
282 $('#wooMlWizardApiKeyBtn').on('click', function (e) {
283
284 let apiKey = $('#wooMlApiKey').val();
285 if (apiKey.length == 0) {
286 if(errors.length == 0 ) {
287 $('.api-key-input').after('<span id="" class="woo-ml-wizard-error">Enter API Key</span>');
288 }
289 } else {
290 $('.woo-ml-wizard-error').remove();
291 buttonLoadingState($(this));
292 $.ajax({
293 url: woo_ml_post.ajax_url,
294 type: "post",
295 data: {
296 action: "post_woo_ml_validate_key",
297 key: apiKey
298 },
299 success: function (a) {
300 buttonLoadingState($('#wooMlWizardApiKeyBtn'), true);
301 window.onbeforeunload = null;
302 window.location.reload();
303 },
304 error: function (b) {
305 buttonLoadingState($('#wooMlWizardApiKeyBtn'), true);
306 console.log(b);
307 },
308 });
309
310 }
311 });
312 $('#createGroupModal').on('click', function () {
313 document.getElementById('wooMlWizardCreateGroupModal').style.display = "block";
314 });
315
316 $('#wooMlResetIntegration').on('click', function () {
317 document.getElementById('wooMlResetIntegrationModal').style.display = "block";
318 });
319
320 $('#resetIntegration').on('submit', function() {
321 buttonLoadingState($('#resetIntegrationBtn'));
322 });
323
324 $('#createGroup').on('click', function () {
325 $('.woo-ml-wizard-error').remove();
326 if(!$('#wooMlCreateGroup').val()) {
327 $('#wooMlCreateGroup').after(`<span id="" class="woo-ml-wizard-error">Enter group name</span>`);
328 return false;
329 }
330 buttonLoadingState($(this));
331 let modal = document.getElementById('wooMlWizardCreateGroupModal');
332 modal.style.display = "block";
333 $.ajax({
334 url: woo_ml_post.ajax_url,
335 type: "post",
336 data: {
337 action: "woo_ml_create_group",
338 createGroup: $('#wooMlCreateGroup').val()
339 },
340 success: function (res) {
341 if(res.data?.id) {
342 $('#wooMlSubGroup').append($('<option>', {
343 value: res.data.id,
344 text: res.data.name
345 }));
346 removeExistingErrors();
347 $('.woo-ml-header').after(`
348 <div class="woo-ml-alert-success">
349 <span class="woo-ml-closebtn-success"
350 onClick="this.parentElement.style.display='none';">&times;</span>
351 Group created successfully
352 </div>
353 `);
354 groupSelectInput(res.data.id);
355 } else if(res?.id) {
356 $('#wooMlSubGroup').append($('<option>', {
357 value: res.id,
358 text: res.name
359 }));
360 removeExistingErrors();
361 $('.woo-ml-header').after(`
362 <div class="woo-ml-alert-success">
363 <span class="woo-ml-closebtn-success"
364 onClick="this.parentElement.style.display='none';">&times;</span>
365 Group created successfully
366 </div>
367 `);
368 groupSelectInput(res.id);
369 }
370 if(res.error) {
371 if(errors.length > 0 ) {
372 $('.woo-ml-wizard-error').remove();
373 }
374 $('#wooMlCreateGroup').after(`<span id="" class="woo-ml-wizard-error">${res.error}</span>`);
375 } else {
376 modal.style.display = "none";
377 }
378 buttonLoadingState($('#createGroup'), true);
379 },
380 error: function (x, status) {
381 buttonLoadingState($('#createGroup'), true);
382 console.log(x,status);
383 }
384 });
385 });
386 $('#startImport').on('click', function () {
387 if(errors.length > 0 ) {
388 $('.woo-ml-wizard-error').remove();
389 }
390 if(!$('#wooMlSubGroup').val()) {
391 $('.select2-container--mailerlite').parent().after(`<span id="" class="woo-ml-wizard-error">Select group</span>`);
392 return false;
393 } else if($('#consumerKey').length && !$('#consumerKey').val()) {
394 $('#consumerKey').after(`<span id="" class="woo-ml-wizard-error">Enter consumer key</span>`);
395 return false;
396 } else if($('#consumerSecret').length && !$('#consumerSecret').val()) {
397 $('#consumerSecret').after(`<span id="" class="woo-ml-wizard-error">Enter consumer secret</span>`);
398 return false;
399 }
400 importResources();
401 });
402
403 $("input[name='disable_checkout_sync']").change(function() {
404 if(this.checked) {
405 $(this).parent().nextAll('.woo-ml-alert-warning-small').show();
406 } else {
407 $(this).parent().nextAll('.woo-ml-alert-warning-small').hide();
408 }
409 });
410
411 $("input[name='checkout']").change(function() {
412 let block = $(this).closest('.settings-block');
413 let inputs = block.find('input, select');
414
415 if(this.checked) {
416 $('.checkout-settings-group').show();
417 inputs.map(function(key, elem){
418 elem.disabled = false;
419 });
420 } else {
421 inputs.map(function(key, elem){
422 $('.checkout-settings-group').hide();
423 if(elem.name !== 'checkout') {
424 elem.disabled = true;
425 }
426 });
427 }
428
429 });
430
431 $('#woo_ml_wizard_back_step_1').on('click', function(e) {
432 window.location.href = window.location.href + '&step=1';
433 });
434
435 $('#updateSettingsForm').on('submit', function (e) {
436 if(!$('#wooMlSubGroup').val()) {
437 e.preventDefault();
438 if(errors.length > 0 ) {
439 $('.woo-ml-wizard-error').remove();
440 }
441 $('.select2-container--mailerlite').parent().after(`<span id="" class="woo-ml-wizard-error mt-0">Select group</span>`);
442
443 $([document.documentElement, document.body]).animate({
444 scrollTop: $(".select2-container--mailerlite").parent().offset().top
445 }, 1000);
446 return false;
447 }
448 buttonLoadingState($("#updateSettingsBtn"));
449 $(this).unbind('submit').submit();
450
451 });
452 const importResources = function(completed = 0) {
453 let modal = document.getElementById('wooMlSyncModal');
454 modal.style.display = "block";
455 removeExistingErrors();
456 if($('#consumerKey').length) {
457 let error = false;
458 $('.woo-ml-wizard-error').remove();
459 if(!$('#consumerKey').val().startsWith('ck_')) {
460 error = true;
461 $('#consumerKey').after(`<span id="" class="woo-ml-wizard-error">Invalid consumer key</span>`);
462 }
463 if(!$('#consumerSecret').val().startsWith('cs_')) {
464 error = true;
465 $('#consumerSecret').after(`<span id="" class="woo-ml-wizard-error">Invalid consumer secret</span>`);
466 }
467 if(error) {
468 modal.style.display = "none";
469 return false;
470 }
471 }
472 if(((selectedGroup !== $('#wooMlSubGroup').val()) || $('#consumerKey').length) && !groupSaved) {
473 groupSaved = true;
474 let data = {};
475 if($('#consumerKey').length) {
476 data.consumer_key = $('#consumerKey').val();
477 data.consumer_secret = $('#consumerSecret').val();
478 }
479
480 data.action = "woo_ml_save_group";
481 data.section = "mailerlite";
482 data.group = jQuery('#wooMlSubGroup').val();
483 syncResourcesRequest = $.ajax({
484 url: woo_ml_post.ajax_url,
485 type: "post",
486 data: data,
487 success: function (res) {
488 if(res.error) {
489 $('.woo-ml-header').after(`
490 <div class="woo-ml-alert">
491 <span class="woo-ml-closebtn"
492 onClick="this.parentElement.style.display='none';">&times;</span>
493 ${res.message ? res.message : "Something went wrong!"}
494 </div>
495 `);
496 modal.style.display = 'none';
497 return false;
498 }
499 selectedGroup = jQuery('#wooMlSubGroup').val();
500 $.ajax({
501 url: woo_ml_post.ajax_url,
502 type: "post",
503 xhr: function(){ return progressBar(completed)},
504 data: {
505 action: "post_woo_ml_sync_untracked_resources"
506 },
507 success: function (res) {
508 let response = JSON.parse(res);
509
510 if (response.allDone) {
511 console.log("done!");
512 modal.style.display = "none";
513 window.onbeforeunload = null;
514 location.reload();
515 } else if (response.error) {
516 if (response.message) {
517 console.log(response.message);
518 }
519 removeExistingErrors();
520 $('.woo-ml-header').after(`
521 <div class="woo-ml-alert">
522 <span class="woo-ml-closebtn"
523 onClick="this.parentElement.style.display='none';">&times;</span>
524 Something went wrong, please retry.
525 </div>
526 `);
527 modal.style.display = "none";
528 } else {
529 importResources((response.completed ? response.completed : 0) + completed);
530 }
531 },
532 error: function (x) {
533 modal.style.display = "none";
534 console.log(x);
535 }
536 });
537 },
538 error: function (x, status) {
539 console.log(x,status);
540 }
541 });
542 } else {
543 syncResourcesRequest = $.ajax({
544 url: woo_ml_post.ajax_url,
545 type: "post",
546 xhr: function(){ return progressBar(completed)},
547 data: {
548 action : "post_woo_ml_sync_untracked_resources"
549 },
550 success: function (res) {
551 let response = JSON.parse(res);
552 if (response.allDone) {
553 console.log("done!");
554 modal.style.display = "none";
555 window.onbeforeunload = null;
556 location.reload();
557 } else if (response.error) {
558 if (response.message) {
559 console.log(response.message);
560 }
561 removeExistingErrors();
562 $('.woo-ml-header').after(`
563 <div class="woo-ml-alert">
564 <span class="woo-ml-closebtn"
565 onClick="this.parentElement.style.display='none';">&times;</span>
566 Something went wrong, please retry.
567 </div>
568 `);
569 modal.style.display = "none";
570 } else {
571 importResources((response.completed ? response.completed : 0) + completed);
572 }
573 },
574 error: function (x) {
575 modal.style.display = "none";
576 console.log(x);
577 }
578 });
579 }
580
581 };
582
583 $('#confirmCancelSync').on('click', function () {
584 buttonLoadingState($(this));
585 syncResourcesRequest?.abort();
586
587 if($('#woo_ml_wizard_step').length && ($('#woo_ml_wizard_step').val() == 2)) {
588 window.location.reload();
589 return;
590 }
591 resetResourcesSync(true);
592 });
593
594 function progressBar(completed = 0)
595 {
596 let xhr = new window.XMLHttpRequest();
597 xhr.upload.addEventListener("progress", function (evt) {
598 let percentage = ((100 * completed) / $('#totalUntrackedResources').val());
599 if(percentage <= 100) {
600 $('#wooMlWizardProgress').css({
601 width: percentage + '%'
602 });
603 $('#progressPercentage').text(Math.round(percentage) + '%');
604 }
605 }, false);
606 return xhr;
607 }
608
609
610
611 function groupSelectInput(selected = false)
612 {
613 $('#wooMlSubGroup').select2({theme: 'mailerlite'});
614 if(selected) {
615 $('#wooMlSubGroup').val(selected).trigger('change');
616 }
617
618 }
619
620 $('#woo-ml-reset-integration').on('click', function () {
621 $.ajax({
622 url: woo_ml_post.ajax_url,
623 type: "post",
624 data: {
625 action: "woo_ml_reset_integration"
626 },
627 success: function (res) {
628 window.onbeforeunload = null;
629 location.reload();
630 },
631 error: function (x) {
632 console.log(x);
633 }
634 });
635 });
636
637 const removeExistingErrors = function () {
638 if (jQuery('.woo-ml-header').next().hasClass('woo-ml-alert')) {
639 jQuery('.woo-ml-header').next().remove();
640 }
641 if (jQuery('.woo-ml-header').next().hasClass('woo-ml-alert-success')) {
642 jQuery('.woo-ml-header').next().remove();
643 }
644 };
645
646 function buttonLoadingState(elem, release = false)
647 {
648 elem.toggleClass('woo-ml-button-loading');
649 if(release)
650 {
651 elem.attr('disabled', false);
652 } else {
653 elem.attr('disabled', true);
654 }
655
656 }
657
658 })(jQuery);
659
660 });
661
662