Search code examples
javascriptelfinder

Adding a custom context menu item to elFinder


I'm using elfinder and I would like to add new functionality by adding a command to the context menu. I found a solution on the github issue tracker of the project but I can't get it to work. Here's what I do:

var elf;
jQuery().ready(function() {
    elFinder.prototype._options.commands.push('editimage');
    elFinder.prototype._options.contextmenu.files.push('editimage');
    elFinder.prototype.i18.en.messages['cmdeditimage'] = 'Edit Image';          
    elFinder.prototype.i18.de.messages['cmdeditimage'] = 'Bild bearbeiten';
    elFinder.prototype.commands.editimage = function() {
        this.exec = function(hashes) {
             console.log('hallo');
        }
    }
    elf = jQuery('#elfinder').elfinder({
    ...
    //elfinder initialization

The context menu item does not show up, no error message is to be found in the console. I also tried putting editimage under contextmenu->"files" in the init part in case that was overwritten by the initialization.


Solution

  • I found the solution: The examples don't show the fact that you need to have a function called this.getstate inside of the elFinder.prototype.commands.yourcommand function. It shall return 0 when the icon is enabled and -1 when it's disabled.

    So the full code for adding your own menu item or context menu item looks like this:

    var elf;
    jQuery().ready(function() {
    
        elFinder.prototype.i18.en.messages['cmdeditimage'] = 'Edit Image';          
        elFinder.prototype.i18.de.messages['cmdeditimage'] = 'Bild bearbeiten';
        elFinder.prototype._options.commands.push('editimage');
        elFinder.prototype.commands.editimage = function() {
            this.exec = function(hashes) {
                 //do whatever
            }
            this.getstate = function() {
                //return 0 to enable, -1 to disable icon access
                return 0;
            }
        }
        ...
        elf = jQuery('#elfinder').elfinder({
            lang: 'de',             // language (OPTIONAL)
            url : '/ext/elfinder-2.0-rc1/php/connector.php',  //connector URL
            width:'100%',
            uiOptions : {
                // toolbar configuration
                toolbar : [
                    ...
                    ['quicklook', 'editimage'],
                    /*['copy', 'cut', 'paste'],*/
                    ...
                ]},
            contextmenu : {
                ...
                // current directory file menu
                files  : [
                    'getfile', '|','open', 'quicklook', 'editimage', ...
                ]
            }
        }).elfinder('instance');
    
    });
    

    Hope this helps someone with the same problem.