ELFinderAPI = function(baseURL, params) { var baseURL = (baseURL||'').match(/\/$/) ? baseURL : baseURL + '/', params = (params || {}); this.delete = function(type, path, name) { var path = guess_path(path), name = guess_name(name); var warning = "You are going to delete \"" + name + "\" " + type + ". "; warning += "This action can NOT be undone! Continue?"; if (!confirm(warning)) return false; $.ajax({ type: 'DELETE', url: baseURL, data: { action: type, path: path, name: name }, complete: function(xhr, txtResponse) { if(txtResponse == 'success') { if (type == 'file') { redirect({location: params.location.URL}); } else { if(params.location.URL.match(new RegExp("^" + path + "/" + name)) || (path == params.location.path && name == params.location.name)) redirect({location: path}); else reload(); } } else displayError(xhr); } }); } this.download = function(path, name) { var url = baseURL + build_query_string({ action: 'download', path: guess_path(path), name: guess_name(name) }); $('#ELFinderDownloadIframe').attr('src', url); } this.rename = function(selector) { if (selector) { var element = $(selector); var path = element.attr('data-path'), name = element.attr('data-name'), new_name = element.val(); } else var path, name, new_name = $('#input-rename').val(); $.ajax({ type: 'POST', url: baseURL, data: { action: 'rename', path: guess_path(path), name: guess_name(name), new_name: new_name }, complete: function(xhr, txtResponse) { if(txtResponse == 'success') { if (params.file) { redirect({location: params.location.URL, file: [params.file.path, new_name].join('/')}); } else { if (selector) window.location.reload(); else redirect({location: [params.location.path, new_name].join('/')}); } } else displayError(xhr); } }); } this.save_file = function() { $.ajax({ type: 'POST', url: baseURL, data: { action: 'update', path: params.file.path, name: params.file.name, content: $('#ELFinderEditor').val() }, complete: function(xhr, txtResponse) { if(txtResponse == 'success') ELFinder.alert(params.file.name + ' Successfully Updated'); else displayError(xhr); } }); } this.create = function(type) { var new_name = $('#input-create').val(); $.ajax({ type: 'POST', url: baseURL, data: { action: type, path: params.location.path, name: params.location.name, new_name: new_name }, complete: function(xhr, txtResponse) { if(txtResponse == 'success') reload(); else displayError(xhr); } }); } this.move = function(source, target) { $.ajax({ type: 'POST', url: baseURL, data: { action: 'move', source: source, target: target }, complete: function(xhr, txtResponse) { if(txtResponse == 'success') { var actual = params.location.path + '/' + params.location.name; if(actual.match(new RegExp("^" + source))) redirect({location: target}); else reload(); } else displayError(xhr); } }); } this.loadScript = function(url, checkLoaded, callback) { if (checkLoaded && checkLoaded()) return callback && callback(); var script = document.createElement("script"); script.type = "text/javascript"; script.async = true; if (script.readyState) { script.onreadystatechange = function () { if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback && callback(); } } } else { script.onload = function() { callback && callback(); } } script.src = url; document.head.appendChild(script); } this.loadStylesheet = function(url, checkForClasses) { if (checkForClasses) { if (typeof checkForClasses == "string") checkForClasses = checkForClasses.split(/\s+/g); var hasClass = false, styleSheets = document.styleSheets; for (var x = 0; x < styleSheets.length; x++) { var sheetClasses = styleSheets[x].rules || document.styleSheets[x].cssRules; for (var y = 0; y < sheetClasses.length; y++) { if ( checkForClasses.indexOf(sheetClasses[y].selectorText) > -1 ) { hasClass = true; break; } } } if (hasClass) return true; } var link = document.createElement("link"); link.media = "all"; link.type = "text/css" link.rel = "stylesheet" link.async = true; link.href = url; document.head.appendChild(link); } this.alert = function(msg, timeout) { noty({ text: msg, type: 'information', layout: 'topRight', timeout: timeout || 2000 }); } this.sticky_alert = function(msg) { noty({ text: msg, type: 'information', layout: 'topRight' }); } this.warn = function(msg, timeout) { noty({ text: msg, type: 'warning', layout: 'topRight', timeout: timeout || 3000 }); } this.sticky_warn = function(msg) { noty({ text: msg, type: 'warning', layout: 'topRight' }); } this.error = function(msg) { msg = msg.replace(//gm, '>'). replace(/\n|\r/gm, '
').replace(/\t/gm, '  '); noty({ text: '
' + msg + '
', type: 'warning', layout: 'top', modal: true, closeWith: ['button'], buttons: [ {addClass: 'btn btn-primary', text: 'Close', onClick: function($noty) { $noty.close(); } } ] }); } var redirect = function(_url, _params) { var requestURI = $(location).attr('pathname'); if((typeof _url) == 'object') { params = _url; url = requestURI; } else { params = _params || params; url = _url || requestURI; } window.location = url + build_query_string(params); } var reload = function() { window.location.reload(true); } var build_query_string = function(_params) { var params = _params || params; if(jQuery.isEmptyObject(params)) return ''; var query_string = []; $.each(params, function(k,v) { query_string.push(k + '=' + v) }); return '?' + query_string.join('&'); } var guess_path = function(path) { return (path || (params.file ? params.file.path : params.location.path)); } var guess_name = function(name) { return (name || (params.file ? params.file.name : params.location.name)); } var displayError = function(xhr) { if(xhr.status == 400) ELFinder.warn(xhr.responseText); else if(xhr.status == 500) ELFinder.error(xhr.responseText); } }