source: trunk/prototype/app/plugins/treeview/jquery.treeview.async.js @ 5136

Revision 5136, 2.9 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo prototype.

Line 
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
18function load(settings, root, child, container) {
19        function createNode(parent) {
20                var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
21                if (this.classes) {
22                        current.children("span").addClass(this.classes);
23                }
24                if (this.expanded) {
25                        current.addClass("open");
26                }
27                if (this.hasChildren || this.children && this.children.length) {
28                        var branch = $("<ul/>").appendTo(current);
29                        if (this.hasChildren) {
30                                current.addClass("hasChildren");
31                                createNode.call({
32                                        classes: "placeholder",
33                                        text: "&nbsp;",
34                                        children:[]
35                                }, branch);
36                        }
37                        if (this.children && this.children.length) {
38                                $.each(this.children, createNode, [branch])
39                        }
40                }
41        }
42        $.ajax($.extend(true, {
43                url: settings.url,
44                dataType: "json",
45                data: {
46                        root: root
47                },
48                success: function(response) {
49                        child.empty();
50                        $.each(response, createNode, [child]);
51                $(container).treeview({add: child});
52            }
53        }, settings.ajax));
54        /*
55        $.getJSON(settings.url, {root: root}, function(response) {
56                function createNode(parent) {
57                        var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
58                        if (this.classes) {
59                                current.children("span").addClass(this.classes);
60                        }
61                        if (this.expanded) {
62                                current.addClass("open");
63                        }
64                        if (this.hasChildren || this.children && this.children.length) {
65                                var branch = $("<ul/>").appendTo(current);
66                                if (this.hasChildren) {
67                                        current.addClass("hasChildren");
68                                        createNode.call({
69                                                classes: "placeholder",
70                                                text: "&nbsp;",
71                                                children:[]
72                                        }, branch);
73                                }
74                                if (this.children && this.children.length) {
75                                        $.each(this.children, createNode, [branch])
76                                }
77                        }
78                }
79                child.empty();
80                $.each(response, createNode, [child]);
81        $(container).treeview({add: child});
82    });
83    */
84}
85
86var proxied = $.fn.treeview;
87$.fn.treeview = function(settings) {
88        if (!settings.url) {
89                return proxied.apply(this, arguments);
90        }
91        var container = this;
92        if (!container.children().size())
93                load(settings, "source", this, container);
94        var userToggle = settings.toggle;
95        return proxied.call(this, $.extend({}, settings, {
96                collapsed: true,
97                toggle: function() {
98                        var $this = $(this);
99                        if ($this.hasClass("hasChildren")) {
100                                var childList = $this.removeClass("hasChildren").find("ul");
101                                load(settings, this.id, childList, container);
102                        }
103                        if (userToggle) {
104                                userToggle.apply(this, arguments);
105                        }
106                }
107        }));
108};
109
110})(jQuery);
Note: See TracBrowser for help on using the repository browser.