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

WebUI: Replace getElements & getChildren #22220

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

skomerko
Copy link
Contributor

This PR further reduces Mootools usage.

@skomerko skomerko changed the title WebUI: Replace getElements WebUI: Replace getElements & getChildren Jan 30, 2025
src/webui/www/private/scripts/dynamicTable.js Outdated Show resolved Hide resolved
@@ -490,7 +490,9 @@ window.addEventListener("DOMContentLoaded", () => {
const categoryList = document.getElementById("categoryFilterList");
if (!categoryList)
return;
categoryList.getChildren().each(c => c.destroy());

for (const category of categoryList.querySelectorAll("li"))
Copy link
Member

Choose a reason for hiding this comment

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

Won't this be fragile? Also it looks like it will get grandchildren too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Won't this be fragile?

AFAIK it's fine to use querySelectorAll/static collections to remove elements (unless you meant something else??) Loop won't break because collection is static.

I was concerned about it limits itself to li elements. Previously the code would not care about the type of elements, just direct children. Or do you think it make sense to target specific type of children elements?

Copy link
Member

Choose a reason for hiding this comment

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

Also would it be wrong if you used Element.children property? It is a live HTMLCollection but it shouldn't matter when we only call destroy() on each element. I mean, the live collection iterator shouldn't be invalidated with this kind of operation, unless destroy() is doing more than expected...

Copy link
Contributor Author

@skomerko skomerko Jan 31, 2025

Choose a reason for hiding this comment

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

  1. We can do something like this:

for (const category of [...categoryList.children])

  1. but we can't do this:

for (const category of categoryList.children)

I mean, the live collection iterator shouldn't be invalidated with this kind of operation, unless destroy() is doing more than expected...

destroy() detaches child from the parent and that causes children collection to be updated. For example, this is what happens if we add category in second case (it's all mangled):

image

I was concerned about it limits itself to li elements. Previously the code would not care about the type of elements, just direct children. Or do you think it make sense to target specific type of children elements?

No, I think you are right in that it should be generic. Should I change everything to use 1)? Or maybe keep querySelectorAll and do something like this container.querySelectorAll(":scope > *")? Which one do you prefer?

Copy link
Member

@Chocobo1 Chocobo1 Feb 1, 2025

Choose a reason for hiding this comment

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

destroy() detaches child from the parent and that causes children collection to be updated. For example, this is what happens if we add category in second case (it's all mangled):

Thanks for confirming!

Which one do you prefer?

I would go with the first option.
Also, it could be written like this to make it one line: Array.from().forEach();

src/webui/www/private/scripts/contextmenu.js Outdated Show resolved Hide resolved
src/webui/www/private/scripts/dynamicTable.js Outdated Show resolved Hide resolved
src/webui/www/private/scripts/dynamicTable.js Outdated Show resolved Hide resolved
@Chocobo1 Chocobo1 added the WebUI WebUI-related issues/changes label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WebUI WebUI-related issues/changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants