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