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

Merged
merged 4 commits into from
Dec 20, 2024
Merged

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

I don't have strong feelings, but app.js is a bit meaningless to me, yeah. How about djangoproject.js?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great! I just pushed the change in f3ab40d.

@@ -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.

Copy link
Member

Choose a reason for hiding this comment

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

Comments can be useful for developers but increases the size of the website and page lightness is a priority if we want to be accessible also for people with old devices or slow connection.

What do you think about having a minified version of the JavaScript file with all comments stripped?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if we should start getting refactors in to see how much JavaScript we're going to end up with before making the decision to minify it. I'm confident that it's going to be way less than what's currently on production, and that's not minified.

Just to be clear, are you saying that we should set up django-compressor for app.js?

Also, I'm fine just not having the comment too.

Copy link
Member

Choose a reason for hiding this comment

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

It's an interesting consideration about size of js and slow connections.

My opinion on this is that Adam has removed so many lines of js that a few comments are allowed 😁

django-compressorwould be the solution to this for me, but that's out of scope for this PR I would say.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, an amazing job has been done here. We can think about minified JS in another PR

@bmispelon
Copy link
Member

I've only skimmed the PR, but it seems pretty good so far. Thanks for working on this! 🎸

Could we start by setting up a formatter/linter/ci for js before we start the refactors?

@adamzap
Copy link
Member Author

adamzap commented Dec 19, 2024

@bmispelon Sure. See #1838.

- 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.
@adamzap adamzap force-pushed the refactor-doc-switcher-js branch from f9c0811 to 249c1e6 Compare December 20, 2024 21:48
@adamzap
Copy link
Member Author

adamzap commented Dec 20, 2024

This has been rebased to get 6dc8f41 under it. Let me know about renaming app.js and whether or not we want a comment before the new code.

Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

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

This is looking good to me, I'm going to do a final test on my local version before I merge. Thanks again 👍🏻

@adamzap
Copy link
Member Author

adamzap commented Dec 20, 2024

@bmispelon Thanks! I pushed one tiny edit to the comment!

Copy link
Member

@pauloxnet pauloxnet left a comment

Choose a reason for hiding this comment

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

LGTM

@bmispelon bmispelon merged commit 0e00679 into django:main Dec 20, 2024
4 checks passed
@adamzap adamzap deleted the refactor-doc-switcher-js branch December 20, 2024 23:08
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.

3 participants