| 1 |
jQuery(document).ready(function ($) { |
| 2 |
|
| 3 |
/** |
| 4 |
* Output refresh groups icon |
| 5 |
*/ |
| 6 |
var settingGroups = $( '#woocommerce_mailerlite_group'); |
| 7 |
|
| 8 |
if ( settingGroups.length !== 0 ) { |
| 9 |
|
| 10 |
var settingGroupsSelectContainer = settingGroups.next('.select2-container'); |
| 11 |
|
| 12 |
$( '<span id="woo-ml-refresh-groups" class="woo-ml-icon-refresh" data-woo-ml-refresh-groups="true"></span>' ).insertAfter( settingGroupsSelectContainer ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Handle refresh groups |
| 17 |
*/ |
| 18 |
var refreshGroupsRunning = false; |
| 19 |
|
| 20 |
$(document).on('click', '[data-woo-ml-refresh-groups]', function (event) { |
| 21 |
|
| 22 |
event.preventDefault(); |
| 23 |
|
| 24 |
if ( refreshGroupsRunning ) // don't do anything if an AJAX request is running |
| 25 |
return; |
| 26 |
|
| 27 |
var refreshIcon = $(this); |
| 28 |
|
| 29 |
refreshIcon.removeClass('error'); |
| 30 |
refreshIcon.addClass('running'); |
| 31 |
refreshGroupsRunning = true; |
| 32 |
|
| 33 |
// Request |
| 34 |
jQuery.ajax({ |
| 35 |
url: woo_ml_post.ajax_url, |
| 36 |
type: 'post', |
| 37 |
data: { |
| 38 |
action: 'post_woo_ml_refresh_groups' |
| 39 |
}, |
| 40 |
success: function (response) { |
| 41 |
|
| 42 |
// Hide form if success |
| 43 |
if ( response.indexOf( "success" ) >= 0 ) { |
| 44 |
//console.log('done!'); |
| 45 |
refreshIcon.removeClass('running'); |
| 46 |
} |
| 47 |
|
| 48 |
if ( response ) { |
| 49 |
// Redirect page in order to show refreshed list |
| 50 |
location.reload(); |
| 51 |
} else { |
| 52 |
// Something went wrong |
| 53 |
refreshIcon.addClass('error'); |
| 54 |
} |
| 55 |
|
| 56 |
refreshGroupsRunning = false; |
| 57 |
} |
| 58 |
|
| 59 |
}); |
| 60 |
}); |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Resets the re-synchronization for all orders |
| 65 |
*/ |
| 66 |
$(document).on('click', '[data-woo-ml-reset-orders-sync]', function(event) { |
| 67 |
|
| 68 |
event.preventDefault(); |
| 69 |
|
| 70 |
jQuery.ajax({ |
| 71 |
url: woo_ml_post.ajax_url, |
| 72 |
type: 'post', |
| 73 |
data: { |
| 74 |
action: 'post_woo_ml_reset_orders_sync' |
| 75 |
}, |
| 76 |
async: false, // In order not to break our while due to async actions |
| 77 |
success: function (response) { |
| 78 |
|
| 79 |
window.location.reload(); |
| 80 |
} |
| 81 |
|
| 82 |
}); |
| 83 |
}); |
| 84 |
|
| 85 |
/** |
| 86 |
* Synchronize untracked orders |
| 87 |
*/ |
| 88 |
var syncUntrackedOrdersRunning = false; |
| 89 |
var untrackedOrdersCount = 0; |
| 90 |
var untrackedOrdersLeft = 0; |
| 91 |
var untrackedOrdersCycle = 0; |
| 92 |
var untrackedOrdersProgressBar = $('#woo-ml-sync-untracked-orders-progress-bar'); |
| 93 |
var untrackedOrdersProgress = 0; |
| 94 |
|
| 95 |
$(document).on('click', '[data-woo-ml-sync-untracked-orders]', function (event) { |
| 96 |
|
| 97 |
event.preventDefault(); |
| 98 |
|
| 99 |
if ( syncUntrackedOrdersRunning ) // don't do anything if an AJAX request is running |
| 100 |
return; |
| 101 |
|
| 102 |
syncUntrackedOrdersRunning = true; |
| 103 |
|
| 104 |
console.log( 'Synchronize untracked orders' ); |
| 105 |
|
| 106 |
// Elements |
| 107 |
var button = $(this); |
| 108 |
|
| 109 |
button.prop( 'disabled', true ); |
| 110 |
untrackedOrdersProgressBar.show(); |
| 111 |
|
| 112 |
// Prepare cycling |
| 113 |
untrackedOrdersCount = button.data('woo-ml-untracked-orders-count'); |
| 114 |
untrackedOrdersLeft = button.data('woo-ml-untracked-orders-left'); |
| 115 |
untrackedOrdersCycle = button.data('woo-ml-untracked-orders-cycle'); |
| 116 |
|
| 117 |
// Debugging |
| 118 |
//untrackedOrdersCount = 20; |
| 119 |
//untrackedOrdersLeft = untrackedOrdersCount; |
| 120 |
|
| 121 |
console.log( 'untrackedOrdersCount >> ' + untrackedOrdersCount ); |
| 122 |
console.log( 'untrackedOrdersLeft >> ' + untrackedOrdersLeft ); |
| 123 |
console.log( 'untrackedOrdersCycle >> ' + untrackedOrdersCycle ); |
| 124 |
|
| 125 |
while ( untrackedOrdersLeft > 0 ) { |
| 126 |
|
| 127 |
console.log( 'inside the loop!' ); |
| 128 |
|
| 129 |
// Request |
| 130 |
jQuery.ajax({ |
| 131 |
url: woo_ml_post.ajax_url, |
| 132 |
type: 'post', |
| 133 |
data: { |
| 134 |
action: 'post_woo_ml_sync_untracked_resources' |
| 135 |
}, |
| 136 |
async: false, // In order not to break our while due to async actions |
| 137 |
success: function (response) { |
| 138 |
|
| 139 |
// Hide form if success |
| 140 |
if ( response.indexOf( "success" ) >= 0 ) { |
| 141 |
console.log('done!'); |
| 142 |
} |
| 143 |
|
| 144 |
if ( response ) { |
| 145 |
// Redirect page in order to show refreshed list |
| 146 |
console.log('Response: True'); |
| 147 |
updateUntrackedOrdersProgress(); |
| 148 |
} else { |
| 149 |
// Something went wrong |
| 150 |
console.log('Response: False'); |
| 151 |
} |
| 152 |
|
| 153 |
//refreshGroupsRunning = false; |
| 154 |
} |
| 155 |
|
| 156 |
}); |
| 157 |
} |
| 158 |
|
| 159 |
console.log( 'loop finished!' ); |
| 160 |
|
| 161 |
button.hide(); |
| 162 |
untrackedOrdersProgressBar.hide(); |
| 163 |
$('#woo-ml-sync-untracked-orders-success').show(); |
| 164 |
|
| 165 |
}); |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Synchronize untracked products |
| 170 |
*/ |
| 171 |
var syncUntrackedProductsRunning = false; |
| 172 |
var untrackedProductsCount = 0; |
| 173 |
var untrackedProductsLeft = 0; |
| 174 |
var untrackedProductsCycle = 0; |
| 175 |
var untrackedProductsProgressBar = $('#woo-ml-sync-untracked-products-progress-bar'); |
| 176 |
var untrackedProductsProgress = 0; |
| 177 |
|
| 178 |
$(document).on('click', '[data-woo-ml-sync-untracked-products]', function (event) { |
| 179 |
|
| 180 |
event.preventDefault(); |
| 181 |
|
| 182 |
if ( syncUntrackedProductsRunning ) // don't do anything if an AJAX request is running |
| 183 |
return; |
| 184 |
|
| 185 |
syncUntrackedProductsRunning = true; |
| 186 |
|
| 187 |
console.log( 'Synchronize untracked products' ); |
| 188 |
|
| 189 |
// Elements |
| 190 |
var button = $(this); |
| 191 |
|
| 192 |
button.prop( 'disabled', true ); |
| 193 |
untrackedProductsProgressBar.show(); |
| 194 |
|
| 195 |
// Prepare cycling |
| 196 |
untrackedProductsCount = button.data('woo-ml-untracked-products-count'); |
| 197 |
untrackedProductsLeft = button.data('woo-ml-untracked-products-left'); |
| 198 |
untrackedProductsCycle = button.data('woo-ml-untracked-products-cycle'); |
| 199 |
|
| 200 |
console.log( 'untrackedProductsCount >> ' + untrackedProductsCount ); |
| 201 |
console.log( 'untrackedProductsLeft >> ' + untrackedProductsLeft ); |
| 202 |
console.log( 'untrackedProductsCycle >> ' + untrackedProductsCycle ); |
| 203 |
|
| 204 |
while ( untrackedProductsLeft > 0 ) { |
| 205 |
|
| 206 |
console.log( 'inside the loop! (products)' ); |
| 207 |
|
| 208 |
// Request |
| 209 |
jQuery.ajax({ |
| 210 |
url: woo_ml_post.ajax_url, |
| 211 |
type: 'post', |
| 212 |
data: { |
| 213 |
action: 'post_woo_ml_sync_untracked_products' |
| 214 |
}, |
| 215 |
async: false, // In order not to break our while due to async actions |
| 216 |
success: function (response) { |
| 217 |
|
| 218 |
// Hide form if success |
| 219 |
if ( response.indexOf( "success" ) >= 0 ) { |
| 220 |
console.log('done!'); |
| 221 |
} |
| 222 |
|
| 223 |
if ( response ) { |
| 224 |
// Redirect page in order to show refreshed list |
| 225 |
console.log('Response: True'); |
| 226 |
updateUntrackedProductsProgress(); |
| 227 |
} else { |
| 228 |
// Something went wrong |
| 229 |
console.log('Response: False'); |
| 230 |
} |
| 231 |
|
| 232 |
//refreshGroupsRunning = false; |
| 233 |
} |
| 234 |
|
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
console.log( 'loop finished!' ); |
| 239 |
|
| 240 |
button.hide(); |
| 241 |
untrackedProductsProgressBar.hide(); |
| 242 |
$('#woo-ml-sync-untracked-products-success').show(); |
| 243 |
|
| 244 |
}); |
| 245 |
|
| 246 |
function updateUntrackedOrdersProgress() { |
| 247 |
|
| 248 |
// Reduce orders left count |
| 249 |
untrackedOrdersLeft -= untrackedOrdersCycle; |
| 250 |
|
| 251 |
// Calculate progress |
| 252 |
untrackedOrdersProgress = ( ( 100 * ( untrackedOrdersCount - untrackedOrdersLeft ) ) / untrackedOrdersCount ); |
| 253 |
|
| 254 |
// Update progress bar |
| 255 |
var progressBarInnerElement = untrackedOrdersProgressBar.find('div'); |
| 256 |
|
| 257 |
progressBarInnerElement.css( 'width', untrackedOrdersProgress + '%' ); |
| 258 |
} |
| 259 |
|
| 260 |
function updateUntrackedProductsProgress() { |
| 261 |
|
| 262 |
// Reduce orders left count |
| 263 |
untrackedProductsLeft -= untrackedProductsCycle; |
| 264 |
|
| 265 |
// Calculate progress |
| 266 |
untrackedProductsProgress = ( ( 100 * ( untrackedProductsCount - untrackedProductsLeft ) ) / untrackedProductsCount ); |
| 267 |
|
| 268 |
// Update progress bar |
| 269 |
var progressBarInnerElement = untrackedProductsProgressBar.find('div'); |
| 270 |
|
| 271 |
progressBarInnerElement.css( 'width', untrackedProductsProgress + '%' ); |
| 272 |
} |
| 273 |
|
| 274 |
}); |