[fix] algoliasearch is not defined (#542)
This commit is contained in:
parent
beb5f3d348
commit
6259ac8e98
|
@ -202,6 +202,7 @@ search:
|
||||||
appId:
|
appId:
|
||||||
apiKey:
|
apiKey:
|
||||||
indexName:
|
indexName:
|
||||||
|
js: https://gcore.jsdelivr.net/algoliasearch/3/algoliasearch.min.js
|
||||||
|
|
||||||
|
|
||||||
######## Comments ########
|
######## Comments ########
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
<script>
|
<script>
|
||||||
window.addEventListener('DOMContentLoaded', (event) => {
|
window.addEventListener('DOMContentLoaded', (event) => {
|
||||||
window.searchConfig = {
|
window.searchConfig = {
|
||||||
appId: '<%- conf.appId %>',
|
appId: '<%- conf.appId %>',
|
||||||
apiKey: '<%- conf.apiKey %>',
|
apiKey: '<%- conf.apiKey %>',
|
||||||
indexName: '<%- conf.indexName %>',
|
indexName: '<%- conf.indexName %>',
|
||||||
hitsPerPage: 100,
|
js: '<%- conf.js %>',
|
||||||
};
|
hitsPerPage: 100,
|
||||||
utils.js('https://gcore.jsdelivr.net/algoliasearch/3/algoliasearch.min.js', { defer: true });
|
};
|
||||||
utils.js('/js/search/algolia-search.js', { defer: true });
|
utils.js('/js/search/algolia-search.js', { defer: true });
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -1,75 +1,77 @@
|
||||||
utils.jq(() => {
|
utils.js(window.searchConfig.js).then(() => {
|
||||||
var $inputArea = $("input#search-input");
|
utils.jq(() => {
|
||||||
if ($inputArea.length === 0) {
|
var $inputArea = $("input#search-input");
|
||||||
return;
|
if ($inputArea.length === 0) {
|
||||||
}
|
|
||||||
|
|
||||||
var $resultArea = $("#search-result");
|
|
||||||
var $searchWrapper = $("#search-wrapper");
|
|
||||||
var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey);
|
|
||||||
var index = client.initIndex(window.searchConfig.indexName);
|
|
||||||
|
|
||||||
function filterResults(hits, filterPath) {
|
|
||||||
if (!filterPath || filterPath === '/') return hits;
|
|
||||||
var regex = new RegExp(filterPath);
|
|
||||||
return hits.filter(hit => regex.test(hit.url));
|
|
||||||
}
|
|
||||||
|
|
||||||
function displayResults(hits) {
|
|
||||||
var $resultList = $("<ul>").addClass("search-result-list");
|
|
||||||
if (hits.length === 0) {
|
|
||||||
$searchWrapper.addClass('noresult');
|
|
||||||
} else {
|
|
||||||
$searchWrapper.removeClass('noresult');
|
|
||||||
hits.forEach(function(hit) {
|
|
||||||
var contentSnippet = hit._snippetResult.content.value;
|
|
||||||
var title = hit.hierarchy.lvl1 || 'Untitled';
|
|
||||||
var $item = $("<li>").html(`<a href="${hit.url}"><span class='search-result-title'>${title}</span><p class="search-result-content">${contentSnippet}</p></a>`);
|
|
||||||
$resultList.append($item);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
$resultArea.html($resultList);
|
|
||||||
}
|
|
||||||
|
|
||||||
$inputArea.on("input", function() {
|
|
||||||
var query = $(this).val().trim();
|
|
||||||
var filterPath = $inputArea.data('filter');
|
|
||||||
|
|
||||||
if (query.length <= 0) {
|
|
||||||
$searchWrapper.attr('searching', 'false');
|
|
||||||
$resultArea.empty();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$searchWrapper.attr('searching', 'true');
|
var $resultArea = $("#search-result");
|
||||||
|
var $searchWrapper = $("#search-wrapper");
|
||||||
|
var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey);
|
||||||
|
var index = client.initIndex(window.searchConfig.indexName);
|
||||||
|
|
||||||
index.search(query, {
|
function filterResults(hits, filterPath) {
|
||||||
hitsPerPage: window.searchConfig.hitsPerPage,
|
if (!filterPath || filterPath === '/') return hits;
|
||||||
attributesToHighlight: ['content'],
|
var regex = new RegExp(filterPath);
|
||||||
attributesToSnippet: ['content:30'],
|
return hits.filter(hit => regex.test(hit.url));
|
||||||
highlightPreTag: '<span class="search-keyword">',
|
|
||||||
highlightPostTag: '</span>',
|
|
||||||
restrictSearchableAttributes: ['content']
|
|
||||||
}).then(function(responses) {
|
|
||||||
displayResults(filterResults(responses.hits, filterPath));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
$inputArea.on("keydown", function(e) {
|
|
||||||
if (e.which == 13) {
|
|
||||||
e.preventDefault();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
var observer = new MutationObserver(function(mutationsList) {
|
function displayResults(hits) {
|
||||||
if (mutationsList.length === 1) {
|
var $resultList = $("<ul>").addClass("search-result-list");
|
||||||
if (mutationsList[0].addedNodes.length) {
|
if (hits.length === 0) {
|
||||||
$searchWrapper.removeClass('noresult');
|
|
||||||
} else if (mutationsList[0].removedNodes.length) {
|
|
||||||
$searchWrapper.addClass('noresult');
|
$searchWrapper.addClass('noresult');
|
||||||
|
} else {
|
||||||
|
$searchWrapper.removeClass('noresult');
|
||||||
|
hits.forEach(function(hit) {
|
||||||
|
var contentSnippet = hit._snippetResult.content.value;
|
||||||
|
var title = hit.hierarchy.lvl1 || 'Untitled';
|
||||||
|
var $item = $("<li>").html(`<a href="${hit.url}"><span class='search-result-title'>${title}</span><p class="search-result-content">${contentSnippet}</p></a>`);
|
||||||
|
$resultList.append($item);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
$resultArea.html($resultList);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
observer.observe($resultArea[0], { childList: true });
|
$inputArea.on("input", function() {
|
||||||
|
var query = $(this).val().trim();
|
||||||
|
var filterPath = $inputArea.data('filter');
|
||||||
|
|
||||||
|
if (query.length <= 0) {
|
||||||
|
$searchWrapper.attr('searching', 'false');
|
||||||
|
$resultArea.empty();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$searchWrapper.attr('searching', 'true');
|
||||||
|
|
||||||
|
index.search(query, {
|
||||||
|
hitsPerPage: window.searchConfig.hitsPerPage,
|
||||||
|
attributesToHighlight: ['content'],
|
||||||
|
attributesToSnippet: ['content:30'],
|
||||||
|
highlightPreTag: '<span class="search-keyword">',
|
||||||
|
highlightPostTag: '</span>',
|
||||||
|
restrictSearchableAttributes: ['content']
|
||||||
|
}).then(function(responses) {
|
||||||
|
displayResults(filterResults(responses.hits, filterPath));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$inputArea.on("keydown", function(e) {
|
||||||
|
if (e.which == 13) {
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var observer = new MutationObserver(function(mutationsList) {
|
||||||
|
if (mutationsList.length === 1) {
|
||||||
|
if (mutationsList[0].addedNodes.length) {
|
||||||
|
$searchWrapper.removeClass('noresult');
|
||||||
|
} else if (mutationsList[0].removedNodes.length) {
|
||||||
|
$searchWrapper.addClass('noresult');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe($resultArea[0], { childList: true });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue