jquery.treemenu.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. treeMenu - jQuery plugin
  3. version: 0.6
  4. Copyright 2014 Stepan Krapivin
  5. */
  6. (function($){
  7. $.fn.treemenu = function(options) {
  8. options = options || {};
  9. options.delay = options.delay || 0;
  10. options.openActive = options.openActive || false;
  11. options.closeOther = options.closeOther || false;
  12. options.activeSelector = options.activeSelector || ".active";
  13. this.addClass("treemenu");
  14. if (!options.nonroot) {
  15. this.addClass("treemenu-root");
  16. }
  17. options.nonroot = true;
  18. this.find("> li").each(function() {
  19. e = $(this);
  20. var subtree = e.find('> ul');
  21. var button = e.find('.toggler').eq(0);
  22. if(button.length == 0) {
  23. // create toggler
  24. var button = $('<span>');
  25. button.addClass('toggler');
  26. e.prepend(button);
  27. }
  28. if(subtree.length > 0) {
  29. subtree.hide();
  30. e.addClass('tree-closed');
  31. e.find(button).click(function() {
  32. var li = $(this).parent('li');
  33. if (options.closeOther && li.hasClass('tree-closed')) {
  34. var siblings = li.parent('ul').find("li:not(.tree-empty)");
  35. siblings.removeClass("tree-opened");
  36. siblings.addClass("tree-closed");
  37. siblings.removeClass(options.activeSelector);
  38. siblings.find('> ul').slideUp(options.delay);
  39. }
  40. li.find('> ul').slideToggle(options.delay);
  41. li.toggleClass('tree-opened');
  42. li.toggleClass('tree-closed');
  43. li.toggleClass(options.activeSelector);
  44. });
  45. $(this).find('> ul').treemenu(options);
  46. } else {
  47. $(this).addClass('tree-empty');
  48. }
  49. });
  50. if (options.openActive) {
  51. var cls = this.attr("class");
  52. this.find(options.activeSelector).each(function(){
  53. var el = $(this).parent();
  54. while (el.attr("class") !== cls) {
  55. el.find('> ul').show();
  56. if(el.prop("tagName") === 'UL') {
  57. el.show();
  58. } else if (el.prop("tagName") === 'LI') {
  59. el.removeClass('tree-closed');
  60. el.addClass("tree-opened");
  61. el.show();
  62. }
  63. el = el.parent();
  64. }
  65. });
  66. }
  67. return this;
  68. }
  69. })(jQuery);