1jQuery(function($){ 2 // Apply the select2 library on your select elements 3 if ($(".filter").length) { 4 $(".filter").select2({ 5 minimumResultsForSearch: Infinity, 6 }); 7 } 8 if ($(".filter-year").length) { 9 $(".filter-year").select2({ 10 minimumResultsForSearch: Infinity, 11 }); 12 } 13 14 function loadPosts(page = 1) { 15 var data = { 16 action: 'customfilter', 17 page: page, 18 }; 19 if ($('#investors_news_categories').length) { 20 data.investors_news_categories = $('#investors_news_categories').val(); 21 } 22 if ($('#year').length) { 23 data.year = $('#year').val(); 24 } 25 $.ajax({ 26 url: wpApiSettingsPress.root + 'custom/v1/filter/', 27 method: 'POST', 28 dataType: 'json', 29 data: data, 30 beforeSend: function (xhr) { 31 xhr.setRequestHeader('X-WP-Nonce', wpApiSettingsPress.nonce); 32 $('.loading').show(); 33 }, 34 success: function (response) { 35 if ($('#response').length) { 36 $('#response').html(response.content); 37 if(response.content.trim() != '<p>'+wpApiSettingsPress.no_posts_found+'</p>') { 38 generatePagination(response.pages, page); 39 } 40 } 41 }, 42 error: function(jqXHR, textStatus, errorThrown) { 43 console.error('Error:', errorThrown); 44 console.error('Error status:', jqXHR.status); 45 console.error('Error status text:', jqXHR.statusText); 46 console.error('Error response:', jqXHR.responseText); 47 $('.loading').hide(); 48 } 49 }); 50 } 51 52 function generatePagination(pages, currentPage) { 53 var pagination = ''; 54 $('#pagination').addClass("d-none"); 55 if ($('#pagination').length) { 56 if(pages > 1 && $('#response').html().trim() != '<p>'+wpApiSettingsPress.no_posts_found+'</p>') { 57 $('#pagination').removeClass("d-none"); 58 59 // Determine if previous and next buttons should be disabled 60 var previousDisabled = currentPage === 1 ? 'disabled' : ''; 61 var nextDisabled = currentPage === pages ? 'disabled' : ''; 62 63 pagination += '<a class="order-2 order-md-1 ' + previousDisabled + '" href="#" data-page="' + Math.max(1, currentPage - 1) + '">'+wpApiSettingsPress.previous+'</a>'; 64 pagination += '<ul class="d-flex mb-0 order-1 order-md-2">'; 65 66 var firstPage = Math.max(1, currentPage - 2); 67 var lastPage = Math.min(pages, firstPage + 4); 68 69 // Ensure we always show 5 pages 70 if (lastPage - firstPage < 4){ 71 firstPage = Math.max(1, lastPage - 4); 72 } 73 74 // If first page is not 1, show ... 75 if(firstPage > 1) { 76 pagination += '<li><a href="#" data-page="1">1</a></li>'; 77 pagination += '<li><a href="#">...</a></li>'; 78 } 79 80 for (var i = firstPage; i <= lastPage; i++) { 81 if (i === currentPage) { 82 pagination += '<li><a href="#" class="active" data-page="' + i + '">' + i + '</a></li>'; 83 } else { 84 pagination += '<li><a href="#" data-page="' + i + '">' + i + '</a></li>'; 85 } 86 } 87 88 // If last page is not the actual last page, show ... 89 if(lastPage < pages) { 90 pagination += '<li><a href="#">...</a></li>'; 91 pagination += '<li><a href="#" data-page="' + pages + '">' + pages + '</a></li>'; 92 } 93 94 pagination += '</ul>'; 95 pagination += '<a class="order-3 ' + nextDisabled + '" href="#" data-page="' + Math.min(pages, currentPage + 1) + '">'+wpApiSettingsPress.next+'</a>'; 96 } 97 98 $('#pagination').html(pagination); 99 bindPaginationLinks(); 100 } 101 } 102 103 104 function bindPaginationLinks() { 105 $('#pagination a').on('click', function (e) { 106 e.preventDefault(); 107 var page = $(this).data('page'); 108 loadPosts(page); 109 }); 110 } 111 if ($('.filter').length || $('.filter-year').length) { 112 // When the filter changes, load the first page 113 $('.filter, .filter-year').on('change', function () { 114 loadPosts(1); 115 }); 116 } 117 118 // Load the first page when the page loads 119 $(document).ready(function() { 120 if ($('#response').length) { 121 loadPosts(1); 122 } 123 }); 124});
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.