-
Notifications
You must be signed in to change notification settings - Fork 1
/
masha_nav.min.js
11 lines (11 loc) · 5.33 KB
/
masha_nav.min.js
1
2
3
4
5
6
7
8
9
10
11
(function(e,f){"function"===typeof define&&define.amd?define(["masha"],f):"object"===typeof exports&&"undefined"!==typeof module?module.exports=f(require("masha")):e.MaShaNav=f({MaSha:e.MaSha})})(this,function(e){function f(){var a=window.pageYOffset?window.pageYOffset:0,b=document.documentElement?document.documentElement.scrollTop:0,c=document.body?document.body.scrollTop:0,a=a?a:0;b&&(!a||a>b)&&(a=b);return c&&(!a||a>c)?c:a}function m(a){if(!a||!a.getClientRects().length)return{top:0,left:0};var b=
a.getBoundingClientRect();if(b.width||b.height){var c=a.ownerDocument;a=c.defaultView;c=c.documentElement;return{top:b.top+a.pageYOffset-c.clientTop,left:b.left+a.pageXOffset-c.clientLeft}}return b}e=e.MaSha;var d=e.$M,g=function(a){a=a||{};this.options=d.extend({},g.defaultOptions,a);d.extend(this,{counter:0,savedSel:[],ranges:{},childs:[],blocks:{}});this.init()};g.defaultOptions={selectable:"selectable-content",t_offsetTop:100};g.prototype={init:function(){this.selectable="string"===typeof this.options.selectable?
document.getElementById(this.options.selectable):this.options.selectable;this.total=this.countTotal();this.drawNav();1<this.total?(this.fillNav(),this.current=0,this.getElements(),this.noScrollEvent=!1,document.getElementById("mashajs-nav-current").innerHTML=this.current+1):null!==document.getElementById("mashajs-nav")&&(document.getElementById("mashajs-nav").style.display="none");this.scrollTimeout=null},addEvents:function(){var a=document.getElementById("mashajs-up"),b=document.getElementById("mashajs-down");
this.windowScroll=d.bind(this.calculateCurrent,this);d.addEvent(window,"scroll",this.windowScroll);this.upClick=d.bind(function(){this.goTo(this.current-1)},this);d.addEvent(a,"click",this.upClick);this.downClick=d.bind(function(){this.goTo(this.current+1)},this);d.addEvent(b,"click",this.downClick)},removeEvents:function(){var a=document.getElementById("mashajs-up"),b=document.getElementById("mashajs-down");d.removeEvent(window,"scroll",this.windowScroll);a&&d.removeEvent(a,"click",this.upClick);
b&&d.removeEvent(b,"click",this.downClick)},countTotal:function(){var a=0,b;for(b in this.options.ranges)if(this.options.ranges.hasOwnProperty(b))for(var c in this.options.ranges[b])this.options.ranges[b].hasOwnProperty(c)&&a++;return a},fillNav:function(){1<this.total?(document.getElementById("mashajs-nav").style.display="block",document.getElementById("mashajs-nav-total").innerHTML=this.total):null!==document.getElementById("mashajs-nav")&&(document.getElementById("mashajs-nav").style.display="none")},
resetData:function(a){this.options.ranges=[];this.options.ranges.push(a);this.total=this.countTotal();this.drawNav();this.fillNav();1<this.total&&this.calculateCurrent()},calculateCurrent:function(){if(this.noScrollEvent)return this.noScrollEvent=!1;window.clearTimeout(this.scrollTimeout);var a=f();this.getElements();this.current=this.getClosestEl(a+this.options.t_offsetTop,this.elements)[1];document.getElementById("mashajs-nav-current").innerHTML=this.current+1;this.refreshArrows()},getElements:function(){var a=
{},b;for(b in this.options.ranges)if(this.options.ranges.hasOwnProperty(b))for(var c in this.options.ranges[b])if(this.options.ranges[b].hasOwnProperty(c)){var e=d.byClassName(this.options.selectable,c);a[c]=m(e[0]).top}b=[];for(var l in a)a.hasOwnProperty(l)&&b.push([l,a[l]]);b.sort(function(a,b){return a[1]-b[1]});this.elements=b},getClosestEl:function(a,b){if(b.length){for(var c,d,e=b[0][1],h=b[0][0],f=0,k=0;k<b.length;k++)c=Math.abs(a-e),d=Math.abs(a-b[k][1]),d<c&&(e=b[k][1],h=b[k][0],f=k);return[h,
f]}return!1},refreshArrows:function(){(0===this.current?d.addClass:d.removeClass)(document.getElementById("mashajs-up"),"disabled");(this.current==this.total-1?d.addClass:d.removeClass)(document.getElementById("mashajs-down"),"disabled")},goTo:function(a){if(0<=a&&this.elements.length>=a+1)this.noScrollEvent=!0,this.smoothScroll(0<this.elements[a][1]-this.options.t_offsetTop?this.elements[a][1]-this.options.t_offsetTop:0,this.elements[a]),this.current=a,document.getElementById("mashajs-nav-current").innerHTML=
a+1,this.refreshArrows();else return 0<a&&(this.current=this.elements.length-1),!1},drawNav:function(){var a=document.getElementById("mashajs-nav-position");null===a&&(a=document.createElement("DIV"),a.setAttribute("id","mashajs-nav-position"),a.innerHTML='<div id="mashajs-nav-center"><div id="mashajs-nav"><div id="mashajs-up"></div><div id="mashajs-down"></div><div class="num"><span id="mashajs-nav-current"></span> / <span id="mashajs-nav-total"></span><span class="right"></span></div></div></div></div>',
a.style.width=this.selectable.offsetWidth+"px",this.selectable.parentNode.insertBefore(a,this.selectable.nextSibling),this.addEvents());a.style.display=1<this.total?"block":"none"},smoothScroll:function(a){function b(){c+=h;0<h&&c>a||0>h&&c<a?c=a:g.scrollTimeout=window.setTimeout(b,e);window.scrollTo(0,c);g.noScrollEvent=!0}var c=f(),d=a-c,e=Math.round(Math.abs(d)/20);20<=e&&(e=20);var h=Math.round(d/25);window.clearTimeout(this.scrollTimeout);if(2>Math.abs(h))this.noScrollEvent=!0,window.scrollTo(0,
a);else{var g=this;this.scrollTimeout=window.setTimeout(b,e)}}};var n=e.prototype.updateHash;e.prototype.showNav=function(){this.nav=new g({ranges:[this.ranges],selectable:this.selectable});this.nav.calculateCurrent()};e.prototype.updateHash=function(){n.call(this);"undefined"!=typeof this.nav&&this.nav.resetData(this.ranges)};return g});