PluginProbe
PostNL for WooCommerce / 3.1.4
PostNL for WooCommerce v3.1.4
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / assets / js / wcmp-admin.js

wcmp-admin.js in PostNL for WooCommerce 3.1.4, at assets/js/wcmp-admin.js

564 lines 21.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(function($) {
2 /* enable color picker */
3 $('.wcmp-color-picker').wpColorPicker();
4
5 /* move shipment options to 'Ship to' column */
6 $('.wp-list-table .wcmp_shipment_options, .wp-list-table .wcmp_shipment_summary').each(function(index) {
7 var $ship_to_column = $(this).closest('tr').find('td.shipping_address');
8 $(this).appendTo($ship_to_column);
9 /* hidden by default - make visible */
10 $(this).show();
11 });
12
13 /* disable ALL shipment options form fields to avoid conflicts with order search field */
14 $('.wp-list-table .wcmp_shipment_options_form :input').prop('disabled', true);
15
16 /* show and enable options when clicked */
17 $('.wcmp_show_shipment_options').click(function(event) {
18 event.preventDefault();
19 $form = $(this).next('.wcmp_shipment_options_form');
20 if ($form.is(':visible')) {
21 /* disable all input fields again */
22 $form.find(':input').prop('disabled', true);
23 /* hide form */
24 $form.slideUp();
25 } else {
26 /* enable all fields on this form */
27 $form.find(':input').prop('disabled', false);
28 /* set init states according to change events */
29 $form.find(':input').change();
30 /* show form */
31 $form.slideDown();
32 }
33 });
34 /* hide options form when click outside */
35 $(document).click(function(event) {
36 if (!$(event.target).closest('.wcmp_shipment_options_form').length) {
37 if (!($(event.target).hasClass('wcmp_show_shipment_options') || $(event.target).parent().hasClass('wcmp_show_shipment_options')) && $('.wcmp_shipment_options_form').is(":visible")) {
38 /* disable all input fields again */
39 $('.wcmp_shipment_options_form :input').prop('disabled', true);
40 /* hide form */
41 $('.wcmp_shipment_options_form').slideUp();
42 }
43 }
44 });
45
46 /* show summary when clicked */
47 $('.wcmp_show_shipment_summary').click(function(event) {
48 $summary_list = $(this).next('.wcmp_shipment_summary_list');
49 if ($summary_list.is(":visible") || $summary_list.data('loaded') != '') {
50 /* just open / close */
51 $summary_list.slideToggle();
52 } else if ($summary_list.is(":hidden") && $summary_list.data('loaded') == '') {
53 $summary_list.addClass('ajax-waiting');
54 $summary_list.find('.wcmp_spinner').show();
55 $summary_list.slideToggle();
56 var data = {
57 security: wc_postnl.nonce,
58 action: "wcmp_get_shipment_summary_status",
59 order_id: $summary_list.data('order_id'),
60 shipment_id: $summary_list.data('shipment_id'),
61 };
62 xhr = $.ajax({
63 type: 'POST',
64 url: wc_postnl.ajax_url,
65 data: data,
66 context: $summary_list,
67 success: function(response) {
68 this.removeClass('ajax-waiting');
69 this.html(response);
70 this.data('loaded', 'yes');
71 }
72 });
73
74 }
75
76 });
77 /* hide summary when click outside */
78 $(document).click(function(event) {
79 if (!$(event.target).closest('.wcmp_shipment_summary_list').length) {
80 if (!($(event.target).hasClass('wcmp_show_shipment_summary') || $(event.target).parent().hasClass('wcmp_shipment_summary')) && $('.wcmp_shipment_summary_list').is(":visible")) {
81 $('.wcmp_shipment_summary_list').slideUp();
82 }
83 }
84 });
85
86 /* hide automatic order status if automation not enabled */
87 $('.wcmp_shipment_options input#order_status_automation').change(function() {
88 var order_status_select = $('.wcmp_shipment_options select.automatic_order_status');
89 if (this.checked) {
90 $(order_status_select).prop('disabled', false);
91 $('.wcmp_shipment_options tr.automatic_order_status').show();
92 } else {
93 $(order_status_select).prop('disabled', true);
94 $('.wcmp_shipment_options tr.automatic_order_status').hide();
95 }
96 });
97
98 /* hide automatic barcode in note title if barcode in note is not enabled */
99 $('.wcmp_shipment_options input#barcode_in_note').change(function() {
100 var barcode_in_note_select = $('.wcmp_shipment_options select.barcode_in_note_title');
101 if (this.checked) {
102 $(barcode_in_note_select).prop('disabled', false);
103 $('.wcmp_shipment_options tr.barcode_in_note_title').show();
104 } else {
105 $(barcode_in_note_select).prop('disabled', true);
106 $('.wcmp_shipment_options tr.barcode_in_note_title').hide();
107 }
108 });
109
110 /* select > 500 if insured amount input is >499 */
111 $('.wcmp_shipment_options input.insured_amount').each(function(index) {
112 if ($(this).val() > 499) {
113 var insured_select = $(this).closest('table').parent().find('select.insured_amount');
114 $(insured_select).val('');
115 }
116 });
117
118 /* hide insurance options if insured not checked */
119 $('.wcmp_shipment_options input.insured').change(function() {
120 var insured_select = $(this).closest('table').parent().find('select.insured_amount');
121 var insured_input = $(this).closest('table').parent().find('input.insured_amount');
122 if (this.checked) {
123 $(insured_select).prop('disabled', false);
124 $(insured_select).closest('tr').show();
125 $('select.insured_amount').change();
126 } else {
127 $(insured_select).prop('disabled', true);
128 $(insured_select).closest('tr').hide();
129 $(insured_input).closest('tr').hide();
130 }
131 });
132
133 /* hide & disable insured amount input if not needed */
134 $('.wcmp_shipment_options select.insured_amount').change(function() {
135 var insured_check = $(this).closest('table').parent().find('input.insured');
136 var insured_select = $(this).closest('table').parent().find('select.insured_amount');
137 var insured_input = $(this).closest('table').find('input.insured_amount');
138 if ($(insured_select).val()) {
139 $(insured_input).val('');
140 $(insured_input).prop('disabled', true);
141 $(insured_input).closest('tr').hide();
142 } else {
143 $(insured_input).prop('disabled', false);
144 $(insured_input).closest('tr').show();
145 }
146 });
147
148 /* hide all options if not a parcel */
149 $('.wcmp_shipment_options select.package_type').change(function() {
150 var $package_type = $(this).val();
151 var parcel_options = $('.wcpostnl_settings_table.parcel_options');
152
153 enable_options = function(div) {
154 $(div).find('input, textarea, button, select').prop('disabled', false);
155 $(div).show();
156 };
157
158 disable_options = function(div) {
159 $(div).find('input, textarea, button, select').prop('disabled', true);
160 $(div).hide();
161 };
162
163 if ($package_type == '1') {
164 enable_options(parcel_options);
165 } else {
166 disable_options(parcel_options);
167 $(parcel_options).find('.insured').prop('checked', false).change();
168 }
169
170 });
171
172 /* hide delivery options details if disabled */
173 $('input.wcmp_delivery_option').change(function() {
174 if ($(this).is(':checked')) {
175 $(this).parent().find('.wcmp_delivery_option_details').show();
176 } else {
177 $(this).parent().find('.wcmp_delivery_option_details').hide();
178 }
179 });
180
181 /* check witch radio button of A4 or A6 is activated and disable/enable print position */
182 $('input[id^=\'label_format\']').change(function() {
183
184 /* when the options are not selected */
185 if (!$(this).is(':checked')) {
186 return;
187 }
188
189 var parent_offset = $("#print_position_offset").parent().parent();
190
191 if ($(this).attr("value") == "A4") {
192 parent_offset.show();
193 return;
194 }
195
196 /* Always A6 */
197 parent_offset.hide();
198 $("#print_position_offset").prop("checked", false);
199 });
200
201 /* Hide all checkout options if disabled */
202 $('#woocommerce-postnl-settings #postnl_checkout').change(function() {
203 $next_settings_rows = $(this).closest('tr').nextAll('tr');
204 $next_settings_headers = $(this).closest('table').nextAll('h2');
205 $next_settings_forms = $(this).closest('table').nextAll('table');
206 if ($(this).is(':checked')) {
207 $next_settings_rows.show();
208 $next_settings_forms.show();
209 $next_settings_headers.show();
210 } else {
211 $next_settings_rows.hide();
212 $next_settings_forms.hide();
213 $next_settings_headers.hide();
214 }
215 });
216
217 /* hide automatic pickup express if pickup is not enabled */
218 $('.wcmp_shipment_options input#pickup_enabled').change(function() {
219 var pickup_express = $('.wcmp_shipment_options select.pickup_express_enabled');
220 if (this.checked) {
221 $(pickup_express).prop('disabled', false);
222 $('.wcmp_shipment_options tr.pickup_express').show();
223 } else {
224 $(pickup_express).prop('disabled', true);
225 $('.wcmp_shipment_options tr.pickup_express').hide();
226 }
227 });
228
229 /* init options on settings page and in bulk form */
230 $('#woocommerce-postnl-settings :input, .wcmp_bulk_options_form :input').change();
231
232 /* postnl_checkout */
233
234 /* saving shipment options via AJAX */
235 $('.wcmp_save_shipment_settings')
236 .on('click', 'a.button.save', function() {
237 var order_id = $(this).data().order;
238 var $form = $(this).closest('.wcmp_shipment_options').find('.wcmp_shipment_options_form');
239 var package_type = $form.find('select.package_type option:selected').text();
240 var $package_type_text_element = $(this).closest('.wcmp_shipment_options').find('.wcpm_package_type');
241
242 /* show spinner */
243 $form.find('.wcmp_save_shipment_settings .waiting').show();
244
245 var form_data = $form.find(":input").serialize();
246 var data = {
247 action: 'wcmp_save_shipment_options',
248 order_id: order_id,
249 form_data: form_data,
250 security: wc_postnl.nonce,
251 };
252
253 $.post(wc_postnl.ajax_url, data, function(response) {
254 /* console.log(response); */
255
256 /* set main text to selection */
257 $package_type_text_element.text(package_type);
258
259 /* hide spinner */
260 $form.find('.wcmp_save_shipment_settings .waiting').hide();
261
262 /* disable all input fields again */
263 $form.find(':input').prop('disabled', true);
264
265 /* hide the form */
266 $form.slideUp();
267 });
268 });
269
270 /* Print queued labels */
271 var print_queue = $("#wcmp_printqueue").val();
272 var print_queue_offset = $("#wcmp_printqueue_offset").val();
273 if (typeof print_queue !== 'undefined') {
274 if (typeof print_queue_offset === 'undefined') {
275 print_queue_offset = 0;
276 }
277 postnl_print($.parseJSON(print_queue), print_queue_offset);
278 }
279
280 /* Bulk actions */
281 $("#doaction, #doaction2").click(function(event) {
282 var actionselected = $(this).attr("id").substr(2);
283 /* check if action starts with 'wcmp_' */
284 if ($('select[name="' + actionselected + '"]').val().substring(0, 5) == "wcmp_") {
285 event.preventDefault();
286 /* remove notices */
287 $('.postnl_notice').remove();
288
289 /* strip 'wcmp_' from action */
290 var action = $('select[name="' + actionselected + '"]').val().substring(5);
291
292 /* Get array of checked orders (order_ids) */
293 var order_ids = [];
294 $('tbody th.check-column input[type="checkbox"]:checked').each(
295 function() {
296 order_ids.push($(this).val());
297 }
298 );
299
300 /* execute action */
301 switch (action) {
302 case 'export':
303 bulk_spinner(this, 'show');
304 postnl_export(order_ids);
305 break;
306 case 'print':
307 bulk_spinner(this, 'show');
308 var offset = wc_postnl.offset == 1 ? $('.wc_postnl_offset').val() : 0;
309 postnl_print(order_ids, offset);
310 break;
311 case 'export_print':
312 bulk_spinner(this, 'show');
313 postnl_export(order_ids, 'after_reload'); /* 'yes' initializes print mode and disables refresh */
314 break;
315 }
316
317 return;
318 }
319 });
320
321 /* Single actions click. The .wc_actions .single_wc_actions for support wc > 3.3.0 */
322 $(".order_actions, .single_order_actions, .wc_actions, .single_wc_actions")
323 .on('click', 'a.button.postnl', function(event) {
324 event.preventDefault();
325 var button_action = $(this).data('request');
326 var order_ids = [$(this).data('order-id')];
327
328 /* execute action */
329 switch (button_action) {
330 case 'add_shipment':
331 var button = this;
332 button_spinner(button, 'show');
333 postnl_export(order_ids);
334 break;
335 case 'get_labels':
336 if (wc_postnl.offset == 1) {
337 contextual_offset_dialog(order_ids, event);
338 } else {
339 postnl_print(order_ids);
340 }
341 break;
342 case 'add_return':
343 postnl_modal_dialog(order_ids, 'return');
344 break;
345 }
346 });
347
348 $(window).bind('tb_unload', function() {
349 /* re-enable scrolling after closing thickbox */
350 $("body").css({overflow: 'inherit'})
351 });
352
353 /* Add offset dialog when address labels option is selected */
354 $("select[name='action'], select[name='action2']").change(function() {
355 var actionselected = $(this).val();
356 /* alert(actionselected); */
357 if ((actionselected == 'wcmp_print' || actionselected == 'wcmp_export_print') && wc_postnl.offset == 1) {
358 var insert_position = $(this).attr("name") == 'action' ? 'top' : 'bottom';
359 $('#wcpostnl_offset_dialog')
360 .attr('style', 'clear:both') /* reset styles */
361 .insertAfter('div.tablenav.' + insert_position)
362 .show();
363
364 /* make sure button is not shown */
365 $('#wcpostnl_offset_dialog').find('button').hide();
366 /* clear input */
367 $('#wcpostnl_offset_dialog').find('input').val('');
368 } else {
369 $('#wcpostnl_offset_dialog')
370 .appendTo('body')
371 .hide();
372 }
373 });
374
375 /* Click offset dialog button (single export) */
376 $("#wcpostnl_offset_dialog button").click(function(event) {
377 $dialog = $(this).parent();
378
379 /* set print variables */
380 var order_ids = [$dialog.find('input.order_id').val()];
381 var offset = $dialog.find('input.wc_postnl_offset').val();
382
383 /* hide dialog */
384 $dialog.hide();
385
386 /* print labels */
387 postnl_print(order_ids, offset);
388 });
389
390 function contextual_offset_dialog(order_ids, event) {
391 /* place offset dialog at mouse tip */
392 $('#wcpostnl_offset_dialog')
393 .show()
394 .appendTo('body')
395 .css({
396 position: "absolute",
397 "background-color": "white",
398 padding: "6px",
399 width: "100px",
400 border: "1px solid #ccc",
401 top: event.pageY,
402 left: event.pageX,
403 "margin-left": "-100px",
404 });
405
406 $('#wcpostnl_offset_dialog').find('button')
407 .show()
408 .data('order_id', order_ids);
409
410 /* clear input */
411 $('#wcpostnl_offset_dialog').find('input').val('');
412
413 $('#wcpostnl_offset_dialog').append('<input type=hidden class="order_id"/>');
414 $('#wcpostnl_offset_dialog input.order_id').val(order_ids);
415 }
416
417 function button_spinner(button, display) {
418 if (display === 'show') {
419 $button_img = $(button).find('.wcmp_button_img');
420 $button_img.hide();
421 /* console.log($( button ).parent().find('.wcmp_spinner')); */
422 $(button).parent().find('.wcmp_spinner')
423 .insertAfter($button_img)
424 .show();
425 } else {
426 $(button).parent().find('.wcmp_spinner').hide();
427 $(button).find('.wcmp_button_img').show();
428 }
429 }
430
431 function bulk_spinner(action, display) {
432 if (display === 'show') {
433 $submit_button = $(action).parent().find('.button.action');
434 $('.wcmp_bulk_spinner').insertAfter($submit_button).show();
435 } else {
436 $('.wcmp_bulk_spinner').hide();
437 }
438 }
439
440 /* export orders to PostNL via AJAX */
441 function postnl_export(order_ids, print) {
442 if (typeof print === 'undefined') {
443 print = 'no';
444 }
445 var offset = wc_postnl.offset === 1 ? $('.wc_postnl_offset').val() : 0;
446 var data = {
447 action: 'wc_postnl',
448 request: 'add_shipments',
449 order_ids: order_ids,
450 offset: offset,
451 print: print,
452 security: wc_postnl.nonce,
453 };
454
455 $.post(wc_postnl.ajax_url, data, function(response) {
456 response = $.parseJSON(response);
457
458 if (print === 'no' || print === 'after_reload') {
459 /* refresh page, admin notices are stored in options and will be displayed automatically */
460 redirect_url = updateUrlParameter(window.location.href, 'postnl_done', 'true');
461 window.location.href = redirect_url;
462 return;
463 } else {
464 /* when printing, output notices directly so that we can init print in the same run */
465 if (response !== null && typeof response === 'object' && 'error' in response) {
466 postnl_admin_notice(response.error, 'error');
467 }
468
469 if (response !== null && typeof response === 'object' && 'success' in response) {
470 postnl_admin_notice(response.success, 'success');
471 }
472
473 /* load PDF */
474 postnl_print(order_ids, offset);
475 }
476
477 return;
478 });
479
480 }
481
482 function postnl_modal_dialog(order_ids, dialog) {
483 var request_prefix = (wc_postnl.ajax_url.indexOf("?") !== -1) ? '&' : '?';
484 var thickbox_height = $(window).height() - 120;
485 var thickbox_parameters = '&TB_iframe=true&height=' + thickbox_height + '&width=720';
486 var url = wc_postnl.ajax_url + request_prefix + 'order_ids=' + order_ids + '&action=wc_postnl&request=modal_dialog&dialog=' + dialog + '&security=' + wc_postnl.nonce + thickbox_parameters;
487
488 /* disable background scrolling */
489 $("body").css({overflow: 'hidden'});
490
491 tb_show('', url);
492 }
493
494 /* export orders to PostNL via AJAX */
495 function postnl_return(order_ids) {
496 var data = {
497 action: 'wc_postnl',
498 request: 'add_return',
499 order_ids: order_ids,
500 security: wc_postnl.nonce,
501 };
502
503 $.post(wc_postnl.ajax_url, data, function(response) {
504 response = $.parseJSON(response);
505 if (response !== null && typeof response === 'object' && 'error' in response) {
506 postnl_admin_notice(response.error, 'error');
507 }
508 return;
509 });
510
511 }
512
513 /* Request PostNL labels */
514 function postnl_print(order_ids, offset) {
515 if (typeof offset === 'undefined') {
516 offset = 0;
517 }
518
519 var request_prefix = (wc_postnl.ajax_url.indexOf("?") !== -1) ? '&' : '?';
520 var url = wc_postnl.ajax_url + request_prefix + 'action=wc_postnl&request=get_labels&security=' + wc_postnl.nonce;
521
522 /* create form to send order_ids via POST */
523 $('body').append('<form action="' + url + '" method="post" target="_blank" id="postnl_post_data"></form>');
524 $('#postnl_post_data').append('<input type="hidden" name="offset" class="offset"/>');
525 $('#postnl_post_data input.offset').val(offset);
526 $('#postnl_post_data').append('<input type="hidden" name="order_ids" class="order_ids"/>');
527 $('#postnl_post_data input.order_ids').val(JSON.stringify(order_ids));
528
529 /* submit data to open or download pdf */
530 $('#postnl_post_data').submit();
531
532 bulk_spinner('', 'hide');
533 }
534
535 function postnl_admin_notice(message, type) {
536 $main_header = $('#wpbody-content > .wrap > h1:first');
537 var notice = '<div class="postnl_notice notice notice-' + type + '"><p>' + message + '</p></div>';
538 $main_header.after(notice);
539 $('html, body').animate({scrollTop: 0}, 'slow');
540 }
541
542 /* Add / Update a key-value pair in the URL query parameters */
543
544 /* https://gist.github.com/niyazpk/f8ac616f181f6042d1e0 */
545 function updateUrlParameter(uri, key, value) {
546 /* remove the hash part before operating on the uri */
547 var i = uri.indexOf('#');
548 var hash = i === -1 ? '' : uri.substr(i);
549 uri = i === -1 ? uri : uri.substr(0, i);
550
551 var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
552 var separator = uri.indexOf('?') !== -1 ? "&" : "?";
553 if (uri.match(re)) {
554 uri = uri.replace(re, '$1' + key + "=" + value + '$2');
555 } else {
556 uri = uri + separator + key + "=" + value;
557 }
558 return uri + hash; /* finally append the hash as well */
559 }
560
561 $(document.body).trigger('wc-enhanced-select-init');
562 });
563
564