autoc.min.js 4.48 KB
/* 
 AutocJS - Simple & Fast to create a table of contents menu for your article. 
 Copyright (c) 2016 yaohaixiao, all right reserved. 
 homepage: https://github.com/yaohaixiao/AutocJS/#readme 
 version: 0.1.2 
 author: yaohaixiao  
 license: MIT 
 */
!function(a,b){"use strict";"function"==typeof define&&define.amd?define("autocjs",["jquery"],b(a,$)):"function"==typeof define&&define.cmd?define("autocjs",function(c,d,e){e.exports=b(a,c("jquery"))}):"object"==typeof exports?module.exports=b(a,require("jquery")):b(a,jQuery)}("undefined"!=typeof window?window:this,function(a,b){"use strict";function c(a){var b;return D+=1,b=a?a+"-"+D:D}var d="toc-show",e="toc-hide",f='<div id="toc" class="toc toc-hide"></div>',g='<h3 class="toc-title" id="toc-title">Table of Contents</h3>',h='<div class="toc-bar"></div>',i='<h2 class="toc-switch" class="toc-switch" title="Toggle Menu">&#926;</h2>',j='<a class="toc-top" id="toc-top" href="#top">TOP</a>',k='<nav id="toc-bd" class="toc-bd"></nav>',l='<ol id="toc-list" class="toc-list"></ol>',m='<ol class="toc-sub-list"></ol>',n='<li class="toc-item"></li>',o="<a></a>",p='<em class="toc-chapter"></em>',q='<div id="toc-overlay" class="toc-overlay toc-hide"></div>',r="h1,h2,h3,h4,h5,h6",s="anchor",t=null,u=null,v=null,w=null,x=null,y=null,z=null,A=null,B=null,C=null,D=-1,E={version:"0.1.2",defaults:{article:"#article",Templates:{WRAP:f,TITLE:g,BAR:h,SWITCH:i,TOP:j,BODY:k,LIST:l,SUB_LIST:l,ITEM:n,LINK:o,CHAPTER:p,OVERLAY:q},selector:r,prefix:s},attributes:{},set:function(a){return b.isPlainObject(a)&&b.extend(this.attributes,a),this},init:function(a){return this.set(this.defaults).set(a)._init().render().attachEvents(),this},_init:function(){var a=this.attributes,c=a.Templates;return t=b(a.article),u=t.find(a.selector),v=b(c.WRAP),w=b(c.TITLE),x=b(c.BAR),y=b(c.SWITCH),z=b(c.TOP),A=b(c.BODY),B=b(c.LIST),C=b(c.OVERLAY),this},render:function(){return x.append(y).append(z),A.append(B),v.append(w).append(A).append(x),b(document.body).append(v).append(C),this.renderChapters(),v.removeClass(e),this.updateLayout(),this},renderChapters:function(){var a=this.getChapters();return b(a).each(function(a,c){var d=b(n),e=null,f=b(o),g="",h=0,i=b(p),j=b("#toc-list-"+c.pid);f.attr({id:"toc-link-"+c.id,href:"#"+c.value}).html(c.text),d.attr({id:"toc-item-"+c.id,title:c.text}).append(f),-1===c.pid?(B.append(d),h=d.index()+1,g=h):(e=b("#toc-item-"+c.pid),j[0]||(j=b(m).attr("id","toc-list-"+c.pid),e.append(j)),j.append(d),h=d.index()+1,g=e.find(".toc-chapter").html()+"."+h),i.attr("data-chapter",h).html(g),i.insertBefore(f)}),this},show:function(){return C.removeClass(e),v.animate({left:0},150,function(){v.addClass(d)}),this},hide:function(){return v.animate({left:-300},150,function(){C.addClass(e),v.removeClass(d)}),this},toggle:function(){return v.hasClass(d)?this.hide():this.show(),this},updateLayout:function(){var a=v[0].offsetHeight,b=w[0].offsetHeight;return A.height(a-b),this},getChapters:function(){var a=this,d=[],e=1,f=0;return u.each(function(g,h){var i=b(h),j=parseInt(i[0].tagName.toUpperCase().replace(/[H]/gi,""),10),k=-1;if(i.attr("id",c(a.attributes.prefix)),j>e)f+=1,k=1===f?-1:g-1;else if(j===e||e>j&&j>f)1===j?(f=1,k=-1):k=d[g-1].pid;else if(f>=j)if(1===j?f=1:f-=e-j,1===f)k=-1;else switch(e-j){case 1:k=d[d[g-1].pid].pid;break;case 2:k=d[d[d[g-1].pid].pid].pid;break;case 3:k=d[d[d[d[g-1].pid].pid].pid].pid;break;case 4:k=d[d[d[d[d[g-1].pid].pid].pid].pid].pid;break;case 5:k=d[d[d[d[d[d[g-1].pid].pid].pid].pid].pid].pid}e=j,d.push({id:g,level:f,text:i.html(),value:i.attr("id"),tag:h.tagName,pid:k})}),d},getAnchors:function(){return u},getWrap:function(){return v},getTitle:function(){return w},getBar:function(){return x},getSwitchButton:function(){return y},getTopButton:function(){return z},getBody:function(){return A},getList:function(){return B},getOverlay:function(){return C},attachEvents:function(){return y.on("click",this._onSwitchClick),z.on("click",this._onTopClick),B.delegate("li","click",this._onChapterClick),C.on("click",this._onOverlayClick),b(a).on("resize",this._onWindowResize),this},_onSwitchClick:function(a){return E.toggle(),a.stopPropagation(),a.preventDefault(),E},_onTopClick:function(a){return E.hide(),a.stopPropagation(),a.preventDefault(),E},_onChapterClick:function(){return E.hide(),E},_onOverlayClick:function(a){return E.hide(),a.stopPropagation(),a.preventDefault(),E},_onWindowResize:function(){return E.updateLayout(),E}};return b.extend(b.fn,{autoc:function(a,b){var c=this,d={article:c,selector:a,prefix:b};E.init(d)}}),a.AutocJS=E,a.autoc=function(a){E.init(a)},E});