1/* JavaScript for Frontend pages */ 2jQuery(document).ready(function($) { 3 /** 4 * Returns the value of a cookie. 5 * 6 * @param string name The cookie name. 7 * @return mixed 8 */ 9 function get_cookie(name) { 10 // If js-cookie is installed, use it 11 if(typeof Cookies != 'undefined') { 12 return Cookies.get(name); 13 } 14 if(typeof $.cookie != 'undefined') { 15 return $.cookie(name); 16 } 17 return null; 18 } 19 20 var params = woocommerce_cache_handler_params; 21 var ajax_url = params.ajax_url; 22 var active_currency = ''; 23 var price_slider_params = { 24 initialized: false 25 } 26 27 // Common arguments for Ajax calls 28 var default_ajax_args = { 29 'action': params.ajax_action, 30 '_ajax_nonce': params.wp_nonce 31 } 32 33 /** 34 * Returns a set of Ajax arguments, which include the default ones. 35 * 36 * @param object args A set of arguments, to replace the default ones. 37 * @return object 38 */ 39 var get_ajax_args = function(args) { 40 return $.extend({}, default_ajax_args, args); 41 } 42 43 /** 44 * Retrieves the active currency via Ajax. 45 * 46 * @return string 47 */ 48 var get_active_currency = function() { 49 if(active_currency == '') { 50 var ajax_args = get_ajax_args({ 51 'exec': 'get_active_currency' 52 }); 53 $.post(ajax_url, ajax_args, function(response) { 54 // Check that the response contains the active currency. Without it, we 55 // can't do anything 56 // @since 1.0.8.180207 57 if(!response || !response['active_currency']) { 58 console.log('Unable to determine the active currency'); 59 console.log('Response for Ajax call "' + ajax_args.exec + '":'); 60 console.log(response); 61 return; 62 } 63 64 active_currency = response['active_currency']; 65 $(document).trigger('active_currency_retrieved', active_currency); 66 }); 67 } 68 } 69 70 /** 71 * Convenience function to invoke jQuery.ui.block(). 72 * 73 * @param object elements The jQuery elements to "block". 74 */ 75 var block_elements = function($elements) { 76 $elements.block({ 77 message: null, 78 overlayCSS: { 79 background: '#fff', 80 opacity: 0.6 81 } 82 }); 83 } 84 85 /** 86 * Updates the product prices on the page, fetching the "live" ones in the 87 * currency that applies to the user. 88 */ 89 var update_product_prices = function() { 90 // Collect the product prices display on the page 91 var $product_prices = $('.aelia_product_price'); 92 93 if($product_prices.length <= 0) { 94 return; 95 } 96 97 var product_ids = $.map($product_prices, function(elem) { 98 return $(elem).data('product_id'); 99 }); 100 101 // "Block" the prices to show that they are being refreshed 102 block_elements($product_prices); 103 104 var ajax_args = get_ajax_args({ 105 'exec': 'get_product_prices_html', 106 'product_ids': product_ids 107 }); 108 $.post(ajax_url, ajax_args, function(response) { 109 // Debug 110 //console.log(response); 111 112 if(response['product_prices_html']) { 113 var product_prices_html = response['product_prices_html']; 114 $product_prices.each(function() { 115 var $elem = $(this); 116 var product_id = $elem.data('product_id'); 117 118 // Update each of the prices using the returned data 119 if(product_prices_html[product_id]) { 120 $elem.html(product_prices_html[product_id]); 121 } 122 }); 123 } 124 else { 125 console.log('Unexpected response received for "' + ajax_args.exec + '"'); 126 console.log(response); 127 } 128 // "Unblock" the prices 129 $product_prices.unblock(); 130 }); 131 } 132 133 /** 134 * Updates the HTML produced by the [aelia_shortcode_currency_amount] shortcode, 135 * showing the values in the active currency. 136 */ 137 var update_currency_amount_shortcodes = function() { 138 // Collect the product prices display on the page 139 var $shortcodes = $('.aelia_shortcode_currency_amount'); 140 141 if($shortcodes.length <= 0) { 142 return; 143 } 144 145 var shortcodes = {}; 146 $.each($shortcodes, function() { 147 var $elem = $(this); 148 shortcodes[$elem.data('shortcode_id')] = $elem.data('shortcode_args'); 149 }); 150 151 // "Block" the prices to show that they are being refreshed 152 block_elements($shortcodes); 153 154 var ajax_args = get_ajax_args({ 155 'exec': 'get_currency_amount_shortcodes_html', 156 'shortcodes': shortcodes 157 }); 158 $.post(ajax_url, ajax_args, function(response) { 159 // Debug 160 //console.log(response); 161 162 if(response['shortcodes_html']) { 163 var shortcodes_html = response['shortcodes_html']; 164 $shortcodes.each(function() { 165 var $elem = $(this); 166 var shortcode_id = $elem.data('shortcode_id'); 167 168 // Update each of the prices using the returned data 169 if(shortcodes_html[shortcode_id]) { 170 $elem.html(shortcodes_html[shortcode_id]); 171 } 172 }); 173 } 174 else { 175 console.log('Unexpected response received for "' + ajax_args.exec + '"'); 176 console.log(response); 177 } 178 // "Unblock" the prices 179 $shortcodes.unblock(); 180 }); 181 } 182 183 /** 184 * Updates the currency selector rendered by the Aelia Currency Switcher, so 185 * that they show the currency passed as a parameter as "selected". 186 * 187 * @param string currency_code A currency code. 188 */ 189 var update_currency_selectors = function(currency_code) { 190 $('.widget_wc_aelia_currencyswitcher_widget').each(function() { 191 var $widget = $(this); 192 $widget.find('select').val(currency_code); 193 $widget.find('button:not(.' + currency_code + ')').removeClass('active'); 194 $widget.find('button.' + currency_code).addClass('active'); 195 }); 196 197 // Updates the "flags" currency selectors 198 // @since 1.1.0.230328 199 $('.widget_wc_aelia_currencyswitcher_widget.currency_selector.flags').each(function() { 200 const $currency_selector = $(this); 201 202 // Take the flag from the active currency 203 const $currency_image = $currency_selector.find(`.currencies .currency.${currency_code} .currency_flag img`); 204 $currency_selector.find('.selected_currency .currency_flag img').attr('src', $currency_image.attr('src')); 205 206 // Take the label from the active currency 207 const currency_name = $currency_selector.find(`.currencies .currency.${currency_code} .currency_name`).html(); 208 $currency_selector.find('.selected_currency .currency_name').html(currency_name); 209 210 // Add the "selected" attribute to the active currency in the dropdown 211 $currency_selector.find(`.currencies .selected`).removeClass('selected'); 212 $currency_selector.find(`.currencies .currency.${currency_code}`).addClass('selected'); 213 }); 214 } 215 216 /** 217 * Indicates if a country exists amongst the options in a given country 218 * selector widget. 219 * 220 * @param object $widget 221 * @param string country_code 222 * @since 1.0.9.180212 223 */ 224 var country_exists_in_selector = function($widget, country_code) { 225 return $widget.find('select.countries option[value="' + country_code + '"]').length > 0; 226 } 227 228 /** 229 * Updates the country selector rendered by the Aelia plugins, so 230 * that they show the country passed as a parameter as "selected". 231 */ 232 var update_country_selectors = function() { 233 var country_code = get_cookie('aelia_customer_country') || ''; 234 var state_code = get_cookie('aelia_customer_state') || ''; 235 if(country_code == '') { 236 return; 237 } 238 239 // Update country and state selectors 240 $('.widget_wc_aelia_country_selector_widget, .widget_wc_aelia_customer_country_selector_widget').each(function() { 241 var $widget = $(this); 242 var location_changed = false;
243 244 // If the country we fetched doesn't exist in the country selector, it 245 // can't be selected. Trying to select it anyway would cause an infinite 246 // loop, as the country selector would have a different value at the next 247 // page load, and triggered the (pointless) selection again 248 // @since 249 if(!country_exists_in_selector($widget, country_code)) { 250 return; 251 } 252 253 // Only change the country if the selected one is different 254 // @since 1.0.8.180207 255 if($widget.find('select.countries').val() != country_code) { 256 $widget.find('select.countries').val(country_code); 257 location_changed = true; 258 } 259 260 // Only change the state/province if the selected one is different 261 // @since 1.0.8.180207 262 if((state_code != '') && ($widget.find('select.states').val() != 'state_code')) { 263 $widget.find('select.states').val(state_code); 264 location_changed = true; 265 } 266 267 if(location_changed) { 268 $widget.find('select.countries').change(); 269 } 270 }); 271 272 // Updates the "flags" country selectors 273 // @since 1.1.0.230328 274 $('.widget_wc_aelia_currencyswitcher_widget.country_switcher.flags').each(function() { 275 const $country_selector = $(this); 276 277 // Find the country in the dropdown list 278 const $active_country_item = $country_selector.find(`.countries .country.${country_code}`); 279 280 // If the country is not in the list, do nothing 281 if($active_country_item.length <= 0) { 282 return; 283 } 284 285 // Take the flag from the active country 286 const $country_image = $active_country_item.find('.country_flag img'); 287 $country_selector.find('.selected_country .country_flag img').attr('src', $country_image.attr('src')); 288 289 // Take the label from the active country 290 const country_name = $active_country_item.find('.country_name').html(); 291 $country_selector.find('.selected_country .country_name').html(country_name); 292 293 // Add the "selected" attribute to the active country in the dropdown 294 $country_selector.find(`.countries .selected`).removeClass('selected'); 295 $country_selector.find(`.countries .country.${country_code}`).addClass('selected'); 296 }); 297 } 298 299 /** 300 * Updates text elements that show currency information, such as currency 301 * symbols or codes. 302 * 303 * @param string currency_code A currency code. 304 */ 305 var update_static_currency_elements = function(currency_code) { 306 $('.active_currency_code').text(currency_code); 307 308 var currency_symbol = params['currencies'][currency_code]['symbol']; 309 $('.active_currency_symbol').text(currency_symbol); 310 } 311 312 /** 313 * Updates the price slider filter widget, if visible, so that it shows the 314 * values in the correct currency. 315 */ 316 var update_price_slider = function() { 317 // To update the slider, we need to have an active currency and wait for the 318 // slider to be initialised 319 if((active_currency == '') || !price_slider_params.initialized) { 320 return; 321 } 322 323 var $slider = $('.price_slider').first(); 324 if($slider.length <= 0) { 325 return; 326 } 327 328 var $slider_widget = $slider.parents('.widget_price_filter').first(); 329 // If the filter is already in the correct currency, there's no need to take 330 // action 331 if($slider_widget.find('#price_filter_currency').val() === active_currency) { 332 return; 333 } 334 335 var current_min_value = price_slider_params.current_min_value; 336 var current_max_value = price_slider_params.current_max_value; 337 338 block_elements($slider_widget); 339 340 // Update the currency symbol 341 woocommerce_price_slider_params.currency_symbol = params['currencies'][active_currency]['symbol']; 342 343 var current_values = $slider.slider('values'); 344 var ajax_args = get_ajax_args({ 345 'exec': 'convert_amounts', 346 'amounts': { 347 'min_current_value': current_min_value, 348 'max_current_value': current_max_value, 349 'min_price': $slider_widget.find('#min_price').data('min'), 350 'max_price': $slider_widget.find('#max_price').data('max') 351 }, 352 'from_currency': $slider_widget.find('#price_filter_currency').val() 353 }); 354 355 $.post(ajax_url, ajax_args, function(response) { 356 // Debug 357 // console.log(response); 358 if(!$.isEmptyObject(response['converted_amounts'])) { 359 var converted_amounts = response['converted_amounts']; 360 361 var new_current_values = [ 362 parseInt(converted_amounts['min_current_value']), 363 parseInt(converted_amounts['max_current_value']), 364 ]; 365 366 var min_amount = parseInt(converted_amounts['min_price']); 367 var max_amount = parseInt(converted_amounts['max_price']); 368 369 $slider_widget.find('#min_price').val(new_current_values[0]); 370 $slider_widget.find('#max_price').val(new_current_values[1]); 371 $slider_widget.find('#price_filter_currency').val(active_currency); 372 $slider.slider('option', 'min', min_amount); 373 $slider.slider('option', 'max', max_amount); 374 $slider.slider('values', new_current_values); 375 376 $(document.body).trigger('price_slider_slide', new_current_values); 377 } 378 else { 379 console.log('Unexpected response received for "' + ajax_args.exec + '"'); 380 console.log(response); 381 } 382 $slider_widget.unblock(); 383 }); 384 } 385 386 // Update the values on the page when the active currency is retrieved 387 $(document).on('active_currency_retrieved', function(event, active_currency) { 388 update_currency_selectors(active_currency); 389 update_country_selectors(); 390 391 update_product_prices(); 392 update_currency_amount_shortcodes(); 393 394 update_static_currency_elements(active_currency); 395 update_price_slider(); 396 }); 397 398 // Update the price slider after it has been initialised 399 $(document.body).on('price_slider_create', function(event, current_min_value, current_max_value) { 400 price_slider_params = { 401 initialized: true, 402 current_min_value: current_min_value, 403 current_max_value: current_max_value 404 } 405 406 // Call the price slider update. We have to call it here as well, because 407 // we can't predict if the "active_currency_retrieved" event will be fired 408 // before of after "price_slider_create" 409 update_price_slider(); 410 411 //console.log(current_min_value, current_max_value); 412 }); 413 414 // Retrieve the active currency. This will also trigger the update of various 415 // elements on the page 416 get_active_currency(); 417});
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.