jquery.orgchart.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * JQuery Organisation Chart Plugin-in.
  3. *
  4. * Author: Mark Lee.
  5. *
  6. * Copyright (C)2010 Caprica Software Limited.
  7. * http://www.capricasoftware.co.uk
  8. *
  9. * This software is licensed under the Creative Commons Attribution-ShareAlike
  10. * 3.0 License.
  11. *
  12. * See here for license terms:
  13. *
  14. * http://creativecommons.org/licenses/by-sa/3.0
  15. */
  16. (function($) {
  17. $.fn.orgChart = function(options, $appendTo) {
  18. var opts = $.extend({}, $.fn.orgChart.defaults, options);
  19. return this.each(function() {
  20. $this = $(this);
  21. var $container = $("<div class='" + opts.chartClass + "'/>");
  22. if($this.is("ul")) {
  23. buildNode($this.find("li:first"), $container, 0, opts);
  24. }
  25. else if($this.is("li")) {
  26. buildNode($this, $container, 0, opts);
  27. }
  28. $appendTo.append($container);
  29. });
  30. };
  31. $.fn.orgChart.defaults = {
  32. depth : -1,
  33. stack : false,
  34. chartClass : "orgChart",
  35. hoverClass : "hover",
  36. nodeText : function($node) {return "";}
  37. };
  38. function buildNode($node, $appendTo, level, opts) {
  39. var $table = $("<table cellpadding='0' cellspacing='0' border='0'/>");
  40. var $tbody = $("<tbody/>");
  41. // Make this node...
  42. var $nodeRow = $("<tr/>").addClass("nodes");
  43. var $nodeCell = $("<td/>").addClass("node").attr("colspan", 2);
  44. var $childNodes = $node.children("ul:first").children("li");
  45. if($childNodes.length > 1) {
  46. $nodeCell.attr("colspan", $childNodes.length * 2);
  47. }
  48. var $heading = $("<h2>").text(opts.nodeText($node));
  49. $nodeDiv = $("<div>").addClass("node").append($heading);
  50. $nodeCell.append($nodeDiv);
  51. $nodeRow.append($nodeCell);
  52. $tbody.append($nodeRow);
  53. $nodeDiv.click(function() {
  54. var $this = $(this);
  55. var $row = $this.closest("tr");
  56. if($row.next("tr").is(":visible")) {
  57. $row.nextAll("tr").fadeOut("slow");
  58. $row.addClass('nodeCollapsed');
  59. }
  60. else {
  61. $row.nextAll("tr").fadeIn("slow");
  62. $row.removeClass('nodeCollapsed');
  63. }
  64. });
  65. $nodeDiv.hover(function() {$(this).addClass(opts.hoverClass);}, function() {$(this).removeClass(opts.hoverClass);});
  66. if($childNodes.length > 0) {
  67. if(opts.depth == -1 || (level+1 < opts.depth)) {
  68. var $downLineRow = $("<tr/>").addClass("lines");
  69. var $downLineCell = $("<td/>").attr("colspan", $childNodes.length*2);
  70. $downLineRow.append($downLineCell);
  71. var $downLineTable = $("<table cellpadding='0' cellspacing='0' border='0'>");
  72. $downLineTable.append("<tbody>");
  73. var $downLineLine = $("<tr/>").addClass("lines");
  74. var $downLeft = $("<td>").addClass("line left");
  75. var $downRight = $("<td>").addClass("line right");
  76. $downLineLine.append($downLeft).append($downRight);
  77. $downLineTable.children("tbody").append($downLineLine);
  78. $downLineCell.append($downLineTable);
  79. $tbody.append($downLineRow);
  80. // Recursively make child nodes...
  81. var $linesRow = $("<tr/>").addClass("lines");
  82. $childNodes.each(function() {
  83. var $left = $("<td/>").addClass("line left top");
  84. var $right = $("<td/>").addClass("line right top");
  85. $linesRow.append($left).append($right);
  86. });
  87. $linesRow.find("td:first").removeClass("top");
  88. $linesRow.find("td:last").removeClass("top");
  89. $tbody.append($linesRow);
  90. var $childNodesRow = $("<tr/>");
  91. $childNodes.each(function() {
  92. var $td = $("<td/>");
  93. $td.attr("colspan", 2);
  94. buildNode($(this), $td, level+1, opts);
  95. $childNodesRow.append($td);
  96. });
  97. }
  98. else if(opts.stack) {
  99. // TODO what to do about this?
  100. var $list = $("<ul>");
  101. $childNodes.each(function() {
  102. $item = $("<li>").text($(this).textChildren());
  103. $list.append($item);
  104. });
  105. $nodeDiv.after($list);
  106. }
  107. $tbody.append($childNodesRow);
  108. }
  109. $table.append($tbody);
  110. $appendTo.append($table);
  111. };
  112. })(jQuery);