This repository has been archived on 2020-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
IRProject/ui/search.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-11-20 20:58:45 +00:00
// vim: set ts=2 sw=2 et tw=80:
const solr = "http://localhost:8983/solr/photo";
2020-11-20 22:58:45 +00:00
const q = document.querySelector("#q");
const docs = document.querySelector("#docs");
2020-11-20 20:58:45 +00:00
const templateHTML = document.querySelector("#document").innerHTML;
2020-11-20 22:58:45 +00:00
const urlParams = new URLSearchParams(window.location.search);
const query = urlParams.get('q');
2020-11-20 20:58:45 +00:00
let foamtree = null;
let queryResult = null;
2020-11-20 22:58:45 +00:00
let docMap = {}
2020-11-20 20:58:45 +00:00
function buildCard(docData) {
const emptyOr = s => s ? s : "";
return templateHTML
.replaceAll("{{ id }}", emptyOr(docData.id))
.replaceAll("{{ title }}", emptyOr(docData.t_title))
.replaceAll("{{ author }}", emptyOr(docData.t_author))
.replaceAll("{{ description }}", emptyOr(docData.t_description))
.replaceAll("{{ url }}", emptyOr(docData.img_url))
.replaceAll("{{ site_url }}", "#");
}
2020-11-20 22:58:45 +00:00
function updateResults(docList) {
docs.innerHTML = docList.map(d => {
2020-11-20 20:58:45 +00:00
return buildCard(d);
2020-11-20 22:58:45 +00:00
}).reduce((a, b) => a.trim() + b.trim(), "");
//docs.innerHTML += "<div class='document white'></div>".repeat(28);
}
function filterGroup(info) {
if (info.groups.length == 0) {
updateResults(queryResult.response.docs);
return;
}
const list = [].concat(...info.groups.map(e => e.docs)).map(id => docMap[id]);
updateResults(list);
2020-11-20 20:58:45 +00:00
}
async function buildQuery(e) {
if (e) e.preventDefault();
let list = await fetch(solr + "/clustering?q=" + q.value);
queryResult = await list.json();
console.log(queryResult);
2020-11-20 22:58:45 +00:00
updateResults(queryResult.response.docs);
2020-11-20 20:58:45 +00:00
for (const e of queryResult.response.docs) {
docMap[e.id] = e;
}
let clusters = queryResult.clusters.map(e => {
return {
label: e.labels[0],
weight: e.score,
2020-11-20 22:58:45 +00:00
docs: e.docs,
2020-11-20 20:58:45 +00:00
};
});
2020-11-20 22:58:45 +00:00
2020-11-20 20:58:45 +00:00
if (foamtree === null) {
foamtree = new CarrotSearchFoamTree({
id: "visualization",
layout: "squarified",
groupLabelFontFamily: "Fira Sans",
2020-11-20 22:58:45 +00:00
dataObject: { groups: clusters },
onGroupSelectionChanged: [filterGroup],
2020-11-20 20:58:45 +00:00
});
} else {
foamtree.set({ dataObject: { groups: clusters }});
}
};
if (query) {
q.value = query;
2020-11-20 22:58:45 +00:00
window.addEventListener("load", () => buildQuery().catch(console.error));
2020-11-20 20:58:45 +00:00
}
document.querySelector("#form").addEventListener("submit", buildQuery);
2020-11-20 22:58:45 +00:00
window.addEventListener('resize', () => {
if (foamtree) foamtree.resize()
});