Skip to content

Refactored Javascript in a more modern and modular way#865

Open
smallsaucepan wants to merge 7 commits intoMLTSHP:masterfrom
smallsaucepan:modern-js
Open

Refactored Javascript in a more modern and modular way#865
smallsaucepan wants to merge 7 commits intoMLTSHP:masterfrom
smallsaucepan:modern-js

Conversation

@smallsaucepan
Copy link
Copy Markdown
Contributor

An attempt to refactor much of mltshp's Javascript in a more modern way, perhaps as a stepping stone to using a few web components here and there. Changes are significant, so broken up into different commits to hopefully help reviewing and track changes across files.

Briefly:

  • split a few module-ish classes / structures out into their own module files
  • use singleton modules, closures, and classes in various cases
  • use built in fetch() over jQuery .post() and .fetch() where possible
  • refactor some methods to be async where necessary
  • rename variables and functions according to Javascript conventions
  • import JS into web page as a module (no longer need IIFE to wrap)

Still working through testing, though so far the new code seems to be equivalent. Seeking feedback on the changes to date.

There will need to be a follow up PR to introduce some sort of build step. As is, files included from main.js aren't "versioned" so could lead to CDN / caching issues. Probably easier in the long run to generate a single bundle to import.

… Remainder of changes are prettier standardising on double quotes, indenting, and wrapping a few long lines.
… files in anticipation of turning them into real classes. Not entirely necessary, though should help with making sure decoupled from main code. Code only copied verbatim in this commit.
…rk again. Made minimal changes to deliver equivalent behaviour e.g. singleton vs repeated objects per page, load automatically or on demand.
…g on whether the associated client code is run once, multiple times, or generates concrete objet instances, went with plain module, closures over context variables, or full class definitions. Pulled setCaret and toText out into a common file for reuse.
…e strings. Removed quite a few jQuery .post() and .get() in favour of built in fetch(). Renamed variables to more widely used JS scheme. Switched to async event handlers as a flow on from that in many places.
Copy link
Copy Markdown
Member

@dieseltravis dieseltravis left a comment

Choose a reason for hiding this comment

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

As a future enhancement we should add an editorconfig and/or eslint config to keep our styles consistent with this. I can't view the large diffs on my phone but the small ones I went through look great.

Comment thread static/js/NSFWCover.js
// Per invocation functions that will close over the context dependent
// variables defined above.
function clickShowImage(ev) {
var location = document.location,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we change all the remaining vars to lets?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will definitely do this, and make it an eslint rule. Prob left this as wasn't changing the $.get just yet and trying to minimise diffs.

@smallsaucepan
Copy link
Copy Markdown
Contributor Author

smallsaucepan commented Apr 19, 2026

@bradchoate presuming you're supportive of the direction, how about we create a branch e.g. dev-new-js and merge this PR into that rather than main? That way we can proceed something like:

  1. merge this PR into the dev branch, perhaps just based on desk checking the diffs
  2. as a seperate PR figure out a permanent home for these files, eslint, etc and a simple bundling process
  3. pull in the latest from main and test once from the dev branch
  4. merge back into main and deploy when the time is right

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.

2 participants