forked from ytlabs/ng-slim-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-slim-scroll.min.js
1 lines (1 loc) · 3.29 KB
/
ng-slim-scroll.min.js
1
angular.module("ngSlimScroll",[]).directive("slimScroll",["$window","$document","$timeout",function(a,b,c){"use strict";var d="data-slim-scroll-init",e={minHeight:25,delay:100,wrapperClass:"slim-scroll-wrapper",scrollbarContainerClass:"slim-scroll-scrollbar-container",scrollbarClass:"slim-scroll-scrollbar",specialClass:"animate"};return{restrict:"A",transclude:!0,replace:!0,scope:{options:"="},template:'<div><div class="slim-scroll-wrapper" data-ng-transclude></div></div>',link:function(f,g){g.removeAttr(d);var h=angular.extend({},e,f.options),i=g.children()[0],j=angular.element(i);g.attr(d,"1"),j.addClass(h.wrapperClass);var k=angular.element(a.document.createElement("div"));k.addClass(h.scrollbarContainerClass);var l=angular.element(a.document.createElement("div"));l.addClass(h.scrollbarClass),g.append(k),k.append(l);var m={},n=function(a){var b=document.documentElement.scrollTop;return a.getBoundingClientRect().top+(b||document.body.scrollTop)},o=function(a){var b=parseInt(l[0].style.top.replace("%",""))*a/100;return b||0},p=function(){i.offsetHeight<i.scrollHeight?(j.css("right","-18px"),j.css("padding-right","8px"),l.removeClass("hide")):(j.css("right","0"),j.css("padding-right","0"),l.addClass("hide")),m.height=k[0].offsetHeight,m.scrollHeight=i.scrollHeight,m.position=m.height/m.scrollHeight*100,m.scrollbarHeight=m.scrollHeight*m.height/100,m.scrollPosition=h.fixedHeight?h.fixedHeight/m.height*100:m.scrollbarHeight<h.minHeight?h.minHeight/m.height*100:m.position,m.remainder=100-m.scrollPosition,m.x=(m.scrollHeight-m.height)*((m.scrollPosition-m.position)/(100-m.position)),m.heightRate=Math.abs(m.x/m.remainder+m.scrollHeight/100),l[0].style.height=m.scrollPosition+"%",m.reposition=o(m.height)},q=function(a){a=a||event;var b=a.target||event.srcElement,c=b.parentElement||b.parentNode;if(m&&c!==k[0]){var d=a.pageY||event.clientY,e=(d-n(i.parentElement||i.parentNode))/m.height*100-m.scrollPosition/2;e>m.remainder?e=m.remainder:e<0&&(e=0),l[0].style.top=e+"%",i.scrollTop=e*m.heightRate,k.addClass(h.specialClass)}},r=function(c){var d=a.getSelection?a.getSelection():a.document.selection;d&&(d.removeAllRanges?d.removeAllRanges():d.empty&&d.empty()),c=c||event;c.currentTarget||c.srcElement;b.bind("mousemove",s),b.bind("mouseup",t),m.offsetTop=n(i),m.firstY=c.pageY||event.clientY,m.reposition||(m.reposition=o(m.height)),j.addClass("unselectable")},s=function(a){a=a||event;var b=a.pageY||a.clientY,c=(m.reposition+b-m.firstY)/m.height*100;m.remainder<c&&(c=m.remainder),m.previousTop||(m.previousTop=c+1);var d=c>=0&&m.firstY>m.offsetTop;(m.previousTop>c&&d||d&&i.scrollTop+m.height!==m.scrollHeight)&&(l[0].style.top=c+"%",m.previousTop=c,i.scrollTop=c*m.heightRate),k.removeClass(h.specialClass)},t=function(a){b.unbind("mousemove",s),b.unbind("mouseup",t),m.reposition=0,j.removeClass("unselectable"),k.addClass(h.specialClass)},u=function(a){m&&(k.removeClass(h.specialClass),l[0].style.top=i.scrollTop/m.heightRate+"%",k.addClass(h.specialClass))};h.keepFocus&&(j.attr("tabindex","-1"),i.focus()),l.bind("mousedown",r),k.bind("click",q),j.bind("scroll",u),angular.element(a).bind("resize",function(){c(p,h.delay)}),f.$watch(function(){return i.scrollHeight},function(a,b){p()},!0),c(p,h.delay),f.$on("$destroy",function(){l.unbind("mousedown"),k.unbind("click"),j.unbind("scroll"),angular.element(a).unbind("resize",p)})}}}]);