/* * jquery ui multilevel accordion v.1 * * copyright (c) 2011 pieter pareit * * http://www.scriptbreaker.com * */ //plugin definition (function($){ $.fn.extend({ //pass the options variable to the function accordion: function(options) { var defaults = { accordion: 'true', speed: 300, closedsign: '[+]', openedsign: '[-]' }; // extend our default options with those provided. var opts = $.extend(defaults, options); //assign current element to variable, in this case is ul element var $this = $(this); //add a mark [+] to a multilevel menu $this.find("li").each(function() { if($(this).find("ul").size() != 0){ //add the multilevel sign next to the link $(this).find("a:first").append(""+ opts.closedsign +""); //avoid jumping to the top of the page when the href is an # if($(this).find("a:first").attr('href') == "#"){ $(this).find("a:first").click(function(){return false;}); } } }); //open active level $this.find("li.active").each(function() { $(this).parents("ul").slidedown(opts.speed); $(this).parents("ul").parent("li").find("span:first").html(opts.openedsign); }); $this.find("li a").click(function() { if($(this).parent().find("ul").size() != 0){ if(opts.accordion){ //do nothing when the list is open if(!$(this).parent().find("ul").is(':visible')){ parents = $(this).parent().parents("ul"); visible = $this.find("ul:visible"); visible.each(function(visibleindex){ var close = true; parents.each(function(parentindex){ if(parents[parentindex] == visible[visibleindex]){ close = false; return false; } }); if(close){ if($(this).parent().find("ul") != visible[visibleindex]){ $(visible[visibleindex]).slideup(opts.speed, function(){ $(this).parent("li").find("span:first").html(opts.closedsign); }); } } }); } } if($(this).parent().find("ul:first").is(":visible")){ $(this).parent().find("ul:first").slideup(opts.speed, function(){ $(this).parent("li").find("span:first").delay(opts.speed).html(opts.closedsign); }); }else{ $(this).parent().find("ul:first").slidedown(opts.speed, function(){ $(this).parent("li").find("span:first").delay(opts.speed).html(opts.openedsign); }); } } }); } }); })(jquery);