2 * Treeview 1.5pre - 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 5759 2008-07-01 07:50:28Z joern.zaefferer $
19 // TODO rewrite as a widget, removing all the extra plugins
21 swapClass: function(c1, c2) {
22 var c1Elements = this.filter('.' + c1);
23 this.filter('.' + c2).removeClass(c2).addClass(c1);
24 c1Elements.removeClass(c1).addClass(c2);
27 replaceClass: function(c1, c2) {
28 return this.filter('.' + c1).removeClass(c1).addClass(c2).end();
30 hoverClass: function(className) {
31 className = className || "hover";
32 return this.hover(function() {
33 $(this).addClass(className);
35 $(this).removeClass(className);
38 heightToggle: function(animated, callback) {
40 this.animate({ height: "toggle" }, animated, callback) :
42 jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
44 callback.apply(this, arguments);
47 heightHide: function(animated, callback) {
49 this.animate({ height: "hide" }, animated, callback);
56 prepareBranches: function(settings) {
57 if (!settings.prerendered) {
58 // mark last tree items
59 this.filter(":last-child:not(ul)").addClass(CLASSES.last);
60 // collapse whole tree, or only those marked as closed, anyway except those marked as open
61 this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
63 // return all items with sublists
64 return this.filter(":has(>ul)");
66 applyClasses: function(settings, toggler) {
67 // TODO use event delegation
68 this.filter(":has(>ul):not(:has(>a))").find(">span").unbind("click.treeview").bind("click.treeview", function(event) {
69 // don't handle click events on children, eg. checkboxes
70 if ( this == event.target )
71 toggler.apply($(this).next());
72 }).add( $("a", this) ).hoverClass();
74 if (!settings.prerendered) {
75 // handle closed ones first
76 this.filter(":has(>ul:hidden)")
77 .addClass(CLASSES.expandable)
78 .replaceClass(CLASSES.last, CLASSES.lastExpandable);
81 this.not(":has(>ul:hidden)")
82 .addClass(CLASSES.collapsable)
83 .replaceClass(CLASSES.last, CLASSES.lastCollapsable);
85 // create hitarea if not present
86 var hitarea = this.find("div." + CLASSES.hitarea);
88 hitarea = this.prepend("<div class=\"" + CLASSES.hitarea + "\"/>").find("div." + CLASSES.hitarea);
89 hitarea.removeClass().addClass(CLASSES.hitarea).each(function() {
91 $.each($(this).parent().attr("class").split(" "), function() {
92 classes += this + "-hitarea ";
94 $(this).addClass( classes );
98 // apply event to hitarea
99 this.find("div." + CLASSES.hitarea).click( toggler );
101 treeview: function(settings) {
103 settings = $.extend({
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 );
166 this.data("toggler", toggler);
168 function serialize() {
169 function binary(arg) {
173 branches.each(function(i, e) {
174 data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
176 $.cookie(settings.cookieId, data.join(""), settings.cookieOptions );
179 function deserialize() {
180 var stored = $.cookie(settings.cookieId);
182 var data = stored.split("");
183 branches.each(function(i, e) {
184 $(e).find(">ul")[ parseInt(data[i]) ? "show" : "hide" ]();
189 // add treeview class to activate styles
190 this.addClass("treeview");
192 // prepare branches and find all tree items with child lists
193 var branches = this.find("li").prepareBranches(settings);
195 switch(settings.persist) {
197 var toggleCallback = settings.toggle;
198 settings.toggle = function() {
200 if (toggleCallback) {
201 toggleCallback.apply(this, arguments);
207 var current = this.find("a").filter(function() {
208 return this.href.toLowerCase() == location.href.toLowerCase();
210 if ( current.length ) {
211 // TODO update the open/closed classes
212 var items = current.addClass("selected").parents("ul, li").add( current.next() ).show();
213 if (settings.prerendered) {
214 // if prerendered is on, replicate the basic class swapping
216 .swapClass( CLASSES.collapsable, CLASSES.expandable )
217 .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
219 .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
220 .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea );
226 branches.applyClasses(settings, toggler);
228 // if control option is set, create the treecontroller and show it
229 if ( settings.control ) {
230 treeController(this, settings.control);
231 $(settings.control).show();
238 // classes used by the plugin
239 // need to be styled via external stylesheet, see first example
241 var CLASSES = ($.treeview.classes = {
244 expandable: "expandable",
245 expandableHitarea: "expandable-hitarea",
246 lastExpandableHitarea: "lastExpandable-hitarea",
247 collapsable: "collapsable",
248 collapsableHitarea: "collapsable-hitarea",
249 lastCollapsableHitarea: "lastCollapsable-hitarea",
250 lastCollapsable: "lastCollapsable",
251 lastExpandable: "lastExpandable",