forked from lifelynl/angular-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-carousel.min.js
More file actions
1 lines (1 loc) · 4.41 KB
/
angular-carousel.min.js
File metadata and controls
1 lines (1 loc) · 4.41 KB
1
angular.module("angular-carousel",[]).factory("Carousel",function(){var n={};n.instances={},n.add=function(o,t,a,i){if(t=t||!1,!t)return"Error: no carousel name specified";o=o||0;var r=n.instances[t]||!1;if(r)return"Error: carousel instance already exists";var s=new e(o,a,i);return n.instances[t]=s,s},n.get=function(e){var o=n.instances[e]||!1;return o?o:"Error: carousel with name '"+e+"' does not exist"},n.remove=function(e){delete n.instances[e]};var e=function(n,e,o){o=o||{},"undefined"==typeof o.looping&&(o.looping=!0);var t=this;this.slidesCount=n,this.currentSlide=0,this.onSlideChangeCallbacks=[],this.toIndex=function(n,o){o=o||!1,this.currentSlide=n%this.slidesCount,angular.forEach(this.onSlideChangeCallbacks,function(n){"function"==typeof n&&n(t.currentSlide,o)}),setTimeout(function(){e.$apply()},0)},this.next=function(){var n=this.currentSlide+1,e=!1;return n>this.slidesCount-1&&(o.looping?(n=0,e="right"):n=this.slidesCount-1),this.toIndex(n,e),n},this.previous=function(){var n=this.currentSlide-1,e=!1;return 0>n&&(o.looping?(n=this.slidesCount-1,e="left"):n=0),this.toIndex(n,e),n},this.onSlideChange=function(n){return this.onSlideChangeCallbacks.push(n),this.onSlideChangeCallbacks.indexOf(n)},this.unbindOnSlideChangeCallback=function(n){"undefined"!=typeof this.onSlideChangeCallbacks[n]&&this.onSlideChangeCallbacks.splice(n,1)}};return n}).directive("ngCarousel",["Carousel","$compile","$document","$timeout",function(n,e,o,t){function a(){return"ontouchstart"in document.documentElement}var i=25;return{restrict:"AE",replace:!0,scope:{ngCarouselWatch:"="},link:function(e,r,s){function l(){v=angular.element(m[0].outerHTML),S=angular.element(m[m.length-1].outerHTML)}function u(){v=angular.element('<slide class="empty"></slide>'),S=angular.element('<slide class="empty"></slide>')}var c=!1,d=!1,f=!1,h="",g=!1,C=!0;c="undefined"!=typeof s.ngCarouselTimer&&parseInt(s.ngCarouselTimer,10)>0?parseInt(s.ngCarouselTimer,10):!1,f="undefined"!=typeof s.ngCarouselRandom,g=!("false"===s.ngCarouselLoop),C=!("false"===s.ngCarouselPan);var m,p,v,S,b,y,h,k=function(){r.addClass("ng-carousel"),r.addClass(a()?"carousel-touch":"carousel-no-touch"),b=r.find("slidecontainer");var o=!1,t=!1;h&&(o=n.get(h).currentSlide,t=n.get(h).onSlideChangeCallbacks,n.remove(h));var i=function(){var n=angular.element(r[0].querySelectorAll(".carousel-slide-copy"));n.length>0&&n.remove()};if(i(),m=r.find("slide"),m.length>0){h=s.ngCarouselName,p=n.add(m.length,s.ngCarouselName,e,{looping:g}),angular.forEach(t,function(n){p.onSlideChange(n),p.unbindOnSlideChangeCallback(0)});var d=function(){i(),m=r.find("slide"),g?l():u(),v.addClass("carousel-slide-copy"),S.addClass("carousel-slide-copy"),b.append(v),b.prepend(S),b.addClass("carousel-ignore-first-slide")};d();var k=function(n,e){var o=n+1;"left"===e?o=0:"right"===e&&(o=m.length+1),T(o,!0,function(){"left"===e?T(m.length,!1):"right"===e&&T(1,!1)}),E(),d()};if(p.onSlideChange(k),o!==!1&&p.currentSlide!==o&&(k(p.currentSlide,!1),p.toIndex(o)),f){var x=Math.floor(Math.random()*p.slidesCount);p.toIndex(x)}c&&p.slidesCount>=2&&E()}else console.log("ng-carousel error: No slides found");b[0]?(y=new Hammer.Manager(b[0]),y.add(new Hammer.Pan({direction:Hammer.DIRECTION_HORIZONTAL,threshold:0})),C===!0&&y.on("panleft panright",function(n){n.isFinal||I(n.deltaX)})):console.log("ng-carousel error: No slidecontainer found")},E=function(){!c||p.slidesCount<2||(d&&t.cancel(d),d=t(function(){p.next()},c))},T=function(n,e,o){e?b.addClass("carousel-animate"):b.removeClass("carousel-animate");var t="translate(-"+100*n+"%, 0)";b.css({"-webkit-transform":t,"-moz-transform":t,"-ms-transform":t,"-o-transform":t,transform:t}),e&&b.on("transitionend oTransitionEnd webkitTransitionEnd",function(){"function"==typeof o&&o(),b.off("transitionend oTransitionEnd webkitTransitionEnd"),T(p.currentSlide+1,!1)})},x=0,w=0,I=function(n){x=n/w,x=x>1?1:-1>x?-1:x,T(p.currentSlide+1-x,!1)},$=function(){w=b[0].offsetWidth},H=function(){Math.abs(x)>i/100?(x>0?p.previous():p.next(),x=0):(x>0||0>x)&&T(p.currentSlide+1,!0,function(){x=0})},M=a()?"touchstart":"mousedown",N=a()?"touchend":"mouseup";o.on(M,$),o.on(N,H),r.on("mouseover",function(){d&&t.cancel(d)}),r.on("mouseout",E);var O=t(k,0);"undefined"!=typeof s.ngCarouselWatch&&e.$watch("ngCarouselWatch",function(){t(k)},!0),e.$on("$destroy",function(){t.cancel(O),r.off("mouseover mouseout"),o.off(M),o.off(N),b.off("transitionend oTransitionEnd webkitTransitionEnd"),p.onSlideChangeCallbacks=[],n.remove(h)})}}}]);