diff --git a/README.md b/README.md index 107f8c5..0e75ed5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # angular-swiper -Directive that wraps nolimits4web/swiper library for AngularJS. +Directive that wraps [nolimits4web/swiper](https://github.com/nolimits4web/swiper) library for AngularJS. License: Apache ## 1 Installation @@ -23,7 +23,7 @@ Add ` @@ -152,9 +152,9 @@ angular.module('swiperApp') $scope.swiper = {}; $scope.onReadySwiper = function (swiper) { - + swiper.on('slideChangeStart', function () { - + console.log('slideChangeStart'); }); }; diff --git a/bower.json b/bower.json index 0542c6f..fe56811 100644 --- a/bower.json +++ b/bower.json @@ -1,26 +1,29 @@ { - "name": "angular-swiper", - "version": "0.3.2", - "homepage": "https://github.com/ksachdeva/angular-swiper", - "authors": [ - "Kapil Sachdeva " - ], - "description": "Angular directive for nolimits4web/Swiper javascript library", - "main": "dist/angular-swiper.js", - "keywords": [ - "angular", - "swiper" - ], - "license": "Apache", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "tests" - ], - "dependencies": { - "swiper": "~3.0.5", - "angular": ">=1.3" - } -} \ No newline at end of file + "name": "angular-swiper", + "version": "0.3.3", + "homepage": "https://github.com/ksachdeva/angular-swiper", + "authors": [ + "Kapil Sachdeva " + ], + "description": "Angular directive for nolimits4web/Swiper javascript library", + "main": "dist/angular-swiper.js", + "keywords": [ + "angular", + "swiper" + ], + "license": "Apache", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "dependencies": { + "swiper": "~3.3.1", + "angular": ">=1.3" + }, + "devDependencies": { + "jquery": "^2.2.3" + } +} diff --git a/demos/11b-nested-and-synced.html b/demos/11b-nested-and-synced.html new file mode 100644 index 0000000..2017978 --- /dev/null +++ b/demos/11b-nested-and-synced.html @@ -0,0 +1,140 @@ + + + + + + Simple + + + + + + + + + +
+ + Destroy? +
+
+

1

+
+ + + Slide {{s}} + + + +
+

2

+
+ + + + + Horizontal {{h}} + Slide {{s}} + + + + + +
+
+
+
+ + + + + + + + + + + diff --git a/demos/11c-nested-synced-keyboard.html b/demos/11c-nested-synced-keyboard.html new file mode 100644 index 0000000..1a5528e --- /dev/null +++ b/demos/11c-nested-synced-keyboard.html @@ -0,0 +1,152 @@ + + + + + + Simple + + + + + + + + + +
+ + Destroy? +
+
+

1

+
+ + + Slide {{s}} + + + +
+

2

+
+ + + + + {{slide}} + + + + + +
+
+
+
+ + + + + + + + + + + diff --git a/demos/11d-dynamic-grid.html b/demos/11d-dynamic-grid.html new file mode 100644 index 0000000..2059201 --- /dev/null +++ b/demos/11d-dynamic-grid.html @@ -0,0 +1,188 @@ + + + + + + Simple + + + + + + + + + +
+ + + + +
+
+

1

+
+ + + Slide {{s}} + + + +
+

2

+
+ + + + + {{slide}} + + + + + +
+
+
+
+ + + + + + + + + + + diff --git a/dist/angular-swiper.js b/dist/angular-swiper.js index 9d086c4..99dd398 100644 --- a/dist/angular-swiper.js +++ b/dist/angular-swiper.js @@ -1 +1 @@ -!function(e,i,r){"use strict";function t(){for(var e=[],i="0123456789abcdef",r=0;36>r;r++)e[r]=i.substr(Math.floor(16*Math.random()),1);e[14]="4",e[19]=i.substr(3&e[19]|8,1),e[8]=e[13]=e[18]=e[23]="-";var t=e.join("");return t}function a(){return{restrict:"E",transclude:!0,scope:{onReady:"&",slidesPerView:"=",slidesPerColumn:"=",spaceBetween:"=",parallax:"=",parallaxTransition:"@",paginationIsActive:"=",paginationClickable:"=",showNavButtons:"=",showScrollBar:"=",loop:"=",autoplay:"=",initialSlide:"=",containerCls:"@",wrapperCls:"@",paginationCls:"@",slideCls:"@",direction:"@",swiper:"=",overrideParameters:"="},controller:["$scope","$element","$timeout",function(e,r,a){var n=t();e.swiper_uuid=n;var s={slidesPerView:e.slidesPerView||1,slidesPerColumn:e.slidesPerColumn||1,spaceBetween:e.spaceBetween||0,direction:e.direction||"horizontal",loop:e.loop||!1,initialSlide:e.initialSlide||0,showNavButtons:!1};i.isUndefined(e.autoplay)||"number"!=typeof e.autoplay||(s=i.extend({},s,{autoplay:e.autoplay})),e.paginationIsActive===!0&&(s=i.extend({},s,{paginationClickable:e.paginationClickable||!0,pagination:"#paginator-"+e.swiper_uuid})),e.showNavButtons===!0&&(s.nextButton="#nextButton-"+e.swiper_uuid,s.prevButton="#prevButton-"+e.swiper_uuid),e.showScrollBar===!0&&(s.scrollbar="#scrollBar-"+e.swiper_uuid),e.overrideParameters&&(s=i.extend({},s,e.overrideParameters)),a(function(){var t=null;i.isObject(e.swiper)?(e.swiper=new Swiper(r[0].firstChild,s),t=e.swiper):t=new Swiper(r[0].firstChild,s),i.isUndefined(e.onReady)||e.onReady({swiper:t})})}],link:function(e,r){var t=e.swiper_uuid,a="paginator-"+t,n="prevButton-"+t,s="nextButton-"+t,o="scrollBar-"+t,l=r[0];i.element(l.querySelector(".swiper-pagination")).attr("id",a),i.element(l.querySelector(".swiper-button-next")).attr("id",s),i.element(l.querySelector(".swiper-button-prev")).attr("id",n),i.element(r[0].querySelector(".swiper-scrollbar")).attr("id",o)},template:'
'}}function n(){return{restrict:"E",require:"^ksSwiperContainer",transclude:!0,template:'
',replace:!0}}i.module("ksSwiper",[]).directive("ksSwiperContainer",a).directive("ksSwiperSlide",n)}(window,angular,void 0); \ No newline at end of file +!function(e,i,r){"use strict";function n(){for(var e=[],i="0123456789abcdef",r=0;36>r;r++)e[r]=i.substr(Math.floor(16*Math.random()),1);e[14]="4",e[19]=i.substr(3&e[19]|8,1),e[8]=e[13]=e[18]=e[23]="-";var n=e.join("");return n}function t(){return{restrict:"E",transclude:!0,scope:{onReady:"&",slidesPerView:"=",slidesPerColumn:"=",spaceBetween:"=",parallax:"=",parallaxTransition:"@",paginationIsActive:"=",paginationClickable:"=",showNavButtons:"=",showScrollBar:"=",keyboardControl:"@",loop:"=",autoplay:"=",initialSlide:"=",containerCls:"@",wrapperCls:"@",pagination:"@",paginationCls:"@",slideCls:"@",direction:"@",swiper:"=",overrideParameters:"="},controller:["$scope","$element","$timeout",function(e,t,a){function o(e){if(s!==r){var i=e.scope().$index;i<=s.activeIndex&&s.slideNext(!1,0),s.update()}}var s,l=n();e.swiper_uuid=l;var p={slidesPerView:e.slidesPerView||1,slidesPerColumn:e.slidesPerColumn||1,spaceBetween:e.spaceBetween||0,direction:e.direction||"horizontal",keyboardControl:e.keyboardControl||!1,loop:e.loop||!1,initialSlide:e.initialSlide||0,showNavButtons:!1,pagination:e.pagination};return i.isUndefined(e.autoplay)||"number"!=typeof e.autoplay||(p=i.extend({},p,{autoplay:e.autoplay})),e.paginationIsActive===!0&&(p=i.extend({},p,{paginationClickable:e.paginationClickable||!0,pagination:e.pagination||"#paginator-"+e.swiper_uuid})),e.showNavButtons===!0&&(p.nextButton="#nextButton-"+e.swiper_uuid,p.prevButton="#prevButton-"+e.swiper_uuid),e.showScrollBar===!0&&(p.scrollbar="#scrollBar-"+e.swiper_uuid),e.overrideParameters&&(p=i.extend({},p,e.overrideParameters)),a(function(){var r=null;i.isString(p.pagination)&&(p.pagination=$(p.pagination,t[0].firstChild)),i.isObject(e.swiper)?(e.swiper=new Swiper(t[0].firstChild,p),r=e.swiper):r=new Swiper(t[0].firstChild,p),s=r,i.isUndefined(e.onReady)||e.onReady({swiper:r})}),e.$on("$destroy",function(){s.destroy(!0)}),{newSlideLinked:o}}],link:function(e,r){var n=e.swiper_uuid,t="paginator-"+n,a="prevButton-"+n,o="nextButton-"+n,s="scrollBar-"+n,l=r[0];i.element(l.querySelector(".swiper-pagination")).attr("id",t),i.element(l.querySelector(".swiper-button-next")).attr("id",o),i.element(l.querySelector(".swiper-button-prev")).attr("id",a),i.element(r[0].querySelector(".swiper-scrollbar")).attr("id",s)},template:'
',replace:!1}}function a(){return{restrict:"E",require:"^ksSwiperContainer",transclude:!0,template:'
',replace:!0,link:function(e,i,r,n){n.newSlideLinked(i)}}}i.module("ksSwiper",[]).directive("ksSwiperContainer",t).directive("ksSwiperSlide",a)}(window,angular,void 0); \ No newline at end of file diff --git a/src/angular-swiper.js b/src/angular-swiper.js index e57492e..8bb672d 100644 --- a/src/angular-swiper.js +++ b/src/angular-swiper.js @@ -37,11 +37,13 @@ paginationClickable: '=', showNavButtons: '=', showScrollBar: '=', + keyboardControl: '@', loop: '=', autoplay: '=', initialSlide: '=', containerCls: '@', wrapperCls: '@', + pagination: '@', paginationCls: '@', slideCls: '@', direction: '@', @@ -50,6 +52,7 @@ }, controller: function($scope, $element, $timeout) { var uuid = createUUID(); + var swiperInstance; $scope.swiper_uuid = uuid; @@ -59,9 +62,11 @@ slidesPerColumn: $scope.slidesPerColumn || 1, spaceBetween: $scope.spaceBetween || 0, direction: $scope.direction || 'horizontal', + keyboardControl: $scope.keyboardControl || false, loop: $scope.loop || false, initialSlide: $scope.initialSlide || 0, - showNavButtons: false + showNavButtons: false, + pagination: $scope.pagination }; if (!angular.isUndefined($scope.autoplay) && typeof $scope.autoplay === 'number') { @@ -73,7 +78,7 @@ if ($scope.paginationIsActive === true) { params = angular.extend({}, params, { paginationClickable: $scope.paginationClickable || true, - pagination: '#paginator-' + $scope.swiper_uuid + pagination: $scope.pagination || '#paginator-' + $scope.swiper_uuid }); } @@ -90,9 +95,16 @@ params = angular.extend({}, params, $scope.overrideParameters); } + //delay Swiper creation after linking $timeout(function() { var swiper = null; + //find HTMLElements for pagination + if (angular.isString(params.pagination)) { + //if we do not convert here, Swiper will search all the DOM + params.pagination = $(params.pagination, $element[0].firstChild); + } + if (angular.isObject($scope.swiper)) { $scope.swiper = new Swiper($element[0].firstChild, params); swiper = $scope.swiper; @@ -100,6 +112,8 @@ swiper = new Swiper($element[0].firstChild, params); } + swiperInstance = swiper; + //If specified, calls this function when the swiper object is available if (!angular.isUndefined($scope.onReady)) { $scope.onReady({ @@ -107,6 +121,27 @@ }); } }); + + $scope.$on('$destroy', function () { + swiperInstance.destroy(true); + }); + + function newSlideLinked(iElement) { + if (swiperInstance !== undefined) { + //check for prepend + //element has index in $scope.$index + var newElementIndex = iElement.scope().$index; + if (newElementIndex <= swiperInstance.activeIndex) { + //we have a new prepended slide + swiperInstance.slideNext(false, 0); + } + swiperInstance.update(); //updateTranslate defaults to false + } + } + + return { + newSlideLinked: newSlideLinked + } }, link: function(scope, element) { @@ -140,7 +175,8 @@ '
' + '
' + '
' + - '' + '', + replace: false //so do not forget to add CSS class for this directive tag }; } @@ -151,8 +187,11 @@ require: '^ksSwiperContainer', transclude: true, template: '
', - replace: true + replace: true, + link: function link(scope, iElement, iAttrs, controller) { + controller.newSlideLinked(iElement); + } }; } -})(window, angular, undefined); \ No newline at end of file +})(window, angular, undefined);