Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tag filtering in kiwix serve #711

Merged
merged 8 commits into from
Jun 25, 2022
Merged

Add tag filtering in kiwix serve #711

merged 8 commits into from
Jun 25, 2022

Conversation

juuz0
Copy link
Collaborator

@juuz0 juuz0 commented Feb 11, 2022

Fixes #582

@juuz0 juuz0 requested a review from kelson42 February 11, 2022 16:33
@codecov
Copy link

codecov bot commented Feb 11, 2022

Codecov Report

Merging #711 (45c4cd6) into master (b9e03d2) will not change coverage.
The diff coverage is n/a.

❗ Current head 45c4cd6 differs from pull request most recent head 43ab6df. Consider uploading reports for the commit 43ab6df to get more accurate results

@@           Coverage Diff           @@
##           master     #711   +/-   ##
=======================================
  Coverage   64.21%   64.21%           
=======================================
  Files          59       59           
  Lines        4102     4102           
  Branches     2227     2227           
=======================================
  Hits         2634     2634           
  Misses       1466     1466           
  Partials        2        2           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9e03d2...43ab6df. Read the comment docs.

Copy link
Collaborator

@kelson42 kelson42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the principle, this is what I meant. But we have to improve a few things:

  • "Search" button size is somehow changed now, should be the same size like before
  • The tag link should be a real link, and there should be clear visually that we can click on it
  • One link per tag, it seems it can only take two tags together (if we have two tags)

There is as well a few other visual details, but we can have a look to that later.

@juuz0
Copy link
Collaborator Author

juuz0 commented Feb 13, 2022

The tag link should be a real link, and there should be clear visually that we can click on it

Do you mean the real link as <a> tag?
Note that the whole book div is covered by another <a>, HTML specification doesn't allow nesting of anchor tags

@kelson42
Copy link
Collaborator

kelson42 commented Feb 13, 2022

@juuz0 Any chance we could try to reduce the surface of the tile link? Any way this is not a blocker if we can not have a real link.

@juuz0
Copy link
Collaborator Author

juuz0 commented Feb 13, 2022

@juuz0 Any chance we could try to reduce the surface of the tile link? Any way this is not a blocker if we can not have a real link.

Reducing surface could be neat, I'll try thanks

@juuz0
Copy link
Collaborator Author

juuz0 commented Feb 21, 2022

Rebased too

@kelson42
Copy link
Collaborator

@juuz0 I consider to be an acceptable first version of this feature.
@veloman-yunkan Would you be able please to make a code review?

Copy link
Collaborator

