jquery.treeview.async.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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: jquery.treeview.async.js,v 1.1 2009/10/01 17:59:38 ccheng Exp $
  13. *
  14. */
  15. ;(function($) {
  16. function load(settings, root, child, container) {
  17. $.getJSON(settings.url, {root: root}, function(response) {
  18. function createNode(parent) {
  19. var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
  20. if (this.classes) {
  21. current.children("span").addClass(this.classes);
  22. }
  23. if (this.expanded) {
  24. current.addClass("open");
  25. }
  26. if (this.hasChildren || this.children && this.children.length) {
  27. var branch = $("<ul/>").appendTo(current);
  28. if (this.hasChildren) {
  29. current.addClass("hasChildren");
  30. createNode.call({
  31. text:"placeholder",
  32. id:"placeholder",
  33. children:[]
  34. }, branch);
  35. }
  36. if (this.children && this.children.length) {
  37. $.each(this.children, createNode, [branch])
  38. }
  39. }
  40. }
  41. $.each(response, createNode, [child]);
  42. $(container).treeview({add: child});
  43. });
  44. }
  45. var proxied = $.fn.treeview;
  46. $.fn.treeview = function(settings) {
  47. if (!settings.url) {
  48. return proxied.apply(this, arguments);
  49. }
  50. var container = this;
  51. load(settings, "source", this, container);
  52. var userToggle = settings.toggle;
  53. return proxied.call(this, $.extend({}, settings, {
  54. collapsed: true,
  55. toggle: function() {
  56. var $this = $(this);
  57. if ($this.hasClass("hasChildren")) {
  58. var childList = $this.removeClass("hasChildren").find("ul");
  59. childList.empty();
  60. load(settings, this.id, childList, container);
  61. }
  62. if (userToggle) {
  63. userToggle.apply(this, arguments);
  64. }
  65. }
  66. }));
  67. };
  68. })(jQuery);