jquery.tableofcontents.min.js 2.9 KB

1
  1. (function(a){a.TableOfContents=function(d,c,b){var e=this;e.$el=a(d);e.el=d;e.toc="";e.listStyle=null;e.tags=["h1","h2","h3","h4","h5","h6"];e.init=function(){e.options=a.extend({},a.TableOfContents.defaultOptions,b);if(typeof(c)=="undefined"||c==null){c=document.body}e.$scope=a(c);var g=e.$scope.find(e.tags.join(", ")).filter(":first");if(g.length!=1){return}e.starting_depth=e.options.startLevel;if(e.options.depth<1){e.options.depth=1}var f=e.tags.splice(e.options.startLevel-1,e.options.depth);e.$headings=e.$scope.find(f.join(", "));if(e.options.topLinks!==false){var h=a(document.body).attr("id");if(h==""){h=e.options.topBodyId;document.body.id=h}e.topLinkId=h}if(e.$el.is("ul")){e.listStyle="ul"}else{if(e.$el.is("ol")){e.listStyle="ol"}}e.buildTOC();if(e.options.proportionateSpacing===true&&!e.tieredList()){e.addSpacing()}return e};e.tieredList=function(){return(e.listStyle=="ul"||e.listStyle=="ol")};e.buildTOC=function(){e.current_depth=e.starting_depth;e.$headings.each(function(g,f){var h=this.nodeName.toLowerCase().substr(1,1);if(g>0||(g==0&&h!=e.current_depth)){e.changeDepth(h)}e.toc+=e.formatLink(this,h,g)+"\n";if(e.options.topLinks!==false){e.addTopLink(this)}});e.changeDepth(e.starting_depth,true);if(e.tieredList()){e.toc="<li>\n"+e.toc+"</li>\n"}e.$el.html(e.toc)};e.addTopLink=function(f){var h=(e.options.topLinks===true?"Top":e.options.topLinks);var g=a("<a href='#"+e.topLinkId+"' class='"+e.options.topLinkClass+"'></a>").html(h);a(f).append(g)};e.formatLink=function(h,i,g){var j=h.id;if(j==""){j=e.buildSlug(a(h).text());h.id=j}var f="<a href='#"+j+"'";if(!e.tieredList()){f+=" class='"+e.depthClass(i)+"'"}f+=">"+e.options.levelText.replace("%",a(h).text())+"</a>";return f};e.changeDepth=function(k,j){if(j!==true){j=false}if(!e.tieredList()){e.current_depth=k;return true}if(k>e.current_depth){var l=[];for(var h=e.current_depth;h<k;h++){l.push("<"+e.listStyle+' class="nav">\n')}var f="<li>\n";e.toc+=l.join(f)+f}else{if(k<e.current_depth){var g=[];for(var h=e.current_depth;h>k;h--){g.push("</"+e.listStyle+' class="nav">\n')}e.toc+="</li>\n"+g.join("</li>\n");if(!j){e.toc+="</li>\n<li>\n"}}else{if(!j){e.toc+="</li>\n<li>\n"}}}e.current_depth=k};e.buildSlug=function(f){f=f.toLowerCase().replace(/[^a-z0-9 -]/gi,"").replace(/ /gi,"-");f=f.substr(0,50);return f};e.depthClass=function(f){return e.options.levelClass.replace("%",(f-(e.starting_depth-1)))};e.addSpacing=function(){var f=e.$headings.filter(":first").position().top;e.$headings.each(function(g,h){var j=e.$el.find("a:eq("+g+")");var k=((a(this).position().top-f)/(e.$scope.height()-f))*e.$el.height();j.css({position:"absolute",top:k})})};return e.init()};a.TableOfContents.defaultOptions={startLevel:1,depth:3,levelClass:"toc-depth-%",levelText:"%",topLinks:false,topLinkClass:"toc-top-link",topBodyId:"toc-top",proportionateSpacing:false};a.fn.tableOfContents=function(c,b){return this.each(function(){var d=new a.TableOfContents(this,c,b);delete d})}})(jQuery);