|
1 | 1 | // Init the search box |
2 | | -$(function(config) { |
3 | | - 'use strict'; |
4 | | - |
5 | | - var applicationId = config.applicationId; |
6 | | - var apiKey = config.apiKey; |
7 | | - var indexName = config.indexName; |
8 | | - |
9 | | - var algolia = algoliasearch(applicationId, apiKey); |
10 | | - var helper = algoliasearchHelper(algolia, indexName); |
11 | | - helper.setQueryParameter('distinct', true); |
12 | | - helper.on('result', onResult); |
13 | | - |
14 | | - // Input listening for queries |
15 | | - var $searchInput = $('.js-algolia__input'); |
16 | | - $searchInput.on('keyup', onQueryChange); |
17 | | - |
18 | | - // Content to hide/show when searching |
19 | | - var $initialContent = $('.js-algolia__initial-content'); |
20 | | - var $searchContent = $('.js-algolia__search-content'); |
21 | | - var $searchContentResults = $searchContent.find('.algolia__results'); |
22 | | - $searchContentResults.on('click', 'a', onLinkClick); |
23 | | - // Rendering templates |
24 | | - var templateResult = Hogan.compile($('#algolia__template').html()); |
25 | | - var templateNoResults = $('#algolia__template--no-results').html(); |
26 | | - |
27 | | - var lastQuery; |
28 | | - |
29 | | - // Toggle result page |
30 | | - function showResults() { |
31 | | - window.scroll(0, 0); |
32 | | - $initialContent.addClass('algolia__initial-content--hidden'); |
33 | | - $searchContent.addClass('algolia__search-content--active'); |
34 | | - |
35 | | - } |
36 | | - function hideResults() { |
37 | | - $initialContent.removeClass('algolia__initial-content--hidden'); |
38 | | - $searchContent.removeClass('algolia__search-content--active'); |
39 | | - } |
40 | | - |
41 | | - // Handle typing query |
42 | | - function onQueryChange() { |
43 | | - lastQuery = $(this).val(); |
44 | | - if (lastQuery.length === 0) { |
45 | | - hideResults(); |
46 | | - return false; |
| 2 | +$(function (config) { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + var applicationId = config.applicationId; |
| 6 | + var apiKey = config.apiKey; |
| 7 | + var indexName = config.indexName; |
| 8 | + |
| 9 | + var algolia = algoliasearch(applicationId, apiKey); |
| 10 | + var helper = algoliasearchHelper(algolia, indexName); |
| 11 | + helper.setQueryParameter('distinct', true); |
| 12 | + helper.on('result', onResult); |
| 13 | + |
| 14 | + // Input listening for queries |
| 15 | + var $searchInput = $('.js-algolia__input'); |
| 16 | + $searchInput.on('keyup', onQueryChange); |
| 17 | + |
| 18 | + // Content to hide/show when searching |
| 19 | + var $initialContent = $('.js-algolia__initial-content'); |
| 20 | + var $searchContent = $('.js-algolia__search-content'); |
| 21 | + var $searchContentResults = $searchContent.find('.algolia__results'); |
| 22 | + $searchContentResults.on('click', 'a', onLinkClick); |
| 23 | + // Rendering templates |
| 24 | + var templateResult = Hogan.compile($('#algolia__template').html()); |
| 25 | + var templateNoResults = $('#algolia__template--no-results').html(); |
| 26 | + |
| 27 | + var lastQuery; |
| 28 | + |
| 29 | + // Toggle result page |
| 30 | + function showResults() { |
| 31 | + window.scroll(0, 0); |
| 32 | + $initialContent.addClass('algolia__initial-content--hidden'); |
| 33 | + $searchContent.addClass('algolia__search-content--active'); |
| 34 | + |
47 | 35 | } |
48 | | - helper.setQuery(lastQuery).search(); |
49 | | - showResults(); |
50 | | - } |
51 | | - |
52 | | - function onResult(data) { |
53 | | - // Avoid race conditions, discard results that do not match the latest query |
54 | | - if (data.query !== lastQuery) { |
55 | | - return false; |
| 36 | + |
| 37 | + function hideResults() { |
| 38 | + $initialContent.removeClass('algolia__initial-content--hidden'); |
| 39 | + $searchContent.removeClass('algolia__search-content--active'); |
56 | 40 | } |
57 | | - var content = data.nbHits ? renderResults(data) : templateNoResults; |
58 | | - $searchContentResults.html(content); |
59 | | - } |
60 | | - |
61 | | - function renderResults(data) { |
62 | | - return $.map(data.hits, function(hit) { |
63 | | - if (hit.posted_at) { |
64 | | - hit.posted_at_readable = moment.unix(hit.posted_at).fromNow(); |
65 | | - } |
66 | | - hit.css_selector = encodeURI(hit.css_selector); |
67 | | - hit.full_url = config.baseurl + hit.url; |
68 | | - |
69 | | - return templateResult.render(hit); |
70 | | - }).join(''); |
71 | | - } |
72 | | - |
73 | | - // Scroll page to correct element |
74 | | - function getAnchorSelector(hash) { |
75 | | - var anchor = hash.substring(1); |
76 | | - if (!anchor.match(/^algolia:/)) { |
77 | | - return false; |
| 41 | + |
| 42 | + // Handle typing query |
| 43 | + function onQueryChange() { |
| 44 | + lastQuery = $(this).val(); |
| 45 | + if (lastQuery.length === 0) { |
| 46 | + hideResults(); |
| 47 | + return false; |
| 48 | + } |
| 49 | + helper.setQuery(lastQuery).search(); |
| 50 | + showResults(); |
78 | 51 | } |
79 | | - return decodeURI(anchor.replace(/^algolia:/, '')); |
80 | | - } |
81 | | - |
82 | | - function scrollPageToSelector(selector) { |
83 | | - var target = $('.page,.post').find(selector); |
84 | | - var targetOffset = target[0].getBoundingClientRect().top + window.pageYOffset - 20; |
85 | | - window.setTimeout(function() { |
86 | | - window.scroll(0, targetOffset); |
87 | | - }, 100); |
88 | | - } |
89 | 52 |
|
90 | | - function onLinkClick(event) { |
91 | | - var selector = getAnchorSelector(event.target.hash); |
92 | | - // Normal link, going to another page |
93 | | - if (event.target.pathname !== window.location.pathname || !selector) { |
94 | | - return true; |
| 53 | + function onResult(data) { |
| 54 | + // Avoid race conditions, discard results that do not match the latest query |
| 55 | + if (data.query !== lastQuery) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + var content = data.nbHits ? renderResults(data) : templateNoResults; |
| 59 | + $searchContentResults.html(content); |
| 60 | + } |
| 61 | + |
| 62 | + function renderResults(data) { |
| 63 | + return $.map(data.hits, function (hit) { |
| 64 | + if (hit.posted_at) { |
| 65 | + hit.posted_at_readable = moment.unix(hit.posted_at).fromNow(); |
| 66 | + } |
| 67 | + hit.css_selector = encodeURI(hit.css_selector); |
| 68 | + hit.full_url = config.baseurl + hit.url; |
| 69 | + |
| 70 | + return templateResult.render(hit); |
| 71 | + }).join(''); |
95 | 72 | } |
96 | | - // Scrolling to a result on the same page |
97 | | - hideResults(); |
98 | | - scrollPageToSelector(selector); |
99 | | - event.preventDefault(); |
100 | | - return false; |
101 | | - } |
102 | | - |
103 | | - window.setTimeout(function() { |
104 | | - var selector = getAnchorSelector(window.location.hash); |
105 | | - if (selector) { |
106 | | - scrollPageToSelector(selector); |
| 73 | + |
| 74 | + // Scroll page to correct element |
| 75 | + function getAnchorSelector(hash) { |
| 76 | + var anchor = hash.substring(1); |
| 77 | + if (!anchor.match(/^algolia:/)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + return decodeURI(anchor.replace(/^algolia:/, '')); |
| 81 | + } |
| 82 | + |
| 83 | + function scrollPageToSelector(selector) { |
| 84 | + var target = $('.page,.post').find(selector); |
| 85 | + var targetOffset = target[0].getBoundingClientRect().top + window.pageYOffset - 20; |
| 86 | + window.setTimeout(function () { |
| 87 | + window.scroll(0, targetOffset); |
| 88 | + }, 100); |
107 | 89 | } |
108 | | - }, 100); |
| 90 | + |
| 91 | + function onLinkClick(event) { |
| 92 | + var selector = getAnchorSelector(event.target.hash); |
| 93 | + // Normal link, going to another page |
| 94 | + if (event.target.pathname !== window.location.pathname || !selector) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + // Scrolling to a result on the same page |
| 98 | + hideResults(); |
| 99 | + scrollPageToSelector(selector); |
| 100 | + event.preventDefault(); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + window.setTimeout(function () { |
| 105 | + var selector = getAnchorSelector(window.location.hash); |
| 106 | + if (selector) { |
| 107 | + scrollPageToSelector(selector); |
| 108 | + } |
| 109 | + }, 100); |
109 | 110 |
|
110 | 111 |
|
111 | 112 | }(window.ALGOLIA_CONFIG)); |
0 commit comments