/* * NTG CollapsableList * written by Hrodrigu * http://www.newtenberg.com * 2010-10-27 * GPL (GPL-LICENSE.txt) licenses. * * Built for jQuery library * Depends jqueryui when you use effects * http://jquery.com */ /* * markup example for $("#lista").ntg_collapsableList(); *
* *
*/ (function($){ $.fn.ntg_collapsableList = function(options){ // default configuration properties var defaults = { expandClass : "expandir", collapseClass :"contraer", expandText : "+", collapseText : "-", buttonsTag : "span", animationEffect:{effect:"slide",options:{direction: "up"}}, duration : "fast", currentClass : "current", disableBranchLink : true }; var options = $.extend(defaults, options); this.each(function(){ var obj = this; this.toogle = function(node,trueorfalse){ if($.data(node,"NTG_COLLAPSABLELIST_EXPANDED_STATE") && (trueorfalse != undefined ? trueorfalse : true)){ $(node).children(options.buttonsTag + "." + options.expandClass).show(); $(node).children(options.buttonsTag + "." + options.collapseClass).hide(); if(options.animationEffect){ $(node).children("a + ul").hide(options.animationEffect.effect,options.animationEffect.options,options.animationEffect.duration); }else{ $(node).children("a + ul").hide( (trueorfalse == undefined ? options.duration: undefined)); } $.data(node,"NTG_COLLAPSABLELIST_EXPANDED_STATE",false); }else{ $(node).children(options.buttonsTag + "." + options.collapseClass).show(); $(node).children(options.buttonsTag + "." + options.expandClass).hide(); if(options.animationEffect){ $(node).children("a + ul").show(options.animationEffect.effect,options.animationEffect.options,options.animationEffect.duration); }else{ $(node).children("a + ul").show( (trueorfalse == undefined ? options.duration: undefined)); } $.data(node,"NTG_COLLAPSABLELIST_EXPANDED_STATE",true); } }; $("li:has(ul)",obj).each(function(){ var expandButton, collapseButton, originalAnchor; expandButton = '< '+ options.buttonsTag +' class="' + options.expandClass +'">'; expandButton += '' + options.expandText +''; expandButton += ''; collapseButton = '<' + options.buttonsTag +' class="' + options.collapseClass +'">'; collapseButton += '' + options.collapseText + ''; collapseButton += ''; $(this).prepend('<' + options.buttonsTag +' class="' + options.expandClass +'">' + options.expandText +'<' + options.buttonsTag +' class="' + options.collapseClass +'">' + options.collapseText + ''); originalAnchor = $(this).children("a"); if(options.disableBranchLink){ originalAnchor.attr("href","#"); } originalAnchor.click(function(){ obj.toogle(this.parentNode); }); $("ul",this).hide(); $.data(this,"NTG_COLLAPSABLELIST_EXPANDED_STATE",false); }); $(options.buttonsTag + "." + options.expandClass,this).click( function(){ obj.toogle(this.parentNode); }); $(options.buttonsTag + "." + options.collapseClass,this).click( function(){ obj.toogle(this.parentNode); }).hide(); if(options.currentClass){ $("li." + options.currentClass +" > ul",this).each(function() { obj.toogle(this.parentNode, true); }); } }); }; })(jQuery);