// jQuery File Tree Plugin // // Version 1.01 // // Cory S.N. LaViska // A Beautiful Site (http://abeautifulsite.net/) // 24 March 2008 // // Visit http://abeautifulsite.net/notebook.php?article=58 for more information // // Usage: $('.fileTreeDemo').fileTree( options, callback ) // // Options: root - root folder to display; default = / // script - location of the serverside AJAX file to use; default = jqueryFileTree.php // folderEvent - event to trigger expand/collapse; default = click // expandSpeed - default = 500 (ms); use -1 for no animation // collapseSpeed - default = 500 (ms); use -1 for no animation // expandEasing - easing function to use on expand (optional) // collapseEasing - easing function to use on collapse (optional) // multiFolder - whether or not to limit the browser to one subfolder at a time // loadMessage - Message to display while initial tree loads (can be HTML) // // History: // // 1.01 - updated to work with foreign characters in directory/file names (12 April 2008) // 1.00 - released (24 March 2008) // // TERMS OF USE // // jQuery File Tree is licensed under a Creative Commons License and is copyrighted (C)2008 by Cory S.N. LaViska. // For details, visit http://creativecommons.org/licenses/by/3.0/us/ // var refresh=''; if(jQuery) (function($){ $.extend($.fn, { fileTree: function(o, h) { // Defaults if( !o ) var o = {}; if( o.root == undefined ) o.root = '/'; if( o.script == undefined ) o.script = 'jqueryFileTree.php'; if( o.folderEvent == undefined ) o.folderEvent = 'click'; if( o.expandSpeed == undefined ) o.expandSpeed= 500; if( o.collapseSpeed == undefined ) o.collapseSpeed= 500; if( o.expandEasing == undefined ) o.expandEasing = null; if( o.collapseEasing == undefined ) o.collapseEasing = null; if( o.multiFolder == undefined ) o.multiFolder = true; if( o.loadMessage == undefined ) o.loadMessage = 'Loading...'; $(this).each( function() { function showTree(c, t, showroot) { $(c).addClass('wait'); $(".jqueryFileTree.start").remove(); $.post(o.script, { dir: t, showroot:showroot}, function(data) { $(c).find('.start').html(''); if (data.length>1) { $(c).removeClass('wait').append(data); if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing }); bindTree(c); } else { $(c).removeClass('wait'); $(c).children(0).removeClass('directory').addClass('emptydirectory'); } $("#filetree ul li a.file").contextMenu("filemenu", { bindings: {'modify': function(t) {editfile(o, t.parentNode);}, 'open': function(t) {openfile(t.parentNode);}, 'delete': function(t) {deletefile(o, t.parentNode);}} }); $("#filetree ul li.nosubdir a.directory").contextMenu("directorymenunosub", { bindings: {'rename': function(t) {renamefolder(o, t.parentNode)}, 'add': function(t) {addfolder(o, t.parentNode);}, 'remove': function(t) {removefolder(o, t.parentNode);}, 'addfile': function(t) {addfile(o, t.parentNode);} } }); $("#filetree ul li.issubdir a.directory").contextMenu("directorymenu", { bindings: {'rename': function(t) {renamefolder(o, t.parentNode)}, 'add': function(t) {addfolder(o, t.parentNode);}, 'remove': function(t) {removefolder(o, t.parentNode);}, 'addfile': function(t) {addfile(o, t.parentNode);} } }); $("#filetree ul li a.emptydirectory").contextMenu("directorymenunosub", { bindings: {'rename': function(t) {renamefolder(o, t.parentNode)}, 'add': function(t) {addfolder(o, t.parentNode);}, 'remove': function(t) {removefolder(o, t.parentNode);}, 'addfile': function(t) {addfile(o, t.parentNode);} } }); $("#filetree ul li.issubdir a.rootdirectory").contextMenu("rootdirectorymenu", { bindings: {'add': function(t) {addfolder(o, t.parentNode);}, 'addfile': function(t) {addfile(o, t.parentNode);} } }); $("#filetree ul li.nosubdir a.rootdirectory").contextMenu("rootdirectorymenunosub", { bindings: {'add': function(t) {addfolder(o, t.parentNode);}, 'addfile': function(t) {addfile(o, t.parentNode);} } }); $("#filetree ul li a.rootdirectoryempty").contextMenu("rootdirectorymenunosub", { bindings: {'add': function(t) {addfolder(o, t.parentNode);}, 'addfile': function(t) {addfile(o, t.parentNode);} } }); }); } function bindTree(t) { $(t).find('LI A').bind(o.folderEvent, function() { if( $(this).hasClass('directory') || $(this).hasClass('rootdirectory') || $(this).hasClass('rootdirectoryempty') || $(this).hasClass('emptydirectory')) { if( $(this).hasClass('collapsed') ) { // Expand if( !o.multiFolder ) { $(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing }); $(this).parent().parent().find('A.directory').removeClass('expanded').addClass('collapsed'); } $(this).parent().find('UL').remove(); // cleanup showTree( $(this).parent(), escape ($(this).attr('rel').match( /.*\// )) ); $(this).removeClass('collapsed').addClass('expanded'); } else { // Collapse $(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing }); $(this).removeClass('expanded').addClass('collapsed'); } } else { h($(this).attr('rel')); } return false; }); // Prevent A from triggering the # on non-click events if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; }); } function RefreshFolder(responseText, statusText) { // $('#actionform').empty().append(responseText); if (responseText=='error') { alert('Some error occured...'); } else { $('#filetree').removeClass('hidden'); $('#actionform').addClass('hidden').empty(); li=$(refresh); a=$(refresh).children(0); li.find('UL').remove(); // cleanup showTree(li, escape(a.attr('rel').match( /.*\// )) ); a.removeClass('collapsed').addClass('expanded'); }/**/ } function addfolder(o, folder) { $.post(o.script, { action: 'new', dir:folder.id}, function(data) { $('#filetree').addClass('hidden'); $('#actionform').removeClass('hidden').append(data); refresh=folder; $('#form').ajaxForm({success:RefreshFolder}); if ($(folder).children(0).hasClass('emptydirectory')) $(folder).children(0).removeClass('emptydirectory').addClass('directory'); if ($(folder).children(0).hasClass('rootdirectoryempty')) $(folder).children(0).removeClass('rootdirectoryempty').addClass('rootdirectory'); }); } function editfile(o, file) { $.post(o.script, { action: 'editfile', file:file.id}, function(data) { $('#filetree').addClass('hidden'); $('#actionform').removeClass('hidden').append(data); refresh=$(file).parent().parent(); $("#filesubmit").bind("click", function(){ if ($("#file").val()) { $.ajaxFileUpload({ url:'filetree?action=updatefile', secureuri:false, fileElementId:'file', directoryElementId:'directory', dataType: 'json', success: RefreshFolder }); } else { alert('File is required!'); } }); }); } function addfile(o, folder) { $.post(o.script, { action: 'newfile', dir:folder.id}, function(data) { $('#filetree').addClass('hidden'); $('#actionform').removeClass('hidden').append(data); refresh=folder; $("#filesubmit").bind("click", function(){ if ($("#file").val()) { $.ajaxFileUpload({ url:'filetree?action=insertfile', secureuri:false, fileElementId:'file', directoryElementId:'directory', dataType: 'json', success: RefreshFolder }); } else { alert('File is required!'); } }); }); if ($(folder).children(0).hasClass('emptydirectory')) $(folder).children(0).removeClass('emptydirectory').addClass('directory'); if ($(folder).children(0).hasClass('rootdirectoryempty')) $(folder).children(0).removeClass('rootdirectoryempty').addClass('rootdirectory'); } function renamefolder(o, folder) { $.post(o.script, { action: 'edit', dir:folder.id}, function(data) { $('#filetree').addClass('hidden'); $('#actionform').removeClass('hidden').append(data); refresh=$(folder).parent().parent();; $('#form').ajaxForm({success:RefreshFolder}); }); } function deletefile(o, file) { if (confirm('Do you really want to delete this file?')) { $.post(o.script, { action: 'delfile', file:file.id}, function(data) { li=$(file).parent().parent(); a=$(file).parent().parent().children(0); li.find('UL').remove(); // cleanup showTree(li, escape(a.attr('rel').match( /.*\// )) ); }); } } function removefolder(o, folder) { if (confirm('Do you really want to delete this directory?')) { $.post(o.script, { action: 'del', dir:folder.id}, function(data) { li=$(folder).parent().parent(); a=$(folder).parent().parent().children(0); li.find('UL').remove(); // cleanup showTree(li, escape(a.attr('rel').match( /.*\// )) ); }); } } function openfile(file) { window.open(WWW_URL+category+'/'+$(file).attr('id').replace(WWW_PATH,'')); } // Loading message $(this).html(''); // Get the initial file list showTree( $(this), escape(o.root), 1); }); } }); })(jQuery); function ShowFolder() { $('#filetree').removeClass('hidden'); $('#actionform').addClass('hidden').empty(); }