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

Refactored doc-switcher.js #1835

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adamzap
Copy link
Member

@adamzap adamzap commented Dec 18, 2024

  • Simplified code
  • Stopped using jQuery
  • Moved refactored code to app.js

This is the first in a series of patches that will move JavaScript code out of require.js modules and into a single file while also refactoring.

This patch should bring no user-facing changes.

To test, ensure that the functionality of this component on documentation pages still works as expected:

image

If the "Language" or "Documentation version" elements are clicked, it should toggle them remaining open when not hovered over. They will get a green border when activated.

Part of #1827

- Simplified code
- Stopped using jQuery
- Moved refactored code to `app.js`

This is the first in a series of patches that will move JavaScript code
out of `require.js` modules and into a single file while also
refactoring.

This patch should bring no user-facing changes.
Copy link
Member Author

Choose a reason for hiding this comment

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

Should this file be renamed? I wasn't sure what to call it.

@@ -0,0 +1,5 @@
document.querySelectorAll('.doc-switcher li.current').forEach(function (el) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This will run on all pages, but in my testing, it seems to be about twice as fast as the old hasClass method from main.js. So I think we're safe here.

// Make version switcher clickable for touch devices

self.switchers.find('li.current').on('click', function () {
$(this).closest("ul").toggleClass('open');
Copy link
Member Author

@adamzap adamzap Dec 18, 2024

Choose a reason for hiding this comment

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

I used the parent element in the new code instead of trying to find the "closest" ul element. The li elements we are looking for should always be direct children of the ul element that needs the class.

@adamzap
Copy link
Member Author

adamzap commented Dec 18, 2024

I suppose we could try to replace this JavaScript with a details element, but I might rather circle back to that effort later.

Comment on lines +1 to +2
// Toggle persistent display of documentation version and language options if
// the element is clicked
Copy link
Member Author

Choose a reason for hiding this comment

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

Should we keep a comment like this? It may be valuable to have a comment for each bit of behavior that goes into this file. I don't feel strongly about it.

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.

1 participant