Templarian / ui.bootstrap.contextMenu

AngularJS Bootstrap UI Context Menu
MIT License
259 stars 127 forks source link

[suggestion] add event hook before menu is shown #11

Closed elgs closed 9 years ago

elgs commented 9 years ago

I'm hoping there will be an event hook callback just before the context menu is shown. If the event hook returns true (default), the context menu will be shown normally, otherwise, it will do nothing. What I need to do is to do some item selection things before the context menu is shown. So I think adding an event hook will be helpful.

Templarian commented 9 years ago

Like below?

<ANY context-menu="menuOptions" context-menu-before="beforeContextMenu(scopeValue)"></ANY>
$scope.beforeContextMenu = function (scopeValue) {
    if (scopeValue == '') {
        return false;
    }
};
elgs commented 9 years ago

Very close to what I was thinking. May be like this:

<ANY context-menu="menuOptions" context-menu-before="beforeContextMenu"></ANY>
$scope.beforeContextMenu = function ($itemScope) {
    // do whatever you like with the $itemScope, if you don't want the menu to show, return a false;
};
Templarian commented 9 years ago

Couldn't I just make menuOptions optionally allow a function instead and you simply return an empty array for no context-menu?

<ANY context-menu="menuOptions" ></ANY>
$scope.menuOptions = function () {
    // maybe return some custom menu options on click.
    return [];
};
elgs commented 9 years ago

Well, I have two goals: 1, to have a chance to do something before the menu is shown, that's important to me; 2, to decide to show the menu or not based on which item is right click on, that's just nice to have to me;

Changing menuOptions from array to function will perfectly satisfy my first goal, which is important to me. However, it doesn't help with the second goal. So I'm totally ok with changing menuOptions to a function.

Templarian commented 9 years ago

Uhh... you can definitely tell which one was clicked.

<ANY ng-repeat="item in items">
    <ANY context-menu="menuOptions(item)" ></ANY>
</ANY>
$scope.menuOptions = function (item) {
    if (item == 'foo') {
        // maybe return some custom menu options on click.
    }
    return [];
};
elgs commented 9 years ago

Aha, awesome!

Yes, that will be perfect! Thank you.

Templarian commented 9 years ago

This already works in the current version... I just never documented it.

http://jsfiddle.net/cLfpxp05/

Templarian commented 9 years ago

I'll take a minute and document soon. Ha.

Templarian commented 9 years ago

Oh wait, it doesn't handle 0 items. Fixing that now.

Templarian commented 9 years ago

Okay, fixed. Question, should it show the default context menu or still prevent the context menu?

Example: http://jsfiddle.net/c6gba904/

Templarian commented 9 years ago

This was added in v0.9.2.

elgs commented 9 years ago

Thank you so much for adding/documenting this feature. This feature will help me to handle item selection when user right clicks on an item.

I actually have no idea whether to show or prevent the default context menu.

Templarian commented 9 years ago

I've updated the docs with an example. Yea, I think just out right preventing always makes more sense since 99% of the time these will be used in list. Showing the default context menu would be very confusing.

elgs commented 9 years ago

Agreed. Thank you @Templarian. I appreciate your awesome project and fast response to my questions.