NTest_Algolia_Search
function safeUrl(url) { if (url == null || typeof url !== 'string') return '#'; var u = url.trim().toLowerCase(); return (u.indexOf('http://') === 0 || u.indexOf('https://') === 0) ? url : '#'; } function safeHref(url) { var u = safeUrl(url); if (u === '#') return '#'; return u.replace(/"/g, '"').replace(/'/g, ''').replace(/0) query = myQuery; const url = 'https://'+ appId+'-dsn.algolia.net/1/indexes/'+indexName+'/query'; // Read operation URL fetch(url, { method: 'POST', // or 'GET' for simpler queries headers: { 'X-Algolia-API-Key': apiKey, 'X-Algolia-Application-Id': appId, 'Content-Type': 'application/json' }, body: JSON.stringify({ query: query, hitsPerPage: _resultsPerPage, page: startIndex, // page starts at 0 highlightPreTag: '', highlightPostTag: '', attributesToSnippet: [ 'content:80' ] }) // Search parameters in the body }) .then(response => response.json()) .then(data => { SearchCompleted(data); }); } var this_response = null; var paginationDone = false; function SearchCompleted(response) { $("#algolia-sr-output").removeClass('algolia-sr-output-disabled'); var html = ""; $("#algolia-sr-output").html(html); $("#algolia-sr-status").html(""); if (response.hits == null || response.hits.length === 0) { // $("#algolia-sr-pagination-output").html(""); // paginationDone = false; $("#algolia-sr-status").html("No matching results found"); $("#algolia-sr-output").html(""); $(".algolia-load-more-btn").attr("hidden", true); // startIndex = 0; $('html, body').animate({ scrollTop: $(".algolia-searchresult-maindiv").offset().top - 180 }, 1000); return; } var no_of_pages = Math.ceil((response.nbHits > (_totalPages * _resultsPerPage) ? (_totalPages * _resultsPerPage > 100 ? 100 : _totalPages * _resultsPerPage) : (response.nbHits > 100 ? 100 : response .nbHits)) / _resultsPerPage); if (response.nbHits == 0) { $('.algolia-load-more-btn').attr('hidden', true); $(".searchpage-pagination").css("display", "none"); } $("#algolia-sr-status").html("About " + addCommas(response.nbHits > 100 ? 100 : response.nbHits) + " results found"); $("[id*=sr-lblPageNumber]").removeClass("sr-current-page"); $("#sr-lblPageNumber" + _pageNumber).addClass("sr-current-page"); for (var i = 0; i < response.hits.length; i++) { var item = response.hits[i]; var thumbnail = ""; if (item.url.match(".pdf$") != null) { thumbnail = '/content/dam/gsusa-redesign/pdf.gif'; } else if (item.url.match(".doc$") != null || item.url.match(".docx$") != null) { thumbnail = '/content/dam/gsusa-redesign/doc.gif'; } else if (item.url.match(".xls$") != null || item.url.match(".xlsx$") != null) { thumbnail = '/content/dam/gsusa-redesign/xls.gif'; } else if (item.url.match(".ppt$") != null || item.url.match(".pptx$") != null) { thumbnail = '/content/dam/gsusa-redesign/ppt.gif'; } var headingClass = "heading-text" + (thumbnail.length == 0 ? " noicon" : ""); html += "
"; html += thumbnail.length > 0 ? "" : ""; html += "" + highlight( q, item.title || '') + ""; html += "" + escapeHtml((item._highlightResult && item._highlightResult.url && item._highlightResult.url.value) ? item._highlightResult.url.value : item.url) + ""; html += "

" + highlight(q, (item._snippetResult && item._snippetResult.content && item._snippetResult.content.value) ? item._snippetResult.content.value : '') + "

"; html += "
"; } var isMobile = $(window).width() <= 767; var no_of_pages='Math.ceil(response.nbHits' _resultsPerPage); Default: hide both $( ).attr( ,="none" true); ).css( ); Case: no results or only 1 page if (response.nbHits="==" 0 || _totalPages="==" 1) { return; hidden already } (isMobile) Mobile: show Load More there are (response.hits && response.hits.length _resultsPerPage) (event_current_val < no_of_pages) ).removeAttr( else loadMore_ajax="1;" reached end Desktop: pagination exist (response.hits.length !="response.nbHits"> 0) { $(".searchpage-pagination").css("display", "block"); } } // Setup pagination plugin $('#algolia-sr-pagination-output').pagination({ total: Math.min( response.nbHits, _totalPages * _resultsPerPage, 100 ), current: event_current_val, length: _resultsPerPage, size: 1, click: function (options, $target) { event_current_val = options.current; $target.next(".show").text('Current: ' + options.current); Search($("#algolia-search-input").val(), event_current_val - 1); } }); // Auto scroll for desktop if (!isMobile && response.hits.length > 0) { $('html, body').animate({ scrollTop: $(".algolia-searchresult-maindiv").offset().top - 180 }, 1000); } // Update results $("#algolia-sr-output").html(html); } var startIndex = 0; $('.algolia-load-more').on("click", function () { startIndex++; $('.algolia-load-more-btn').removeAttr('hidden'); var URLhttp = ''; var html = ""; var _resultsPerPage = $(".resultsPerPage").val() ? $(".resultsPerPage").val() : 10; var _totalPages = $(".totalPages").val() ? $(".totalPages").val() : 10; var apiKey = $('.apiKey').val(); // paid key var indexName = $(".indexName").val(); var appId = $('.appId').val(); var query = 'things'; var searchParams = new URLSearchParams(window.location.search); var myQuery = searchParams.get('q'); if(myQuery.length>0) query = myQuery; const url = 'https://'+ appId+'-dsn.algolia.net/1/indexes/'+indexName+'/query'; // Read operation URL fetch(url, { method: 'POST', // or 'GET' for simpler queries headers: { 'X-Algolia-API-Key': apiKey, 'X-Algolia-Application-Id': appId, 'Content-Type': 'application/json' }, body: JSON.stringify({ query: query, hitsPerPage: _resultsPerPage, page: startIndex, // page starts at 0 highlightPreTag: '', highlightPostTag: '', attributesToSnippet: [ 'content:80' ] }) // Search parameters in the body }) .then(response => response.json()) .then(data => { var $containerDiv = $(".algolia-searchpage-content-div"); var image = '' data.hits.forEach(item => { var thumbnail = ""; if (item.url.match(".pdf$") != null) { thumbnail = '/content/dam/gsusa-redesign/pdf.gif'; } else if (item.url.match(".doc$") != null || item.url .match(".docx$") != null) { thumbnail = '/content/dam/gsusa-redesign/doc.gif'; } else if (item.url.match(".xls$") != null || item.url .match(".xlsx$") != null) { thumbnail = '/content/dam/gsusa-redesign/xls.gif'; } else if (item.url.match(".ppt$") != null || item.url .match(".pptx$") != null) { thumbnail = '/content/dam/gsusa-redesign/ppt.gif'; } var headingClass = "heading-text" + (thumbnail.length == 0 ? " noicon" : ""); html += "
"; html += thumbnail.length > 0 ? "" : ""; html += "" + highlight( q, item.title || '') + ""; html += "" + escapeHtml((item._highlightResult && item._highlightResult.url && item._highlightResult.url.value) ? item._highlightResult.url.value : item.url) + ""; html += "

" + highlight(q, (item._snippetResult && item._snippetResult.content && item._snippetResult.content.value) ? item._snippetResult.content.value : '') + "

"; html += "
"; }); $("#algolia-sr-output").append(html); var no_of_pages = Math.ceil((data.nbHits > ( _totalPages * _resultsPerPage) ? (_totalPages * _resultsPerPage > 100 ? 100 : _totalPages * _resultsPerPage) : (data.nbHits > 100 ? 100 : data.nbHits)) / _resultsPerPage); if (startIndex + 1 >= no_of_pages) { $(".algolia-load-more-btn").attr("hidden", true); startIndex = 0; } else { $(".algolia-load-more-btn").removeAttr("hidden"); } }); }); $("#algolia-search-input").keyup(function (e) { keyword = $('#algolia-search-input').val(); if (keyword.length == 0) { $('.algoliaSearch-txt-clr').addClass('d-none'); } else $('.algoliaSearch-txt-clr').removeClass('d-none'); if (keyword.length > 2) { $(".algolia-search-btn").prop("disabled", false); } else { $(".algolia-search-btn").prop("disabled", true); } if (e.which == 13) { if ($('.algolia-search-btn').prop('disabled') == false) { if($(this).parents(".container.container-404").hasClass("container-404")) { location.href = window.location.origin + "/content/gsusaredesign/us/en/site-search.html?q=" + encodeURIComponent($("#algolia-search-input").val()); return false; } loadMore_ajax = 1; current_val = 1; event_current_val = 0; $(".algolia-searchpage-content-div").html(""); $(".algolia-load-more-btn").attr("hidden", true); $(".searchpage-pagination").css("display", "none"); window.history.replaceState(null, null, '?q=' + encodeURIComponent($("#algolia-search-input").val())); Search(keyword, 0); } } }); $('.algoliaSearch-txt-clr').click(function () { keyword = ''; $('#algolia-search-input').html(''); $(".algolia-search-btn").prop("disabled", true); $(".algolia-searchpage-content-div").html(""); $(".algolia-load-more-btn").attr("hidden", true); $(".searchpage-pagination").css('display', "none"); $(".algolia-results-found-span").html(''); }); var _pageNumber; var q = getParameterByName('q'); if (q == null) q = ""; $(function () { if (q.length > 0) { $("#algolia-search-input").val(q); $(".algolia-search-btn").prop("disabled", false); $('.algoliaSearch-txt-clr').removeClass('d-none'); if($("#algolia-search-input").parents(".container.container-404").hasClass("container-404")) { location.href = window.location.origin + "/content/gsusaredesign/us/en/site-search.html?q=" + encodeURIComponent($("#algolia-search-input").val()); return false; } Search($("#algolia-search-input").val(), 0); } }); }); function displayevent(page) { $("div.searchpage-pagination").each(function () { if ($(this).css('display') === 'block') { $(this).addClass('selected'); } else { $(this).removeClass('selected'); } }); page = parseInt(page) + 1; Search($("#algolia-search-input").val(), page); //$("div.divControl").hide(); for (var k = (page * 2) - 1; k <= (page * 2); k++) { $( +=")" k ).show(); } var event_current_val="0;" < script>