Context.js icon indicating copy to clipboard operation
Context.js copied to clipboard

Menu data reference

Open gilbertoalbino opened this issue 11 years ago • 14 comments

I have been fascinated by the beauty of Context.js. However, it is not clear for me how to get the information from the clicked element. Lets say I have an anchor that has some data attributes like this:

< a data-id="10" data-made="2014" data-type="car" data-color="red" href="#" >Ferrari< /a >

How can I get those data stuff?

I tried to use (this), but it returns the context menu itself.

gilbertoalbino avatar Jul 02 '14 12:07 gilbertoalbino

I would very much like to see a solution to the above problem

AchoArnold avatar Jul 02 '14 15:07 AchoArnold

I'm going to be releasing a new version of Context.js that uses Bootstrap 3 and far cleaner javascript. The API will be more extensive as well. I'll try to release it tonight. Thanks for the kind words!

jakiestfu avatar Jul 02 '14 15:07 jakiestfu

@jakiestfu You said "I'll will try to realease it tonight" but that was 7 days ago man and I haven't seen a release yet

AchoArnold avatar Jul 09 '14 09:07 AchoArnold

Bootstrap is nice. Will there also be standalone version?

altmind avatar Aug 11 '14 18:08 altmind

I created small and ugly fix for that - just save target in dropdown menu data attribute.

Compare and see usage example at https://github.com/altmind/Context.js/compare/altmind:e05571...click-target

Hope there will be a better solution soon.

altmind avatar Aug 11 '14 18:08 altmind

Sorry guys. There has been support for this. Within the action function, reference the this variable to get the element that was clicked.

{
    action: function(){
        var el = this;
    }
}

jakiestfu avatar Aug 11 '14 18:08 jakiestfu

@jakiestfu i suppose, this is misunderstanding. Take a look at screenshot https://www.dropbox.com/s/pdi7afprw6qiqzq/contextjs-this.png

@gilbertoalbino need a way to get a reference to a item, which was right-mose-button-clicked to sdhow context menu. For istance, in screenshot above, he need to get a ref to dom element with text "Empty", not a dom element "Cut". "this" variable points to "Cut", not "Empty"

This issue possibly still need a fix

altmind avatar Aug 11 '14 19:08 altmind

I see, I'm an ass, reopening

jakiestfu avatar Aug 11 '14 19:08 jakiestfu

@jakiestfu you are not an ass.

@jakiestfu and @altmind : I have created a fix for this issue. It now returns the clicked element.

Adding https://github.com/gilbertoalbino/Context.js/blob/master/context.js#L77 And https://github.com/gilbertoalbino/Context.js/blob/master/context.js#L102

Hope this can solve this permanently.

gilbertoalbino avatar Aug 11 '14 20:08 gilbertoalbino

I have created a pull request which fixes the aforementioned problem. @gilbertoalbino I have tried to used your approach but since the master branch implementation is different that the one you are using it was not working.

My current approach exposes a data attribute (selection) in the event that is returned in the action. The selection attribute is actually a function which returns the element that triggered the click to context menu (originated event). Every time the context menu is triggered the selection attribute is updated. Additionally i have added the attribute selection in the context Object so at any point, even out of an action you can get the last element that triggered the context menu. @altmind @najela @gilbertoalbino @jakiestfu Let me know how it works for you guys. Feedback is more than welcome..!

Option A: using data selection from event (e.data.selection())

action: function(e) {
    e.preventDefault();
    $(e.data.selection()).css('background',$(e.target).attr('data-background'));
}

Option B: using context

context.selection();

margaritis avatar Oct 01 '14 13:10 margaritis

@kmargaritis could you make this work in the latest version of the file (which seems to have changed considerably since you did this merge request)?

marc-gist avatar Feb 23 '15 12:02 marc-gist

Sure thing @marc-gist !

margaritis avatar Feb 23 '15 12:02 margaritis

a 'solution' that i found was to just do a:

$('.select-class').each(function() {
    var obj = $(this);
    context.attach(obj, [ ... ]);
});

and inside the menu action function i can reference the obj variable and it works.

EDIT: although this works... when there are a large number, or you need to dynamically change the objects that contain context menus' this approach can hang the browser :(

marc-gist avatar Feb 23 '15 15:02 marc-gist

This would be a great feature.

michaelbilow avatar May 06 '15 18:05 michaelbilow