PDA

View Full Version : Preventing right click on a context menu



csky
17 Sep 2009, 11:18 AM
I have a context menu the pops up when the user right clicks on a tree node. Now, I am trying to prevent the browser's context menu from appearing if the user should happen to right click on the menu. (Users some times accidentally double click the right mouse button.)

Neither Ext.menu.Menu or Ext.menu.Item have a contextmenu event that I can try to trap. Is there an easy way to do this?

evant
17 Sep 2009, 4:40 PM
You'd need to bind it onto the element, something like:



myMenu.on('show', function(){
myMenu.getEl().on('contextmenu', Ext.emptyFn, null, {stopEvent: true});
}, null, {single: true});

csky
18 Sep 2009, 5:32 AM
That works great. Thanks much.

csky
18 Sep 2009, 6:43 AM
I figured I would make this into a plugin so I don't have to copy the code in multiple places. The following works, but is this the proper style to make a plugin (which I've not done before)?



function DisableContextmenu()
{
this.init = function(menu) {
menu.on('show', function() {
menu.getEl().on('contextmenu', Ext.emptyFn, null, {stopeEvent:true});
}, null, {single:true});
}
}

aconran
18 Sep 2009, 7:39 AM
It looks good; three suggestions:
- Namespace your new plugin class rather than putting it in the global namespace
- Move the init method from an instance method to the prototype of DisableContextmenu.
- small mistype in posted code stopeEvent -> stopEvent

csky
18 Sep 2009, 8:15 AM
It looks good; three suggestions:
- Namespace your new plugin class rather than putting it in the global namespace
- Move the init method from an instance method to the prototype of DisableContextmenu.
- small mistype in posted code stopeEvent -> stopEvent

Thanks for the suggestions. This is what I came up with:



Ext.namespace('Ext.ux.plugin');

Ext.ux.plugin.DisableContenxtMenu = function(){}

Ext.apply( Ext.ux.plugin.DisableContenxtMenu.prototype,
{
init: function(menu)
{
menu.on('show', function()
{
menu.getEl().on('contextmenu', Ext.emptyFn, null, {stopEvent:true});
}, null, {single:true});
}
});

I've always been unsure about when to extending and when to plugin.