raw.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* jQuery treeTable Plugin 2.2.2 - http://ludo.cubicphuse.nl/jquery-plugins/treeTable/ */
  2. (function($) {
  3. // Helps to make options available to all functions
  4. // TODO: This gives problems when there are both expandable and non-expandable
  5. // trees on a page. The options shouldn't be global to all these instances!
  6. var options;
  7. $.fn.treeTable = function(opts) {
  8. options = $.extend({}, $.fn.treeTable.defaults, opts);
  9. return this.each(function() {
  10. $(this).addClass("treeTable").find("tbody tr").each(function() {
  11. // Initialize root nodes only if possible
  12. if(!options.expandable || $(this)[0].className.search("child-of-") == -1) {
  13. initialize($(this));
  14. } else if(options.initialState == "collapsed") {
  15. this.style.display = "none"; // Performance! $(this).hide() is slow...
  16. }
  17. });
  18. });
  19. };
  20. $.fn.treeTable.defaults = {
  21. childPrefix: "child-of-",
  22. clickableNodeNames: false,
  23. expandable: true,
  24. indent: 19,
  25. initialState: "collapsed",
  26. treeColumn: 0
  27. };
  28. // Recursively hide all node's children in a tree
  29. $.fn.collapse = function() {
  30. $(this).addClass("collapsed");
  31. childrenOf($(this)).each(function() {
  32. if(!$(this).hasClass("collapsed")) {
  33. $(this).collapse();
  34. }
  35. $(this).hide();
  36. });
  37. return this;
  38. };
  39. // Recursively show all node's children in a tree
  40. $.fn.expand = function() {
  41. $(this).removeClass("collapsed").addClass("expanded");
  42. childrenOf($(this)).each(function() {
  43. initialize($(this));
  44. if($(this).is(".expanded.parent")) {
  45. $(this).expand();
  46. }
  47. $(this).show();
  48. });
  49. return this;
  50. };
  51. // Add an entire branch to +destination+
  52. $.fn.appendBranchTo = function(destination) {
  53. var node = $(this);
  54. var parent = parentOf(node);
  55. var ancestorNames = $.map(ancestorsOf($(destination)), function(a) { return a.id; });
  56. // Conditions:
  57. // 1: +node+ should not be inserted in a location in a branch if this would
  58. // result in +node+ being an ancestor of itself.
  59. // 2: +node+ should not have a parent OR the destination should not be the
  60. // same as +node+'s current parent (this last condition prevents +node+
  61. // from being moved to the same location where it already is).
  62. // 3: +node+ should not be inserted as a child of +node+ itself.
  63. if($.inArray(node[0].id, ancestorNames) == -1 && (!parent || (destination.id != parent[0].id)) && destination.id != node[0].id) {
  64. indent(node, ancestorsOf(node).length * options.indent * -1); // Remove indentation
  65. if(parent) { node.removeClass(options.childPrefix + parent[0].id); }
  66. node.addClass(options.childPrefix + destination.id);
  67. move(node, destination); // Recursively move nodes to new location
  68. indent(node, ancestorsOf(node).length * options.indent);
  69. }
  70. return this;
  71. };
  72. // Add reverse() function from JS Arrays
  73. $.fn.reverse = function() {
  74. return this.pushStack(this.get().reverse(), arguments);
  75. };
  76. // Toggle an entire branch
  77. $.fn.toggleBranch = function() {
  78. if($(this).hasClass("collapsed")) {
  79. $(this).expand();
  80. } else {
  81. $(this).removeClass("expanded").collapse();
  82. }
  83. return this;
  84. };
  85. // === Private functions
  86. function ancestorsOf(node) {
  87. var ancestors = [];
  88. while(node = parentOf(node)) {
  89. ancestors[ancestors.length] = node[0];
  90. }
  91. return ancestors;
  92. };
  93. function childrenOf(node) {
  94. return $("table.treeTable tbody tr." + options.childPrefix + node[0].id);
  95. };
  96. function indent(node, value) {
  97. var cell = $(node.children("td")[options.treeColumn]);
  98. var padding = parseInt(cell.css("padding-left"), 10) + value;
  99. cell.css("padding-left", + padding + "px");
  100. childrenOf(node).each(function() {
  101. indent($(this), value);
  102. });
  103. };
  104. function initialize(node) {
  105. if(!node.hasClass("initialized")) {
  106. node.addClass("initialized");
  107. var childNodes = childrenOf(node);
  108. if(!node.hasClass("parent") && childNodes.length > 0) {
  109. node.addClass("parent");
  110. }
  111. if(node.hasClass("parent")) {
  112. var cell = $(node.children("td")[options.treeColumn]);
  113. var padding = parseInt(cell.css("padding-left"), 10) + options.indent;
  114. childNodes.each(function() {
  115. $($(this).children("td")[options.treeColumn]).css("padding-left", padding + "px");
  116. });
  117. if(options.expandable) {
  118. cell.prepend('<span style="margin-left: -' + options.indent + 'px; padding-left: ' + options.indent + 'px" class="expander"></span>');
  119. $(cell[0].firstChild).click(function() { node.toggleBranch(); });
  120. if(options.clickableNodeNames) {
  121. $(cell).css('cursor', 'pointer');
  122. $(cell).click(function(e) {
  123. // Don't double-toggle if the click is on the existing expander icon
  124. if (e.target.className != 'expander') {
  125. node.toggleBranch();
  126. }
  127. });
  128. }
  129. // Check for a class set explicitly by the user, otherwise set the default class
  130. if(!(node.hasClass("expanded") || node.hasClass("collapsed"))) {
  131. node.addClass(options.initialState);
  132. }
  133. if(node.hasClass("expanded")) {
  134. node.expand();
  135. }
  136. }
  137. }
  138. }
  139. };
  140. function move(node, destination) {
  141. node.insertAfter(destination);
  142. childrenOf(node).reverse().each(function() { move($(this), node[0]); });
  143. };
  144. function parentOf(node) {
  145. var classNames = node[0].className.split(' ');
  146. for(key in classNames) {
  147. if(classNames[key].match("child-of-")) {
  148. return $("#" + classNames[key].substring(9));
  149. }
  150. }
  151. };
  152. })(jQuery);