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,v 1.1 2008/10/27 17:51:05 stompeers Exp $
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 // HACK by stompeers, so that the hitareas won't be duplicated when using Navigate's Cache
83 if (!$(this).find('.hitarea').length > 0) {
84 this.prepend("<div class=\"" + CLASSES.hitarea + "\"/>").find("div." + CLASSES.hitarea).each(function() {
86 $.each($(this).parent().attr("class").split(" "), function() {
87 classes += this + "-hitarea ";
89 $(this).addClass( classes );
94 // apply event to hitarea
95 this.find("div." + CLASSES.hitarea).click( toggler );
97 treeview: function(settings) {
104 return this.trigger("add", [settings.add]);
107 if ( settings.toggle ) {
108 var callback = settings.toggle;
109 settings.toggle = function() {
110 return callback.apply($(this).parent()[0], arguments);
114 // factory for treecontroller
115 function treeController(tree, control) {
116 // factory for click handlers
117 function handler(filter) {
119 // reuse toggle event handler, applying the elements to toggle
120 // start searching for all hitareas
121 toggler.apply( $("div." + CLASSES.hitarea, tree).filter(function() {
122 // for plain toggle, no filter is provided, otherwise we need to check the parent element
123 return filter ? $(this).parent("." + filter).length : true;
128 // click on first element to collapse tree
129 $("a:eq(0)", control).click( handler(CLASSES.collapsable) );
130 // click on second to expand tree
131 $("a:eq(1)", control).click( handler(CLASSES.expandable) );
132 // click on third to toggle tree
133 $("a:eq(2)", control).click( handler() );
136 // handle toggle event
140 // swap classes for hitarea
142 .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
143 .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
145 // swap classes for parent li
146 .swapClass( CLASSES.collapsable, CLASSES.expandable )
147 .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
151 .heightToggle( settings.animated, settings.toggle );
152 if ( settings.unique ) {
155 // swap classes for hitarea
157 .replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
158 .replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
160 .replaceClass( CLASSES.collapsable, CLASSES.expandable )
161 .replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
163 .heightHide( settings.animated, settings.toggle );
167 function serialize() {
168 function binary(arg) {
172 branches.each(function(i, e) {
173 data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
175 var options = new Array();
176 // HACK by stompeers to save state of menu across all pages
178 $.cookie(settings.cookieId, data.join(""), options );
181 function deserialize() {
182 var stored = $.cookie(settings.cookieId);
184 var data = stored.split("");
185 branches.each(function(i, e) {
186 $(e).find(">ul")[ parseInt(data[i]) ? "show" : "hide" ]();
191 // add treeview class to activate styles
192 this.addClass("treeview");
194 // prepare branches and find all tree items with child lists
195 var branches = this.find("li").prepareBranches(settings);
197 switch(settings.persist) {
199 var toggleCallback = settings.toggle;
200 settings.toggle = function() {
202 if (toggleCallback) {
203 toggleCallback.apply(this, arguments);
209 var current = this.find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
210 if ( current.length ) {
211 current.addClass("selected").parents("ul, li").add( current.next() ).show();
216 branches.applyClasses(settings, toggler);
218 // if control option is set, create the treecontroller and show it
219 if ( settings.control ) {
220 treeController(this, settings.control);
221 $(settings.control).show();
224 return this.bind("add", function(event, branches) {
226 .removeClass(CLASSES.last)
227 .removeClass(CLASSES.lastCollapsable)
228 .removeClass(CLASSES.lastExpandable)
230 .removeClass(CLASSES.lastCollapsableHitarea)
231 .removeClass(CLASSES.lastExpandableHitarea);
232 $(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings, toggler);
237 // classes used by the plugin
238 // need to be styled via external stylesheet, see first example
239 var CLASSES = $.fn.treeview.classes = {
242 expandable: "expandable",
243 expandableHitarea: "expandable-hitarea",
244 lastExpandableHitarea: "lastExpandable-hitarea",
245 collapsable: "collapsable",
246 collapsableHitarea: "collapsable-hitarea",
247 lastCollapsableHitarea: "lastCollapsable-hitarea",
248 lastCollapsable: "lastCollapsable",
249 lastExpandable: "lastExpandable",
254 // provide backwards compability
255 $.fn.Treeview = $.fn.treeview;