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

Fix OnContextMenu issue #56 #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FeSuert
Copy link

@FeSuert FeSuert commented Apr 29, 2024

The fix addresses an issue where links generated by the registerPortHandlers function were sometimes incorrect, particularly when right-clicking on them. Here's the breakdown of the changes:

Function Encapsulation: The core change involves encapsulating the logic that builds the link (link = ...) within the event listeners themselves (anchor.addEventListener("click", ...) and anchor.addEventListener("contextmenu", ...)). This is done within a loop iterating over anchor elements using actions.forEach.

Immediate Link Building: By building the link immediately within the event listeners, it ensures that the link generated uses the correct values from the anchor's attributes at the time the click or right-click event occurs.

The original code had a problem due to JavaScript closures. Here's the breakdown of what was happening:

Loop Execution: The loop creating event listeners ran, and the link variable was updated as expected during each iteration.
Closure Created: The anonymous functions created for the click and contextmenu handlers formed closures around the environment in which they were created, including the link variable.
Last Value Used: Since all event handlers were referencing the same link variable, they ended up using the last value it held after the loop finished executing, not the value associated with the specific link that was clicked.
By moving the link creation logic within the event listeners themselves, you ensure that the values of port, protocol, etc. are captured from the correct anchor element at the moment the event is triggered.

@vollkorn1982
Copy link

Hi marcosnils, this is a fix for play-with-docker/play-with-docker.github.io#205

Can I somehow help with the review/approval of this PR?

@vollkorn1982
Copy link

Hi @zapaiamarce and @myitcv maybe you can help us get this fix merged and deployed?

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