|
|
|
@ -1,43 +1,87 @@
|
|
|
|
|
let searchData;
|
|
|
|
|
function getSearchData() {
|
|
|
|
|
if (searchData) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
resolve(searchData);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return fetch("/search.json")
|
|
|
|
|
.then(res => res.json());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function matcher(q, cb) {
|
|
|
|
|
getSearchData()
|
|
|
|
|
.then(data => {
|
|
|
|
|
cb(Object.keys(data)
|
|
|
|
|
.map(fuzzy_match.bind(null, q))
|
|
|
|
|
.sort((a, b) => b[1] - a[1])
|
|
|
|
|
.slice(0, 5)
|
|
|
|
|
.map(res => {
|
|
|
|
|
const formatted = res[3];
|
|
|
|
|
const url = data[res[2]];
|
|
|
|
|
return `<a href="${url}">${formatted}</a>`;
|
|
|
|
|
}));
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$(document).ready(() => {
|
|
|
|
|
$("[data-toggle='tooltip']").tooltip();
|
|
|
|
|
|
|
|
|
|
$("#search").on("input", () => {
|
|
|
|
|
const results = $("#search-results");
|
|
|
|
|
const q = $("#search").val().trim();
|
|
|
|
|
|
|
|
|
|
if (q.length == 0) {
|
|
|
|
|
results.hide();
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
results.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSearchData()
|
|
|
|
|
.then(data => {
|
|
|
|
|
results.html(Object.keys(data)
|
|
|
|
|
.map(fuzzy_match.bind(null, q))
|
|
|
|
|
.sort((a, b) => b[1] - a[1])
|
|
|
|
|
.slice(0, 5)
|
|
|
|
|
.map(res => {
|
|
|
|
|
const formatted = res[3];
|
|
|
|
|
const url = data[res[2]];
|
|
|
|
|
return `<a href="${url}" class="dropdown-item">${formatted}</a>`;
|
|
|
|
|
})
|
|
|
|
|
.join(""));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$("#search").typeahead({
|
|
|
|
|
hint: true,
|
|
|
|
|
highlight: true,
|
|
|
|
|
minLength: 1
|
|
|
|
|
}, {
|
|
|
|
|
name: "search",
|
|
|
|
|
source: matcher
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let searchData;
|
|
|
|
|
function getSearchData() {
|
|
|
|
|
if (searchData) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
resolve(searchData);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return fetch("/search.json")
|
|
|
|
|
.then(res => res.json());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// let dropdownShown = false;
|
|
|
|
|
// $("#search-results-trigger").on("shown.bs.dropdown", () => {
|
|
|
|
|
// console.log("shown");
|
|
|
|
|
// dropdownShown = true;
|
|
|
|
|
// });
|
|
|
|
|
// $("#search-results-trigger").on("hidden.bs.dropdown", () => {
|
|
|
|
|
// console.log("hidden");
|
|
|
|
|
// dropdownShown = false;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// $("#search").on("input", () => {
|
|
|
|
|
// const results = $("#search-results");
|
|
|
|
|
// const q = $("#search").val().trim();
|
|
|
|
|
|
|
|
|
|
// if (q.length == 0) {
|
|
|
|
|
// if (dropdownShown) {
|
|
|
|
|
// $("#search-results-trigger").dropdown("toggle");
|
|
|
|
|
// $("#search").focus();
|
|
|
|
|
// }
|
|
|
|
|
// // results.hide();
|
|
|
|
|
// return;
|
|
|
|
|
// } else {
|
|
|
|
|
// if (!dropdownShown) {
|
|
|
|
|
// $("#search-results-trigger").dropdown("toggle");
|
|
|
|
|
// $("#search").focus();
|
|
|
|
|
// }
|
|
|
|
|
// // results.show();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// getSearchData()
|
|
|
|
|
// .then(data => {
|
|
|
|
|
// results.html(Object.keys(data)
|
|
|
|
|
// .map(fuzzy_match.bind(null, q))
|
|
|
|
|
// .sort((a, b) => b[1] - a[1])
|
|
|
|
|
// .slice(0, 5)
|
|
|
|
|
// .map(res => {
|
|
|
|
|
// const formatted = res[3];
|
|
|
|
|
// const url = data[res[2]];
|
|
|
|
|
// return `<a href="${url}" class="dropdown-item">${formatted}</a>`;
|
|
|
|
|
// })
|
|
|
|
|
// .join(""));
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Source: https://github.com/forrestthewoods/lib_fts
|
|
|
|
|