dtree.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. * jquery tree menu
  3. * dTree.js v1.0.0
  4. * mefe@mefe.net
  5. * 2014-09-03
  6. *
  7. */
  8. (function($) {
  9. $.fn.dTree = function(options) {
  10. var defaults = {
  11. closeSameLevel: true,
  12. useCookie: true,
  13. };
  14. var settings = $.extend({}, defaults, options);
  15. //.css({"color":"red","border":"2px solid red"});
  16. return this.each(function() {
  17. var $ul = $(this).find('ul');
  18. var $li = $(this).find('li');
  19. var $folder = $li.has("ul");
  20. $folder.prepend("<span class=\"plus\"></span><span class=\"folder\"></span>");
  21. $li.not($folder).prepend("<span class=\"join\"></span><span class=\"page-tree\"></span>");
  22. $ul.parent("li").addClass("folder-group");
  23. $ul.children('li:last-child').not($folder).addClass("join-last");
  24. $.fn.dTree.tree_first_element($li.first());
  25. $ul.children('li.folder-group:last-child').addClass("last");
  26. if (settings.useCookie && $.fn.dTree.check_cookie("dTree")) {
  27. var object_index = JSON.parse($.fn.dTree.get_cookie("dTree"));
  28. $.each(object_index, function(key, value) {
  29. $this = $ul.find("li.folder-group").eq(value);
  30. $.fn.dTree.set_icons($this.children('span:first'));
  31. $this.children('ul:first').toggle();
  32. });
  33. } else if ($li.hasClass("active")) {
  34. $active = $ul.find("li.folder-group.active");
  35. $active.each(function() {
  36. $.fn.dTree.set_icons($(this).children('span:first'));
  37. $(this).children('ul:first').toggle();
  38. });
  39. $active.parentsUntil("div", ".folder-group").each(function() {
  40. $.fn.dTree.set_icons($(this).children('span:first'));
  41. $(this).children('ul:first').toggle();
  42. });
  43. }
  44. $(this).on('click', '.plus, .minus', function() {
  45. if (settings.useCookie) {
  46. var obect_index = [];
  47. $(this).parentsUntil("div", ".folder-group").each(function() {
  48. obect_index.push($(this).index(".folder-group"));
  49. });
  50. $.fn.dTree.set_cookie("dTree", JSON.stringify(obect_index));
  51. }
  52. if (settings.closeSameLevel) {
  53. $.fn.dTree.close_same_level($(this));
  54. }
  55. $.fn.dTree.set_icons($(this));
  56. $(this).parent().children('ul:first').toggle(250);
  57. });
  58. });
  59. };
  60. $.fn.dTree.set_cookie = function(name, value) {
  61. document.cookie = name + "=" + value;
  62. };
  63. $.fn.dTree.get_cookie = function(name) {
  64. var value = "; " + document.cookie;
  65. var parts = value.split("; " + name + "=");
  66. if (parts.length === 2) return parts.pop().split(";").shift();
  67. };
  68. $.fn.dTree.check_cookie = function(name) {
  69. var _cookie = document.cookie;
  70. var pattern = new RegExp("" + name + "=([^;=]+)[;\\b]?");
  71. if (pattern.test(_cookie)) {
  72. return true;
  73. }
  74. }
  75. $.fn.dTree.set_icons = function($selected) {
  76. if (!$selected.parent().children('ul:first').is(':visible')) {
  77. $selected.removeClass("plus").addClass("minus");
  78. $selected.siblings("span").removeClass("folder").addClass("folder-open");
  79. } else {
  80. $selected.removeClass("minus").addClass("plus");
  81. $selected.siblings("span").removeClass("folder-open").addClass("folder");
  82. }
  83. };
  84. $.fn.dTree.close_same_level = function($selected) {
  85. var $same_level = $selected.parent().siblings(".folder-group").children('ul:first');
  86. if ($same_level.is(':visible')) {
  87. $same_level.toggle(250);
  88. $.fn.dTree.set_icons($selected.parent().siblings(".folder-group").children('span:first'));
  89. }
  90. };
  91. $.fn.dTree.tree_first_element = function($selected) {
  92. $selected.children("span.join").remove();
  93. $selected.children("span").addClass("main").removeClass("page-tree");
  94. };
  95. }(jQuery));