-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathangular-carousel.js
More file actions
409 lines (336 loc) · 14.8 KB
/
angular-carousel.js
File metadata and controls
409 lines (336 loc) · 14.8 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* @license AngularJS v1.2.0
* (c) 2015 Lifely
* License: MIT
*/
angular.module('angular-carousel', [])
//
// This service controls existing carousel instances
//
.factory('Carousel', function() {
var Carousel = {};
Carousel.instances = {};
//
// Add a new carousel instance
//
Carousel.add = function(slidesCount, name, scope, options) {
// Check if name is specified
name = name || false;
if(!name) {
return 'Error: no carousel name specified';
}
// Check slidesCount
slidesCount = slidesCount || 0;
// Check if carousel already exists
var carouselExists = Carousel.instances[name] || false;
if(carouselExists) {
return 'Error: carousel instance already exists';
}
// Create carousel instance
var instance = new constructor(slidesCount, scope, options);
// Save new carousel instance
Carousel.instances[name] = instance;
return instance;
};
//
// Get an existing carousel instance by name
//
Carousel.get = function(name) {
var instance = Carousel.instances[name] || false;
return instance ? instance : 'Error: carousel with name \'' + name + '\' does not exist';
};
//
// Remove a carousel
//
Carousel.remove = function(name) {
delete Carousel.instances[name];
};
//
// Carousel prototype definition
//
var constructor = function(slidesCount, scope, options) {
options = options || {};
if (typeof options.looping === 'undefined') options.looping = true;
var instance = this;
this.slidesCount = slidesCount;
this.currentSlide = 0;
this.onSlideChangeCallbacks = [];
// Operation: to specified index
this.toIndex = function(index, wrapping) {
wrapping = wrapping || false;
this.currentSlide = index % this.slidesCount;
// Own on slide change callbacks
angular.forEach(this.onSlideChangeCallbacks, function(callback) {
if(typeof(callback) === 'function') {
callback(instance.currentSlide, wrapping);
}
});
// If the scope is not currently updating, trigger one update using a timeout of zero
setTimeout(function() {
scope.$apply();
}, 0);
};
// Operation: to next slide
this.next = function() {
var nextSlide = this.currentSlide + 1,
wrapping = false;
if(nextSlide > this.slidesCount - 1) {
if(options.looping){
nextSlide = 0;
wrapping = 'right';
}
else {
nextSlide = this.slidesCount - 1;
}
}
this.toIndex(nextSlide, wrapping);
return nextSlide;
};
// Operation: to previous slide
this.previous = function() {
var previousSlide = this.currentSlide - 1,
wrapping = false;
if(previousSlide < 0) {
if(options.looping){
previousSlide = this.slidesCount - 1;
wrapping = 'left';
}
else {
previousSlide = 0;
}
}
this.toIndex(previousSlide, wrapping);
return previousSlide;
};
// Operation: on slide change
this.onSlideChange = function(callback) {
this.onSlideChangeCallbacks.push(callback);
return this.onSlideChangeCallbacks.indexOf(callback);
};
// Operation: unbind on slide change callback
this.unbindOnSlideChangeCallback = function(index) {
if(typeof(this.onSlideChangeCallbacks[index]) === 'undefined') return;
this.onSlideChangeCallbacks.splice(index, 1);
};
}
return Carousel;
})
//
// This directive makes an element with class 'ng-carousel' interactive
// using own UI logic and HammerJS
//
.directive('ngCarousel', ['Carousel', '$compile', '$document', '$timeout', function(Carousel, $compile, $document, $timeout) {
function isTouchDevice() {
return 'ontouchstart' in document.documentElement;
}
var MOVE_TRESHOLD_PERCENTAGE = 25;
function createEmptySlide(){
return angular.element('<slide class="empty"></slide>');
}
return {
restrict: 'AE',
replace: true,
scope: {
ngCarouselWatch: '='
},
link: function(scope, element, attrs) {
// Options
var interval = false, timeoutPromise = false, random = false, name = '', looping = false;
interval = typeof(attrs.ngCarouselTimer) !== 'undefined' && parseInt(attrs.ngCarouselTimer, 10) > 0 ? parseInt(attrs.ngCarouselTimer, 10) : false;
random = typeof(attrs.ngCarouselRandom) !== 'undefined';
looping = !(attrs.ngCarouselLoop === 'false');
// Function to initialize interaction with dom (should be loaded after the dom has changed)
var slides, currentCarousel, firstSlideCopy, lastSlideCopy, slideContainer, hammer, name;
function copyFirstAndLastSlide() {
firstSlideCopy = angular.element(slides[0].outerHTML);
lastSlideCopy = angular.element(slides[slides.length - 1].outerHTML);
}
function makeFirstAndLastSlideEmpty() {
firstSlideCopy = angular.element('<slide class="empty"></slide>');
lastSlideCopy = angular.element('<slide class="empty"></slide>');
}
var refreshInteractionWithDom = function() {
// Add initial classes
element.addClass('ng-carousel');
element.addClass(isTouchDevice() ? 'carousel-touch' : 'carousel-no-touch');
// Find slide wrapper
slideContainer = element.find('slidecontainer');
// Remove old carousel
var savedSlideIndex = false;
var savedCallbacks = false;
if(name) {
savedSlideIndex = Carousel.get(name).currentSlide;
savedCallbacks = Carousel.get(name).onSlideChangeCallbacks;
Carousel.remove(name);
}
// Remove old duplicated slides
var removeOldVirtualSlides = function() {
var oldSlides = angular.element(element[0].querySelectorAll('.carousel-slide-copy'));
if(oldSlides.length > 0) oldSlides.remove();
};
// Find slides
removeOldVirtualSlides();
slides = element.find('slide');
// Add slides before and after the current slides
if(slides.length > 0) {
// Create new carousel and duplicate slides
name = attrs.ngCarouselName;
currentCarousel = Carousel.add(slides.length, attrs.ngCarouselName, scope, {
looping: looping
});
angular.forEach(savedCallbacks, function(savedCallback) {
currentCarousel.onSlideChange(savedCallback);
currentCarousel.unbindOnSlideChangeCallback(0);
});
// Duplicate first and last slide (for infinite effect)
var refreshVirtualSlides = function() {
removeOldVirtualSlides();
slides = element.find('slide');
if(looping) {
copyFirstAndLastSlide();
}
else {
makeFirstAndLastSlideEmpty();
}
firstSlideCopy.addClass('carousel-slide-copy');
lastSlideCopy.addClass('carousel-slide-copy');
slideContainer.append(firstSlideCopy);
slideContainer.prepend(lastSlideCopy);
slideContainer.addClass('carousel-ignore-first-slide');
};
refreshVirtualSlides();
// On slide change, move the slideContainer
var onSlideChangeCallback = function(slideIndex, wrapping) {
var newSlideIndex = slideIndex + 1; // because the first slide doesn't count
if(wrapping === 'left') {
newSlideIndex = 0; // first slide
} else if(wrapping === 'right') {
newSlideIndex = slides.length + 1; // last slide
}
move(newSlideIndex, true, function() {
if(wrapping === 'left') {
move(slides.length, false);
} else if(wrapping === 'right') {
move(1, false);
}
});
setNextSlideTimeout();
refreshVirtualSlides();
};
currentCarousel.onSlideChange(onSlideChangeCallback);
// If new slide was out of range, move to the new assigned one
if(savedSlideIndex !== false && currentCarousel.currentSlide !== savedSlideIndex) {
onSlideChangeCallback(currentCarousel.currentSlide, false);
currentCarousel.toIndex(savedSlideIndex);
}
// Option: random
if(random) {
var randomSlide = Math.floor(Math.random() * currentCarousel.slidesCount);
currentCarousel.toIndex(randomSlide);
}
// Option: interval
if (interval && currentCarousel.slidesCount >= 2) {
setNextSlideTimeout();
}
} else {
console.log('ng-carousel error: No slides found')
}
// Initialize Hammer
if(slideContainer[0]) {
hammer = new Hammer.Manager(slideContainer[0]);
hammer.add(new Hammer.Pan({ direction: Hammer.DIRECTION_HORIZONTAL, threshold: 0 }));
// On pan left/right
hammer.on("panleft panright", function(ev) {
if(!ev.isFinal) carouselDrag(ev.deltaX);
});
} else {
console.log('ng-carousel error: No slidecontainer found')
}
};
// Reset interval function
var setNextSlideTimeout = function() {
if(!interval || currentCarousel.slidesCount < 2) return;
if(timeoutPromise) $timeout.cancel(timeoutPromise);
timeoutPromise = $timeout(function() {
currentCarousel.next();
}, interval);
};
// UI move function
var move = function(slideIndex, animate, transitionEndCallback) {
if(animate) {
slideContainer.addClass('carousel-animate');
} else {
slideContainer.removeClass('carousel-animate');
}
var rule = 'translate(-' + (100 * slideIndex) + '%, 0)';
slideContainer.css({
'-webkit-transform': rule,
'-moz-transform': rule,
'-ms-transform': rule,
'-o-transform': rule,
'transform': rule
});
if(animate) {
slideContainer.on('transitionend oTransitionEnd webkitTransitionEnd', function() {
if(typeof transitionEndCallback === 'function') transitionEndCallback();
slideContainer.off('transitionend oTransitionEnd webkitTransitionEnd');
move(currentCarousel.currentSlide + 1, false);
});
}
};
// Make the carousel draggable (either with touch or with mouse)
var deltaXFactor = 0,
width = 0;
var carouselDrag = function(newDeltaX) {
deltaXFactor = newDeltaX / width;
deltaXFactor = deltaXFactor > 1 ? 1 : deltaXFactor < -1 ? -1 : deltaXFactor;
move(currentCarousel.currentSlide + 1 - deltaXFactor, false);
};
var carouselPress = function() {
width = slideContainer[0].offsetWidth;
};
var carouselRelease = function() {
if(Math.abs(deltaXFactor) > MOVE_TRESHOLD_PERCENTAGE / 100) {
if(deltaXFactor > 0) {
currentCarousel.previous(); // user dragged right, go to previous slide
} else {
currentCarousel.next(); // user dragged left, go to next slide
}
deltaXFactor = 0;
} else if(deltaXFactor > 0 || deltaXFactor < 0) {
move(currentCarousel.currentSlide + 1, true, function() {
deltaXFactor = 0;
});
}
};
// On release
var pressEvent = isTouchDevice() ? 'touchstart' : 'mousedown';
var releaseEvent = isTouchDevice() ? 'touchend' : 'mouseup';
$document.on(pressEvent, carouselPress);
$document.on(releaseEvent, carouselRelease);
//
element.on('mouseover', function() {
if(timeoutPromise) $timeout.cancel(timeoutPromise);
});
element.on('mouseout', setNextSlideTimeout);
// Events to refresh the dom selectors
var refreshInteractionWithDomTimer = $timeout(refreshInteractionWithDom, 0);
if(typeof(attrs.ngCarouselWatch) !== 'undefined') {
scope.$watch('ngCarouselWatch', function() {
// Wait for angular compile to complete
$timeout(refreshInteractionWithDom);
}, true);
}
// Destroy all binded events on scope destroy
scope.$on('$destroy', function() {
$timeout.cancel(refreshInteractionWithDomTimer);
element.off('mouseover mouseout');
$document.off(pressEvent);
$document.off(releaseEvent);
slideContainer.off('transitionend oTransitionEnd webkitTransitionEnd');
currentCarousel.onSlideChangeCallbacks = [];
Carousel.remove(name);
});
}
};
}]);