-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.cutescroll.min.js
More file actions
1 lines (1 loc) · 11.9 KB
/
Copy pathjquery.cutescroll.min.js
File metadata and controls
1 lines (1 loc) · 11.9 KB
1
!function(e){var t={generator:new function(){this.id=function(){return Date.now().toString(36)+"-"+Math.random().toString(36).substr(2,8)}},el:new function(){this.css=new function(){this.clean=function(e){for(var t in e)e[t]="";return e}},this.defaultsAdd=function(e,t,n){return e.addClass(t.class).css(n)},this.additions=new function(){this.add=function(e,t){e.addClass(t.addClass||null).removeClass(t.removeClass||null).css(t||null)},this.remove=function(e,n){e.addClass(n.removeClass||null).removeClass(n.addClass||null).removeClass(n.class||null).css(t.el.css.clean(n||null))}}}},n={item:function(e,t){var i={},o=new n.el(i,t,e),s=new n.scroller(i,o,e),r=new n.event(i,o,s),a=new n.command(i,o,e,r,s);this.process=function(e){for(var t in a)e&&e[t]&&a[t]&&a[t](e[t])}},observer:function(){var i={};this.itemRemove=function(e){i[e]&&(i[e].jqe.removeData("id"),delete i[e])},this.itemCreateAndProcess=function(o,s){var r=t.generator.id();i[r]={obj:new n.item(new n.noticer(this,r),o),jqe:e(o),jse:o},i[r].jqe.data("id",r),i[r].obj.process(s)},this.itemRecreate=function(e,t){var n=function(e){return i[e]?i[e].jse:null}(e);this.itemRemove(e),this.itemCreateAndProcess(n,t)},this.itemExists=function(e){return!!i[e]},this.itemInvokeAndProcess=function(e,t){if(!i[e])throw new Error(`item does not exist : ${e}`);i[e].obj.process(t)}},noticer:function(e,t){var n=!1,i=!0;this.isCreated=function(){return n},this.setToCreated=function(){n=!0},this.setToNotCreated=function(){n=!1},this.isEnabled=function(){return i},this.setToEnabled=function(){i=!0},this.setToDisabled=function(){i=!1},this.remove=function(){e.itemRemove(t)},this.recreate=function(n){e.itemRecreate(t,n)}},el:function(i,o,s){function r(n,i,o){for(var s in i)o?c[i[s]]&&t.el.additions[n](c[i[s]].jq,o[i[s]]):t.el.additions[n](e(s),i[s])}function a(){c.wnd.invoke(),c.doc.invoke(),c.wrapper.create(),c.canvas.create(),c.rail.create(),c.bar.create()}var c=this,u=["wrapper","canvas","rail","bar"];this.wnd=new n.wnd,this.doc=new n.doc,this.wrapper=new n.wrapper(i,o),this.canvas=new n.canvas(i,o),this.rail=new n.rail(i,o),this.bar=new n.bar(i,o),this.activate=function(){s.isCreated()||a(),r("add",u,i),r("add",i.element)},this.remove=function(){r("remove",u,i),r("remove",i.element),this.bar.remove(),this.rail.remove(),this.canvas.unwrap()}},wnd:function(){this.invoke=function(){this.jq=e(window)}},doc:function(){this.invoke=function(){this.jq=e(document)}},event:function(e,t,i){var o=new n.on(e,t,i);this.activate=function(){o.hover(),o.scrollerBarDrag(),o.wheelScroll(),o.contentChange(),o.resize(),o.touch()},this.remove=function(){t.wnd.jq.off("resize.cs"),t.bar.jq.off("mouseenter.cs mouseleave.cs mousedown.cs"),t.rail.jq.off("mouseenter.cs mouseleave.cs"),t.canvas.jq.off("mouseenter.cs mouseleave.cs wheel.cs mousewheel.cs touchstart.cs touchmove.cs touchend.cs"),o.observer.disconnect()}},on:function(e,t,i){this.observer,this.hover=function(){t.rail.jq.on("mouseenter.cs",function(){i.visibility.shouldBeShown("rail")}),t.rail.jq.on("mouseleave.cs",function(){i.visibility.canBeHidden("rail")}),t.bar.jq.on("mouseenter.cs",function(){i.visibility.shouldBeShown("bar")}),t.bar.jq.on("mouseleave.cs",function(){i.visibility.canBeHidden("bar")}),t.canvas.jq.on("mouseenter.cs",function(){i.visibility.shouldBeShown("canvas")}),t.canvas.jq.on("mouseleave.cs",function(){i.visibility.canBeHidden("canvas")})},this.scrollerBarDrag=function(){t.bar.jq.on("mousedown.cs",function(e){t.doc.jq.on("mousemove.cs",function(t){i.scrollByScrollerBar(t.pageY-e.pageY),e.pageY=t.pageY}),t.doc.jq.on("mouseup.cs",function(e){t.doc.jq.off("mousemove.cs"),t.doc.jq.off("mouseup.cs")}),e.preventDefault()})},this.wheelScroll=function(){t.canvas.jq.on("wheel.cs mousewheel.cs",function(e){var t=Math.sign(e.originalEvent.deltaY);0!==t&&i.scrollByWheel(t),i.calc.shouldItContinueToScrollPage()||e.preventDefault()})},this.contentChange=function(){this.observer=new MutationObserver(function(n){e.on.contentChange&&e.on.contentChange(t.canvas.jq),i.onContentChange()}),this.observer.observe(t.canvas.js,{childList:!0,subtree:!0})},this.resize=function(){t.wnd.jq.on("resize.cs",function(){i.onContentChange()})},this.touch=function(){var o;t.canvas.jq.on("touchstart.cs",function(t){i.visibility.shouldBeShown("touch"),(o=new n.touch(e)).start(t.originalEvent.touches[0].pageY)}),t.canvas.jq.on("touchmove.cs",function(e){o.move(e.originalEvent.touches[0].pageY),i.scrollByTouchMove(o.moveDeltaGetAndShift()),e.preventDefault()}),t.canvas.jq.on("touchend.cs",function(e){i.visibility.canBeHidden(),i.scrollByTouchSwipe(o.velocityGet(),o.getDelta())})}},wrapper:function(n,i){var o={position:"relative",overflow:"hidden"};this.create=function(){this.jq=t.el.defaultsAdd(e("<div>"),n.wrapper,o),e(i).wrap(this.jq),this.invoke()},this.invoke=function(){this.jq=e(i).parent("."+n.wrapper.class)}},canvas:function(n,i){var o=this,s={overflow:"hidden"};this.js=i,this.create=function(){this.jq=t.el.defaultsAdd(e(i),n.canvas,s)},this.invoke=function(){this.jq=e(i)},this.unwrap=function(){o.jq.css(t.el.css.clean(s)),this.jq.unwrap()}},rail:function(n,i){var o={position:"absolute",zIndex:100,top:0};this.create=function(){this.jq=t.el.defaultsAdd(e("<div>"),n.rail,o),e(i).parent().append(this.jq)},this.invoke=function(){this.jq=e(i).siblings("."+n.rail.class)},this.remove=function(){this.jq.remove()}},bar:function(n,i){var o={position:"absolute",zIndex:110,top:0};this.create=function(){this.jq=t.el.defaultsAdd(e("<div>"),n.bar,o),e(i).parent().append(this.jq)},this.invoke=function(){this.jq=e(i).siblings("."+n.bar.class)},this.remove=function(){this.jq.remove()}},scroller:function(e,t,i){function o(e){i.isEnabled()&&(s.visibility.shouldBeShown(),a.move(e))}var s=this,r=new n.scrollerDisplay(e,t);this.calc=new n.scrollerCalc(e,t);var a=new n.scrollerOperation(e,t,this.calc);this.visibility=new n.scrollerVisibility(e,i,r),this.scrollByScrollerBar=function(e){o(e)},this.scrollByWheel=function(e){o(e*this.calc.wheelIteration())},this.scrollByTouchMove=function(e){o(this.calc.touchIteration(e))},this.scrollByTouchSwipe=function(t,n){if(Math.abs(t)>e.touch.swipeVelocityThreshold&&Math.abs(n)>e.touch.swipeDistanceThreshold){var i=Math.abs(this.calc.touchIteration(n)*t*e.touch.swipeStrengthFactor),s=-Math.sign(t),r=function(){if(i>e.touch.swipeIterationMinDistance){var t=i*s;o(t),i-=Math.abs(t)*e.touch.swipeFadingFactor,setTimeout(r,e.touch.swipeIterationTimeout)}};r()}},this.jumpToTop=function(){a.jumpToTop()},this.jumpToBottom=function(){a.jumpToBottom()},this.onContentChange=function(){this.calc.shouldItBeEnabled()?(i.setToEnabled(),s.visibility.canBeHidden(),a.adjustPosition(),e.on.contentChangeShowScroller&&s.visibility.shouldBeShown()):(i.setToDisabled(),r.hide(),a.jumpToTop())},this.activate=function(){this.onContentChange()}},scrollerDisplay:function(e,t){this.hide=function(){t.rail.jq.hide(),t.bar.jq.hide()},this.fadeOut=function(){t.rail.jq.fadeOut(e.scroller.fadeOutSpeed),t.bar.jq.fadeOut(e.scroller.fadeOutSpeed)},this.fadeIn=function(){e.rail.visible&&t.rail.jq.stop(!0).fadeIn(e.scroller.fadeInSpeed),t.bar.jq.stop(!0).fadeIn(e.scroller.fadeInSpeed)}},scrollerVisibility:function(e,t,n){function i(){t.isEnabled()&&(clearTimeout(o),n.fadeIn(),e.scroller.alwaysVisible||(o=setTimeout(function(){r.shouldBeKeptVisible()||n.fadeOut()},e.scroller.hideDelay)))}var o,s={},r=this;this.shouldBeKeptVisible=function(){for(var e in s)return!0;return!1},this.shouldBeShown=function(e){e&&(s[e]=!0),i()},this.canBeHidden=function(e){e?delete s[e]:s={}}},scrollerCalc:function(e,t){var n;this.wheelIteration=function(){return e.mouse.wheelStep/this.canvasMaxOffset()*this.areaHeight()},this.touchIteration=function(t){return e.touch.moveFactor*t/this.canvasMaxOffset()*this.areaHeight()},this.shouldItBeScrolled=function(){return this.canvasHeight()>this.areaHeight()},this.shouldItContinueToScrollPage=function(){return!(!this.isAtEdge()||!e.mouse.pageScroll)},this.isAtEdge=function(){return 0===n||1===n},this.shouldItBeEnabled=function(){return!(!this.shouldItBeScrolled()&&!e.scroller.alwaysVisible)},this.canvasHeight=function(){return t.canvas.js.scrollHeight},this.canvasMaxOffset=function(){return Math.abs(this.canvasHeight()-this.areaHeight())},this.canvasAdjustedPosition=function(){return n*this.canvasMaxOffset()},this.canvasVisibleProportion=function(){return Math.abs(this.areaHeight()/this.canvasHeight())},this.areaHeight=function(){return t.canvas.js.clientHeight},this.railHeight=function(){return t.rail.jq.outerHeight()},this.barHeight=function(){return t.bar.jq.outerHeight()},this.barPosition=function(){return t.bar.jq.position().top},this.barHeightProportionalToContent=function(){return this.canvasVisibleProportion()*this.railHeight()},this.barMaxOffset=function(){return Math.abs(this.railHeight()-this.barHeight())},this.barJumpToTop=function(){return n=0,0},this.barJumpToBottom=function(e){return n=1,this.barMaxOffset()},this.barMovementDistance=function(e){var t=this.barMaxOffset(),i=this.barPosition()+e;if(i>t)var o=t;else if(i<0)o=0;else o=i;return n=o/t,o}},scrollerOperation:function(e,t,n){function i(){!function(e){t.canvas.jq.scrollTop(e)}(n.canvasAdjustedPosition())}function o(e){!function(e){t.bar.jq.css("top",e)}(e),i()}this.move=function(e){o(n.barMovementDistance(e))},this.adjustPosition=function(){e.bar.height||function(e){t.bar.jq.css("height",e)}(n.barHeightProportionalToContent()),this.move(0),i()},this.jumpToTop=function(){o(n.barJumpToTop())},this.jumpToBottom=function(){o(n.barJumpToBottom())}},touch:function(e){var t={early:null,current:null},n={y:{early:null,last:null,current:null},delta:null};this.velocityGet=function(){return(n.y.current-n.y.early)/(t.current-t.early)},this.moveDeltaGetAndShift=function(){return n.delta=n.y.last-n.y.current,n.y.last=n.y.current,n.delta},this.distanceGet=function(){return Math.abs(n.y.current-n.y.early)},this.getDelta=function(){return n.delta},this.move=function(i){n.y.current=i,t.current=(new Date).getTime(),function(){var i=t.current-t.early;i>e.touchSwipeTimeAdjustmentThreshold&&(t.early+=i/2,n.y.early+=(n.y.current-n.y.early)/2)}()},this.start=function(e){n.y.early=n.y.last=e,t.early=(new Date).getTime()}},command:function(e,t,i,o,s){function r(){i.isCreated()&&(o.remove(),t.remove(),i.setToNotCreated())}var a=new n.configurator(e);this.create=function(e){i.isCreated()||(a.fresh(e),t.activate(),o.activate(),s.activate(),i.setToCreated())},this.recreate=function(e){r(),i.recreate({create:e})},this.update=function(e){i.isCreated()&&(a.proceed(e),t.activate(),s.activate())},this.jumpToTop=function(){i.isCreated()&&s.jumpToTop()},this.jumpToBottom=function(){i.isCreated()&&s.jumpToBottom()},this.log=function(e){console.log(e)},this.alert=function(e){alert(e)},this.remove=function(e){r(),i.remove()}},configurator:function(t){this.proceed=function(n){e.extend(!0,t,function(t){return t.bar=e.extend({},t.scroller,t.bar),t.rail=e.extend({},t.scroller,t.rail),t.canvas=e.extend({},t.area,t.canvas),t.wrapper=e.extend({},t.area,t.wrapper),t}(n))},this.fresh=function(t){this.proceed(e.extend(!0,{},e.fn.cuteScroll.defaults,function(e){return e.rail&&(e.rail.display=e.rail.visible?"block":"none"),e}(t)))},this.cfgClean=function(){for(var e in t)delete t[e]}}},i=new function(){var t=new n.observer;this.process=function(n,i){var o=e(n).data("id");o&&t.itemExists(o)?t.itemInvokeAndProcess(o,i):t.itemCreateAndProcess(n,i)}};e.fn.cuteScroll=function(e){return this.each(function(){i.process(this,e)}),this},e.fn.cuteScroll.defaults={bar:{opacity:.5,minHeight:"4rem",zIndex:110,class:"cuteScroll-bar"},rail:{opacity:.1,height:"100%",zIndex:100,visible:!0,class:"cuteScroll-rail"},scroller:{background:"black",width:"0.5rem",right:0,alwaysVisible:!1,hideDelay:1e3,fadeOutSpeed:"slow",fadeInSpeed:"fast"},canvas:{class:"cuteScroll-canvas"},wrapper:{class:"cuteScroll-wrapper"},area:{height:"20rem",width:"100%"},mouse:{pageScroll:!0,wheelStep:120},touch:{moveFactor:1.1,swipeStrengthFactor:1.25,swipeFadingFactor:.075,swipeVelocityThreshold:.5,swipeDistanceThreshold:10,swipeTimeAdjustmentThreshold:500,swipeIterationMinDistance:.2,swipeIterationTimeout:5},on:{contentChange:!1,contentChangeShowScroller:!0}}}(jQuery);