Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 583
Martin Müller Avatar asked May 17 '13 08:05

Martin Müller


2 Answers

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.

like image 157
Martin Müller Avatar answered Oct 23 '22 15:10

Martin Müller


Thanks for the answer, great!

One thing that wasn't clear was how the variables pass through.

So, for anyone else who finds this page....

elFinder.prototype.commands.editpres = function() {
    this.exec = function(hashes) {
        var file = this.files(hashes);
        var hash = file[0].hash;
        var fm = this.fm;
        var url = fm.url(hash);
        var scope = angular.element("body").scope();
        scope.openEditorEdit(url);
    }
    // Getstate configured to only light up button if a file is selected.
    this.getstate = function() {
        var sel = this.files(sel),
        cnt = sel.length;
        return !this._disabled && cnt ? 0 : -1;
    }
}

To get your icon to show up, add the following to your css file:

.elfinder-button-icon-editpres { background:url('../img/icons/editpres.png')  no-repeat; }
like image 35
Jason Maggard Avatar answered Oct 23 '22 14:10

Jason Maggard