Advertisement
bhengh

jqueryFileTree.js for WordPress

Jul 25th, 2013
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 4.26 KB | None | 0 0
  1. // jQuery File Tree Plugin
  2. //
  3. // Edited for use with WordPress by Ben Miller
  4. // See http://www.benandleanna.com/2013/07/jquery-file-tree-for-wordpress/ for details
  5. //
  6. // Original jQuery plugin by:
  7. // Cory S.N. LaViska
  8. // A Beautiful Site (http://abeautifulsite.net/)
  9. // 24 March 2008
  10. //
  11. // Visit http://abeautifulsite.net/notebook.php?article=58 for more information
  12. //
  13. // Usage: $('.fileTreeDemo').fileTree( options, callback )
  14. //
  15. // Options:  root           - root folder to display; default = /
  16. //           script         - location of the serverside AJAX file to use; default = jqueryFileTree.php
  17. //           folderEvent    - event to trigger expand/collapse; default = click
  18. //           expandSpeed    - default = 500 (ms); use -1 for no animation
  19. //           collapseSpeed  - default = 500 (ms); use -1 for no animation
  20. //           expandEasing   - easing function to use on expand (optional)
  21. //           collapseEasing - easing function to use on collapse (optional)
  22. //           multiFolder    - whether or not to limit the browser to one subfolder at a time
  23. //           loadMessage    - Message to display while initial tree loads (can be HTML)
  24. //           _wpnonce       - WordPress AJAX nonce
  25. //           action         - WordPress AJAX action
  26. //
  27. // History:
  28. //
  29. // 1.01.01 - edited for use with WordPress by Ben Miller (25 July 2013)
  30. // 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
  31. // 1.00 - released (24 March 2008)
  32. //
  33. // TERMS OF USE
  34. //
  35. // This plugin is dual-licensed under the GNU General Public License and the MIT License and
  36. // is copyright 2008 A Beautiful Site, LLC.
  37. //
  38. if(jQuery) (function($){
  39.    
  40.     $.extend($.fn, {
  41.         fileTree: function(o, h) {
  42.             // Defaults
  43.             if( !o ) var o = {};
  44.             if( o.root == undefined ) o.root = '/';
  45.             if( o.script == undefined ) o.script = 'jqueryFileTree.php';
  46.             if( o.folderEvent == undefined ) o.folderEvent = 'click';
  47.             if( o.expandSpeed == undefined ) o.expandSpeed= 500;
  48.             if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
  49.             if( o.expandEasing == undefined ) o.expandEasing = null;
  50.             if( o.collapseEasing == undefined ) o.collapseEasing = null;
  51.             if( o.multiFolder == undefined ) o.multiFolder = true;
  52.             if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';
  53.             if( o._wpnonce == undefined) o._wpnonce = '';
  54.             if( o.action == undefined) o.action = '';
  55.            
  56.             $(this).each( function() {
  57.                
  58.                 function showTree(c, t) {
  59.                     $(c).addClass('wait');
  60.                     $(".jqueryFileTree.start").remove();
  61.                     $.post(o.script, { dir: t, action: o.action, _wpnonce: o._wpnonce }, function(data) {
  62.                         $(c).find('.start').html('');
  63.                         $(c).removeClass('wait').append(data);
  64.                         if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
  65.                         bindTree(c);
  66.                     });
  67.                 }
  68.                
  69.                 function bindTree(t) {
  70.                     $(t).find('LI A').bind(o.folderEvent, function() {
  71.                         if( $(this).parent().hasClass('directory') ) {
  72.                             if( $(this).parent().hasClass('collapsed') ) {
  73.                                 // Expand
  74.                                 if( !o.multiFolder ) {
  75.                                     $(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
  76.                                     $(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
  77.                                 }
  78.                                 $(this).parent().find('UL').remove(); // cleanup
  79.                                 showTree( $(this).parent(), escape($(this).attr('rel').match( /.*\// )) );
  80.                                 $(this).parent().removeClass('collapsed').addClass('expanded');
  81.                             } else {
  82.                                 // Collapse
  83.                                 $(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
  84.                                 $(this).parent().removeClass('expanded').addClass('collapsed');
  85.                             }
  86.                         } else {
  87.                             h($(this).attr('rel'));
  88.                         }
  89.                         return false;
  90.                     });
  91.                     // Prevent A from triggering the # on non-click events
  92.                     if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });
  93.                 }
  94.                 // Loading message
  95.                 $(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
  96.                 // Get the initial file list
  97.                 showTree( $(this), escape(o.root) );
  98.             });
  99.         }
  100.     });
  101.    
  102. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement