Skip to content

Commit

Permalink
Merge pull request #65 from camriddell/enh-materials-search
Browse files Browse the repository at this point in the history
Materials now searchable by page content using filter form on: https://econ-ark.org/materials/
  • Loading branch information
DrDrij authored Mar 7, 2024
2 parents 78e6c3d + f93d663 commit bb6ad25
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions _layouts/materials.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ <h2 class="title"><a href="{{ material.url }}">
</div>

<script>
const table_rows = document.querySelectorAll("tr.material")
const form = document.querySelector("form")
const tags = [...document.querySelectorAll("ul.tags li")].map((v) => { return v.innerText });
const materials = {};
{% for mat in site.materials %}
materials["{{ mat.url }}"] = {
title: "{{ mat.title | normalize_whitespace | replace: '"', "'" }}",
abstract: "{{ mat.abstract | normalize_whitespace | replace: '"', "'" }}",
name: "{{ mat.name | normalize_whitespace | replace: '"', "'" }}",
"remark-name": "{{ mat.remark-name | normalize_whitespace | replace: '"', "'" }}",
content: "{{ mat.content | normalize_whitespace | replace: '"', "'" }}",
authors: [{% for auth in mat.authors %}"{{ auth.given-names }} {{ auth.family-names }}",{% endfor %}],
tags: [{% for t in mat.tags %}"{{ t }}",{% endfor %}],
}
{% endfor %}


const tagSelect = document.getElementById('tagSelect');
const tags = [...document.querySelectorAll("ul.tags li")].map((v) => { return v.innerText });

const form = document.querySelector("form")
form.addEventListener("change", (e) => {
let evt = document.createEvent("Event");
evt.initEvent("submit", true, true);
Expand All @@ -97,33 +109,27 @@ <h2 class="title"><a href="{{ material.url }}">
form.addEventListener("submit", function (e) {
e.preventDefault()
const formData = new FormData(form);

table_rows.forEach(function (row) {
const row_tags = new Set([...row.querySelectorAll("ul.tags li")].map((v) => { return v.innerText }));
const row_title = row.querySelector("h2.title").innerText;
const row_authors = [...row.querySelectorAll("ul.authors li")]
const row_abstract = row.querySelector("p.summary").innerText;

//console.log(row_authors);

const tag_matches = isSuperset(row_tags, formData.getAll("tagSelect"));
const title_matches = row_title.toLowerCase().includes(formData.get("search").toLowerCase());
const author_matches = row_authors.some((li) => {
return li.innerText.toLowerCase().includes(formData.get("search").toLowerCase())
});

console.log(author_matches);

const abstract_matches = row_abstract.toLowerCase().includes(formData.get("search").toLowerCase());
//console.log(title_matches, author_matches, abstract_matches);
if (tag_matches && (title_matches || author_matches || abstract_matches)) {
row.style.display = '';
const searchstr = formData.get("search").toLowerCase();
for (let [key, entry] of Object.entries(materials)) {
const textMatches = [
entry.title.toLowerCase().includes(searchstr),
entry.abstract.toLowerCase().includes(searchstr),
entry.name.toLowerCase().includes(searchstr),
entry["remark-name"].toLowerCase().includes(searchstr),
entry.content.toLowerCase().includes(searchstr),
entry.authors.some((name) => { return name.toLowerCase().includes(searchstr) }),
];

const tagMatches = isSuperset(new Set(entry.tags), tagSelect)
// accounts for accidental duplicate documents
let rows = [...document.querySelectorAll(`a[href="${key}"]`)].map((s) => s.closest('tr'));
if (tagMatches && textMatches.some((x) => x)) {
rows.forEach((elem) => elem.style.display = '');
} else {
row.style.display = 'none';
rows.forEach((elem) => elem.style.display = 'none');
}
});
}
document.documentElement.scrollTop = 0;
return false;
});

function insertTextAndSubmit(event) {
Expand Down Expand Up @@ -162,4 +168,4 @@ <h2 class="title"><a href="{{ material.url }}">

});

</script>
</script>

0 comments on commit bb6ad25

Please sign in to comment.