From 6839bfcc2d3ad6e638983fba4d53b811d7c15aad Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 16 Dec 2018 10:40:31 +0100 Subject: [PATCH] Redirect to first term when pressing enter on docs search --- telethon_generator/data/html/js/search.js | 92 +++++++++++++---------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/telethon_generator/data/html/js/search.js b/telethon_generator/data/html/js/search.js index 7968ccf8b..ffe9269b4 100644 --- a/telethon_generator/data/html/js/search.js +++ b/telethon_generator/data/html/js/search.js @@ -1,7 +1,7 @@ root = document.getElementById("main_div"); root.innerHTML = ` -
@@ -11,17 +11,17 @@ root.innerHTML = `
-
Methods (0) +
Methods (0)
-
Types (0) +
Types (0)
-
Constructors (0) +
Constructors (0)
@@ -35,12 +35,15 @@ searchDiv = document.getElementById("searchDiv"); searchBox = document.getElementById("searchBox"); // Search lists +methodsDetails = document.getElementById("methods"); methodsList = document.getElementById("methodsList"); methodsCount = document.getElementById("methodsCount"); +typesDetails = document.getElementById("types"); typesList = document.getElementById("typesList"); typesCount = document.getElementById("typesCount"); +constructorsDetails = document.getElementById("constructors"); constructorsList = document.getElementById("constructorsList"); constructorsCount = document.getElementById("constructorsCount"); @@ -153,46 +156,55 @@ function buildList(countSpan, resultList, foundElements) { resultList.innerHTML = result; } -function updateSearch() { - if (searchBox.value) { - contentDiv.style.display = "none"; - searchDiv.style.display = ""; - - var query = searchBox.value.toLowerCase(); - - var foundRequests = getSearchArray(requests, requestsu, query); - var foundTypes = getSearchArray(types, typesu, query); - var foundConstructors = getSearchArray( - constructors, constructorsu, query - ); - - buildList(methodsCount, methodsList, foundRequests); - buildList(typesCount, typesList, foundTypes); - buildList(constructorsCount, constructorsList, foundConstructors); - - // Now look for exact matches - var original = requests.concat(constructors); - var originalu = requestsu.concat(constructorsu); - var destination = []; - var destinationu = []; - - for (var i = 0; i < original.length; ++i) { - if (original[i].toLowerCase().replace("request", "") == query) { - destination.push(original[i]); - destinationu.push(originalu[i]); - } +function updateSearch(event) { + var query = searchBox.value.toLowerCase(); + if (!query) { + contentDiv.style.display = ""; + searchDiv.style.display = "none"; + return; + } + + contentDiv.style.display = "none"; + searchDiv.style.display = ""; + + var foundRequests = getSearchArray(requests, requestsu, query); + var foundTypes = getSearchArray(types, typesu, query); + var foundConstructors = getSearchArray(constructors, constructorsu, query); + + if (event && event.keyCode == 13) { + if (methodsDetails.open && foundRequests[1].length) { + window.location = foundRequests[1][0]; + } else if (typesDetails.open && foundTypes[1].length) { + window.location = foundTypes[1][0]; + } else if (constructorsDetails.open && foundConstructors[1].length) { + window.location = foundConstructors[1][0]; } + return; + } - if (destination.length == 0) { - exactMatch.style.display = "none"; - } else { - exactMatch.style.display = ""; - buildList(null, exactList, [destination, destinationu]); - return destinationu[0]; + buildList(methodsCount, methodsList, foundRequests); + buildList(typesCount, typesList, foundTypes); + buildList(constructorsCount, constructorsList, foundConstructors); + + // Now look for exact matches + var original = requests.concat(constructors); + var originalu = requestsu.concat(constructorsu); + var destination = []; + var destinationu = []; + + for (var i = 0; i < original.length; ++i) { + if (original[i].toLowerCase().replace("request", "") == query) { + destination.push(original[i]); + destinationu.push(originalu[i]); } + } + + if (destination.length == 0) { + exactMatch.style.display = "none"; } else { - contentDiv.style.display = ""; - searchDiv.style.display = "none"; + exactMatch.style.display = ""; + buildList(null, exactList, [destination, destinationu]); + return destinationu[0]; } }