/*
 * autoCatalog - v1.0.0 - 2018-09-04 18:19:07 Tuesday
 * https://github.com/heanes/autoCatalog.js
 * Copyright (c) 2018 Heanes
 * Licensed MIT License
 */

"use strict";!function(I,k,t,h){var s="autoCatalog",e={outlineSelector:["h2","h3","h4"],catalogContainer:h,autoCounter:!1,showChapterNumber:!1,showChapterSymbol:!0,formatChapterAnchor:h,changeUrlAnchor:!0,scrollContentAnimationDelay:400,scrollToContentOffset:0,scrollCatalogAnimationDelay:400,clickToScrollCatalogStep:48,alwaysShow:!0,collapseOnInit:!1,dataChapterFieldName:"data-chapter",onChapterClick:h,treeOption:{option:{data:[],getDataUrl:"",nodeIcon:"fa fa-list",iconCollapse:"",iconExpand:"",iconEmpty:"icon-empty",showCollapseExpandIcon:!1,showIcon:!1,nodeDefaultState:{selected:!1,expanded:!0,disabled:!1,checked:!1},afterRenderNode:function(t,e){},enableLink:!0,showSingleNodeIcon:!0},node:{text:"",nodeIcon:"fa fa-list",href:"",iconSelected:"",selectable:!0,state:{selected:!1,expanded:!0,disabled:!1,checked:!1},switchToShow:!0,target:"",nodes:[]}}},i=function(t,e){return this._defaults=this.getDefaults(),this.options=null,this.$element=null,this.inState=null,this.init(t,e),{options:this.options,init:I.proxy(this.init,this),destroy:I.proxy(this.destroy,this),refreshOption:I.proxy(this.refreshOption,this),show:I.proxy(this.show,this),hide:I.proxy(this.hide,this),toggle:I.proxy(this.toggle,this)}};function l(t){k.console&&k.console.error(t)}i.V=i.VERSION="1.0.0",i.DEFAULTS=e,i.prototype.init=function(t,e){return this.$element=I(t),this.$el_=this.$element.clone(!0),this.options=this.getOptions(e),this.treeData=this.getChapterTreeData(),this.renderCatalog(this.treeData),this.bindEvent(),this},i.prototype.bindEvent=function(){var t=this.$element,e=this.$catalog,i=this.options,a=t.position(),n=t.height(),l=this,o=e.find(".catalog-list-wrap"),s=e.find(".catalog-list"),r=e.find(".node-wrap"),d=r.eq(0).height(),p=e.find(".scrollbar-location-arrow"),c=p.height(),h=(d-c)/2,f=this.$outlines,u=f.length,g=o.height(),v=s.height()-g,m=e.find(".go-up-btn"),C=e.find(".go-down-btn"),b=i.clickToScrollCatalogStep,y=i.scrollCatalogAnimationDelay,w=i.scrollContentAnimationDelay,T=0,x=0,S=void 0,A=void 0,N=void 0,O=void 0,$=0;I(k).on("scroll",function(){$=I(this).scrollTop();for(var t=u-1;0<t;t--){if(S=f.eq(t),$>=S.offset().top-d)return O=S.attr(i.dataChapterFieldName),A=s.find("["+i.dataChapterFieldName+'="'+O+'"]'),N=A.position(),r.removeClass("active"),A.addClass("active"),0<v&&(N.top+d<=g?(T=0,m.addClass("disabled"),C.removeClass("disabled")):N.top+d>g&&N.top<v?(T=-N.top+g/2-d,m.removeClass("disabled"),C.removeClass("disabled")):N.top>v&&(T=-v,m.removeClass("disabled"),C.addClass("disabled")),0<y?s.stop().animate({top:T},y):s.css({top:T})),x=N.top+h,0<y?p.stop().animate({top:x},y):p.css({top:x}),!1;x=h,0<y?p.stop().animate({top:x},y):p.css({top:x}),r.removeClass("active").eq(0).addClass("active")}i.alwaysShow||($>=a.top+n?e.animate({opacity:0,visibility:"hidden"},1e3):e.animate({opacity:1,visibility:"visible"},1e3))}),(!i.changeUrlAnchor||0<i.scrollToContentOffset)&&e.on("click",function(t){var e=I("html,body"),a=I(t.target),n=a.hasClass("node-wrap")?a:a.closest(".node-wrap");if(i.changeUrlAnchor)k.location.href=k.location.href.split("#")[0]+n.attr("href");else{var o=n.attr(i.dataChapterFieldName);console.log(i.dataChapterFieldName+'="'+o+'"');var s=l.$element.find("["+i.dataChapterFieldName+'="'+o+'"]');0<w?e.animate({scrollTop:s.offset().top+i.scrollToContentOffset},w):e.scrollTop(s.offset().top+i.scrollToContentOffset)}return!1}),m.on("click",function(){var t=I(this);return t.hasClass("disabled")||(Math.abs(parseInt(s.css("top")))>=b?(s.stop().animate({top:"+="+b},"fast"),C.removeClass("disabled")):(s.stop().animate({top:"0"},"fast"),t.addClass("disabled"))),!1}),C.on("click",function(){var t=I(this);return t.hasClass("disabled")||(v-Math.abs(parseInt(s.css("top")))>=b?(e.find(".catalog-list").stop().animate({top:"-="+b},"fast"),m.removeClass("disabled")):(s.stop().animate({top:-v},"fast"),t.addClass("disabled"))),!1});var L=e.find(".go-top-btn"),D=e.find(".toggle-show-btn");return L.on("click",function(){I("html,body").animate({scrollTop:a.top},"fast")}),D.on("click",function(){l.$catalog.toggleClass("collapse")}),I(k).trigger("scroll"),this},i.prototype.getChapterTreeData=function(t){var e=t||this.$element,i=this.options,l=i.outlineSelector,a=l.join(","),n=e.find(a);this.$outlines=n;var r=[],d=this;return I.each(n,function(t,e){var a=I(e),n={nodes:[]};n.text=a.text();var o=d.matchOutlineSelectorLevel(a,l);if(1===(n.level=o))n.outline=[r.length+1],n.href="#"+d.formatChapterAnchor(n),r.push(n);else{var s=d.getParentNodeByLevel(r,o);n.outline=s.outline,n.href="#"+d.formatChapterAnchor(n),s.parentNode.nodes.push(n)}d.insertChapterAnchorToContent(a,n),a.attr(i.dataChapterFieldName,n.outline.join("."))}),console.log(r),r},i.prototype.getParentNodeByLevel=function(t,e){if(1===e)return t;var a=t[t.length-1],n=[];if(2===e)return{parentNode:a,outline:n=[t.length,a.nodes.length+1]};n=[t.length,a.nodes.length];for(var o=2;o<e;o++){var s=a.nodes;a=s[1<s.length?s.length-1:0],n.push(a.nodes.length+1)}return{parentNode:a,outline:n}},i.prototype.formatChapterAnchor=function(t,e){var a=this.options.formatChapterAnchor;return"function"==typeof a?a(t):(e=e||".",t.outline.join(e)+e+t.text)},i.prototype.matchOutlineSelectorLevel=function(a,t){var e=this.options;t=t||e.outlineSelector;var n=1;return I.each(t,function(t,e){if(a.hasClass(e)||a[0].tagName.toLowerCase()===e.toLowerCase())return n=t+1,!1}),n},i.prototype.insertChapterAnchorToContent=function(t,e){var a=this.generateOutlineAnchor(e);return t.before(a),this},i.prototype.generateOutlineAnchor=function(t){return'<div class="auto-catalog-outline-anchor">\n            <a class="auto-catalog-anchor-href" name="'+this.formatChapterAnchor(t)+'"></a>\n        </div>'},i.prototype.renderCatalog=function(t){var e=this.options;this.$catalog=I(this.template.main);var a=e.catalogContainer;!e.catalogContainer instanceof jQuery&&(a=I(e.catalogContainer));var n=this.$catalog.find(".catalog-list");return a.empty().append(this.$catalog),this.convertToStandardTree(t),n.append(this.buildTree(t)),e.collapseOnInit&&this.$catalog.addClass("collapse"),this.$catalog.find(".catalog-list-wrap").height()>=this.$catalog.find(".catalog-list").height()&&this.$catalog.find(".catalog-scrollbar-right").hide(),this},i.prototype.template={main:'<div class="auto-catalog">\n            <div class="catalog-wrap">\n                <div class="catalog-scrollbar-left">\n                    <em class="circle start"></em>\n                    <em class="circle end"></em>\n                </div>\n                <div class="catalog-list-wrap">\n                    <div class="catalog-list tree-list">\n                        <div class="scrollbar-location-arrow">\n                            <i class="rectangle"></i>\n                            <i class="triangle-right"></i>\n                        </div>\n                    </div>\n                </div>\n                <div class="catalog-scrollbar-right">\n                    <a class="handle-btn go-up-btn disabled" href="javascript:;"><i class="triangle-up"></i></a>\n                    <a class="handle-btn go-down-btn" href="javascript:;"><i class="triangle-down"></i></a>\n                </div>\n            </div>\n            <div class="catalog-toolbar-wrap">\n                <div class="catalog-toolbar">\n                    <a class="handle-btn toggle-show-btn" href="javascript:;"><i class="fa fa-list"></i></a>\n                    <a class="handle-btn go-top-btn" href="javascript:;"><i class="fa fa-angle-up"></i></a>\n                </div>\n            </div>\n        </div>',chapterSymbol:'<em class="chapter-symbol"></em>'},i.prototype.buildTree=function(t,l,e){if(t){var r=this.options,d=this.options.treeOption.option;l=l===h?d.overHeightSwitch?0:1:l;var p=this,c=I(p.treeTemplate.treeListGroup);return I.each(t,function(t,e){var a=I(p.treeTemplate.node).attr("data-nodeId",e.nodeId),n=I(p.treeTemplate.nodeWrap);d.enableLink&&((n=I(p.treeTemplate.nodeLink)).attr("href",e.href),e.target!==h&&n.attr("target",e.target));for(var o=0;o<l-1;o++)n.append(p.treeTemplate.indent);var s=[];if(e.nodes&&0<e.nodes.length?(s.push("node-collapse-expand-icon"),e.state.expanded?(s.push("expand"),s.push(d.iconExpand),a.addClass("expand")):(s.push("collapse"),s.push(d.iconCollapse),a.addClass("collapse"))):s.push(d.iconEmpty),d.showCollapseExpandIcon&&n.append(I(p.treeTemplate.icon).addClass(s.join(" "))),d.showIcon&&(1===l||d.showSingleNodeIcon||e.nodes&&0<e.nodes.length)){var i=["node-icon"];i.push(e.nodeIcon||d.nodeIcon),n.append(I(p.treeTemplate.icon).addClass(i.join(" ")))}if(r.showChapterNumber&&n.addClass("chapter-item").append('<span class="chapter-number">'+e.outline.join(".")+".</span>"),n.append(I(p.treeTemplate.nodeText).append(e.text).addClass("chapter-text")),r.showChapterSymbol&&n.append('<em class="chapter-symbol"></em>'),n.attr(r.dataChapterFieldName,e.outline.join(".")),c.append(a.append(n)),d.afterRenderNode&&"function"==typeof d.afterRenderNode&&d.afterRenderNode(a,l),a.addClass("level"+l),e.nodes&&0<e.nodes.length&&a.append(p.buildTree(e.nodes,l+1)),1===l)return c}),c}},i.prototype.convertToStandardTree=function(a){if(a){var n=this.options,o=n.treeOption.node,s=this;return I.each(a,function(t,e){e.state=I.extend(!0,{},o.state,n.nodeDefaultState,e.state),a[t]=I.extend(!0,{},o,e),e.nodes&&0<e.nodes.length&&(a[t].nodes=s.convertToStandardTree(e.nodes))}),a}},i.prototype.treeTemplate={treeTopWrap:'<div class="tree-top-wrap"></div>',treeTop:'<ul class="tree-top-list"></ul>',treeListWrap:'<div class="tree-list-wrap"></div>',treeList:'<div class="tree-list"></div>',treeListGroupWrap:'<div class="tree-group-wrap"></div>',treeListGroup:'<ul class="tree-group"></ul>',nodeWrap:'<span class="node-wrap"></span>',nodeLink:'<a class="node-wrap"></a>',node:'<li class="tree-node"></li>',link:'<a href="javascript:;" class="node-wrap"></a>',nodeText:'<span class="node-text"></span>',indent:'<span class="indent"></span>',icon:'<i class="icon"></i>',badge:'<span class="badge"></span>',leftTreeHandle:'<div class="tree-left-handle"></div>',handleContractLeftTree:'<span class="left-tree-handle-btn handle-contract" title="收缩/展开"><i class="handle-icon fa fa-exchange"></i></span>',handleCollapseAll:'<span class="left-tree-handle-btn handle-collapse-all" title="一键折叠全部"><i class="handle-icon fa fa-compress"></i></span>',handleExpandAll:'<span class="left-tree-handle-btn handle-expand-all" title="一键展开全部"><i class="handle-icon fa fa-expand"></i></span>',treeSearch:'<div class="tree-search"><input type="text" class="tree-search-input" placeholder="search" /></div>',collapseAllHandle:'<div class="collapse-all-handle"><span class="collapse-all-handle-btn collapsed" title="一键折叠/展开全部"><i class="handle-icon fa fa-compress"></i></span></div>'},i.prototype.getOptions=function(t){return this.handleToStandardOption(t)},i.prototype.handleToStandardOption=function(t){var e=this.getDefaults();return(t=I.extend({},e,this.$element.data(),t)).changeUrlAnchor||(t.treeOption.option.enableLink=!1),t},i.prototype.refreshOption=function(t){return this.options=I.extend(!0,{},this.options,t),this.getOptions(this.options),this.destroy(),this.init(),this},i.prototype.destroy=function(){return this.$element.html(this.$el_.html()),this},i.prototype.getDefaults=function(){return i.DEFAULTS},I.fn[s]=function(a,n){var o=h;return this.each(function(){var t=I(this),e=I.data(this,s);"string"==typeof a?e?I.isFunction(e[a])&&"_"!==a.charAt(0)?("destroy"===a&&t.removeData(s),n instanceof Array||(n=[n]),o=e[a].apply(e,n)):l("No such method : "+a):l("Not initialized, can not call method : "+a):"boolean"==typeof a?o=e:I.data(this,s,new i(this,I.extend(!0,{},a)))}),o||this}}(jQuery,window,document);
//# sourceMappingURL=autoCatalog.js.map