| @@ -48,25 +48,94 @@ | ||
| 48 | 48 | } |
| 49 | 49 | return ""; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | + function isSaveOrderItemsRequest(settings) { | |
| 53 | + if (!settings || !settings.data) { | |
| 54 | + return false; | |
| 55 | + } | |
| 56 | + | |
| 57 | + if (typeof settings.data === 'string') { | |
| 58 | + try { | |
| 59 | + return new URLSearchParams(settings.data).get('action') === 'woocommerce_save_order_items'; | |
| 60 | + } catch (e) { | |
| 61 | + return settings.data.indexOf('action=woocommerce_save_order_items') !== -1; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + if (typeof settings.data === 'object') { | |
| 66 | + return settings.data.action === 'woocommerce_save_order_items'; | |
| 67 | + } | |
| 68 | + | |
| 69 | + return false; | |
| 70 | + } | |
| 71 | + | |
| 72 | + function refreshRouteFeeButton() { | |
| 73 | + if (typeof woocommerce_admin_meta_boxes === 'undefined') { | |
| 74 | + return; | |
| 75 | + } | |
| 76 | + | |
| 77 | + var data = { | |
| 78 | + action: 'routeapp_refresh_admin_fee_button', | |
| 79 | + security: woocommerce_admin_meta_boxes.order_item_nonce, | |
| 80 | + order_id: woocommerce_admin_meta_boxes.post_id | |
| 81 | + }; | |
| 82 | + | |
| 83 | + $.post( | |
| 84 | + woocommerce_admin_meta_boxes.ajax_url, | |
| 85 | + data, | |
| 86 | + function( response ) { | |
| 87 | + if (!response.success) { | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $('#add-route-fee, #remove-route-fee').remove(); | |
| 92 | + | |
| 93 | + if (response.data && response.data.html) { | |
| 94 | + $('.wc-order-bulk-actions .add-items .calculate-action').first().before(response.data.html); | |
| 95 | + } | |
| 96 | + } | |
| 97 | + ).fail(function(xhr) { | |
| 98 | + console.error('Failed to refresh Route fee button.', xhr); | |
| 99 | + }); | |
| 100 | + } | |
| 101 | + | |
| 52 | 102 | $(document).ready(function () { |
| 53 | - if (!getRouteCookie('route_installed') && document.getElementById("route_counter") !== null) { | |
| 103 | + $('.routeapp-redirect-countdown').each(function () { | |
| 104 | + var $wrap = $(this); | |
| 105 | + var mode = $wrap.data('countdown-mode') || 'once'; | |
| 106 | + var alwaysRun = mode === 'always'; | |
| 54 | 107 | |
| 55 | - setRouteCookie('route_installed', 1, 360); | |
| 108 | + if (!alwaysRun && getRouteCookie('route_installed')) { | |
| 109 | + $wrap.closest('.notice-success').hide(); | |
| 110 | + return; | |
| 111 | + } | |
| 56 | 112 | |
| 57 | - let intervalId = setInterval(function () { | |
| 58 | - let seconds = document.getElementById("route_counter").innerText; | |
| 59 | - if(seconds > 0) { | |
| 60 | - document.getElementById("route_counter").innerText = seconds - 1; | |
| 61 | - }else{ | |
| 62 | - clearInterval(intervalId) | |
| 63 | - window.location = $("#route_counter").data('redirect-src'); | |
| 113 | + if (!alwaysRun) { | |
| 114 | + setRouteCookie('route_installed', 1, 360); | |
| 115 | + } | |
| 116 | + | |
| 117 | + var $num = $wrap.find('.routeapp-redirect-countdown__num'); | |
| 118 | + var seconds = parseInt($wrap.data('seconds'), 10); | |
| 119 | + if (isNaN(seconds) || seconds < 1) { | |
| 120 | + seconds = 5; | |
| 121 | + } | |
| 122 | + $num.text(seconds); | |
| 123 | + | |
| 124 | + var intervalId = setInterval(function () { | |
| 125 | + seconds--; | |
| 126 | + if (seconds > 0) { | |
| 127 | + $num.text(seconds); | |
| 128 | + } else { | |
| 129 | + clearInterval(intervalId); | |
| 130 | + $num.text(0); | |
| 131 | + var dest = $wrap.attr('data-redirect-src'); | |
| 132 | + if (dest) { | |
| 133 | + window.location.href = dest; | |
| 134 | + } | |
| 64 | 135 | } |
| 65 | - },1000) | |
| 66 | - }else{ | |
| 67 | - $('#route_counter').parents('.notice-success').hide() | |
| 68 | - } | |
| 136 | + }, 1000); | |
| 137 | + }); | |
| 69 | 138 | |
| 70 | 139 | $(document).on('click', '#add-route-fee', function (e) { |
| 71 | 140 | e.preventDefault(); |
| 72 | 141 | |
| @@ -134,8 +203,16 @@ | ||
| 134 | 203 | } |
| 135 | 204 | } |
| 136 | 205 | ); |
| 137 | 206 | |
| 207 | + }); | |
| 208 | + | |
| 209 | + $(document.body).on('order-totals-recalculate-success', refreshRouteFeeButton); | |
| 210 | + | |
| 211 | + $(document).ajaxSuccess(function(event, xhr, settings) { | |
| 212 | + if (isSaveOrderItemsRequest(settings)) { | |
| 213 | + refreshRouteFeeButton(); | |
| 214 | + } | |
| 138 | 215 | }); |
| 139 | 216 | |
| 140 | 217 | }) |
| 141 | 218 | |