Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikolaifedorov/6174060 to your computer and use it in GitHub Desktop.
Save nikolaifedorov/6174060 to your computer and use it in GitHub Desktop.
Adding Custom Context Menu for elFinder
# ======================================================================
# admin/file_managers.js.coffee
#
# Description:
# contains snippets & functions for handling file-manager-related operation.
# ======================================================================
root = exports ? this
$ ->
# Customize elfinder with new menu
elFinder.prototype._options.commands.push('ftpsend')
elFinder.prototype._options.contextmenu.files.push('ftpsend')
elFinder.prototype.i18.en.messages['cmdftpsend'] = 'Send file using FTP'
# Forge csrf to bypass rails authenticity checking
rails_csrf = {}
rails_csrf[$('meta[name=csrf-param]').attr('content')] = $('meta[name=csrf-token]').attr('content')
# Initiate elfinder
$('#elfinder').elfinder
lang: 'en'
url: '/admin/file-manager/elfinder'
transport: new elFinderSupportVer1()
customData: rails_csrf
# handle ftpsend
$("#dialog-box").on "click", ".js-ftpsend-file", (e) ->
e.preventDefault()
selector = $(this)
# if button is still disabled don't do anything
if selector.is('.disabled')
return false
selector.addClass('disabled')
$.ajax
type: "POST"
url: selector.attr('href')
data: {
"file": $('#file').val(),
"server_id": $('#server_id').val()
}
dataType: 'json'
success: (data, status, xhr) ->
$("#dialog-box.modal").modal("hide")
$("#dialog-box.modal").html("")
root.generateSimpleMessage(data)
error: (xhr, textStatus, errorThrown) ->
selector.removeClass('disabled')
return false
"use strict";
/**
* @class elFinder command "ftpsend"
* Send a file using ftp
*
* @type elFinder.command
* @author Giovanni (gio) Sakti
*/
elFinder.prototype.commands.ftpsend = function() {
var fm = this.fm;
this.getstate = function(sel) {
var sel = this.files(sel),
cnt = sel.length;
return !this._disabled && cnt && fm.cwd().write && $.map(sel, function(f) { return f.phash && f.read ? f : null }).length == cnt ? 0 : -1;
}
this.exec = function(hashes) {
var fm = this.fm,
files = this.files(hashes),
cnt = files.length,
dfrd = $.Deferred()
.fail(function(error) {
error && fm.error(error);
}),
args = [];
if (!cnt || cnt > 1 || this._disabled) {
return dfrd.reject();
}
var file = files[0]
if (!file.read || !fm.file(file.phash).write) {
return !dfrd.reject(['errCopy', file.name, 'errPerm']);
}
if (file.mime == 'directory') {
return dfrd.reject();
}
if (dfrd.state() == 'rejected') {
return dfrd;
}
// Open the dialog box for sending file using FTP
$.ajax({
type: "GET",
url: "/admin/file-manager/ftpsend_form",
data: {
file: fm.path(file.hash).substring(4)
},
dataType: 'json',
success: function(data, status, xhr) {
$("#dialog-box.modal").html(data.html);
return $("#dialog-box.modal").modal("show");
},
error: function(xhr, textStatus, errorThrown) {}
});
return false
// fm.request({
// data : {cmd : 'duplicate', targets : this.hashes(hashes)},
// notify : {type : 'copy', cnt : cnt}
// });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment