jquery.treeview.async.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Async Treeview 0.1 - Lazy-loading extension for Treeview
  3. *
  4. * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
  5. *
  6. * Copyright (c) 2007 Jörn Zaefferer
  7. *
  8. * Dual licensed under the MIT and GPL licenses:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. * http://www.gnu.org/licenses/gpl.html
  11. *
  12. * Revision: $Id$
  13. *
  14. */
  15. ;(function($) {
  16. function load(settings, root, child, container) {
  17. function createNode(parent) {
  18. var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
  19. if (this.classes) {
  20. current.children("span").addClass(this.classes);
  21. }
  22. if (this.expanded) {
  23. current.addClass("open");
  24. }
  25. if (this.hasChildren || this.children && this.children.length) {
  26. var branch = $("<ul/>").appendTo(current);
  27. if (this.hasChildren) {
  28. current.addClass("hasChildren");
  29. createNode.call({
  30. classes: "placeholder",
  31. text: "&nbsp;",
  32. children:[]
  33. }, branch);
  34. }
  35. if (this.children && this.children.length) {
  36. $.each(this.children, createNode, [branch])
  37. }
  38. }
  39. }
  40. $.ajax($.extend(true, {
  41. url: settings.url,
  42. dataType: "json",
  43. data: {
  44. root: root
  45. },
  46. success: function(response) {
  47. child.empty();
  48. $.each(response, createNode, [child]);
  49. $(container).treeview({add: child});
  50. }
  51. }, settings.ajax));
  52. /*
  53. $.getJSON(settings.url, {root: root}, function(response) {
  54. function createNode(parent) {
  55. var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
  56. if (this.classes) {
  57. current.children("span").addClass(this.classes);
  58. }
  59. if (this.expanded) {
  60. current.addClass("open");
  61. }
  62. if (this.hasChildren || this.children && this.children.length) {
  63. var branch = $("<ul/>").appendTo(current);
  64. if (this.hasChildren) {
  65. current.addClass("hasChildren");
  66. createNode.call({
  67. classes: "placeholder",
  68. text: "&nbsp;",
  69. children:[]
  70. }, branch);
  71. }
  72. if (this.children && this.children.length) {
  73. $.each(this.children, createNode, [branch])
  74. }
  75. }
  76. }
  77. child.empty();
  78. $.each(response, createNode, [child]);
  79. $(container).treeview({add: child});
  80. });
  81. */
  82. }
  83. var proxied = $.fn.treeview;
  84. $.fn.treeview = function(settings) {
  85. if (!settings.url) {
  86. return proxied.apply(this, arguments);
  87. }
  88. var container = this;
  89. if (!container.children().size())
  90. load(settings, "source", this, container);
  91. var userToggle = settings.toggle;
  92. return proxied.call(this, $.extend({}, settings, {
  93. collapsed: true,
  94. toggle: function() {
  95. var $this = $(this);
  96. if ($this.hasClass("hasChildren")) {
  97. var childList = $this.removeClass("hasChildren").find("ul");
  98. load(settings, this.id, childList, container);
  99. }
  100. if (userToggle) {
  101. userToggle.apply(this, arguments);
  102. }
  103. }
  104. }));
  105. };
  106. })(jQuery);