jQuery-contextMenu
jQuery-contextMenu copied to clipboard
Visible option to item: options argument doesn't include $trigger
Unlike disabled, for example, when defining an item's visible option, the options (second arg) doesn't include the $trigger, which makes this new (and very appreciated!) feature hard to use.
That seems like a good addition indeed.
I know this is not exactly what you are looking for, but there is an options.context which contains the $trigger... might help temporarily...
A workaround is to take advantage of the "icon" function event:
// (...)
items: {
"view": {
name: "Show Data",
icon: function($element, key, item) {
var css = '';
if ($element.context !== undefined) {
// Check context and add CSS class
// that implies "display: none" to "css" variable.
}
return css;
}
},
// (...)
},
// (...)
You can also print the $element
on the browser console ( console.log($element);
) to see other attributes that may be helpful for you.
according to the documentation this issue is fixed, but it still seems broken on 2.1.1. Documentation:
var items = {
firstCommand: {
name: "Copy",
visible: function(key, opt){
// Hide this item if the menu was triggered on a div
if(opt.$trigger.nodeName === 'div'){
return false;
}
}
}
}
Hmm, just scrolled through the source, and it seems it is only populated if you use the 'build' callback. I think i can move it outside that part and make it available pretty much always.
https://github.com/swisnl/jQuery-contextMenu/blob/master/src/jquery.contextMenu.js#L316
- [ ] Move opt.$trigger outside build command.
Any update on this? Any documentation on how to use the 'build' callback?
$.contextMenu({
selector: 'myselector',
build: function($triggerElement, e) {
return {
items: window.buildItems()
}
}
});
still didnt work for me though :(
Hi there. This seems to still be an issue. The documentation on the visible parameter states we can use opt.$trigger just like in the "disabled" parameter, but $trigger isn't defined for the visible parameter. Any idea if this will ever get fixed or is there any other way to access it? Thanks.
Quick followup. I've manually changed the following instruction on lines 328-331
// backreference for custom command type creation
e.data.$trigger = $currentTrigger;
op.create(e.data);
To around line 333, just after the closing bracket of the "if" that begins on line 308.
I didn't make a PR because I'm not exactly sure if this is the correct way to fix this issue. It is now working for my specific use case but it's better for a developer to fix this as its not very well tested and may have some side effects.
Thanks and good job on this plugin.
thanks for the hint. Hopefully i find some time to work on some features sometime soon
It's fixed after I use the build function to get the $trigger
Yeah, but it would be nice to be able to have a consistent acces to trigger elements/events :)