1(function ($) { 2 3// Run on page load 4function onPageload() { 5 var selectedValue = 'SE3'; 6 var selectedCountry = 'Sweden'; 7 var queryString = window.location.search; 8 // If URL has a query string 9 if (queryString.length) { 10 var params = new URLSearchParams(queryString); 11 selectedValue = params.get('region'); 12 //console.log('Loaded query string value: ' + selectedValue); 13 // Set region & country on the selector 14 $('#ps-selected .ps-region').html(selectedValue); 15 $('#ps-selected .ps-country').html(selectedCountry); 16 } else { 17 selectedValue = 'SE3'; 18 //console.log('Loaded non-query string value: ' + selectedValue); 19 $('#ps-selected .ps-region').html(selectedValue); 20 $('#ps-selected .ps-country').html(selectedCountry); 21 } 22 // Check menu #dd-menu a, if it the value is the seelcted value, add class to it dd-item-active 23 $('#dd-menu a').each(function () { 24 var currentMenuItem = $(this).text().replace(/^Sweden\s/, ''); 25 if (currentMenuItem === selectedValue) { 26 $(this).addClass('dd-item-active'); 27 } 28 }); 29} 30onPageload(); 31 32// Uptate on click 33$('#dd-menu a, #ps__map a').on('click', function (e) { 34 // For menu 35 if ($(this).is('#dd-menu a')) { 36 // Get selected values from the dropdown menu. Remove the country name from the selected value 37 selectedValue = $(this).text(); 38 selectedValue = selectedValue.replace(/^Sweden\s/, ''); 39 } 40 // For map 41 else { 42 // Extract the id attribute value 43 var selectedValue = $(this).attr('id'); 44 } 45 updateUlrandRunAjax(selectedValue); 46 e.preventDefault(); 47}); 48 49 50function updateUlrandRunAjax(selectedValue) { 51 52 //console.log('From updateUlrandRunAjax: ' + selectedValue); 53 // Remove all classes and add class to selected element 54 $('#dd-menu').removeClass(); 55 $('#dd-menu a').removeClass(); 56 57 // Check menu #dd-menu a, if it the value is the seelcted value, add class to it dd-item-active 58 $('#dd-menu a').each(function () { 59 var currentMenuItem = $(this).text().replace(/^Sweden\s/, ''); 60 if (currentMenuItem === selectedValue) { 61 $(this).addClass('dd-item-active'); 62 } 63 }); 64 65 // Check if the accordion is open, if so, close it 66 if ($('.ps-map .fl-accordion-item-active')) { 67 $('.ps-map .fl-accordion-item-active').removeClass('fl-accordion-item-active').find('.fl-accordion-content').slideUp(); 68 $('.ps-map a > i').removeClass('fa-minus').addClass('fa-plus'); 69 } 70 71 // Get the query string from the URL 72 var queryString = window.location.search; 73 74 // If URL has a query string 75 if (queryString.length) { 76 var params = new URLSearchParams(queryString); 77 var currentRegion = params.get('region'); 78 79 // If it's not the current query string, update the URL with the new query string and extract the value ofthe query string in a variable called SelectedValue: 80 if (selectedValue !== currentRegion) { 81 // Update the URL with the new query string 82 params.set('region', selectedValue); 83 window.history.replaceState({}, '', '?' + params.toString()); 84 //console.log('Selected value: ' + selectedValue); 85 //console.log('Current region: ' + currentRegion); 86 // Run the ajax function 87 runAjax(selectedValue); 88 } else { 89 //console.log('The selected value is the same as the current region. Do nothing.'); 90 } 91 // URL with no query string 92 } else { 93 var params = new URLSearchParams(); 94 params.set('region', selectedValue); 95 window.history.replaceState({}, '', '?' + params.toString()); 96 // Run the ajax function 97 runAjax(selectedValue); 98 } 99} 100 101// The ajax function 102function runAjax(selectedValue) { 103 var selectedCountry = 'Sweden'; 104 105 //console.log('From ajax: ' + selectedValue); 106 $.ajax({
107 url: my_ajax_object.ajax_url, 108 type: 'POST', 109 data: { 110 action: 'my_ajax_function', 111 selected_value: selectedValue 112 }, 113 beforeSend: function () { 114 // Show the loading image 115 $('#plug-save').addClass('loading'); 116 $('#plug-save .loader').show(); 117 118 }, 119 success: function (data) { 120 $('#plug-save').removeClass('loading'); 121 $('#plug-save .loader').hide(); 122 123 // Parse the response HTML code 124 var parsedHtml = jQuery.parseHTML(data); 125 126 // Select the savingsdata element 127 var savingsData = jQuery(parsedHtml).find('.ps-savings-nr'); 128 129 // Display the selected value in multiple elements 130 $('#plug-save').html(data); 131 $('#ps-savings .ps-savings-nr').html(savingsData.html()); 132 // Set region & country on the selector 133 $('#ps-selected .ps-region').html(selectedValue); 134 $('#ps-selected .ps-country').html(selectedCountry); 135 }, 136 error: function () { 137 console.log("An error occurred:"); 138 $('#plug-save').removeClass('loading'); 139 $('#plug-save .loader').hide(); 140 } 141 }); 142} 143 144/* Show/hide menu on click */ 145$('#ps-selected, #dd-menu .sub-menu-wrap a').on('click', function (e) { 146 $('#dd-menu').toggleClass('open-menu'); 147 e.preventDefault(); 148}); 149 150/* Rotate new submenu icon on click */ 151$('#dd-menu > li > a').on('click', function (e) { 152 e.preventDefault(); 153}); 154 155})(jQuery)
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.