Skip to content

[UI] Add namespace to global click event , index.js #15687

Closed
@derekhu

Description

@derekhu

$(document).on('click', (event) => {

  $(document).on('click', (event) => {
    const urlTarget = $(':target');
    if (urlTarget.length === 0) return;

    const urlTargetId = urlTarget.attr('id');
    if (!urlTargetId) return;
    if (!/^(issue|pull)(comment)?-\d+$/.test(urlTargetId)) return;

    const $target = $(event.target);

    if ($target.closest(`#${urlTargetId}`).length === 0) {
      const scrollPosition = $(window).scrollTop();
      window.location.hash = '';
      $(window).scrollTop(scrollPosition);
      window.history.pushState(null, null, ' ');
    }
  });

This block of code performaces different UI experience with Gitlab issue comment.

When I got focus on certain comment, it gets highlight and hash changed ok.
And when click blank place in the page, the hash changes to empty.

However, I want to keep the same experience as Gitlab done: keep the #hash .
I can .off() the global document click event, if the code provides events namespace, and I only have to add custom code in foot.tmpl instead of replace and modify the whole index.js.

eg: index.js

 $(document).on('click.syncemptyhash' , ... )  // or any namespace you like
...

eg:custom,foot.tmpl

<script>
   $(document).off('click.syncemptyhash')
</script>

Activity

zeripath

zeripath commented on May 11, 2021

@zeripath
Contributor

Pop up a PR if you can.

I haven't completely understood what the problem is from your issue report. It would be good if you could give us some clear steps to reproduce the issue using try.gitea.io telling us what you expected instead.

Is it something like:

  1. Go to https://try.gitea.io/owner/repo/issue/6#comment-id
  2. Click anywhere
  3. Note that the hash disappears from the URL bar
  4. I expected the hash to stay
derekhu

derekhu commented on May 11, 2021

@derekhu
Author

Pop up a PR if you can.

I haven't completely understood what the problem is from your issue report. It would be good if you could give us some clear steps to reproduce the issue using try.gitea.io telling us what you expected instead.

Is it something like:

  1. Go to https://try.gitea.io/owner/repo/issue/6#comment-id
  2. Click anywhere
  3. Note that the hash disappears from the URL bar
  4. I expected the hash to stay

yeah, exact

added a commit that references this issue on Aug 27, 2021
wxiaoguang

wxiaoguang commented on Nov 19, 2021

@wxiaoguang
Contributor

I think the behavior can be fixed by a capture event listener.

https://stackoverflow.com/questions/12462721/does-stoppropgation-stop-the-event-from-propagating-in-the-capture-phase

Then we do not need to add event namespaces one by one.

I will close this issue and PR, feel free to feedback.

locked and limited conversation to collaborators on Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zeripath@derekhu@wxiaoguang

        Issue actions

          [UI] Add namespace to global click event , index.js · Issue #15687 · go-gitea/gitea