|
| 1 | +<script> |
| 2 | +// Including InstantSearch.js library and styling |
| 3 | +const loadSearch = function() { |
| 4 | + const loadCSS = function(src) { |
| 5 | + var link = document.createElement('link'); |
| 6 | + link.rel = 'stylesheet'; |
| 7 | + link.type = 'text/css'; |
| 8 | + link.href = src; |
| 9 | + link.media = 'all'; |
| 10 | + document.head.appendChild(link); |
| 11 | + }; |
| 12 | + |
| 13 | + var script = document.createElement('script'); |
| 14 | + script.setAttribute("type", "text/javascript"); |
| 15 | + script.setAttribute("src", "https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.js"); |
| 16 | + script.addEventListener("load", function() { |
| 17 | + // Instantiating InstantSearch.js with Algolia credentials |
| 18 | + const search = instantsearch({ |
| 19 | + appId: '{{ site.algolia.application_id }}', |
| 20 | + apiKey: '{{ site.algolia.search_only_api_key }}', |
| 21 | + indexName: '{{ site.algolia.index_name }}', |
| 22 | + searchParameters: { |
| 23 | + restrictSearchableAttributes: ['title', 'content'] |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + const hitTemplate = function(hit) { |
| 28 | + const url = hit.url; |
| 29 | + const hightlight = hit._highlightResult; |
| 30 | + const title = hightlight.title && hightlight.title.value || ""; |
| 31 | + const content = hightlight.html && hightlight.html.value || ""; |
| 32 | + |
| 33 | + return ` |
| 34 | + <div class="list__item"> |
| 35 | + <article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork"> |
| 36 | + <h2 class="archive__item-title" itemprop="headline"><a href="{{ site.baseurl }}${url}">${title}</a></h2> |
| 37 | + <div class="archive__item-excerpt" itemprop="description">${content}</div> |
| 38 | + </article> |
| 39 | + </div> |
| 40 | + `; |
| 41 | + } |
| 42 | + |
| 43 | + // Adding searchbar and results widgets |
| 44 | + search.addWidget( |
| 45 | + instantsearch.widgets.searchBox({ |
| 46 | + container: '.search-searchbar', |
| 47 | + {% unless site.algolia.powered_by == false %}poweredBy: true,{% endunless %} |
| 48 | + placeholder: '{{ site.data.ui-text[site.locale].search_placeholder_text | default: "Enter your search term..." }}' |
| 49 | + }) |
| 50 | + ); |
| 51 | + search.addWidget( |
| 52 | + instantsearch.widgets.hits({ |
| 53 | + container: '.search-hits', |
| 54 | + templates: { |
| 55 | + item: hitTemplate, |
| 56 | + empty: '{{ site.data.ui-text[site.locale].search_algolia_no_results | default: "No results" }}', |
| 57 | + } |
| 58 | + }) |
| 59 | + ); |
| 60 | + |
| 61 | + if (!search.started) { |
| 62 | + search.start(); |
| 63 | + } |
| 64 | + }); |
| 65 | + document.body.appendChild(script); |
| 66 | + |
| 67 | + loadCSS("https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.css"); |
| 68 | + loadCSS("https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch-theme-algolia.min.css"); |
| 69 | +}; |
| 70 | + |
| 71 | +// Starting the search only when toggle is clicked |
| 72 | +$(document).ready(function() { |
| 73 | + var scriptLoaded = false; |
| 74 | + |
| 75 | + $(".search__toggle").on("click", function() { |
| 76 | + if (!scriptLoaded) { |
| 77 | + loadSearch(); |
| 78 | + scriptLoaded = true; |
| 79 | + } |
| 80 | + }); |
| 81 | +}); |
| 82 | +</script> |
0 commit comments