1/*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order */ 2window.germanized = window.germanized || {}; 3 4( function( $, germanized ) { 5 6 germanized.unit_price_observer_queue = { 7 queue: {}, 8 timeout: null, 9 params: {}, 10 request: null, 11 12 init: function() { 13 this.params = wc_gzd_unit_price_observer_queue_params; 14 this.queue = {}; 15 this.queuesInExecution = {}; 16 this.latestQueueInExection = ''; 17 this.aborted = {}; 18 this.timeout = null; 19 this.request = null; 20 }, 21 22 execute: function() { 23 var self = germanized.unit_price_observer_queue, 24 data = [], 25 currentQueueId = Date.now() + ''; 26 27 self.queuesInExecution[ currentQueueId ] = { ...self.queue }; 28 self.latestQueueInExection = currentQueueId; 29 self.queue = {}; 30 31 /** 32 * Reverse queue 33 */ 34 Object.keys( self.queuesInExecution[ currentQueueId ] ).forEach( function( queueKey ) { 35 data = data.concat( [{ 36 'product_id': self.queuesInExecution[ currentQueueId ][ queueKey ].productId, 37 'price' : self.queuesInExecution[ currentQueueId ][ queueKey ].priceData.price, 38 'price_sale': self.queuesInExecution[ currentQueueId ][ queueKey ].priceData.sale_price, 39 'quantity' : self.queuesInExecution[ currentQueueId ][ queueKey ].priceData.quantity, 40 'key' : queueKey, 41 }] ); 42 }); 43 44 self.request = $.ajax({ 45 type: "POST", 46 url: self.params.wc_ajax_url.toString().replace( '%%endpoint%%', 'gzd_refresh_unit_price' ), 47 data: { 48 'security' : self.params.refresh_unit_price_nonce, 49 'products' : data, 50 'queue_id' : currentQueueId, 51 }, 52 queueId: currentQueueId, 53 success: function( data ) { 54 var xhrQueueId = this.queueId, 55 currentQueue = self.queuesInExecution.hasOwnProperty( xhrQueueId ) ? self.queuesInExecution[ xhrQueueId ] : {}, 56 aborted = self.aborted.hasOwnProperty( xhrQueueId ) ? self.aborted[ xhrQueueId ] : {}; 57 58 Object.keys( currentQueue ).forEach( function( queueId ) { 59 if ( ! aborted.hasOwnProperty( queueId ) ) { 60 var current = currentQueue[ queueId ], 61 observer = current.observer, 62 priceData = current.priceData, 63 priceSelector = current.priceSelector, 64 isPrimary = current.isPrimary, 65 unitPrices = self.getUnitPricesFromMap( priceData.unit_price ); 66 67 if ( observer ) { 68 if ( data.products.hasOwnProperty( queueId ) ) { 69 var response = data.products[ queueId ]; 70 71 observer.stopObserver( observer, priceSelector ); 72 73 /** 74 * Do only adjust unit price in case current product id has not changed 75 * in the meantime (e.g. variation change). 76 */ 77 if ( parseInt( response.product_id ) === observer.getCurrentProductId( observer ) ) { 78 if ( response.hasOwnProperty( 'unit_price_html' ) ) { 79 observer.unsetUnitPriceLoading( observer, unitPrices, response.unit_price_html ); 80 } else { 81 observer.unsetUnitPriceLoading( observer, unitPrices ); 82 } 83 } else { 84 observer.unsetUnitPriceLoa
84ding( observer, unitPrices ); 85 } 86 87 observer.startObserver( observer, priceSelector, isPrimary ); 88 } else { 89 observer.stopObserver( observer, priceSelector ); 90 observer.unsetUnitPriceLoading( observer, unitPrices ); 91 observer.startObserver( observer, priceSelector, isPrimary ); 92 } 93 } 94 } else { 95 delete self.aborted[ xhrQueueId ][ queueId ]; 96 } 97 }); 98 99 delete self.queuesInExecution[ xhrQueueId ]; 100 }, 101 error: function() { 102 var xhrQueueId = this.queueId, 103 currentQueue = self.queuesInExecution.hasOwnProperty( xhrQueueId ) ? self.queuesInExecution[ xhrQueueId ] : {}, 104 aborted = self.aborted.hasOwnProperty( xhrQueueId ) ? self.aborted[ xhrQueueId ] : {}; 105 106 Object.keys( currentQueue ).forEach( function( queueId ) { 107 if ( ! aborted.hasOwnProperty( queueId ) ) { 108 var current = currentQueue[ queueId ], 109 observer = current.observer, 110 priceData = current.priceData, 111 priceSelector = current.priceSelector, 112 isPrimary = current.isPrimary, 113 unitPrices = self.getUnitPricesFromMap( priceData.unit_price ); 114 115 if ( observer ) { 116 observer.stopObserver( observer, priceSelector ); 117 observer.unsetUnitPriceLoading( observer, unitPrices ); 118 observer.startObserver( observer, priceSelector, isPrimary ); 119 } 120 } else { 121 delete self.aborted[ xhrQueueId ][ queueId ]; 122 } 123 }); 124 125 delete self.queuesInExecution[ xhrQueueId ]; 126 }, 127 dataType: 'json' 128 } ); 129 }, 130 131 getUnitPricesFromMap: function( unitPriceMap ) { 132 let unitPrices = []; 133 134 unitPriceMap.forEach( function( unitPrice ) { 135 unitPrices = $.merge( unitPrices, $( unitPrice ) ); 136 } ); 137 138 return $( unitPrices ); 139 }, 140 141 getQueueKey: function( productId ) { 142 return ( productId + '' ).replace( /[^a-zA-Z0-9]/g, '' ); 143 }, 144 145 getLatestQueueInExecution() { 146 var self = germanized.unit_price_observer_queue; 147 148 return self.queuesInExecution.hasOwnProperty( self.latestQueueInExection ) ? self.queuesInExecution[ self.latestQueueInExection ] : {}; 149 }, 150 151 exists: function( productId ) { 152 var self = germanized.unit_price_observer_queue, 153 queueKey = self.getQueueKey( productId ); 154 155 return self.queue.hasOwnProperty( queueKey ) || self.getLatestQueueInExecution().hasOwnProperty( queueKey ); 156 }, 157 158 get: function( productId ) { 159 var self = germanized.unit_price_observer_queue, 160 queueKey = self.getQueueKey( productId ), 161 queueInExecution = self.getLatestQueueInExecution().hasOwnProperty( queueKey ); 162 163 if ( queueInExecution.hasOwnProperty( queueKey ) ) { 164 return queueInExecution[ queueKey ]; 165 } else if ( self.queue.hasOwnProperty( queueKey ) ) { 166 return self.queue[ queueKey ]; 167 } 168 169 return false; 170 }, 171 172 abort: function( productId ) { 173 var self = germanized.unit_price_observer_queue, 174 queueKey = self.getQueueKey( productId ), 175 latestQueueInExecutionKey = self.latestQueueInExection, 176 latestQueueInExecution = self.queuesInExecution.hasOwnProperty( latestQueueInExecutionKey ) ? self.queuesInExecution[ latestQueueInExecutionKey ] : {}; 177 178 if ( latestQueueInExecution.hasOwnProperty( queueKey ) ) { 179 var current = latestQueueInExecution[ queueKey ], 180 observer = current.observer, 181 priceData = current.priceData, 182 priceSelector = current.priceSelector, 183 isPrimary = current.isPrimary, 184 unitPrices = self.getUnitPricesFromMap( priceData.unit_price ); 185 186 if ( observer ) { 187 observer.stopObserver( observer, priceSelector );
188 observer.unsetUnitPriceLoading( observer, unitPrices ); 189 observer.startObserver( observer, priceSelector, isPrimary ); 190 } 191 192 if ( ! self.aborted.hasOwnProperty( latestQueueInExecutionKey ) ) { 193 self.aborted[ latestQueueInExecutionKey ] = {}; 194 } 195 196 self.aborted[ latestQueueInExecutionKey ][ queueKey ] = current; 197 198 return true; 199 } 200 201 return false; 202 }, 203 204 add: function( observer, productId, priceData, priceSelector, isPrimary ) { 205 var self = germanized.unit_price_observer_queue, 206 queueKey = self.getQueueKey( productId ); 207 208 if ( self.queue.hasOwnProperty( queueKey ) ) { 209 priceData['unit_price'].each( function( i, obj ) { 210 if ( ! self.queue[ queueKey ]['priceData']['unit_price'].has( obj ) ) { 211 self.queue[ queueKey ]['priceData']['unit_price'].set( obj, obj ); 212 } 213 }); 214 } else { 215 var unitPrices = new Map(); 216 217 priceData['unit_price'].each( function( i, obj ) { 218 unitPrices.set( obj, obj ); 219 }); 220 221 priceData['unit_price'] = unitPrices; 222 223 self.queue[ queueKey ] = { 224 'productId' : productId, 225 'observer' : observer, 226 'priceData' : priceData, 227 'priceSelector': priceSelector, 228 'isPrimary' : isPrimary 229 }; 230 } 231 232 clearTimeout( self.timeout ); 233 self.timeout = setTimeout( self.execute, 500 ); 234 }, 235 }; 236 237 $( document ).ready( function() { 238 germanized.unit_price_observer_queue.init(); 239 }); 240 241})( jQuery, window.germanized );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.