-
Notifications
You must be signed in to change notification settings - Fork 59
/
responsive-carousel.directive.js
75 lines (67 loc) · 2.41 KB
/
responsive-carousel.directive.js
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
(function() {
'use strict';
angular.module('tcUIComponents').directive('responsiveCarousel', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
templateUrl: 'directives/responsive-carousel/responsive-carousel.directive.html',
scope: {
data: '='
},
controller: ['$log', '$scope', '$element', '$window',
function($log, $scope, $element, $window) {
$scope.slideCounts = {};
activate();
function activate() {
init();
var window = angular.element($window);
window.on('resize', function() {
init();
// don't forget manually trigger $digest()
$scope.$digest();
});
}
function init() {
var width = $window.innerWidth;
console.log(width);
if(width > 1070) {
// desktop
buildCarouselSlide(6);
} else if(width > 900) {
// desktop
buildCarouselSlide(5);
} else if(width <= 900 && width > 768) {
// tablet
buildCarouselSlide(4);
} else {
// phone
buildCarouselSlide(2);
}
}
function buildCarouselSlide(numItemsPerSlide) {
var slidesCollection = [];
var slide = [];
// Might be able to change number of subtracks per slide based
// on screen size if the width of each subtrack is consistent:
// http://stackoverflow.com/questions/26252038/multi-item-responsive-carousel
numItemsPerSlide = numItemsPerSlide || 5;
for(var i = 0; i < $scope.data.length; i++) {
if (slide.length === numItemsPerSlide) {
// When slide is full, push it to collection and make a new slide []
slidesCollection.push(slide);
// updates the slide count object
$scope.slideCounts[slidesCollection.length - 1] = slide.length;
slide = [];
}
slide.push($scope.data[i]);
}
slidesCollection.push(slide);
// updates the slide count object
$scope.slideCounts[slidesCollection.length - 1] = slide.length;
$scope.slidesCollection = slidesCollection;
}
}]
};
});
})();