1(function( $ ) { 2 'use strict'; 3 4 /** 5 * All of the code for your public-facing JavaScript source 6 * should reside in this file. 7 * 8 * Note: It has been assumed you will write jQuery code here, so the 9 * $ function reference has been prepared for usage within the scope 10 * of this function. 11 * 12 * This enables you to define handlers, for when the DOM is ready: 13 * 14 * $(function() { 15 * 16 * }); 17 * 18 * When the window is loaded: 19 * 20 * $( window ).load(function() { 21 * 22 * }); 23 * 24 * ...and/or other possibilities. 25 * 26 * Ideally, it is not considered best practise to attach more than a 27 * single DOM-ready or window-load handler for a particular page. 28 * Although scripts in the WordPress core, Plugins and Themes may be 29 * practising this, we should strive to set a better example in our own work. 30 */ 31 32 // Init API handler when select changes. 33 $( window ).load(function() { 34 $('#omnia-filters-container form' + ' select').on('change', function() { 35 let targetForm = '#omnia-filters-container form'; 36 37 // Make ajax call. 38 omniaDoAjaxCall( 39 null, 40 targetForm 41 ); 42 }); 43 }); 44 45 // Function responsible for AJAX API calls. 46 function omniaDoAjaxCall(callback, targetForm) { 47 48 // Get form data. 49 let targetFormObject = $(targetForm); 50 51 // Get api key. 52 let requestApiKey = targetFormObject.data('api-key'); 53 54 // If the apiKey is invalid? 55 omniaDoAjaxCallWithoutForm(callback, requestApiKey, targetFormObject.serialize()) 56 } 57 58 // Function responsible for AJAX API calls with a form associated. 59 function omniaDoAjaxCallWithoutForm(callback, requestApiKey, requestParameters) { 60 61 // If the apiKey is invalid? 62 if (!omniaAjaxCallConfig.apis.hasOwnProperty(requestApiKey)) { 63 return; 64 } 65 66 // Get the target form (in case it's available). 67 let targetForm = $('form[data-api-key="' + requestApiKey + '"]'); 68 69 // Show the spinner. 70 if (targetForm.length) { 71 targetForm.append('<div class="ajax-spinner form-overlay"><span>Processing...</span></div>'); 72 } 73 74 // Ajax call. 75 $.ajax({ 76 type: "get", 77 dataType: "json", 78 contentType: false, 79 processData: true, 80 url: omniaAjaxCallConfig.meta.url + 'action=' + omniaAjaxCallConfig.apis[requestApiKey].action, 81 data: requestParameters, 82 success: function (response) { 83 84 // If it's a success? 85 if (! response.hasErrors) { 86 87 // Let's find the form "form-success-response" container. 88 let targetFormSuccessContainer = targetForm.parent().parent().find('div.form-success-response'); 89 90 // Do we have the container? 91 if (targetFormSuccessContainer.length > 0) { 92 93 // First empty it. 94 targetFormSuccessContainer.empty(); 95 96 // Update the success container. 97 if (response.data.html != '') { 98 targetFormSuccessContainer.append(response.data.html); 99 } 100 } 101 102 // Pass data back to the callback. 103 if (callback) { 104 callback(response.data); 105 } 106 107 } else { 108 109 // Let's find the form "form-error-response" container. 110 let targetFormErrorContainer = targetForm.find('div.form-error-response'); 111 112 // Do we have the container? 113 if (targetFormErrorContainer.length > 0) { 114 115 // First empty it. 116 targetFormErrorContainer.empty(); 117 118 // Update the error container. 119 targetFormErrorContainer.html('<p><strong>Error:</strong> ' + response.message + '</p>'); 120 121 } else { 122 alert(response.message); 123 } 124 } 125 }, 126 complete: function () { 127 128 // Remove the spinner. 129 if (targetForm.length) { 130 targetForm.find('.ajax-spinner.form-overlay').remove(); 131 } 132 } 133 }); 134 } 135 136})( 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.