jQuery-contextMenu icon indicating copy to clipboard operation
jQuery-contextMenu copied to clipboard

Visible option to item: options argument doesn't include $trigger

Open r02b opened this issue 8 years ago • 12 comments

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.

r02b avatar Jan 03 '16 13:01 r02b

That seems like a good addition indeed.

bbrala avatar Jan 04 '16 10:01 bbrala

I know this is not exactly what you are looking for, but there is an options.context which contains the $trigger... might help temporarily...

arashdalir avatar Feb 11 '16 13:02 arashdalir

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.

mmr99869 avatar May 02 '16 08:05 mmr99869

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;
            }            
        }
    }
}

HaroldStruwig avatar Jun 06 '16 20:06 HaroldStruwig

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.

bbrala avatar Jun 06 '16 20:06 bbrala

Any update on this? Any documentation on how to use the 'build' callback?

HaroldStruwig avatar Jun 27 '16 09:06 HaroldStruwig

$.contextMenu({
     selector: 'myselector',
     build: function($triggerElement, e) {
         return {
             items: window.buildItems()
         }
     }
 });

still didnt work for me though :(

briangsweeney avatar Nov 29 '16 20:11 briangsweeney

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.

ptdev avatar Jul 05 '17 11:07 ptdev

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.

ptdev avatar Jul 05 '17 14:07 ptdev

thanks for the hint. Hopefully i find some time to work on some features sometime soon

bbrala avatar Jul 07 '17 19:07 bbrala

It's fixed after I use the build function to get the $trigger

philoyang avatar Sep 08 '17 06:09 philoyang

Yeah, but it would be nice to be able to have a consistent acces to trigger elements/events :)

bbrala avatar Sep 08 '17 10:09 bbrala