johndoh / roundcube-contextmenu

Adds context menus to various parts of Roundcube
https://plugins.roundcube.net/#/packages/johndoh/contextmenu
94 stars 30 forks source link

How to pass an argument to my command/function? #134

Closed Offerel closed 2 years ago

Offerel commented 2 years ago

I try to add a contextmenu for a list of li objects in my plugin. This works so far. The contextmenu comes up and i can click the entries there. After that the specified command is executed. But executing the commands make only sense, if i pass the id from the context to the command. For example the "plugin.myplugin.show" expects one parameter. This should be a integer, which is identical to the id of the <li> element, to where the contextmenu is opened. If i understand it right, i have to use "props", to pass such an argument to my command function. But i dont get it, what i have to enter here. This is my current code:

if(rcmail.env.contextmenu) {
        var cm_menu = rcmail.contextmenu.init({
            menu_name: 'mymenu',
            menu_source: [
                '#mymenu', {
                    label: rcmail.gettext("show", "myplugin"),
                    command: 'plugin.myplugin.show',
                    props: '',
                    classes: 'extwin'
                },{
                    label: rcmail.gettext("edit", "myplugin"),
                    command: 'plugin.myplugin.edit',
                    classes: 'edit'
                },{
                    label: rcmail.gettext("send", "myplugin"),
                    command: 'plugin.myplugin.send',
                    classes: 'send'
                },{
                    label: rcmail.gettext("download", "myplugin"),
                    command: 'plugin.myplugin.download',
                    classes: 'download'
                },{
                    label: rcmail.gettext("delete", "myplugin"),
                    command: 'plugin.myplugin.delete',
                    classes: 'delete'
                }
            ]

        });
johndoh commented 2 years ago

The props property works much like the prop option when doing something like output->button() in the php. It is a string passed through to the core command function.

ret = this.command_handlers[command](props, obj, event); it is what gets put into 'props' in the above. For example something like 'mail' in the switch-task command. The second param which is always passed to the target function (this is part of the rc core code) is obj which is a reference to the source object. There is also the environmental variable rcmail.env.context_menu_source_id which contains the ID of the specific element that the context menu was triggered on.