-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathangular-swiper.js
More file actions
196 lines (168 loc) · 7.67 KB
/
angular-swiper.js
File metadata and controls
196 lines (168 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function(window, angular, undefined) {
'use strict';
angular.module('ksSwiper', [])
.directive('ksSwiperContainer', SwiperContainer)
.directive('ksSwiperSlide', SwiperSlide);
function createUUID() {
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
var uuid = s.join("");
return uuid;
}
/* @ngInject */
function SwiperContainer() {
return {
restrict: 'E',
transclude: true,
scope: {
onReady: '&',
slidesPerView: '=',
slidesPerColumn: '=',
spaceBetween: '=',
parallax: '=',
parallaxTransition: '@',
paginationIsActive: '=',
paginationClickable: '=',
showNavButtons: '=',
showScrollBar: '=',
loop: '=',
autoplay: '=',
initialSlide: '=',
containerCls: '@',
wrapperCls: '@',
paginationCls: '@',
slideCls: '@',
direction: '@',
swiper: '=',
overrideParameters: '=',
currentSlide: '='
},
controller: function($scope, $element, $timeout) {
var uuid = createUUID();
$scope.swiper_uuid = uuid;
// directive defaults
var params = {
slidesPerView: $scope.slidesPerView || 1,
slidesPerColumn: $scope.slidesPerColumn || 1,
spaceBetween: $scope.spaceBetween || 0,
direction: $scope.direction || 'horizontal',
loop: $scope.loop || false,
initialSlide: $scope.initialSlide || 0,
showNavButtons: false
};
if (!angular.isUndefined($scope.autoplay) && typeof $scope.autoplay === 'number') {
params = angular.extend({}, params, {
autoplay: $scope.autoplay
});
}
if ($scope.paginationIsActive === true) {
params = angular.extend({}, params, {
paginationClickable: $scope.paginationClickable || true,
pagination: '#paginator-' + $scope.swiper_uuid
});
}
if ($scope.showNavButtons === true) {
params.nextButton = '#nextButton-' + $scope.swiper_uuid;
params.prevButton = '#prevButton-' + $scope.swiper_uuid;
}
if ($scope.showScrollBar === true) {
params.scrollbar = '#scrollBar-' + $scope.swiper_uuid;
}
if ($scope.overrideParameters) {
params = angular.extend({}, params, $scope.overrideParameters);
}
$timeout(function() {
var swiper = null;
if (angular.isObject($scope.swiper)) {
$scope.swiper = new Swiper($element[0].firstChild, params);
swiper = $scope.swiper;
} else {
swiper = new Swiper($element[0].firstChild, params);
}
if (!angular.isUndefined($scope.currentSlide)){
swiper.on('slideChangeEnd', function(){
$timeout (function(){
$scope.internalSlide = true;
if (swiper.params.loop){
var currentSlideElement = $element[0].querySelector('.swiper-slide-active');
var currentSlideIndexString = currentSlideElement.attributes['data-swiper-slide-index'].value;
$scope.currentSlide = parseInt(currentSlideIndexString, 10);
} else {
$scope.currentSlide = Math.abs(swiper.activeIndex);
}
});
});
}
$scope.$watch('currentSlide', function(newValue, oldValue){
if ($scope.internalSlide){
$scope.internalSlide = false;
return;
}
if ((!angular.isUndefined(newValue)) && (newValue != oldValue)){
if (swiper.params.loop){
var possibleDestinationIndexes = [];
for (var index = 0; index < swiper.slides.length; index++){
var slide = swiper.slides[index];
var dataIndex = parseInt(slide.attributes['data-swiper-slide-index'].value, 10);
if(dataIndex === newValue){
swiper.slideTo(index);
break;
}
}
} else {
swiper.slideTo(newValue);
}
}
});
//If specified, calls this function when the swiper object is available
if (!angular.isUndefined($scope.onReady)) {
$scope.onReady({
swiper: swiper
});
}
});
},
link: function(scope, element) {
var uuid = scope.swiper_uuid;
var paginatorId = "paginator-" + uuid;
var prevButtonId = "prevButton-" + uuid;
var nextButtonId = "nextButton-" + uuid;
var scrollBarId = 'scrollBar-' + uuid;
var containerElement = element[0];
angular.element(containerElement.querySelector('.swiper-pagination'))
.attr('id', paginatorId);
angular.element(containerElement.querySelector('.swiper-button-next'))
.attr('id', nextButtonId);
angular.element(containerElement.querySelector('.swiper-button-prev'))
.attr('id', prevButtonId);
angular.element(element[0].querySelector('.swiper-scrollbar'))
.attr('id', scrollBarId);
},
template: '<div class="swiper-container {{containerCls}}">' +
'<div class="parallax-bg" data-swiper-parallax="{{parallaxTransition}}" ng-show="parallax"></div>' +
'<div class="swiper-wrapper {{wrapperCls}}" ng-transclude></div>' +
'<div class="swiper-pagination {{paginationCls}}"></div>' +
'<div class="swiper-button-next" ng-show="showNavButtons"></div>' +
'<div class="swiper-button-prev" ng-show="showNavButtons"></div>' +
'<div class="swiper-scrollbar" ng-show="showScrollBar"></div>' +
'</div>'
};
}
/* @ngInject */
function SwiperSlide() {
return {
restrict: 'E',
require: '^ksSwiperContainer',
transclude: true,
template: '<div class="swiper-slide" ng-transclude></div>',
replace: true
};
}
})(window, angular, undefined);