Skip to content

Commit

Permalink
Push searches into the history.
Browse files Browse the repository at this point in the history
blast007 committed Mar 14, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 75a7578 commit c3efc48
Showing 2 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions _pages/search/index.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
permalink: /search/
disableDocScope: true
title: Search
---

{% extends '_layouts/page.html.twig' %}
35 changes: 29 additions & 6 deletions assets/js/search.js
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
doc: {
id: 'id',
field: [
"title",
"content"
'title',
'content'
]
}
});
@@ -50,7 +50,7 @@
}

const regexpStr = '(' + keywords.join('|') + ')';
const regex = new RegExp(regexpStr, "gi");
const regex = new RegExp(regexpStr, 'gi');
return string.replace(regex, '<span>$1</span>');
};

@@ -136,13 +136,22 @@
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}


/**
* Clear search results.
*
*/
function clearResults() {
searchResults.innerHTML = '';
}

/**
* Load search results into the dom.
*
* @param {string} query
*/
function performSearch(query) {
searchResults.innerHTML = '';
clearResults();

if (!query) {
return;
@@ -168,18 +177,32 @@
const searchResults = document.getElementById('search-results');
const searchForm = document.getElementById('search-field');

function pushHistory(query) {
history.pushState({}, 'Search - ' + query + ' - BZFlag', '/search/' + ((query.length)?'?query=' + encodeURIComponent(query):''));
}

let pushHistoryTimeout;
searchForm.addEventListener('input', function (e) {
const query = e.currentTarget.value.trim();

performSearch(query);

clearTimeout(pushHistoryTimeout);
pushHistoryTimeout = setTimeout(pushHistory, 500, query);
});

window.onload = function () {
function pullFromQueryString() {
const query = getParameterByName('query');

if (query) {
searchForm.value = query.trim();
performSearch(searchForm.value);
}
};
else {
performSearch('');
}
}

window.addEventListener('load', pullFromQueryString);
window.addEventListener('popstate', pullFromQueryString);
})();

0 comments on commit c3efc48

Please sign in to comment.