@kelson42 kelson42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
const tagList = tags.split(';').filter(tag => {return !(tag.split(':')[0].startsWith('_'))})
.map((tag) => {return tag.charAt(0).toUpperCase() + tag.slice(1)});
const tagFilterLinks = tagList.map((tagValue) => {
tagValue = tagValue.toLowerCase().replace(/\s/g, '_');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can tags contain whitespace?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the only forbidden char is ; https://wiki.openzim.org/wiki/Tags

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the next question is - if whitespace is replaced with an underscore here, where is the effect of this transformation reversed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in line 442,
using humanFriendlyTitle function

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the transformed tag value infiltrates the link (see the several lines below). I think that's incorrect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I can't fathom how it infiltrates.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me explain it as inline comments in the generateTagLink() function:

function generateTagLink(tagValue) {
    tagValue = tagValue.toLowerCase().replace(/\s/g, '_');
    // From now on tagValue has whitespace replaced with underscores 
    let tempParams = new URLSearchParams(window.location.search);
    tempParams.set('tag', tagValue);
    // In tempParams the parameter 'tag' has a value where whitespace has been replaced with underscores
    const currentLink = `${window.location.href.split('?')[0]}?${tempParams.toString()}`;
    // currentLink was generated with a tag value where whitespace has been replaced with underscores
    // Therefore underscores (not originally present in the tag value) have infiltrated the link. QED.
    return `<a class='tag__link' href='${currentLink}'>${humanFriendlyTitle(tagValue)}</a>`
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's intentional (replacing whitespaces with underscores in the link).
A tag like science in the bath can't be used like: tag=science in the bath. For that, I changed it to tag=science_in_the_bath which works

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tag like science in the bath can't be used like: tag=science in the bath. For that, I changed it to tag=science_in_the_bath which works

Is the treatment of underscores in the tag value a document feature? Which code does implement it? Is there a unit-test for it?

Copy link
Collaborator Author

@juuz0 juuz0 Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I didn't realise the tag value had underscores previously too so .replace(/\s/g, '_'); is useless.
The tag was always science_in_the_bath.
My reasoning for whitespaces is also incorrect, I will remove that whitespace-underscore replacement. Thanks!

static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
@stale
Copy link

stale bot commented Mar 30, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be now be reviewed manually. Thank you for your contributions.

Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the last issue still remaining from the previous iteration (see #711 (comment))

Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This time I played with the code instead of reviewing it. Here are a couple of observations:

  1. When no books match the selection criteria "No result. Would you like to reset filter?" is shown. If filtering by tag was enabled (and the tag label was displayed), clicking the reset filter link doesn't remove the tag label (though filtering by tag gets disabled).
  2. With tag filtering enabled, when I type text in the search box and click the search button, filtering by tag is preserved. However, if I type text in the search box and hit Enter, filtering by tag is removed.
  3. Clicking the tag link resets any language and/or category filters.
  4. History tracking doesn't work well (and that doesn't seem to be restricted to tags)

@kelson42
Copy link
Collaborator

@juuz0 Any feedback?

@veloman-yunkan
Copy link
Collaborator

In fixup commit, I removed all the reset link logic, now it's only a simple link. In the onload handler, we are checking if the link with params is same as current, helps not to push the same thing twice. Seems to work well for all 3 you mentioned

1. open the kiwix-serve welcome/library page

2. click on a book or enter another (unrelated) URL (e.g. openzim.org) in the address bar

3. go back. Once you return to the kiwix-serve library, you cannot navigate forward in browsing history.

Great!

Now you have to make sure that the color of the filter fields is also properly updated during history navigation. For example, consider the following scenario:

  1. Open the library without any filter applied. All fields are white.
  2. Enter some garbage in the search field so that no book matches it. You end up with a gray search field and "No result. Would you like to reset filter" instead of a list of books.
  3. Navigate back. The search field becomes empty but stays gray.
  4. Navigate forward. The text in the search field is restored, but the field becomes white.

@juuz0
Copy link
Collaborator Author

juuz0 commented Jun 24, 2022

Now you have to make sure that the color of the filter fields is also properly updated......

Done ✔️

Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also make the simplification recommended in the first part of #711 (comment) (as a separate clean-up commit) and rebase-fixup this PR for the last iteration.

Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite the history by moving all cleanup & refactoring changes to the beginning.

static/skin/index.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost at the finish line!

Yet, please address the new review comments in fixup commits.

static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
static/skin/index.js Outdated Show resolved Hide resolved
Adds a function to wrap logic to update select boxes on history change
Earlier we were using the full URL, now only query string is passed in pushState, much cleaner!
Previously, if the following steps were executed:
1. Click a book tile/visit an unrelated link from the address bar
2. Press back button
Then forward history was discarded (forward button gets disabled).
This happened because of the window.history.pushState on every window.onload event. This led to the same link being added in history and thus discarding the previous "forward-history"
This change adds a condition to only push the current state if the queries are not same.
This removes the onclick handler around the reset-filter link which redirected to '/?lang='
Everything under the handler was already done on window.onload
Extracted the code from the un-named function in setTimeout for easier understanding.
This is done to retain the button design in more button designs (ex: tags)
Move hover behaviour as a different class - kiwixButtonHover
Add cursor:pointer to kiwixButton
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
Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your patience!

@veloman-yunkan veloman-yunkan merged commit 12e0fb6 into master Jun 25, 2022
@veloman-yunkan veloman-yunkan deleted the tagFilter branch June 25, 2022 14:22
mgautierfr added a commit that referenced this pull request Nov 30, 2022
* [API Break] Remove wrapper around libzim (@mgautierfr #789)
* Allow kiwix-serve to use custom resource files (@veloman-yunkan #779)
* Properly handle searchProtocolPrefix when rendering search result (@veloman-yunkan #823)
* Prevent search on multi language content (@veloman-yunkan #838)
* Use new `zim::Archive::getMediaCount` from libzim (@mgautierfr #836)
* Catalog:
 - Include tags in free text catalog search (@veloman-yunkan #802)
 - Illustration's url is based on book's uuid (@veloman-yunkan #804)
 - Cleanup of the opds-dumper (@veloman-yunkan #829)
 - Allow filtering of catalog content using multiple languages (@veloman-yunkan #841)
 - Make opds-dumper respect the namemapper (@mgautierfr #837)
* Server:
 - Correctly handle `\` in suggestion json generation (@veloman-yunkan #843)
 - Better http caching (@veloman-yunkan #833)
 - Make `/suggest` endpoint thread-safe (@veloman-yunkan #834)
 - Better redirection of main page (@veloman-yunkan #827)
 - Remove jquery (@mgautierfr @juuz0 #796)
 - Better Viewer of zim content :
   . Introduce `/content` endpoints (@veloman-yunkan #806)
   . Switch to iframe based content viewer (@veloman-yunkan #716)
 - Optimised design of the welcome page:
   . Alignement (@juuz0 @kelson42 #786)
   . Exit download modal on pressing escape key (@Juzz0 #800)
   . Add favicon for different devices (@Juzz0 #805)
   . Fix auto hidding of the toolbar (@veloman-yunkan #821)
   . Allow user to filter books by tags in the front page (@juuz0 #711)
* CI :
  - Trigger CI on pull_request (@kelson42 #791)
  - Drop Ubuntu Impish packaging (@legoktm #825)
  - Add Ubuntu Kinetic packaging (@legoktm #801)
* Testing:
  - Test ICULanguageInfo (@veloman-yunkan #795)
  - Introduce fake `test` language to test i18n (@veloman-yunkan #848)
* Fix documentation (@kelson42 #816)
* Udpate translation (#787 #839 #847)
@mgautierfr mgautierfr mentioned this pull request Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to filter by tags in Kiwix-serve welcome page
4 participants