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

How can I get the location of the upper-left corner of the context menu - location where I right-clicked #174

Open
nicolaedaian opened this issue Oct 3, 2019 · 3 comments

Comments

@nicolaedaian
Copy link

I use context menu to create some elements on the page so i need the exact location where I right-clicked. The "MouseEvent" parameter is the event when I left-clicked on the context-menu-item.

Please see the attached picture, I need the location indicated by the green arrow not the location indicated by the red arrow.

ngx-context-menu-location

@nicolaedaian
Copy link
Author

nicolaedaian commented Oct 4, 2019

I managed to get the coordinates of the context-menu using the below code, but that is not good enough.

  1. If I right-click close to the edge of the screen, the context menu is displayed in a different position to fit it in the screen. In that case, the top-left position of the context menu is not the point where I right-clicked.
  2. If there are sub-menus I'll get the position of the sub-menu instead of the main-menu
private getContextMenuOrigin = (contextMenuEvent: ExecuteContextMenuEvent): Coord =>
  {
    let menuItem = contextMenuEvent.event.target as HTMLElement;
    let parent = menuItem.parentElement;
    while (parent && !(parent.classList.contains("cdk-overlay-pane") && parent.classList.contains("ngx-contextmenu"))) {
      parent = parent.parentElement;
    }
    if (!(parent.classList.contains("cdk-overlay-pane") && parent.classList.contains("ngx-contextmenu"))) return undefined;

    var boundingRect = parent.getBoundingClientRect();
    return new Coord(boundingRect.left, boundingRect.top);
  }

@nguyentanphu
Copy link

Having the same issue. How can I get a hold of contextmenu event not the item click event. :(

@nguyentanphu
Copy link

@nicolaedaian I manage to get away with this issue by using the contextMenuComponent.event (you can have a ViewChild of it).
Then you can have the event.clientX, event.ClientY.

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

No branches or pull requests

2 participants