Skip to content

Overriding getstate function of command #3269

@mguinness

Description

@mguinness

I had a requirement to limit the cut command to files only, not directories. I knew that I needed to override the getstate function of that command as shown below:

this.getstate = function(select) {
var sel = this.files(select),
cnt = sel.length;
return cnt && $.grep(sel, function(f) { return f.read && ! f.locked && ! fm.isRoot(f) ? true : false; }).length == cnt ? 0 : -1;
};

I struggled how to achieve this without replacing the entire command as shown in Overriding the “Open” Menu Item in elFinder.

Then I came across the question How to change function inside constructor in JavaScript? which allowed me to achieve what I wanted. The solution below can be added to bootCallback function in the configuration options.

(function (old) {
    fm.commands.cut = function () {
        old.apply(this);
        this.getstate = function (select) {
            var sel = this.files(select),
                cnt = sel.length;

            return cnt && $.grep(sel, function (f) {
                return f.mime !== 'directory' && f.read && !f.locked && !fm.isRoot(f) ? true : false;
            }).length == cnt ? 0 : -1;
        };
    };
    var fn = function () { };
    fn.prototype = old.prototype;
    fm.commands.cut.prototype = new fn();
}(fm.commands.cut));

I hope this helps others as it took me a while to figure out. If anyone has a more elegant way to achieve the same result please share!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions