`; if (hits.length != 0) { if (renderOptions.results.index === 'defiprime2') { hits = groupBy(hits, 'category|collection'); let indexOfCategory = 0; for (const hitCategory in hits) { template += `
${hitCategory.toString() != 'undefined' ? capitalize(hitCategory) : 'Uncategorized'}
`; template += hits[hitCategory].map((item) => `
${item.title}
${$(item.html).text()}
`).join('') indexOfCategory++; if (indexOfCategory != Object.keys(hits).length) { // prevent inserting on last one template += `
` } } } else if (renderOptions.results.index === 'defiprime2') { hits = groupBy(hits, 'category.name'); let indexOfCategory = 0; for (const hitCategory in hits) { template += `
${hitCategory.toString() != 'undefined' ? capitalize(hitCategory) : 'Uncategorized'}
`; template += hits[hitCategory].map((item) => `
${item.topic.title}
${item.content}
`).join('') indexOfCategory++; if (indexOfCategory != Object.keys(hits).length) { // prevent inserting on last one template += `
` } } } } widgetParams.container.innerHTML = template; }; // Create the custom widget const customHits = instantsearch.connectors.connectHits(renderHits); // Instanciating InstantSearch.js with Algolia credentials const search = instantsearch({ searchFunction: (helper) => { if (helper.state.query.length === 0) { emptyBox = true; return; // do not trigger search } emptyBox = false; helper.search(); // trigger search }, indexName: 'defiprime2', searchClient: algoliasearch('18BDKQYV27', 'adb162fc8601b6a98051de76dd941a6b') }); // Adding searchbar and results widgets search.addWidgets([ instantsearch.widgets.searchBox({ container: '#search-searchbar', placeholder: 'Search Defiprime', showLoadingIndicator: false, poweredBy: true, // This is required if you're on the free Community plan cssClasses: { root: 'defi-searchbox-root', input: 'defi-search-inputbox', reset: 'defi-clear-searchbox', form: 'defi-search-form', submit: 'defi-search-submit', loadingIndicator: 'defi-search-loading' }, }), instantsearch.widgets .index({ indexName: 'defiprime2' }) .addWidgets([ customHits({ container: document.querySelector('#search-hits-defiprime'), }), instantsearch.widgets.poweredBy({ container: '#search-powered-by', }) ]) ]); // Instantiate the custom widget search.addWidgets([ customHits({ container: document.querySelector('#search-hits'), }), instantsearch.widgets.poweredBy({ container: '#search-powered-by', }) ]); // Starting the search search.start(); if (document.querySelector("#custom-searchbox")) { document.querySelector("#custom-searchbox").addEventListener("click", function (e) { this.style.display = 'none'; document.querySelector("#defi-search").style.display = 'block'; document.querySelector(".defi-search-inputbox").click(); }); } var groupBy = function (arr, criteria) { if (!Array.isArray(arr)) { return arr; } return arr.reduce(function (obj, item) { // Check if the criteria is a function to run on the item or a property of it var key = typeof criteria === 'function' ? criteria(item) : item[criteria]; if (criteria.indexOf('|') > -1) { var criterias = criteria.split('|'); for (let subCriteria of criterias) { if (item[subCriteria] != undefined) { var key = item[subCriteria]; break; } } } // If the key doesn't exist yet, create it if (!obj.hasOwnProperty(key)) { obj[key] = []; } // Push the value to the object obj[key].push(item); // Return the object to the next item in the loop return obj; }, {}); }; const capitalize = (s) => { if (typeof s !== 'string') return '' return s.charAt(0).toUpperCase() + s.slice(1) } window.addEventListener('load', () => { //change search icon svg from search-icon.svg $(".defi-search-submit").html('
'); function exitSearchMode() { if (document.querySelector("#custom-searchbox")) { $("#custom-searchbox").show(); $("#defi-search").hide(); } $("#defi-search-fullpage").css('display', 'none'); $("#search-container").css('display', 'none'); $(".defi-search-inputbox").removeClass('searchbox-opened'); $(".defi-search-form").stop(true, true).animate({ maxWidth: '300px' }, 200) $(".defi-search-submit").removeClass("active_magnifying_icon"); $(".defi-clear-searchbox").hide(); $("#search-powered-by").removeClass('inner-powered-by') $("#search-powered-by").removeAttr('style'); } $(".defi-clear-searchbox").click(() => exitSearchMode()); $(document).keyup(function (e) { if (e.key === "Escape") { // escape key maps to keycode `27` exitSearchMode(); } }); $(document).on('click', '.defi-search-inputbox', () => { $('.defi-search-inputbox').addClass('searchbox-opened'); $("#defi-search-fullpage").css('display', 'block'); $("#search-container").css('display', 'block'); $(".defi-search-form").stop(true, true).animate({ maxWidth: '730px' }, 200) $(".defi-search-submit").addClass("active_magnifying_icon"); $(".defi-clear-searchbox").show(); $("#search-powered-by").addClass('inner-powered-by') const rightOffset = ($("#defi-search").width() - $('.defi-search-no-records').width()) / 2 + $('.defi-search-no-records').width() + 24; console.log(window.innerWidth) $("#search-powered-by").css("left", rightOffset - $('#search-powered-by').width() + 'px'); if (window.innerWidth < 480) { $('#search-powered-by').attr('style', function (i, s) { return s + 'top: -24px !important;' }); } }) });
I'm an enthusiast with in-depth knowledge of the topic at hand. Now, let's delve into the concepts used in the provided code snippet related to the Defiprime article.
The code appears to be a part of a web application using Algolia for search functionality. Here's a breakdown of the key concepts:
-
Algolia Search Integration:
- The code integrates Algolia for search functionality.
- Algolia is a search-as-a-service provider, and the code uses their JavaScript API (
algoliasearch
) for indexing and searching.
-
InstantSearch.js:
- The application uses InstantSearch.js, a JavaScript library provided by Algolia to build a search UI.
- It includes a search box, search results, and a "powered by Algolia" label.
-
Custom Widget:
- The code defines a custom widget for displaying search hits.
- The widget is connected to the Algolia index and renders search results based on user queries.
-
Search Interface:
- The search interface includes a search box with specific styling (
defi-searchbox-root
,defi-search-inputbox
, etc.). - There's a custom layout for displaying search hits (
#search-hits-defiprime
).
- The search interface includes a search box with specific styling (
-
Search Functionality:
- The search is triggered when the user enters a query, and the search box is styled for user interaction.
- There's a mechanism to handle empty queries and prevent unnecessary searches.
-
Responsive Design:
- The code includes responsive design elements, adjusting the layout based on the window width.
- Certain elements are hidden or displayed based on screen size.
-
Event Handling:
- Event listeners are used to handle user interactions, such as clicking on the search box or clearing the search.
-
Grouping and Rendering:
- The search results are grouped and rendered based on categories or topics.
- The code includes logic for grouping hits by category or collection and rendering them accordingly.
-
UI Animations:
- The code includes animations for transitioning between search modes.
- CSS classes and animations are applied for a smooth user experience.
In summary, the provided code is a client-side implementation of a search functionality using Algolia's services. It incorporates InstantSearch.js and includes various features for a user-friendly search experience, such as responsive design, custom widgets, and interactive elements.