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

Add Promise support for main menu

Open CodeCollector opened this issue 6 years ago • 4 comments

I would like to build my menu items dynamically through an ajax request. This works for sub-menus, but not for the top level menu.

Inside of op.create I was able to make a slight modification to allow this using the same pattern used for handling the sub-menu promise.

  • Wrapped the "each" call in a function.
  • Check if items.then is a function or not
  • In the finish, assign the items to op.items, call the wrapped "each" call, then call show with 'maintain' since its already been called in the first pass with the right position.

I'm not sure if this will have any unintended side effects, but it seems to be working for my use case.

var createCustom = function (opt) {
// create contextMenu items
$.each(opt.items, function (key, item) {
.....all the existing code just wrapped it in a function
};

if ('function' === typeof opt.items.then) {

                    function completedPromise(opt, items) {
                        // Completed promise (dev called promise.resolve). We now have a list of items which can
                        // be used to create the rest of the context menu.
                        if (typeof items === 'undefined') {
                            // Null result, dev should have checked
                            errorPromise(undefined);//own error object
                        }
                        finishPromiseProcess(opt, items);
                    }

                    function errorPromise(opt, errorItem) {
                        // User called promise.reject() with an error item, if not, provide own error item.
                        if (typeof errorItem === 'undefined') {
                            errorItem = {
                                "error": {
                                    name: "No items and no error item",
                                    icon: "context-menu-icon context-menu-icon-quit"
                                }
                            };
                            if (window.console) {
                                (console.error || console.log).call(console, 'When you reject a promise, provide an "items" object, equal to normal sub-menu items');
                            }
                        } else if (typeof errorItem === 'string') {
                            errorItem = { "error": { name: errorItem } };
                        }
                        finishPromiseProcess(opt, errorItem);
                    }

                    function finishPromiseProcess(opt, items) {
                        opt.items = items;
                        createCustom(opt);
                        op.show.call(opt.$trigger, opt, 'maintain', 'maintain');
                    }
                    
                    opt.items.then(completedPromise.bind(this, opt), errorPromise.bind(this, opt));
                }
                else {
                    createCustom(opt);
                }

CodeCollector avatar Oct 12 '17 20:10 CodeCollector

May I ask if there are any plans on the way to make this possible? this is a much-needed feature which we would love to use as otherwise, the automation of dynamic menu generation gets really complicated.

arashdalir avatar Dec 07 '17 16:12 arashdalir

I would love to see this possible, but i must be honest that i'm not sure when i would make the time to get this up and running.

bbrala avatar Dec 11 '17 12:12 bbrala

@bbrala, I would gladly pair with you on this to make this happen. I spent a while trying to figure it out on my own and convinced myself that someone familiar with the code could greatly accelerate this.

ericprud avatar Dec 15 '18 21:12 ericprud

The explaination in the opening post is pretty clear i feel right now.

Refactor the each function on line 1164 in the source to a callable method, and call that method with the result of the promise or if it is no promise with the opt variable.

bbrala avatar Jan 16 '19 15:01 bbrala