2 * Treeview 1.4 - jQuery plugin to hide and show branches of a tree
4 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
5 * http://docs.jquery.com/Plugins/Treeview
7 * Copyright (c) 2007 Jörn Zaefferer
9 * Dual licensed under the MIT and GPL licenses:
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.gnu.org/licenses/gpl.html
13 * Revision: $Id: jquery.treeview.js 4684 2008-02-07 19:08:06Z joern.zaefferer $
20 swapClass: function(c1, c2) {
21 var c1Elements = this.filter('.' + c1);
22 this.filter('.' + c2).removeClass(c2).addClass(c1);
23 c1Elements.removeClass(c1).addClass(c2);
26 replaceClass: function(c1, c2) {
27 return this.filter('.' + c1).removeClass(c1).addClass(c2).end();
29 hoverClass: function(className) {
30 className = className || "hover";
31 return this.hover(function() {
32 $(this).addClass(className);
34 $(this).removeClass(className);
37 heightToggle: function(animated, callback) {
39 this.animate({ height: "toggle" }, animated, callback) :
41 jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
43 callback.apply(this, arguments);
46 heightHide: function(animated, callback) {
48 this.animate({ height: "hide" }, animated, callback);
55 prepareBranches: function(settings) {
56 if (!settings.prerendered) {
57 // mark last tree items
58 this.filter(":last-child:not(ul)").addClass(CLASSES.last);
59 // collapse whole tree, or only those marked as closed, anyway except those marked as open
60 this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
62 // return all items with sublists
63 return this.filter(":has(>ul)");
65 applyClasses: function(settings, toggler) {
66 this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event) {
67 toggler.apply($(this).next());
68 }).add( $("a", this) ).hoverClass();
70 if (!settings.prerendered) {
71 // handle closed ones first
72 this.filter(":has(>ul:hidden)")
73 .addClass(CLASSES.expandable)
74 .replaceClass(CLASSES.last, CLASSES.lastExpandable);
77 this.not(":has(>ul:hidden)")
78 .addClass(CLASSES.collapsable)
79 .replaceClass(CLASSES.last, CLASSES.lastCollapsable);
82 this.prepend("<div class=\"" + CLASSES.hitarea + "\"/>").find("div." + CLASSES.hitarea).each(function() {
84 $.each($(this).parent().attr("class").split(" "), function() {
85 classes += this + "-hitarea ";
87 $(this).addClass( classes );
91 // apply event to hitarea
92 this.find("div." + CLASSES.hitarea).click( toggler );
94 treeview: function(settings) {
101 return this.trigger("add", [settings.add]);
104 if ( settings.toggle ) {
105 var callback = settings.toggle;
106 settings.toggle = function() {
107 return callback.apply($(this).parent()[0], arguments);
111 // factory for treecontroller
112 function treeController(tree, control) {
113 // factory for click handlers
114 function handler(filter) {
116 // reuse toggle event handler, applying the elements to toggle
117 // start searching for all hitareas
118 toggler.apply( $("div." + CLASSES.hitarea, tree).filter(function() {
119 // for plain toggle, no filter is provided, otherwise we need to check the parent element
120 return filter ? $(this).parent("." + filter).length : true;
125 // click on first element to collapse tree
126 $("a:eq(0)", control).click( handler(CLASSES.collapsable) );
127 // click on second to expand tree
128 $("a:eq(1)", control).click( handler(CLASSES.expandable) );
129 // click on third to toggle tree
130 $("a:eq(2)", control).click( handler() );
133 // handle toggle event
137 // swap classes for hitarea
139 .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
140 .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
142 // swap classes for parent li
143 .swapClass( CLASSES.collapsable, CLASSES.expandable )
144 .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
148 .heightToggle( settings.animated, settings.toggle );
149 if ( settings.unique ) {
152 // swap classes for hitarea
154 .replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
155 .replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
157 .replaceClass( CLASSES.collapsable, CLASSES.expandable )
158 .replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
160 .heightHide( settings.animated, settings.toggle );
164 function serialize() {
165 function binary(arg) {
169 branches.each(function(i, e) {
170 data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
172 $.cookie(settings.cookieId, data.join("") );
175 function deserialize() {
176 var stored = $.cookie(settings.cookieId);
178 var data = stored.split("");
179 branches.each(function(i, e) {
180 $(e).find(">ul")[ parseInt(data[i]) ? "show" : "hide" ]();
185 // add treeview class to activate styles
186 this.addClass("treeview");
188 // prepare branches and find all tree items with child lists
189 var branches = this.find("li").prepareBranches(settings);
191 switch(settings.persist) {
193 var toggleCallback = settings.toggle;
194 settings.toggle = function() {
196 if (toggleCallback) {
197 toggleCallback.apply(this, arguments);
203 var current = this.find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
204 if ( current.length ) {
205 current.addClass("selected").parents("ul, li").add( current.next() ).show();
210 branches.applyClasses(settings, toggler);
212 // if control option is set, create the treecontroller and show it
213 if ( settings.control ) {
214 treeController(this, settings.control);
215 $(settings.control).show();
218 return this.bind("add", function(event, branches) {
220 .removeClass(CLASSES.last)
221 .removeClass(CLASSES.lastCollapsable)
222 .removeClass(CLASSES.lastExpandable)
224 .removeClass(CLASSES.lastCollapsableHitarea)
225 .removeClass(CLASSES.lastExpandableHitarea);
226 $(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings, toggler);
231 // classes used by the plugin
232 // need to be styled via external stylesheet, see first example
233 var CLASSES = $.fn.treeview.classes = {
236 expandable: "expandable",
237 expandableHitarea: "expandable-hitarea",
238 lastExpandableHitarea: "lastExpandable-hitarea",
239 collapsable: "collapsable",
240 collapsableHitarea: "collapsable-hitarea",
241 lastCollapsableHitarea: "lastCollapsable-hitarea",
242 lastCollapsable: "lastCollapsable",
243 lastExpandable: "lastExpandable",
248 // provide backwards compability
249 $.fn.Treeview = $.fn.treeview;