@@ -58,42 +58,11 @@ if (toggleContentNav !== null) {
5858 });
5959}*/
6060
61−function initToggleSearchWindow() {
62− var toggleSearchWindow = document.getElementById('js-search-window-toggle');
63− var bodyEl = document.body;
64− var searchOverlay = document.getElementById('search-overlay');
65− var searchWindow = document.getElementById('search-window');
66− var searchActive = bodyEl.classList.contains('has-active-search-window');
67− var searchInput = document.getElementById('search');
68− if (toggleSearchWindow !== null) {
69− toggleSearchWindow.onclick = function() {
70− //activate search
71− bodyEl.classList.toggle('has-active-search-window');
72− //put input in focus when activating search
73− searchInput.focus();
74− }
75− searchOverlay.addEventListener('click', function(e){
76− if (!searchWindow.contains(e.target)){
77− bodyEl.classList.toggle('has-active-search-window');
78− }
79− })
80− }
81−}
82−
8361//
8462 // hotkeys
8563//
86−function initHotKeys() {
87− /*document.onkeydown = function(evt) {
88− evt = evt || window.event;
89− if (evt.keyCode == 27) {
90− }
9164
92− //when search is active TAB puts search input into focus
93− if (evt.keyCode == 27) {
94− document.body.classList.remove('has-active-search-window');
95− }
96− };*/
65+function initHotKeys() {
9766
9867 var bodyEl = document.body;
9968 var searchInput = document.getElementById('search');
@@ -114,9 +83,7 @@ function initHotKeys() {
11483 if (bodyEl.classList.contains('has-active-search-window')) {
11584 if (key === 'Tab' || key === 'Tab' || key === 9) {
11685 searchInput.focus();
117− console.log('focus');
11886 }
119− console.log('got here');
12087 }
12188 });
12289};
@@ -288,6 +255,7 @@ function formatSearchResultItem(item, terms) {
288255
289256function initSearch() {
290257 var searchInput = document.getElementById("search");
258+
291259 if (!searchInput) {
292260 return;
293261 }
@@ -297,8 +265,6 @@ function initSearch() {
297265 var searchResultsItems = document.querySelector(".search-results__items");
298266 var MAX_ITEMS = 200;
299267
300− searchResultsHeader.value='';
301−
302268 var options = {
303269 bool: "AND",
304270 fields: {
@@ -309,7 +275,34 @@ function initSearch() {
309275 var currentTerm = "";
310276 var index = elasticlunr.Index.load(window.searchIndex);
311277
278+ // toggle search window
279+ var toggleSearchWindow = document.getElementById('js-search-window-toggle');
280+ var bodyEl = document.body;
281+ var searchOverlay = document.getElementById('search-overlay');
282+ var searchWindow = document.getElementById('search-window');
283+ var searchActive = bodyEl.classList.contains('has-active-search-window');
284+ if (toggleSearchWindow !== null) {
285+ toggleSearchWindow.onclick = function() {
286+ //activate search
287+ bodyEl.classList.toggle('has-active-search-window');
288+ //put input in focus when activating search
289+ searchInput.focus();
290+ searchInput.value = "";
291+ searchResultsHeader.value = "";
292+ searchResults.style.display = searchInput.value.trim() === "" ? "none" : "block";
293+ }
294+ searchOverlay.addEventListener('click', function(e){
295+ if (!searchWindow.contains(e.target)){
296+ bodyEl.classList.toggle('has-active-search-window');
297+ }
298+ })
299+ }
300+
301+ // when typing
312302 searchInput.addEventListener("keyup", debounce(function() {
303+
304+ searchResultsHeader.value = "";
305+
313306 var term = searchInput.value.trim();
314307
315308 if (term === currentTerm || !index) {
@@ -347,7 +340,6 @@ if (document.readyState === "complete" ||
347340 initToggleSearchWindow();
348341} else {
349342 document.addEventListener("DOMContentLoaded", function () {
350− initToggleSearchWindow();
351343 initSearch();
352344 initHotKeys();
353345 });