Skip to content

Commit

Permalink
Add ability to filter by tags in kiwix serve
Browse files Browse the repository at this point in the history
This change introduces filtering by tags.
To filter, the user can click on the tag name and it will filter it.
A label is added (clickable) to show the tag filter, it can be clicked to remove the filter
  • Loading branch information
juuz0 committed Feb 11, 2022
1 parent 711d324 commit 0c0dad4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions static/skin/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ body {
-webkit-box-orient: vertical;
}

.book__tags--wrapper:hover {
font-weight: bold;
}

.modal-wrapper {
position: fixed;
z-index: 100;
Expand Down Expand Up @@ -423,7 +427,7 @@ body {
@media screen and (max-width: 590px) {

.kiwixNav {
height: 257px;
height: 280px;
}

.kiwixHomeBody {
Expand All @@ -435,7 +439,7 @@ body {
}

.kiwixButton {
margin: 19px 0;
margin: 10px 0;
width: 229px;
}

Expand Down
32 changes: 32 additions & 0 deletions static/skin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,20 @@
}
}

function addTagElement(tagValue) {
const searchFilter = document.getElementById('searchFilter');
const tagElement = document.createElement('button');
tagElement.classList.add('kiwixButton');
tagElement.style.width = 'max-content';
tagElement.style.paddingLeft = tagElement.style.paddingRight = '8px';
tagElement.innerHTML = `🏷 ${tagValue}`;
tagElement.addEventListener('click', (e) => {
searchFilter.parentElement.removeChild(tagElement);
resetAndFilter('tag', '');
});
searchFilter.insertAdjacentElement('afterend', tagElement);
}

window.addEventListener('resize', (event) => {
if (timer) {clearTimeout(timer)}
timer = setTimeout(() => {
Expand Down Expand Up @@ -407,6 +421,21 @@
document.querySelectorAll('.filter').forEach(filter => {
filter.addEventListener('change', () => {resetAndFilter(filter.name, filter.value)});
});
document.querySelectorAll('.book__tags--wrapper').forEach(bookTagElement => {
bookTagElement.addEventListener('click', (e) => {
// prevent <a> tag
if (bookTagElement.contains(e.target)) {
e.preventDefault();
}
let tagValue = bookTagElement.innerText;
filterValue = tagValue.toLowerCase();
filterValue = filterValue.replace(/\s/g, '_');
if (params.get('tag') != filterValue) {
resetAndFilter('tag', filterValue);
addTagElement(tagValue);
}
});
});
if (filters) {
window.history.pushState({}, null, `${window.location.href.split('?')[0]}?${params.toString()}`);
}
Expand All @@ -415,6 +444,9 @@
if (selectBox) {
selectBox.value = value
}
if (key == 'tag' && value.trim() != '') {
addTagElement(humanFriendlyTitle(value));
}
});
document.getElementById('kiwixSearchForm').onsubmit = (event) => {event.preventDefault()};
if (!window.location.search) {
Expand Down

0 comments on commit 0c0dad4

Please sign in to comment.