jquery.treeview.async.js 2.8 KB

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