1. #1
    Sencha User
    Join Date
    Oct 2013
    Posts
    2
    Vote Rating
    0
    arkadin is on a distinguished road

      0  

    Default Answered: LiveGrid Stop default browser RightClick action

    Answered: LiveGrid Stop default browser RightClick action


    I'm using 'com.sencha.gxt.widget.core.client.grid.Grid' to display rows of data which works fine. I need to add my own context-menu on right click on every row, which also works fine. In addition to my right click pop up menu, the browser pops up its default 'right-click popup' which I need to stop.

    I did every this possible to logically sink and stop propagation of the mouse up/down events. Still I can't stop default browser action pop-up. Basically grid mouse-up/down event is still bubbled to native browser and it is handled by the browser.

    Please let me mknow how Ic an stop the default browser action.

    Here is my code.

    import com.google.gwt.user.client.DOM;
    import com.google.gwt.user.client.Event;
    import com.sencha.gxt.data.shared.ListStore;
    import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
    import com.sencha.gxt.widget.core.client.grid.ColumnModel;
    import com.sencha.gxt.widget.core.client.grid.Grid;
    public class EventGrid<M> extends Grid<M> {
    public EventGrid()
    {
    super();
    }
    public EventGrid(ListStore<M> store,ColumnModel<M> cm) {
    super(store,cm);
    DOM.sinkEvents(getElement(), Event.ONMOUSEDOWN | Event.ONMOUSEUP);
    }
    public void onBrowserEvent(Event event)
    {
    switch (DOM.eventGetType(event))
    {
    case Event.ONMOUSEUP:
    ConfirmMessageBox box = new ConfirmMessageBox("Confirm", "You right click : Mouse Up?");
    box.show();
    this.sinkEvents(Event.ONMOUSEUP);
    DOM.eventPreventDefault(event);
    event.stopPropagation();
    DOM.sinkEvents(getElement(), Event.ONMOUSEDOWN | Event.ONMOUSEUP);
    break;

    case Event.ONMOUSEDOWN:
    ConfirmMessageBox box1 = new ConfirmMessageBox("Confirm", "You right click : Mouse Down?");
    box1.show();
    super.sinkEvents(Event.ONMOUSEDOWN);
    DOM.eventPreventDefault(event);
    event.stopPropagation();
    event.preventDefault();
    DOM.sinkEvents(getElement(), Event.ONMOUSEDOWN | Event.ONMOUSEUP);
    break;

    case Event.ONCONTEXTMENU:
    ConfirmMessageBox box2 = new ConfirmMessageBox("Confirm", "You right click : onContext Menu?");
    box2.show();
    super.sinkEvents(Event.ONCONTEXTMENU);
    DOM.eventPreventDefault(event);
    event.stopPropagation();
    event.preventDefault();
    break;
    }
    }
    }






  2. This is very confusing code - as far as I can tell, you aren't actually sinking the context menu event until the context menu has *already* happened - which means you aren't ever doing it. It doesn't (in this case) make any sense to sink an event after the event has happened. Unless there is more code you are missing, you have told the grid here that you are *only* interested in mouseup and mousedown, never interested in right click.

    The event.preventDefault() call should be enough to achieve stopping the right click action, provided it is called as the event happens (which from an initial read of your code, it never will be).

    Consider rebuilding this differently:
    * If you are listening for right click to display a menu, consider the setContextMenu instead.
    * If you are overriding onBrowserEvent to handle just the context menu, make sure to sink that event in the constructor. Then, override onRightClick instead, calling event.preventDefault() and event.stopPropagation() during that method (like the built in contextmenu code does).

  3. #2
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,734
    Vote Rating
    90
    Answers
    109
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    This is very confusing code - as far as I can tell, you aren't actually sinking the context menu event until the context menu has *already* happened - which means you aren't ever doing it. It doesn't (in this case) make any sense to sink an event after the event has happened. Unless there is more code you are missing, you have told the grid here that you are *only* interested in mouseup and mousedown, never interested in right click.

    The event.preventDefault() call should be enough to achieve stopping the right click action, provided it is called as the event happens (which from an initial read of your code, it never will be).

    Consider rebuilding this differently:
    * If you are listening for right click to display a menu, consider the setContextMenu instead.
    * If you are overriding onBrowserEvent to handle just the context menu, make sure to sink that event in the constructor. Then, override onRightClick instead, calling event.preventDefault() and event.stopPropagation() during that method (like the built in contextmenu code does).

  4. #3
    Sencha User
    Join Date
    Oct 2013
    Posts
    2
    Vote Rating
    0
    arkadin is on a distinguished road

      0  

    Default


    Hi Colin
    Thanks for the explanation. I understood sinEvents better now. I'm able to get this going with out using sinkEvents. I called 'event.setCancelled(true)'. I don't have to override on BrowserEvent.
    public void MyFunction()
    {
    Menu cxtMenu = createContextMenu();
    grid.setContextMenu(cxtMenu);
    grid.addShowContextMenuHandler(new ShowContextMenuHandler(){
    @Override
    public void onShowContextMenu(ShowContextMenuEvent event) {

    if(myCondition == true) {
    createMyApplicationContextMenu();
    }
    event.setCancelled(true);
    }
    }
    }