2018-01-20 15:11:22 +03:00
|
|
|
root = document.getElementById("main_div");
|
|
|
|
root.innerHTML = `
|
|
|
|
<!-- You can append '?q=query' to the URL to default to a search -->
|
2018-12-24 20:04:13 +03:00
|
|
|
<input id="searchBox" type="text" onkeyup="updateSearch(event)"
|
2018-01-20 15:11:22 +03:00
|
|
|
placeholder="Search for requests and types…" />
|
|
|
|
|
|
|
|
<div id="searchDiv">
|
|
|
|
<div id="exactMatch" style="display:none;">
|
|
|
|
<b>Exact match:</b>
|
|
|
|
<ul id="exactList" class="together">
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
<details id="methods" open><summary class="title">Methods (<span id="methodsCount">0</span>)</summary>
|
2018-01-20 15:11:22 +03:00
|
|
|
<ul id="methodsList" class="together">
|
|
|
|
</ul>
|
|
|
|
</details>
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
<details id="types" open><summary class="title">Types (<span id="typesCount">0</span>)</summary>
|
2018-01-20 15:11:22 +03:00
|
|
|
<ul id="typesList" class="together">
|
|
|
|
</ul>
|
|
|
|
</details>
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
<details id="constructors"><summary class="title">Constructors (<span id="constructorsCount">0</span>)</summary>
|
2018-01-20 15:11:22 +03:00
|
|
|
<ul id="constructorsList" class="together">
|
|
|
|
</ul>
|
|
|
|
</details>
|
|
|
|
</div>
|
|
|
|
<div id="contentDiv">
|
|
|
|
` + root.innerHTML + "</div>";
|
|
|
|
|
|
|
|
// HTML modified, now load documents
|
|
|
|
contentDiv = document.getElementById("contentDiv");
|
|
|
|
searchDiv = document.getElementById("searchDiv");
|
|
|
|
searchBox = document.getElementById("searchBox");
|
|
|
|
|
|
|
|
// Search lists
|
2018-12-16 12:40:31 +03:00
|
|
|
methodsDetails = document.getElementById("methods");
|
2018-01-20 15:11:22 +03:00
|
|
|
methodsList = document.getElementById("methodsList");
|
|
|
|
methodsCount = document.getElementById("methodsCount");
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
typesDetails = document.getElementById("types");
|
2018-01-20 15:11:22 +03:00
|
|
|
typesList = document.getElementById("typesList");
|
|
|
|
typesCount = document.getElementById("typesCount");
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
constructorsDetails = document.getElementById("constructors");
|
2018-01-20 15:11:22 +03:00
|
|
|
constructorsList = document.getElementById("constructorsList");
|
|
|
|
constructorsCount = document.getElementById("constructorsCount");
|
|
|
|
|
|
|
|
// Exact match
|
|
|
|
exactMatch = document.getElementById("exactMatch");
|
|
|
|
exactList = document.getElementById("exactList");
|
|
|
|
|
|
|
|
try {
|
|
|
|
requests = [{request_names}];
|
|
|
|
types = [{type_names}];
|
|
|
|
constructors = [{constructor_names}];
|
|
|
|
|
|
|
|
requestsu = [{request_urls}];
|
|
|
|
typesu = [{type_urls}];
|
|
|
|
constructorsu = [{constructor_urls}];
|
|
|
|
} catch (e) {
|
|
|
|
requests = [];
|
|
|
|
types = [];
|
|
|
|
constructors = [];
|
|
|
|
requestsu = [];
|
|
|
|
typesu = [];
|
|
|
|
constructorsu = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof prependPath !== 'undefined') {
|
|
|
|
for (var i = 0; i != requestsu.length; ++i) {
|
|
|
|
requestsu[i] = prependPath + requestsu[i];
|
|
|
|
}
|
|
|
|
for (var i = 0; i != typesu.length; ++i) {
|
|
|
|
typesu[i] = prependPath + typesu[i];
|
|
|
|
}
|
|
|
|
for (var i = 0; i != constructorsu.length; ++i) {
|
|
|
|
constructorsu[i] = prependPath + constructorsu[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:54:08 +03:00
|
|
|
// Assumes haystack has no whitespace and both are lowercase.
|
2018-12-15 13:03:31 +03:00
|
|
|
//
|
|
|
|
// Returns the penalty for finding the needle in the haystack
|
|
|
|
// or -1 if the needle wasn't found at all.
|
2018-03-22 20:54:08 +03:00
|
|
|
function find(haystack, needle) {
|
2019-03-16 19:17:25 +03:00
|
|
|
if (haystack.indexOf(needle) != -1) {
|
|
|
|
return 0;
|
2018-03-22 20:54:08 +03:00
|
|
|
}
|
|
|
|
var hi = 0;
|
|
|
|
var ni = 0;
|
2018-12-15 13:03:31 +03:00
|
|
|
var penalty = 0;
|
|
|
|
var started = false;
|
2018-03-22 20:54:08 +03:00
|
|
|
while (true) {
|
|
|
|
while (needle[ni] < 'a' || needle[ni] > 'z') {
|
|
|
|
++ni;
|
|
|
|
if (ni == needle.length) {
|
2018-12-15 13:03:31 +03:00
|
|
|
return penalty;
|
2018-03-22 20:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (haystack[hi] != needle[ni]) {
|
|
|
|
++hi;
|
2018-12-15 13:03:31 +03:00
|
|
|
if (started) {
|
|
|
|
++penalty;
|
|
|
|
}
|
2018-03-22 20:54:08 +03:00
|
|
|
if (hi == haystack.length) {
|
2018-12-15 13:03:31 +03:00
|
|
|
return -1;
|
2018-03-22 20:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++hi;
|
|
|
|
++ni;
|
2018-12-15 13:03:31 +03:00
|
|
|
started = true;
|
2018-03-22 20:54:08 +03:00
|
|
|
if (ni == needle.length) {
|
2018-12-15 13:03:31 +03:00
|
|
|
return penalty;
|
2018-03-22 20:54:08 +03:00
|
|
|
}
|
|
|
|
if (hi == haystack.length) {
|
2018-12-15 13:03:31 +03:00
|
|
|
return -1;
|
2018-03-22 20:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 15:11:22 +03:00
|
|
|
// Given two input arrays "original" and "original urls" and a query,
|
|
|
|
// return a pair of arrays with matching "query" elements from "original".
|
|
|
|
//
|
|
|
|
// TODO Perhaps return an array of pairs instead a pair of arrays (for cache).
|
|
|
|
function getSearchArray(original, originalu, query) {
|
|
|
|
var destination = [];
|
|
|
|
var destinationu = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < original.length; ++i) {
|
2018-12-15 13:03:31 +03:00
|
|
|
var penalty = find(original[i].toLowerCase(), query);
|
|
|
|
if (penalty > -1 && penalty < original[i].length / 3) {
|
2018-01-20 15:11:22 +03:00
|
|
|
destination.push(original[i]);
|
|
|
|
destinationu.push(originalu[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [destination, destinationu];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify "countSpan" and "resultList" accordingly based on the elements
|
|
|
|
// given as [[elements], [element urls]] (both with the same length)
|
|
|
|
function buildList(countSpan, resultList, foundElements) {
|
|
|
|
var result = "";
|
|
|
|
for (var i = 0; i < foundElements[0].length; ++i) {
|
|
|
|
result += '<li>';
|
|
|
|
result += '<a href="' + foundElements[1][i] + '">';
|
|
|
|
result += foundElements[0][i];
|
|
|
|
result += '</a></li>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (countSpan) {
|
|
|
|
countSpan.innerHTML = "" + foundElements[0].length;
|
|
|
|
}
|
|
|
|
resultList.innerHTML = result;
|
|
|
|
}
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
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);
|
|
|
|
|
2019-06-15 23:46:30 +03:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
if (event && event.keyCode == 13) {
|
2019-06-15 23:46:30 +03:00
|
|
|
if (destination.length != 0) {
|
|
|
|
window.location = destinationu[0];
|
|
|
|
} else if (methodsDetails.open && foundRequests[1].length) {
|
2018-12-16 12:40:31 +03:00
|
|
|
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];
|
2018-01-20 15:11:22 +03:00
|
|
|
}
|
2018-12-16 12:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-01-20 15:11:22 +03:00
|
|
|
|
2018-12-16 12:40:31 +03:00
|
|
|
buildList(methodsCount, methodsList, foundRequests);
|
|
|
|
buildList(typesCount, typesList, foundTypes);
|
|
|
|
buildList(constructorsCount, constructorsList, foundConstructors);
|
|
|
|
|
|
|
|
// Now look for exact matches
|
|
|
|
if (destination.length == 0) {
|
|
|
|
exactMatch.style.display = "none";
|
2018-01-20 15:11:22 +03:00
|
|
|
} else {
|
2018-12-16 12:40:31 +03:00
|
|
|
exactMatch.style.display = "";
|
|
|
|
buildList(null, exactList, [destination, destinationu]);
|
|
|
|
return destinationu[0];
|
2018-01-20 15:11:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getQuery(name) {
|
|
|
|
var query = window.location.search.substring(1);
|
|
|
|
var vars = query.split("&");
|
|
|
|
for (var i = 0; i != vars.length; ++i) {
|
|
|
|
var pair = vars[i].split("=");
|
|
|
|
if (pair[0] == name)
|
2018-12-15 13:27:58 +03:00
|
|
|
return decodeURI(pair[1]);
|
2018-01-20 15:11:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 15:04:19 +03:00
|
|
|
document.onkeydown = function (e) {
|
|
|
|
if (e.key == '/' || e.key == 's' || e.key == 'S') {
|
|
|
|
if (document.activeElement != searchBox) {
|
|
|
|
searchBox.focus();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (e.key == '?') {
|
|
|
|
alert('Pressing any of: /sS\nWill focus the search bar\n\n' +
|
|
|
|
'Pressing: enter\nWill navigate to the first match')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 15:11:22 +03:00
|
|
|
var query = getQuery('q');
|
|
|
|
if (query) {
|
|
|
|
searchBox.value = query;
|
|
|
|
}
|
|
|
|
|
2018-03-10 13:52:31 +03:00
|
|
|
var exactUrl = updateSearch();
|
|
|
|
var redirect = getQuery('redirect');
|
|
|
|
if (exactUrl && redirect != 'no') {
|
|
|
|
window.location = exactUrl;
|
|
|
|
}
|