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);
48 heightHide:
function(animated, callback) {
50 this.animate({ height:
"hide" }, animated, callback);
58 prepareBranches:
function(settings) {
59 if (!settings.prerendered) {
61 this.filter(
":last-child:not(ul)").addClass(CLASSES.last);
63 this.filter((settings.collapsed ?
"" :
"." + CLASSES.closed) +
":not(." + CLASSES.open +
")").find(
">ul").hide();
66 return this.filter(
":has(>ul)");
68 applyClasses:
function(settings, toggler) {
70 this.filter(
":has(>ul):not(:has(>a))").find(
">span").unbind(
"click.treeview").bind(
"click.treeview",
function(event) {
72 if (
this == event.target ) {
73 toggler.apply($(this).next());
75 }).add( $(
"a",
this) ).hoverClass();
77 if (!settings.prerendered) {
79 this.filter(
":has(>ul:hidden)")
80 .addClass(CLASSES.expandable)
81 .replaceClass(CLASSES.last, CLASSES.lastExpandable);
84 this.not(
":has(>ul:hidden)")
85 .addClass(CLASSES.collapsable)
86 .replaceClass(CLASSES.last, CLASSES.lastCollapsable);
89 var hitarea = this.find(
"div." + CLASSES.hitarea);
90 if (!hitarea.length) {
91 hitarea = this.prepend(
"<div class=\"" + CLASSES.hitarea +
"\"/>").find(
"div." + CLASSES.hitarea);
93 hitarea.removeClass().addClass(CLASSES.hitarea).each(
function() {
95 $.each($(
this).parent().attr(
"class").split(
" "),
function() {
96 classes +=
this +
"-hitarea ";
98 $(
this).addClass( classes );
103 this.find(
"div." + CLASSES.hitarea).click( toggler );
105 treeview:
function(settings) {
107 settings = $.extend({
111 if ( settings.toggle ) {
112 var callback = settings.toggle;
113 settings.toggle =
function() {
114 return callback.apply($(
this).parent()[0], arguments);
119 function treeController(tree, control) {
121 function handler(filter) {
125 toggler.apply( $(
"div." + CLASSES.hitarea, tree).filter(
function() {
127 return filter ? $(this).parent(
"." + filter).length : true;
133 $(
"a:eq(0)", control).click( handler(CLASSES.collapsable) );
135 $(
"a:eq(1)", control).click( handler(CLASSES.expandable) );
137 $(
"a:eq(2)", control).click( handler() );
146 .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
147 .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
150 .swapClass( CLASSES.collapsable, CLASSES.expandable )
151 .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
155 .heightToggle( settings.animated, settings.toggle );
156 if ( settings.unique ) {
161 .replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
162 .replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
164 .replaceClass( CLASSES.collapsable, CLASSES.expandable )
165 .replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
167 .heightHide( settings.animated, settings.toggle );
170 this.data(
"toggler", toggler);
172 function serialize() {
173 function binary(arg) {
177 branches.each(
function(i, e) {
178 data[i] = $(e).is(
":has(>ul:visible)") ? 1 : 0;
180 $.cookie(settings.cookieId, data.join(
""), settings.cookieOptions );
183 function deserialize() {
184 var stored = $.cookie(settings.cookieId);
186 var data = stored.split(
"");
187 branches.each(
function(i, e) {
188 $(e).find(
">ul")[ parseInt(data[i]) ?
"show" :
"hide" ]();
194 this.addClass(
"treeview");
197 var branches = this.find(
"li").prepareBranches(settings);
199 switch(settings.persist) {
201 var toggleCallback = settings.toggle;
202 settings.toggle =
function() {
204 if (toggleCallback) {
205 toggleCallback.apply(
this, arguments);
211 var current = this.find(
"a").filter(
function() {
212 return this.href.toLowerCase() == location.href.toLowerCase();
214 if ( current.length ) {
216 var items = current.addClass(
"selected").parents(
"ul, li").add( current.next() ).show();
217 if (settings.prerendered) {
220 .swapClass( CLASSES.collapsable, CLASSES.expandable )
221 .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
223 .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
224 .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea );
230 branches.applyClasses(settings, toggler);
233 if ( settings.control ) {
234 treeController(
this, settings.control);
235 $(settings.control).show();
245 var CLASSES = ($.treeview.classes = {
248 expandable:
"expandable",
249 expandableHitarea:
"expandable-hitarea",
250 lastExpandableHitarea:
"lastExpandable-hitarea",
251 collapsable:
"collapsable",
252 collapsableHitarea:
"collapsable-hitarea",
253 lastCollapsableHitarea:
"lastCollapsable-hitarea",
254 lastCollapsable:
"lastCollapsable",
255 lastExpandable:
"lastExpandable",